当前位置:网站首页>Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)

Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)

2022-07-04 07:29:00 Inter personal liabilities^

1. The goal is

  • Simulate the implementation of a text-based interface 《 Family accounting software 》

  • Master preliminary programming and debugging skills

  • It mainly involves the following knowledge points

    • Definition of variables
    • Use of basic data types
    • Loop statement
    • Branch statement
    • Method statement 、 Reception of call and return values
    • Simple screen output format control

2. Requirement specification

  • The simulation is based on the text interface 《 Family accounting software 》
  • The software can record the family's income 、 spending , And can print revenue and expenditure details
  • The project adopts hierarchical menu .

The main menu

 Insert picture description here
Simulation steps

  • Suppose the initial life principal of the family is 10000 element
  • Income per registration ( menu 2) after , The amount of income should be added to the base Fund , And record the details of this income , For subsequent queries
  • Each registration expense ( menu 3) after , The amount of expenditure shall be deducted from the basic fund , And record the details of this expenditure , For subsequent queries
  • Query revenue and expenditure details ( menu 1) when , All revenues will be displayed , List of expenditure details

Registered income

 Insert picture description here

Register expenses

 Insert picture description here

Income and expenditure details
 Insert picture description here

Alignment of parts list , You can simply use tabs ‘\t’ To achieve

sign out
 Insert picture description here

3. Code details

Tool class

package project;
import java.util.Scanner;
/** Utility Tool class :  Encapsulating different functions into methods , You can use its functions directly by calling methods , There is no need to consider the specific function implementation details . */
public class Utility {
    
    private static Scanner scanner = new Scanner(System.in);
    /**  For interface menu selection . This method reads the keyboard , If the user types ’1’-’4’ Any character in , Then the method returns . The return value is the character that the user typed . */
	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 choice , Please re-enter :");
            } else break;
        }
        return c;
    }
	/**  Input for income and expense amounts . This method reads a number from the keyboard that does not exceed 4 An integer of bit length , And take 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(" Wrong number input , Please re-enter :");
            }
        }
        return n;
    }
	/**  Input for income and expense statements . This method reads a number from the keyboard that does not exceed 8 Bit length string , And take it as the return value of the method . */
    public static String readString() {
    
        String str = readKeyBoard(8);
        return str;
    }
	
	/**  Input to confirm selection . This method reads... From the keyboard ‘Y’ or ’N’, And take 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 choice , 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 ( No more than " + limit + ") error , Please re-enter :");
                continue;
            }
            break;
        }

        return line;
    }
}

main interface

package project;

import javax.sound.midi.Soundbank;

public class FamilyAccount {
    

	public static void main(String[] args) {
    
		
		boolean isFlag = true;
		
		String details = "";
		
		int balance = 10000;
		
		while(isFlag)
		{
    
			System.out.println("------------- Household income and expenditure bookkeeping soft -------------\n");
			System.out.println(" 1. Income and expenditure details ");
			System.out.println(" 2. Registered income ");
			System.out.println(" 3. Register expenses ");
			System.out.println(" 4. refund   Out \n");
			System.out.println("  Please select (1-4): ");
			//  Get user's choice :1-4
			char selection = Utility.readMenuSelection();
			switch(selection)
			{
    
				case '1':
					System.out.println("------------- Current revenue and expenditure details record ------------");
					System.out.println(" Income and expenditure \t Account amount \t\t Amount of revenue and expenditure \t\t say   bright ");
					System.out.println(details);
					System.out.println("----------------------------------------");
					break;
				case '2':
					System.out.println(" The amount of income this time :");
					int addMoney = Utility.readNumber();
					System.out.println(" This income statement :");
					String addInfo = Utility.readString();
					
					balance += addMoney;
					
					details += " income \t" + balance + "\t\t" + addMoney + "\t\t" + addInfo + "\n";
					
					System.out.println("-------------- Registration completed ---------------\n");
					break;
				case '3':
					System.out.println(" The amount of this expenditure is :");
					int minusMoney = Utility.readNumber();
					System.out.println(" This expenditure statement :");
					String minusInfo = Utility.readString();
					
					if(balance >= minusMoney)
					{
    
						balance -= minusMoney;
						details += " spending \t" + balance + "\t\t" + minusInfo + "\t\t" + minusInfo + "\n";
					}
					else 
					{
    
						System.out.println(" The expenditure exceeds the account limit , Failure to pay ");
					}
					
					break;
				case '4':
					System.out.println(" Confirm whether to exit (Y/N):");
					char isExit = Utility.readConfirmSelection();
					if(isExit == 'Y')
					{
    
						isFlag = false;
					}
					break;
			}
		}

	}

}

4. Records of basic funds and income and expenditure details

  • The record of basic gold can be used int Variable implementation of type int balance = 10000
  • Revenue and expenditure detail records can use String Type to implement , Its initial value is null
  • When registering income and expenditure , Compare the amount of revenue and expenditure with balance Add or subtract , Revenue and expenditure records are directly linked to details Just behind

5. Implementation of keyboard access

  • The project provides Utility.java class , It can be used to conveniently realize keyboard access
  • This class provides the following static methods :
    • public static char readMenuSelection() : For interface menu selection . This method reads the keyboard , If the user types ’1’-’4’ Any character in , Then the method returns . The return value is the character that the user typed .
    • public static int readNumber() : Input for income and expense amounts . This method reads a number from the keyboard that does not exceed 4 An integer of bit length , And take it as the return value of the method .
    • public static String readString() : Input for income and expense statements . This method reads a number from the keyboard that does not exceed 8 Bit length string , And take it as the return value of the method .
    • public static char readConfirmSelection() : Input to confirm selection . This method reads... From the keyboard ‘Y’ or ’N’, And take it as the return value of the method .
原网站

版权声明
本文为[Inter personal liabilities^]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141533228585.html