个人记账系统

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 = "收支\t收入金额 \t\t收支说明 \n";
        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("<<<<<<<<<<<<<查询完成>>>>>>>>>>>>\n");
                    System.out.println("总支出:"+expend_1+"元");
                    System.out.println("总收入:"+income_1+"元");
                    System.out.println("当前余额:"+balance+"元"+"\n");
                    System.out.println("<<<<<<<<<<<<<统计完成>>>>>>>>>>>>\n");
                    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 + "收入\t" +income +"元"+ "\t"+" 	" + incomeDetail + "\t" + "\n";
                    System.out.println("<<<<<<<<<<<<<记录成功>>>>>>>>>>>>\n");
                    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 + "支出\t" + expend+"元" + "\t"+" 	" +  expendDetail + "\t" + "\n";
                    System.out.println("本次登记完成");
                    System.out.println("<<<<<<<<<<<<<记录成功>>>>>>>>>>>>\n");
                    break;
                case 4:
                    System.out.println("--------记账系统--------退出系统--------");
                    System.out.println("确定要退出吗?(Y/N)");
                    String isExit = in.next();
                    switch (isExit) {
                        case "Y":
                            System.out.println("<<<<<<<退出成功,欢迎下次使用>>>>>>>\n");
                            return;
                        case "N":
                        	System.out.println("<<<<<<<<<<<<<取消退出>>>>>>>>>>>>\n");  }
                    break;
                case 5:
                	System.out.println("--------记账系统--------输出记录--------\n");
                	Writer write =new FileWriter("E://账单.txt");//文件的地址
                	String s= details+"\n"+"总支出:"+expend_1+"元"+"\n"+"总收入:"+income_1+"元"+"\n"+"当前余额:"+balance+"元"+"\n";
            		write.write(s);
            		write.close();
            		System.out.println("文件保存在E://账单.txt");
                	System.out.println("<<<<<<<<<<<<<输出成功>>>>>>>>>>>>\n");
                	break;
            }
            

        }
    }

}

posted @ 2021-01-23 22:19  、晚风、  阅读(402)  评论(0编辑  收藏  举报