答案:文章设计了一个基于Java的个人记账应用,通过Transaction类封装收支记录,Ledger类管理交易列表并提供增删查及统计功能,结合Scanner实现命令行交互,支持添加、查询和分类统计收支,建议扩展文件或数据库持久化。

实现一个简单的个人记账应用,核心是管理收支记录、分类统计和数据持久化。Java 提供了良好的面向对象支持和集合框架,适合构建这类小型应用。下面从结构设计到功能实现逐步说明。
记账的核心是记录每一笔交易。可以创建一个 Transaction 类来表示每条记账条目。
public class Transaction {
    private String description; // 说明,如“午餐”
    private double amount;      // 金额
    private String category;    // 分类,如“餐饮”、“交通”
    private String type;        // 类型:收入 或 支出
    private String date;        // 日期,格式如 "2024-04-05"
    public Transaction(String description, double amount, String category, 
                       String type, String date) {
        this.description = description;
        this.amount = amount;
        this.category = category;
        this.type = type;
        this.date = date;
    }
    // Getter 方法(建议生成)
    public String getDescription() { return description; }
    public double getAmount() { return amount; }
    public String getCategory() { return category; }
    public String getType() { return type; }
    public String getDate() { return date; }
    @Override
    public String toString() {
        return date + " | " + type + " | " + category + " | " 
               + description + " | ¥" + amount;
    }
}使用 ArrayList 存储所有交易记录,并提供增删查功能。
import java.util.ArrayList;
import java.util.List;
public class Ledger {
    private List<Transaction> transactions;
    public Ledger() {
        transactions = new ArrayList<>();
    }
    public void addTransaction(Transaction t) {
        transactions.add(t);
    }
    public void listAll() {
        for (Transaction t : transactions) {
            System.out.println(t);
        }
    }
    public void listByType(String type) {
        for (Transaction t : transactions) {
            if (t.getType().equals(type)) {
                System.out.println(t);
            }
        }
    }
    public double getTotalByType(String type) {
        return transactions.stream()
                .filter(t -> t.getType().equals(type))
                .mapToDouble(Transaction::getAmount)
                .sum();
    }
    public void listByCategory(String category) {
        for (Transaction t : transactions) {
            if (t.getCategory().equals(category)) {
                System.out.println(t);
            }
        }
    }
}通过 Scanner 实现简单的控制台输入输出,让用户添加记录或查看统计。
立即学习“Java免费学习笔记(深入)”;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Ledger ledger = new Ledger();
        Scanner scanner = new Scanner(System.in);
        String command;
        System.out.println("欢迎使用个人记账应用!");
        while (true) {
            System.out.print("\n输入命令 (add/list/income/expense/quit): ");
            command = scanner.next();
            if (command.equals("quit")) {
                System.out.println("再见!");
                break;
            }
            switch (command) {
                case "add":
                    System.out.print("描述: "); String desc = scanner.next();
                    System.out.print("金额: "); double amt = scanner.nextDouble();
                    System.out.print("分类: "); String cat = scanner.next();
                    System.out.print("类型 (income/expense): "); String type = scanner.next();
                    System.out.print("日期 (yyyy-MM-dd): "); String date = scanner.next();
                    ledger.addTransaction(new Transaction(desc, amt, cat, type, date));
                    System.out.println("已添加!");
                    break;
                case "list":
                    ledger.listAll();
                    break;
                case "income":
                    System.out.println("总收入: ¥" + ledger.getTotalByType("income"));
                    break;
                case "expense":
                    System.out.println("总支出: ¥" + ledger.getTotalByType("expense"));
                    break;
                default:
                    System.out.println("未知命令");
            }
        }
        scanner.close();
    }
}当前数据保存在内存中,程序关闭即丢失。可通过以下方式改进:
基本上就这些。从定义模型开始,组织数据操作,再加个简单界面,就能跑起来。后续可加入预算提醒、图表展示等功能。不复杂但容易忽略细节,比如输入校验和日期处理。
以上就是如何在Java中实现个人记账应用的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号