Java project – household accounting program

The main requirement of this project is to simulate the implementation of a text-based interface “household accounting software”, which can record the family’s income and expenditure, and can print the income and expenditure schedule.

1. The item adopts hierarchical menu method.

The main menu is as follows:

—————–Household income and expenditure accounting software—————–

1 Details of income and expenditure

2 Register income

3 Register expenses

4 exit

Please choose (1-4):_

  • Assume that the family’s initial basic living allowance is 10,000 yuan
  • Menu 2: After each income is registered, the amount of income should be added to the basic fund, and the details of this income should be recorded for subsequent inquiries.
  • Menu 3: After each registration of expenditure, the amount of expenditure should be deducted from the basic fund, and the details of this expenditure should be recorded for subsequent inquiries.
  • Menu 1: When inquiring about income and expenditure details, a list of all income and expenditure details will be displayed

2. Implementation process

The main activity flow chart of the specific operation is shown in the figure below:

1. Implement interface display function

——————-Household income and expenditure accounting software———————

1. Income and expenditure details
2. Register income
3. Register spending
4. Exit

Please choose (1-4):

2. Complete the main “income” and “expenditure” functions

  • The amount of income read from the keyboard (int)
  • Reason for reading income from keyboard (String)
  • Store data (array) as required
  • Added successfully

3. Display bill details

3. Implementation code

Main program flow:

package txt;

import java.util.Scanner;
public class table {
    static int r = 0, count = 10000;
    static String[][] arr = new String[10][4];

    public static void main(String[] args) {
        init();
        boolean loopFlag = true;
        while(loopFlag){
            System.out.println("-------------------household income and expenditure accounting software ------------------ ---\
");
            System.out.println("1. Income and expenditure details");
            System.out.println("2. Registration income");
            System.out.println("3. Register expenses");
            System.out.println("4. Exit\
");
            System.out.println();
            System.out.print("Please choose (1-4):");
            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("Are you sure to exit (Y/N):");
                    char selsect = Utility. readConfirmSelection();
                    if(selsect=='Y')
                    loopFlag = false;
                    break;
            }
        }

    }

    public static void init() {
        arr[r][0]="income and expenditure";
        arr[r][1]="Account total amount";
        arr[r][2]="Amount of revenue and expenditure";
        arr[r][3]="Description";
    }

    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 income() {
        System.out.print("Amount of income:");
        int amount = Utility. readNumber();
        System.out.print("Reason for income");
        String reason = Utility. readString();
        storage("income", amount, reason);
        System.out.println("Add successfully!");
    }
    public static void storage(String type, int amount, String reason) {
        r + + ;
        arr[r][0]=type;
        if(type=="income"){
            arr[r][1]=(count + amount) + "";
            count + =amount;
            arr[r][2]=" + " + amount;
        }
        if(type=="expenditure"){
            arr[r][1]=(count-amount) + "";
            count-=amount;
            arr[r][2]=" + " + amount;
        }
        arr[r][3]=reason;
    }
    public static void expend() {
        System.out.print("Expenditure amount: ");
        int amount = Utility. readNumber();
        System.out.print("Expense reason");
        String reason = Utility. readString();
        storage("Expenditure", amount, reason);
        System.out.println("Add successfully!");
    }
}

Utility tool class:

package txt;
import java.util.Locale;
import java.util.Scanner;
public class Utility {
    private static Scanner scanner=new Scanner(System.in);

    public static char readMenuSelection() {
        char c;
        for(;;){
            String str = readKeyBoard(1);
            c=str.charAt(0);//take the first character of the string
            if(c!='1' & amp; & amp;c!='2' & amp; & amp;c!='3' & amp; & amp;c!='4'){
                System.out.print("Wrong choice, please re-acquaintance:");
            } else break;
        }
        return c;
    }
    public static String readKeyBoard(int limit) {
        String line="";
        while(scanner.hasNext()){ //hasNext() method determines whether the input (file, character string, keyboard and other input streams) has the next input item, if so, returns true, otherwise false.
            line = scanner. nextLine();
            if(line.length()<1||line.length()>limit){ //limit: input string length limit
                System.out.print("Input length (not greater than " + limit + ") error, please re-enter:");
                continue;
            }
            break;
        }
        return line;
    }
    public static int readNumber() {
        int n;
        for(;;){
            String str = readKeyBoard(4);
            try {
                n=Integer.parseInt(str);//Integer.parseInt() is a method under the Integer wrapper class, its function is to convert the string type string in () into int type
                break;
            }catch(NumberFormatException e){
                System.out.print("The number is wrong, please re-enter:");
            }
            /*try {
            } {
                  Code that may throw exceptions
                    } catch (exception class name A e) {
                          If an exception of type A occurs, execute this code
                    }*/
    }
        return n;
    }
    public static String readString() {
        String str=readKeyBoard(8);//judging that the length of the string is not greater than 8
        return str;
    }
    public static char readConfirmSelection() {
        char c;
        for(;;){
            String str=readKeyBoard(1).toUpperCase();//Convert lowercase letters to uppercase
            c=str.charAt(0);
            if(c=='Y'||c=='N'){
                break;
            }else{
                System.out.print("The selection is wrong, please re-enter:");
            }
        }
        return c;
    }
}

4. Code implementation results

——————-Household income and expenditure accounting software———————

1. Income and expenditure details
2. Register income
3. Register spending
4. Exit

Please choose (1-4): 2
Amount of income: 100
income reason salary
Added successfully!
——————-Household income and expenditure accounting software———————

1. Income and expenditure details
2. Register income
3. Register spending
4. Exit

Please choose (1-4): 3
Spend Amount: 150
reason for spending to eat
Added successfully!
——————-Household income and expenditure accounting software———————

1. Income and expenditure details
2. Register income
3. Register spending
4. Exit

Please choose (1-4): 1
Income and expenditure Total account amount Income and expenditure amount Description
Income 10100 + 100 salary
Spend 9950 + 150 for meals

——————-Household income and expenditure accounting software———————

1. Income and expenditure details
2. Register income
3. Register spending
4. Exit

Please choose (1-4): 4
Whether to confirm to exit (Y/N): y