首页 > 编程知识 正文

家庭收支记账软件java,家庭收支记账软件app

时间:2023-05-05 20:37:10 阅读:251555 作者:522

需求 目标

模拟实现一个基于文本界面的《家庭记账软件》

需求




实现 第一次实现版 package com.oceanstar;import java.util.Scanner;public class FamilyAccount { private static int baseMoney = 10000; private static String datails = ""; private static Scanner scanner = new Scanner(System.in); public static void mainMenu(){ System.out.print("-----------------家庭收支记账软件-----------------n" + "1 收支明细n" + "2 登记收入n" + "3 登记支出n" + "4 退 出n" + "请选择(1-4):"); } public static void income(){ System.out.print("本次收入金额:"); int t = scanner.nextInt(); if (t < 0){ System.out.println("请输入一个正整数"); return; } baseMoney = baseMoney + t; System.out.print("本次收入说明:"); String s = scanner.next(); datails = datails + "收入t" + baseMoney + "t" + t + "t" + s + "n"; } public static void pay(){ System.out.print("本次支出金额:"); int t = scanner.nextInt(); if (t < 0){ System.out.println("请输入一个正整数"); return; } baseMoney = baseMoney - t; System.out.print("本次支出说明:"); String s = scanner.next(); datails = datails + "支出t" + baseMoney + "t" + t + "t" + s + "n"; } public static void showdetails(){ System.out.println("-----------------当前收支明细记录-----------------"); System.out.println("收支t账户金额t收支金额t说明"); System.out.println(datails); } public static void main(String[] args) { while (true){ mainMenu(); Scanner scanner = new Scanner(System.in); int type = scanner.nextInt(); if (type == 4){ break; }else if (type == 1){ showdetails(); }else if (type == 2){ income(); }else if (type == 3){ pay(); } } }}

评价:好混乱,而且没有错误处理机制:比如nextInt()只能输入int类型的数据,如果不小心输入字符串就会出现异常

改进:以后写程序时注意尽量不要把输入提示和操作混合再一起

第二版 package com.oceanstar;import java.util.Scanner;public class FamilyAccount { private static Scanner scanner = new Scanner(System.in); // 一定能得到n位数 public static String readKeyBoard(int limit) { String line = ""; while (scanner.hasNext()) { line = scanner.nextLine(); if (line.length() < 1 || line.length() > limit) { System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:"); continue; } break; } return line; } public static String readString(){ // 因为这个函数得到的已经是小于等于8长度的字符串了,所以不需要处理,直接返回即可 return readKeyBoard(8); // 各个函数一定是相对封闭的,写函数的时候要确保这个函数独立正确 } // 直到输入正确才跳出循环: 这里它确保了输入正确 private static char readMenuSelection(){ char c; for (; ; ) { String str = readKeyBoard(1); c = str.charAt(0); if (c != '1' && c != '2' && c != '3' && c != '4') { System.out.print("选择错误,请重新输入:"); } else break; } return c; } // 确保能够得到'Y', 'y', 'N', 'n' private static char readConfirmSelection(){ char c; for (; ; ) { String str = readKeyBoard(1); c = str.charAt(0); if (c != 'Y' && c != 'y' && c != 'N' && c != 'n') { System.out.print("选择错误,请重新输入:"); } else break; } return c; } // 直到输入正确才能跳出循环 private static int readNumber(){ int i = 0; for (; ; ) { String str = readKeyBoard(4); try { i = Integer.parseInt(str); if (i < 0){ throw new Exception("必须输入一个正整数"); } break; }catch (Exception e){ System.out.print("必须输入一个正整数:"); } } return i; } public static void main(String[] args) { int balance = 10000; String details = ""; while (true){ System.out.print("-----------------家庭收支记账软件-----------------n" + "1 收支明细n" + "2 登记收入n" + "3 登记支出n" + "4 退 出n" + "请选择(1-4):"); char c = readMenuSelection(); // 工具一定是正确的输出 switch (c){ case '1': System.out.println("-----------------当前收支明细记录-----------------"); System.out.println("收支t账户金额t收支金额t说明"); System.out.println(details); System.out.println("---------------------------------------------"); break; case '2': System.out.println("----------------------本次收入-----------------------"); System.out.print("本次收入金额:"); int t = readNumber();// 工具得到的数一定是正整数 balance = balance + t; System.out.print("本次收入说明:"); String s = readString(); details = details + "收入t" + balance + "t" + t + "t" + s + "n"; System.out.println("---------------------登记完成-------------------"); break; case '3': System.out.println("----------------------本次支出-----------------------"); System.out.print("本次支出金额:"); int t1 = readNumber();// 工具得到的数一定是正整数 if (t1 > balance){ System.out.println("余额不足"); break; } balance = balance - t1; System.out.print("本次支出说明:"); String s1 = readString(); details = details + "支出t" + balance + "t" + t1 + "t" + s1 + "n"; System.out.println("---------------------登记完成-------------------"); break; case '4': System.out.print("确认是否退出(Y/N):"); char d = Character.toLowerCase(readConfirmSelection()); if (d == 'y'){ return; } break; } } }}

最大感触:
*写程序时注意尽量不要把输入提示和操作混合再一起

尽量拦住不正确的操作

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。