Family income and expenditure of the Java project accounting software

Home-based accounting software simulation to achieve a text interface, the software is able to record the family's income and expenditure, balance of payments and the ability to print list.

Project hierarchical menu mode. Main Menu as follows:

The basic assumption that gold family life starting as 10,000 yuan.

After each registration income (menu 2), the amount of revenue to be accumulated on the base metal, and this record earnings details for subsequent query.

After each registration expenses (menu 3), the amount of expenditure should be the basic deduct and record details of this income, so that subsequent queries.

Query income and expenditure details (menu 1), will show all the income and expenditure details of the list. 

And procedure "registered income" interface operation is as follows:

Interface and operation "register expenditure" as follows:

"Detailed balance" interface and the operation is as follows: 

  "Exit" interface and the operation is as follows:

Step 1: The main program implemented structure

1. Create FamilyAccount class and main method

2. In the main method, with reference to the main flow, the main structure implementor

3. Test procedures can be performed to confirm the first and fourth normal menu options

 

Step 2: revenue and expenditure registration process

1. In the main method, with reference to income and expenditure processes to achieve "Revenue registration" function

2. Test "Registration of income" feature

3. In the main method, with reference to income and expenditure processes to achieve "Registration expenditure" function

4. Test the "Registration spending" feature

 

 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 }

Guess you like

Origin www.cnblogs.com/ZengBlogs/p/12153306.html