Small program design of household income and expenditure account

introduction

Friends, after learning the knowledge of arrays and methods for so long, today we will complete the design of a small program, complete it with our knowledge, and learn new knowledge. You can go directly to the end of the article to see the source code

catalogue

introduction

1, Demand

2, Solution

1. Realize the interface display function

         2. Call of utili function

3. Definition of functional method

Source code part

1. Main method code

2. Function method code

3.utility tools

1, Demand

 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 future query.

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

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

II. Solutions

1. Realize the interface display function

This function can be output using output statements

 

 System.out.println("-------------------Household income and expenditure accounting software---------------------");
            System.out.println("                    1.Revenue and expenditure details");
            System.out.println("                    2.Registered income");
            System.out.println("                    3.Registered expenditure");
            System.out.println("                    4.retreat    Out");
            System.out.print("Please select (1)-4): ");

2. Call of utili function

How to make him choose? Here, you can use the utility tool class given in the topic to call, because our bookkeeping needs constant calculation, and we don't know how many accounts I calculate, which can be realized by using the while loop statement.

 //Total bookkeeping items
        String[][] arr = new String[250][4];
        //Control bookkeeping times
        int count = 0;
        //Initial general ledger
        int sumNum = 10000;
        while (true) {
            System.out.println("-------------------Household income and expenditure accounting software---------------------");
            System.out.println("                    1.Revenue and expenditure details");
            System.out.println("                    2.Registered income");
            System.out.println("                    3.Registered expenditure");
            System.out.println("                    4.retreat    Out");
            System.out.print("Please select (1)-4): ");
            char input = Utility.readMenuSelection();

This utility class is well-defined and can be used directly. I will put all the source code at the end of the text.

If you want to realize the corresponding function of the selected number, you can use a branch statement to control it to enter the corresponding function block. For the if else statement I choose on the branch statement side, readers can choose the statement they are good at.

3. Definition of functional method

There are four function blocks we want to implement. First:

Revenue and expenditure details:

public static void getForm(String[][] arr) {
        System.out.println("-------------------Detailed record of revenue and expenditure-----------------------");
        System.out.println("Revenue and expenditure\t\t Total account amount\t\t Revenue and expenditure amount\t\t explain");
        for (int j = 0; j < arr.length; j++) {

            if (arr[j][0] != null) {
                System.out.println(arr[j][0] + "\t\t" + arr[j][1] + "\t\t\t" + arr[j][2] + "\t\t\t" + arr[j][3]);
            }
        }
        System.out.println("--------------------------------------------------------\n");
    }

According to the requirements of the title, what we want to achieve is the corresponding value under each item. Here, j is the two-dimensional element of the two-dimensional array. It controls how many accounts we keep. I have defined 250, so this cycle will be executed 250 times. This arr[j][0] represents revenue or expenditure. If the traversed values are not empty, the values they represent will be output and handed over to the next function block for implementation.

Second: registered income

  public static int getIncome(String[][] arr, int count, int sum) {
        System.out.println("Please enter the amount of this income:");
        int num = Utility.readNumber();
        System.out.println("Please enter the description of this income:");
        String info = Utility.readString();
        arr[count][0] = "income";
        sum += num;
        arr[count][1] = String.valueOf(sum);
        arr[count][2] = String.valueOf(num);
        arr[count][3] = info;
        count++;//The index increases and the next set of data in the two-dimensional array moves down
        System.out.println("Successfully added!");
        return sum;
    }

Define a method with the return value of int type, pass in the arr array of String type, a value controlling the bill item, and a total value.

Similarly, for the value entered by the user, we call the utility method, and the description is the same. Define the one bit array value of each element of the two-dimensional array. 0 corresponds to income, the total value plus the current flow, 1 corresponds to the total account value, 2 corresponds to the current flow, and 3 corresponds to remarks. Finally, return our total value. Note: if the total value is not returned, it will always call the initial value for calculation.

Third, expenditure registration

The principle is the same as that of income, but the total value becomes minus, so you can put the code directly:

public static int getSpend(String[][] arr, int count, int sumNum) {
        System.out.println("Please enter the amount of this expenditure:");
        int num1 = Utility.readNumber();
        System.out.println("Please enter the description of this expenditure:");
        String info1 = Utility.readString();
        arr[count][0] = "expenditure";
        sumNum -= num1;
        arr[count][1] = String.valueOf(sumNum);
        arr[count][2] = String.valueOf(num1);
        arr[count][3] = info1;
        count++;
        System.out.println("Successfully added!");
        return sumNum;

    }

After that, we need to define a main method so that it can run and call the method we just defined

if (input == '1') {
                getForm(arr);
            } else if (input == '2') {
                sumNum = getIncome(arr, count, sumNum);
                count++;

            } else if (input == '3') {

                sumNum = getSpend(arr, count, sumNum);
                count++;
            } else if (input == '4') {
                System.out.println("Confirm whether to exit( Y/N)?:");
                char c1 = Utility.readConfirmSelection();
                if (c1 == 'Y') {
                    System.out.println("bye-bye! Exit succeeded!");
                    return;
                }
            } else {
                System.out.println("Your input is incorrect, please re-enter");
            }

This is very simple. I believe everyone can understand it. Then we will complete our splicing work and this small program will be completed. Now please look at the source code directly.

Source code part

1. Main method code

package project;


import static project.GetFunction.*;

public class HouseBalanceSheet1 {
    public static void main(String[] args) {
        //Total bookkeeping items
        String[][] arr = new String[250][4];
        //Control bookkeeping times
        int count = 0;
        //Initial general ledger
        int sumNum = 10000;
        while (true) {
            System.out.println("-------------------Household income and expenditure accounting software---------------------");
            System.out.println("                    1.Revenue and expenditure details");
            System.out.println("                    2.Registered income");
            System.out.println("                    3.Registered expenditure");
            System.out.println("                    4.retreat    Out");
            System.out.print("Please select (1)-4): ");
            char input = Utility.readMenuSelection();
            if (input == '1') {
                getForm(arr);
            } else if (input == '2') {
                sumNum = getIncome(arr, count, sumNum);
                count++;

            } else if (input == '3') {

                sumNum = getSpend(arr, count, sumNum);
                count++;
            } else if (input == '4') {
                System.out.println("Confirm whether to exit( Y/N)?:");
                char c1 = Utility.readConfirmSelection();
                if (c1 == 'Y') {
                    System.out.println("bye-bye! Exit succeeded!");
                    return;
                }
            } else {
                System.out.println("Your input is incorrect, please re-enter");
            }
        }
    }
}

2. Function method code

package project;


public class GetFunction {
    //Revenue and expenditure function block
    public static void getForm(String[][] arr) {
        System.out.println("-------------------Detailed record of revenue and expenditure-----------------------");
        System.out.println("Revenue and expenditure\t\t Total account amount\t\t Revenue and expenditure amount\t\t explain");
        for (int j = 0; j < arr.length; j++) {

            if (arr[j][0] != null) {
                System.out.println(arr[j][0] + "\t\t" + arr[j][1] + "\t\t\t" + arr[j][2] + "\t\t\t" + arr[j][3]);
            }
        }
        System.out.println("--------------------------------------------------------\n");
    }

    //Revenue function block
    public static int getIncome(String[][] arr, int count, int sum) {
        System.out.println("Please enter the amount of this income:");
        int num = Utility.readNumber();
        System.out.println("Please enter the description of this income:");
        String info = Utility.readString();
        arr[count][0] = "income";
        sum += num;
        arr[count][1] = String.valueOf(sum);
        arr[count][2] = String.valueOf(num);
        arr[count][3] = info;
        count++;//The index increases and the next set of data in the two-dimensional array moves down
        System.out.println("Successfully added!");
        return sum;
    }

    //Expenditure function block
    public static int getSpend(String[][] arr, int count, int sumNum) {
        System.out.println("Please enter the amount of this expenditure:");
        int num1 = Utility.readNumber();
        System.out.println("Please enter the description of this expenditure:");
        String info1 = Utility.readString();
        arr[count][0] = "expenditure";
        sumNum -= num1;
        arr[count][1] = String.valueOf(sumNum);
        arr[count][2] = String.valueOf(num1);
        arr[count][3] = info1;
        count++;
        System.out.println("Successfully added!");
        return sumNum;

    }

}

3.utility tools

package project;
import java.util.Scanner;
public class Utility {


/**0
     Utility Tools:
     Encapsulating different functions into methods means that its functions can be used directly by calling methods without considering the specific function implementation details.
     */


        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 in '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("Selection error, 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("Selection error, 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("Input length (not greater than)" + limit + ")Error, please re-enter:");
                    continue;
                }
                break;
            }

            return line;
        }
    }

Keywords: Java

Added by Yari on Wed, 29 Dec 2021 05:59:44 +0200