Java个人记账系统
要求1:记录收入支出
要求2:输出txt文件
package jizhang;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class test {
public static void main(String[] args) throws IOException {
String details = "收支 收入金额 收支说明
";
double balance = 0;
double income_1=0;
double expend_1=0;
while (true) {
System.out.println("---------欢迎使用记账系统-------");
System.out.println("| 1.收支明细 |");
System.out.println("| 2.登记收入 |");
System.out.println("| 3.登记支出 |");
System.out.println("| 4.退出系统 |");
System.out.println("| 5.输出记录 |");
System.out.println("--------------------------------");
System.out.println("请输入您要执行的操作:(1-5)");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
while (choice != 1 && choice != 2 && choice != 3 && choice != 4&& choice !=5) {
System.out.println("对不起,没有此功能,请重新操作");//保证输入的选择在正确的范围内
int newChoice = in.nextInt();
choice = newChoice;
}
switch (choice) {
case 1:
System.out.println("--------记账系统--------收支明细--------");
System.out.println(details);
System.out.println("<<<<<<<<<<<<<查询完成>>>>>>>>>>>>
");
System.out.println("总支出:"+expend_1+"元");
System.out.println("总收入:"+income_1+"元");
System.out.println("当前余额:"+balance+"元"+"
");
System.out.println("<<<<<<<<<<<<<统计完成>>>>>>>>>>>>
");
break;
case 2:
System.out.println("--------记账系统--------登记收入--------");
System.out.println("请输入收入金额:");
double income = in.nextDouble();
System.out.println("请输入收入类别:");
System.out.println("类别:勤工助学、奖学金、父母汇款、兼职、其他");
String incomeDetail = in.next();
balance += income;
income_1+=income;
details = details + "收入 " +income +"元"+ " "+" " + incomeDetail + " " + "
";
System.out.println("<<<<<<<<<<<<<记录成功>>>>>>>>>>>>
");
break;
case 3:
System.out.println("--------记账系统--------登记支出--------");
System.out.println("请输入支出金额:");
double expend = in.nextDouble();
System.out.println("请消费类别:");
System.out.println("类别:购买文具、服饰、餐饮、交通、其他");
String expendDetail = in.next();
expend_1+=expend;
balance -= expend;
details = details + "支出 " + expend+"元" + " "+" " + expendDetail + " " + "
";
System.out.println("本次登记完成");
System.out.println("<<<<<<<<<<<<<记录成功>>>>>>>>>>>>
");
break;
case 4:
System.out.println("--------记账系统--------退出系统--------");
System.out.println("确定要退出吗?(Y/N)");
String isExit = in.next();
switch (isExit) {
case "Y":
System.out.println("<<<<<<<退出成功,欢迎下次使用>>>>>>>
");
return;
case "N":
System.out.println("<<<<<<<<<<<<<取消退出>>>>>>>>>>>>
"); }
break;
case 5:
System.out.println("--------记账系统--------输出记录--------
");
Writer write =new FileWriter("E://账单.txt");//文件的地址
String s= details+"
"+"总支出:"+expend_1+"元"+"
"+"总收入:"+income_1+"元"+"
"+"当前余额:"+balance+"元"+"
";
write.write(s);
write.close();
System.out.println("文件保存在E://账单.txt");
System.out.println("<<<<<<<<<<<<<输出成功>>>>>>>>>>>>
");
break;
}
}
}
}