答案:该记账本应用通过Entry类定义收支条目,AccountBook类实现添加、查看和统计功能,使用ArrayList存储数据,支持控制台交互操作,并可扩展文件持久化。

做一个简易的记账本应用,核心是记录收入和支出,支持添加、查看和统计功能。Java 适合实现这种结构清晰的小项目,结合类、集合和基础IO操作就能完成。
每一条记账数据都应包含时间、金额、类型(收入/支出)和备注。
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;
}}
使用 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("无效选择,请重试。");
}
}
}}
创建主类启动应用。
public class Main {
public static void main(String[] args) {
AccountBook book = new AccountBook();
book.start();
}
}
如果想让数据持久化,可以简单保存到文本文件:
小提示:输入校验(比如金额是否数字)可以在实际中加入 try-catch 避免崩溃。
基本上就这些。代码结构清晰,适合初学者练手,也能扩展成带界面或数据库的版本。
以上就是如何用Java实现简易的记账本应用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号