Java里如何实现简易记账软件_记账软件开发项目实例解析

答案:该记账软件实现收支记录、查询、统计与文件持久化。通过Bill类存储账单信息,BillManager管理账单并处理数据存取,Main类提供用户交互菜单,支持添加、查看、查询和统计功能,数据保存至文本文件,程序重启后仍可读取,适合Java初学者掌握面向对象与IO操作。

开发一个简易记账软件在Java中是一个非常适合初学者练手的项目。它涵盖了面向对象编程、文件读写、集合操作和基础用户交互等核心知识点。下面通过一个具体实例,带你一步步实现一个命令行版的简易记账系统。

1. 项目需求分析

我们要实现的功能包括:

  • 记录每一笔收支(类型、金额、时间、备注)
  • 查看所有账单记录
  • 按类型查询(如“餐饮”、“工资”)
  • 统计总收入、总支出和结余
  • 数据持久化保存到文件

2. 类设计与结构

根据需求,我们设计以下几个类:

Bill类:表示一条账单记录
public class Bill {
    private String type;     // 收支类型,如“餐饮”、“工资”
    private double amount;   // 金额
    private String date;     // 日期
    private String remark;   // 备注
public Bill(String type, double amount, String date, String remark) {
    this.type = type;
    this.amount = amount;
    this.date = date;
    this.remark = remark;
}

// getter方法(setter可根据需要添加)
public String getType() { return type; }
public double getAmount() { return amount; }
public String getDate() { return date; }
public String getRemark() { return remark; }

@Override
public String toString() {
    return "类型:" + type + " | 金额:" + amount +
           " | 日期:" + date + " | 备注:" + remark;
}

}

BillManager类:管理账单的核心逻辑

import java.util.*;

public class BillManager { private List bills; private final String FILE_PATH = "bills.txt";

public BillManager() {
    bills = new ArrayListzuojiankuohaophpcnyoujiankuohaophpcn();
    loadFromFile(); // 启动时加载数据
}

// 添加账单
public void addBill(Bill bill) {
    bills.add(bill);
    saveToFile();
}

// 显示所有账单
public void showAllBills() {
    if (bills.isEmpty()) {
        System.out.println("暂无账单记录。");
        return;
    }
    for (int i = 0; i zuojiankuohaophpcn bills.size(); i++) {
        System.out.println((i+1) + ". " + bills.get(i));
    }
}

// 按类型查询
public void searchByType(String type) {
    ListzuojiankuohaophpcnBillyoujiankuohaophpcn result = new ArrayListzuojiankuohaophpcnyoujiankuohaophpcn();
    for (Bill b : bills) {
        if (b.getType().equals(type)) {
            result.add(b);
        }
    }
    if (result.isEmpty()) {
        System.out.println("未找到该类型的记录。");
    } else {
        System.out.println("【" + type + "】相关记录:");
        for (Bill b : result) {
            System.out.println(b);
        }
    }
}

// 统计收支情况
public void showSummary() {
    double income = 0, expense = 0;
    for (Bill b : bills) {
        if (b.getAmount() youjiankuohaophpcn 0) {
            income += b.getAmount();
        } else {
            expense += Math.abs(b.getAmount());
        }
    }
    System.out.println("总收入:" + income);
    System.out.println("总支出:" + expense);
    System.out.println("结余:" + (income - expense));
}

// 保存到文件(简单文本格式)
private void saveToFile() {
    try (java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(FILE_PATH))) {
        for (Bill b : bills) {
            out.println(b.getType() + "," + b.getAmount() + "," + b.getDate() + "," + b.getRemark());
        }
    } catch (Exception e) {
        System.out.println("保存失败:" + e.getMessage());
    }
}

// 从文件加载
private void loadFromFile() {
    java.io.File file = new java.io.File(FILE_PATH);
    if (!file.exists()) return;

    try (java.util.Scanner scanner = new java.util.Scanner(file)) {
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] parts = line.split(",");
            if (parts.length == 4) {
                String type = parts[0];
                double amount = Double.parseDouble(parts[1]);
                String date = parts[2];
                String remark = parts[3];
                bills.add(new Bill(type, amount, date, remark));
            }
        }
    } catch (Exception e) {
        System.out.println("读取数据失败:" + e.getMessage());
    }
}

}

Main类:主程序入口,提供菜单交互

import java.util.Scanner;

public class Main { public static void main(String[] args) { BillManager manager = new BillManager(); Scanner input = new Scanner(System.in); int choice;

    do {
        System.out.println("\n--- 简易记账系统 ---");
        System.out.println("1. 添加账单");
        System.out.println("2. 查看所有账单");
        System.out.println("3. 查询某类型记录");
        System.out.println("4. 查看收支统计");
        System.out.println("0. 退出");
        System.out.print("请选择操作:");

        choice = input.nextInt();
        input.nextLine(); // 消费换行符

        switch (choice) {
            case 1:
                System.out.print("类型:"); String type = input.nextLine();
                System.out.print("金额(收入为正,支出为负):"); double amount = input.nextDouble();
                input.nextLine(); // 消费换行
                System.out.print("日期(如2025-04-05):"); String date = input.nextLine();
                System.out.print("备注:"); String remark = input.nextLine();
                manager.addBill(new Bill(type, amount, date, remark));
                System.out.println("添加成功!");
                break;
            case 2:
                manager.showAllBills();
                break;
            case 3:
                System.out.print("请输入要查询的类型:");
                String queryType = input.nextLine();
                manager.searchByType(queryType);
                break;
            case 4:
                manager.showSummary();
                break;
            case 0:
                System.out.println("再见!");
                break;
            default:
                System.out.println("无效选择,请重试。");
        }
    } while (choice != 0);

    input

.close(); }

}

3. 运行效果示例

运行程序后,你可以进行如下操作:

  • 添加一笔“工资”收入:8000元
  • 添加一笔“餐饮”支出:-50元
  • 查看所有记录,确认已保存
  • 查询“餐饮”类别,只显示相关条目
  • 查看统计:总收入8000,总支出50,结余7950

关闭程序再打开,数据依然存在,因为已保存到bills.txt文件中。

4. 可扩展方向

这个版本是基础实现,后续可以增强:

  • 使用JSON或数据库替代文本存储
  • 增加日期排序功能
  • 加入图形界面(Swing或JavaFX)
  • 支持导入导出CSV
  • 添加预算提醒功能

基本上就这些。这个项目虽小,但完整体现了Java开发的基本流程:需求 → 设计 → 编码 → 测试。掌握它,对理解实际应用开发很有帮助。