首页 > Java > java教程 > 正文

如何用Java实现简易的记账本应用

P粉602998670
发布: 2025-10-05 11:00:07
原创
194人浏览过
答案:该记账本应用通过Entry类定义收支条目,AccountBook类实现添加、查看和统计功能,使用ArrayList存储数据,支持控制台交互操作,并可扩展文件持久化。

如何用java实现简易的记账本应用

做一个简易的记账本应用,核心是记录收入和支出,支持添加、查看和统计功能。Java 适合实现这种结构清晰的小项目,结合类、集合和基础IO操作就能完成。

1. 设计记账条目类(Entry)

每一条记账数据都应包含时间、金额、类型(收入/支出)和备注。

public class Entry {
    private String date;      // 记录日期
    private double amount;    // 金额
    private String type;      // 类型:income 或 expense
    private String remark;    // 备注
<pre class='brush:java;toolbar:false;'>public Entry(String date, double amount, String type, String remark) {
    this.date = date;
    this.amount = amount;
    this.type = type;
    this.remark = remark;
}

public String getDate() { return date; }
public double getAmount() { return amount; }
public String getType() { return type; }
public String getRemark() { return remark; }

@Override
public String toString() {
    return date + " | " + amount + " | " + type + " | " + remark;
}
登录后复制

}

AppMall应用商店
AppMall应用商店

AI应用商店,提供即时交付、按需付费的人工智能应用服务

AppMall应用商店 56
查看详情 AppMall应用商店

2. 实现记账本主功能(AccountBook)

使用 ArrayList 存储所有条目,提供增、查、统计功能。

立即学习Java免费学习笔记(深入)”;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
<p>public class AccountBook {
private List<Entry> records;
private Scanner scanner;</p><pre class='brush:java;toolbar:false;'>public AccountBook() {
    records = new ArrayList<>();
    scanner = new Scanner(System.in);
}

// 添加记录
public void addEntry() {
    System.out.print("日期 (如 2025-04-05): ");
    String date = scanner.nextLine();

    System.out.print("金额: ");
    double amount = Double.parseDouble(scanner.nextLine());

    System.out.print("类型 (income/expense): ");
    String type = scanner.nextLine();

    System.out.print("备注: ");
    String remark = scanner.nextLine();

    records.add(new Entry(date, amount, type, remark));
    System.out.println("记录已添加!");
}

// 查看所有记录
public void viewAll() {
    if (records.isEmpty()) {
        System.out.println("暂无记录。");
        return;
    }
    System.out.println("\n--- 所有记录 ---");
    for (Entry e : records) {
        System.out.println(e);
    }
}

// 统计收支情况
public void showSummary() {
    double income = 0, expense = 0;
    for (Entry e : records) {
        if ("income".equals(e.getType())) {
            income += e.getAmount();
        } else if ("expense".equals(e.getType())) {
            expense += e.getAmount();
        }
    }
    System.out.printf("\n--- 统计 ---\n总收入: %.2f\n总支出: %.2f\n结余: %.2f\n", income, expense, income - expense);
}

// 启动菜单
public void start() {
    while (true) {
        System.out.println("\n=== 简易记账本 ===");
        System.out.println("1. 添加记录");
        System.out.println("2. 查看所有记录");
        System.out.println("3. 显示统计");
        System.out.println("4. 退出");
        System.out.print("请选择: ");

        String choice = scanner.nextLine();
        switch (choice) {
            case "1":
                addEntry();
                break;
            case "2":
                viewAll();
                break;
            case "3":
                showSummary();
                break;
            case "4":
                System.out.println("再见!");
                return;
            default:
                System.out.println("无效选择,请重试。");
        }
    }
}
登录后复制

}

3. 编写主程序入口

创建主类启动应用。

public class Main {
    public static void main(String[] args) {
        AccountBook book = new AccountBook();
        book.start();
    }
}
登录后复制

4. 可选增强功能

如果想让数据持久化,可以简单保存到文本文件:

  • 用 PrintWriter 将每条记录写入文件
  • 启动时用 Scanner 读取文件恢复数据
  • 格式可为:日期,金额,类型,备注 每行一条

小提示:输入校验(比如金额是否数字)可以在实际中加入 try-catch 避免崩溃。

基本上就这些。代码结构清晰,适合初学者练手,也能扩展成带界面或数据库的版本。

以上就是如何用Java实现简易的记账本应用的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号