1. Project objectives
• simulate the implementation of a text-based interface "home accounting software"
• master preliminary programming and debugging skills
• it mainly involves the following knowledge points:
definition of variables
use of basic data types
circular statement
branch statement
method declaration, calling and receiving of return value
simple screen output format control
2. Requirements description
• simulate the implementation of family accounting software based on text interface.
• the software can record the family's income and expenditure, and print the detailed statement of income and expenditure.
• the project adopts hierarchical menu. The main menu is as follows:
-----------------Household income and expenditure accounting software-----------------
1. Details of revenue and expenditure
2. Registered income
3. Registered expenditure
4 exit
Please select (1-4):_
• it is assumed that the starting basic living fund of the family is 10000 yuan.
• after each registration of income (menu 2), the amount of income shall be added to the basic fund and recorded
Record the current revenue details for subsequent query.
• after each registration of expenditure (menu 3), the amount of expenditure shall be deducted from the basic fund and recorded
Record the current expenditure details for subsequent query.
• when querying revenue and expenditure details (menu 1), a detailed list of all revenue and expenditure names will be displayed
3. Keyboard access implementation
• utility is provided in the project Java class, which can be used to easily realize keyboard access.
• this class provides the following static methods:
public static char readMenuSelection(): this method reads the keyboard. If the user types in '1' - '4'
Any character, the method returns. The return value is the character typed by the user.
public static int readNumber(): this method reads an integer with a length of no more than 4 bits from the keyboard and
As the return value of the method.
public static String readString(): this method reads a string with a length of no more than 8 bits from the keyboard,
And take it as the return value of the method.
public static char readConfirmSelection(): this method reads' Y 'or' N 'from the keyboard and takes it as
Method.
4. Implementation steps
Step 1 - implement the main program structure
- Create FamilyAccount class and main method
- In the main method, the main structure of the program is realized by referring to the main process diagram
- Test the program and confirm that the 1st and 4th menu options can be executed normally
Step 2 - implement revenue and expenditure registration processing
4. In the main method, refer to the revenue and expenditure process to realize the "register revenue" function
5. Test the "registration revenue" function
6. In the main method, refer to the income and expenditure process to realize the "register expenditure" function
7. Test the "register expenditure" function
5. Specific code
import java.util.Scanner; /** 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. */ 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 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; } }
public class FamilyAccount { public static void main(String[] args) { String details = "Revenue and expenditure\t Account amount\t Revenue and expenditure amount\t say bright\n"; int balance = 10000; boolean loopFlag = true; do { System.out.println("\n-----------------Household income and expenditure accounting software-----------------\n"); 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\n"); System.out.print(" Please select(1-4): "); char key = Utility.readMenuSelection(); System.out.println(); switch (key) { case '1': System.out.println("-----------------Current revenue and expenditure details record-----------------"); System.out.println(details); System.out.println("--------------------------------------------------"); break; case '2': System.out.print("Current revenue amount:"); int amount1 = Utility.readNumber(); System.out.print("Description of this income:"); String desc1 = Utility.readString(); balance += amount1; details += "income\t" + balance + "\t\t" + amount1 + "\t\t" + desc1 + "\n"; System.out.println("---------------------Registration completed---------------------"); break; case '3': System.out.print("Current expenditure amount:"); int amount2 = Utility.readNumber(); System.out.print("Description of this expenditure:"); String desc2 = Utility.readString(); balance -= amount2; details += "expenditure\t" + balance + "\t\t" + amount2 + "\t\t" + desc2 + "\n"; System.out.println("---------------------Registration completed---------------------"); break; case '4': System.out.print("Confirm whether to exit(Y/N): "); char yn = Utility.readConfirmSelection(); if (yn == 'Y') loopFlag = false; break; } } while (loopFlag); } }