Java project — household income and expenditure recording program

Time:2021-10-15

1、 Project requirements and process description

Learning objectives of the project:

1. Simulate and implement a family income and expenditure recording program based on text interface (program console)

2. Master preliminary programming and debugging skills

3. Flexible use of basic knowledge

1. Requirements description

Simulate and implement a family revenue and expenditure recording program based on text interface (program console). The program can record family income and expenditure, and print revenue and expenditure details.

The project adopts hierarchical menu mode.

The main menu is as follows:

Specific requirements:

1. It is assumed that the basic living fund at the beginning of the family is 10000 yuan;

2. After each registration of income (menu 2), the amount of income shall be added to the basic fund, and the details of this income shall be recorded for subsequent query;

3. After each registration of expenditure (menu 3), the amount of expenditure shall be deducted from the basic fund, and the details of this expenditure shall be recorded for subsequent query;

4. When querying revenue and expenditure details (menu 1), a detailed list of all revenue and expenditure names will be displayed.

2. Process description

Project phase 1

First, realize the interface display function and enter each function!

Function code displayed on the main interface:

public static void menu() {
    System. Out. Println ("----------- household income and expenditure accounting software -----------");
    System.out.println();
    System. Out. Println ("\ t \ t \ T1. Revenue and expenditure details");
    System. Out. Println ("\ t \ t \ T2. Registration income");
    System. Out. Println ("\ t \ t \ T3. Register expenditure");
    System. Out. Println ("\ t \ t \ T4. Exit");
    System.out.println();
    System. Out. Print ("please select (1-4):");
}

Enter the code of each function (main method):

public static void main(String[] args) {
    init();
    boolean loopFlag = true;
    while (loopFlag) {
        menu();
        char n = Utility.readMenuSelection();
        switch (n) {
            case '1':
                list();
                System.out.println();
                break;
            case '2':
                income();
                break;
            case '3':
                expend();
                break;
            case '4':
                System. Out. Print ("confirm exit (Y / N):");
                char select = Utility.readConfirmSelection();
                if (select == 'Y')
                    loopFlag = false;
                break;
        }
    }
}

Project phase 2

Complete the main “revenue” and “expenditure” functions in the process of phase 1!

Revenue function:

1) Read the amount of revenue from the keyboard (int)

2) Read the reason of revenue from the keyboard (string)

3) Store data (array) as required

4) Added successfully

Revenue function code:

public static void income() {
    System. Out. Print ("revenue amount:");
    int amount = Utility.readNumber();
    System. Out. Print ("revenue reason:");
    String reason = Utility.readString();
    Storage ("revenue", amount, reason);
    System. Out. Println ("added successfully!");
}

Expenditure function:

1) Read the amount of expenditure from the keyboard (int)

2) Read the reason of expenditure from the keyboard (string)

3) Store data (array) as required

4) Added successfully

Expenditure function code:

public static void expend() {
    System. Out. Print ("expenditure amount:");
    int amount = Utility.readNumber();
    System. Out. Print ("expenditure reason:");
    String reason = Utility.readString();
    Storage ("expenditure", amount, reason);
    System. Out. Println ("added successfully!");
}

Project phase 3

After the bill details are displayed, you can traverse the array.


public static void list() {
    for (int i = 0; i <= r; i++) {
        for (int j = 0; j < 4; j++) {
            if (i == 0)
                System.out.print(arr[i][j] + "\t\t");
            else
                System.out.print(arr[i][j] + "\t\t  ");
        }
        System.out.println();
    }
}

2、 Complete source code and tool class

1. Source code

package Stage3;
import java.util.Scanner;
public class FamilyBookkeeping {
    static int r = 0, count = 10000;
    static String[][] arr = new String[100][4];// Inflexible
    public static void main(String[] args) {
        init();
        boolean loopFlag = true;
        while (loopFlag) {
            menu();
            char n = Utility.readMenuSelection();
            switch (n) {
                case '1':
                    list();
                    System.out.println();
                    break;
                case '2':
                    income();
                    break;
                case '3':
                    expend();
                    break;
                case '4':
                    System. Out. Print ("confirm exit (Y / N):");
                    char select = Utility.readConfirmSelection();
                    if (select == 'Y')
                        loopFlag = false;
                    break;
            }
        }
    }
    public static void menu() {
        System. Out. Println ("----------- household income and expenditure accounting software -----------");
        System.out.println();
        System. Out. Println ("\ t \ t \ T1. Revenue and expenditure details");
        System. Out. Println ("\ t \ t \ T2. Registration income");
        System. Out. Println ("\ t \ t \ T3. Register expenditure");
        System. Out. Println ("\ t \ t \ T4. Exit");
        System.out.println();
        System. Out. Print ("please select (1-4):");
    }
    public static void income() {
        System. Out. Print ("revenue amount:");
        int amount = Utility.readNumber();
        System. Out. Print ("revenue reason:");
        String reason = Utility.readString();
        Storage ("revenue", amount, reason);
        System. Out. Println ("added successfully!");
    }
    public static void expend() {
        System. Out. Print ("expenditure amount:");
        int amount = Utility.readNumber();
        System. Out. Print ("expenditure reason:");
        String reason = Utility.readString();
        Storage ("expenditure", amount, reason);
        System. Out. Println ("added successfully!");
    }
    public static void list() {
        for (int i = 0; i <= r; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == 0)
                    System.out.print(arr[i][j] + "\t\t");
                else
                    System.out.print(arr[i][j] + "\t\t  ");
            }
            System.out.println();
        }
    }
    public static void storage(String type, int amount, String reason) {
        r++;
        arr[r][0] = type;
        If (type = = "revenue"){
            arr[r][1] = (count + amount) + "";
            count += amount;
            arr[r][2] = "+" + amount;
        }
        If (type = = "expense"){
            arr[r][1] = (count - amount) + "";
            count -= amount;
            arr[r][2] = "-" + amount;
        }
        arr[r][3] = reason;
    }
    public static void init() {
        Arr [R] [0] = "revenue and expenditure";
        Arr [R] [1] = "total account amount";
        Arr [R] [2] = "revenue and expenditure amount";
        Arr [R] [3] = "description";
    }
}

2. Tools

The utility class is introduced to realize some wrong input judgment and make the program more perfect. (note that this tool class should be in the same package as the above source code)

The utility code of the tool class is as follows:

package Stage3;
import java.util.Scanner;
/**
 Utility utility classes:
 Encapsulating different functions into methods means that its functions can be used directly by calling methods without considering the specific function implementation details.
 */
public class Utility {
    private static Scanner scanner = new Scanner(System.in);
    /**
     Used to select the interface menu. This method reads the keyboard. If the user types any character from '1' - '4', the method returns. The return value is the character typed by the user.
     */
    public 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 ("wrong selection, please re-enter:");
            } else break;
        }
        return c;
    }
    /**
     Used for input of revenue and expenditure amount. This method reads an integer with a length of no more than 4 bits from the keyboard and takes it as the return value of the method.
     */
    public static int readNumber() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(4);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System. Out. Print ("digital input error, please re-enter:");
            }
        }
        return n;
    }
    /**
     Input for revenue and expense description. This method reads a string no longer than 8 bits from the keyboard and takes it as the return value of the method.
     */
    public static String readString() {
        String str = readKeyBoard(8);
        return str;
    }
    /**
     The input used to confirm the selection. This method reads' y 'or' n 'from the keyboard and takes it as the return value of the method.
     */
    public static char readConfirmSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print ("wrong selection, please re-enter:");
            }
        }
        return c;
    }

    private static String readKeyBoard(int limit) {
        String line = "";
        while (scanner.hasNext()) {
            line = scanner.nextLine();
            if (line.length() < 1 || line.length() > limit) {
                System.out.print ("the input length (not greater than" + limit + ") is wrong, please re-enter:");
                continue;
            }
            break;
        }
        return line;
    }
}

3、 Display of running results of program part

summary

That’s all for this article. I hope it can help you and pay more attention to developeppaer!

Recommended Today

SQL statement of three-level linkage of provinces, cities and counties

The first is the table creation statement Copy codeThe code is as follows: CREATE TABLE `t_address_province` ( `id` INT AUTO_ Increment primary key comment ‘primary key’,`Code ` char (6) not null comment ‘province code’,`Name ` varchar (40) not null comment ‘province name’)Engine = InnoDB default charset = utf8 comment = ‘province information table’; CREATE TABLE […]