Java项目之家庭收支记账软件

模拟实现基于文本界面的家庭记账软件,该软件能够记录家庭的收入支出,并能够打印收支明细表。

项目采用分级菜单方式。主菜单如下:

假设家庭起始的生活基本金为10000元。

每次登记收入(菜单2)后,收入的金额应累加到基本金上,并记录本次收入明细,以便后续的查询。

每次登记支出(菜单3)后,支出的金额应从基本金中扣除,并记录本次收入明细,以便后续的查询。

查询收支明细(菜单1)时,将显示所有的收入、支出明细列表。 

“登记收入”的界面及操作过程如下所示:

“登记支出”的界面及操作过程如下所示:

“收支明细”的界面及操作过程如下所示: 

  “退出”的界面及操作过程如下所示:

第1步:实现主程序结构

1.创建FamilyAccount类及main方法

2.在main方法中,参照主流程,实现程序主体结构

3.测试程序,确认可以正常执行第1和第4菜单选项

第2步:实现收入和支出登记处理

1.在main方法中,参照收入和支出流程,实现“登记收入”功能

2.测试“登记收入”功能

3.在main方法中,参照收入和支出流程,实现“登记支出”功能

4.测试“登记支出”功能

 1 public class FamilyAccount {
 2     public static void main(String[] args) {
 3         String details = "收支\t账户金额\t收支金额\t说    明\n";
 4         int balance = 10000;
 5 
 6         boolean loopFlag = true;
 7         do {
 8             System.out.println("\n-----------------家庭收支记账软件-----------------\n");
 9             System.out.println("                   1 收支明细");
10             System.out.println("                   2 登记收入");
11             System.out.println("                   3 登记支出");
12             System.out.println("                   4 退    出\n");
13             System.out.print("                   请选择(1-4):");
14             
15             char key = Utility.readMenuSelection();
16             System.out.println();
17             switch (key) {
18                 case '1':
19                     System.out.println("-----------------当前收支明细记录-----------------");
20                     System.out.println(details);
21                     System.out.println("--------------------------------------------------");
22                     break;
23                 case '2':
24                     System.out.print("本次收入金额:");
25                     int amount1 = Utility.readNumber();
26                     System.out.print("本次收入说明:");
27                     String desc1 = Utility.readString();
28 
29                     balance += amount1;
30                     details += "收入\t" + balance + "\t\t" +
31                                amount1 + "\t\t" + desc1 + "\n";
32                     System.out.println("---------------------登记完成---------------------");
33                     break;
34                 case '3':
35                     System.out.print("本次支出金额:");
36                     int amount2 = Utility.readNumber();
37                     System.out.print("本次支出说明:");
38                     String desc2 = Utility.readString();
39 
40                     balance -= amount2;
41                     details += "支出\t" + balance + "\t\t" +
42                                amount2 + "\t\t" + desc2 + "\n";
43                     System.out.println("---------------------登记完成---------------------");
44                     break;
45                 case '4':
46                     System.out.print("确认是否退出(Y/N):");
47                     char yn = Utility.readConfirmSelection();
48                     if (yn == 'Y') loopFlag = false;
49                     break;
50             }
51         } while (loopFlag);
52     }    
53 }
 1 import java.util.Scanner;
 2 /**
 3 Utility工具类:
 4 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
 5 */
 6 public class Utility {
 7     private static Scanner scanner = new Scanner(System.in);
 8     /**
 9     用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
10     */
11     public static char readMenuSelection() {
12         char c;
13         for (; ; ) {
14             String str = readKeyBoard(1);
15             c = str.charAt(0);
16             if (c != '1' && c != '2' && c != '3' && c != '4') {
17                 System.out.print("选择错误,请重新输入:");
18             } else break;
19         }
20         return c;
21     }
22     /**
23     用于收入和支出金额的输入。该方法从键盘读取一个不超过4位长度的整数,并将其作为方法的返回值。
24     */
25     public static int readNumber() {
26         int n;
27         for (; ; ) {
28             String str = readKeyBoard(4);
29             try {
30                 n = Integer.parseInt(str);
31                 break;
32             } catch (NumberFormatException e) {
33                 System.out.print("数字输入错误,请重新输入:");
34             }
35         }
36         return n;
37     }
38     /**
39     用于收入和支出说明的输入。该方法从键盘读取一个不超过8位长度的字符串,并将其作为方法的返回值。
40     */
41     public static String readString() {
42         String str = readKeyBoard(8);
43         return str;
44     }
45     
46     /**
47     用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
48     */
49     public static char readConfirmSelection() {
50         char c;
51         for (; ; ) {
52             String str = readKeyBoard(1).toUpperCase();
53             c = str.charAt(0);
54             if (c == 'Y' || c == 'N') {
55                 break;
56             } else {
57                 System.out.print("选择错误,请重新输入:");
58             }
59         }
60         return c;
61     }
62     
63     
64     private static String readKeyBoard(int limit) {
65         String line = "";
66 
67         while (scanner.hasNext()) {
68             line = scanner.nextLine();
69             if (line.length() < 1 || line.length() > limit) {
70                 System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
71                 continue;
72             }
73             break;
74         }
75 
76         return line;
77     }
78 }

猜你喜欢

转载自www.cnblogs.com/ZengBlogs/p/12153306.html
今日推荐