Shang Silicon Valley household income and expenditure accounting software

There is no file storage for this, so I will write a practice hand with file storage in a few days.

Statement of needs:

Main menu category:

public class FamilyAccount {
	public static void main(String[] args) {
        String details = "收支\t账户金额\t收支金额\t说    明\n";
        int balance = 10000;

        boolean loopFlag = true;
        do {
		    System.out.println("\n-----------------家庭收支记账软件-----------------\n");
            System.out.println("                   1 收支明细");
            System.out.println("                   2 登记收入");
            System.out.println("                   3 登记支出");
            System.out.println("                   4 退    出\n");
            System.out.print("                   请选择(1-4):");
            
            char key = Utility.readMenuSelection();
            System.out.println();
            switch (key) {
                case '1':
                    System.out.println("-----------------当前收支明细记录-----------------");
                    System.out.println(details);
                    System.out.println("--------------------------------------------------");
                    break;
                case '2':
                    System.out.print("本次收入金额:");
                    int amount1 = Utility.readNumber();
                    System.out.print("本次收入说明:");
                    String desc1 = Utility.readString();

                    balance += amount1;
                    details += "收入\t" + balance + "\t\t" +
                               amount1 + "\t\t" + desc1 + "\n";
                    System.out.println("---------------------登记完成---------------------");
                    break;
                case '3':
                    System.out.print("本次支出金额:");
                    int amount2 = Utility.readNumber();
                    System.out.print("本次支出说明:");
                    String desc2 = Utility.readString();

                    balance -= amount2;
                    details += "支出\t" + balance + "\t\t" +
                               amount2 + "\t\t" + desc2 + "\n";
                    System.out.println("---------------------登记完成---------------------");
                    break;
                case '4':
                    System.out.print("确认是否退出(Y/N):");
                    char yn = Utility.readConfirmSelection();
                    if (yn == 'Y') loopFlag = false;
                    break;
            }
        } while (loopFlag);
	}    
}

Tools package menu option function

将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功
能实现细节。在 Utility.java 工具类中提供了以下静态方法:
 public static char readMenuSelection() :用于界面菜单的选择。该方法读取键盘,如果
用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
 public static int readNumber() :用于收入和支出金额的输入。该方法从键盘读取一个不
超过 4 位长度的整数,并将其作为方法的返回值。
 public static String readString() :用于收入和支出说明的输入。该方法从键盘读取一个不
超过 8 位长度的字符串,并将其作为方法的返回值。
 public static char readConfirmSelection() :用于确认选择的输入。该方法从键盘读取‘Y’
或’N’,并将其作为方法的返回值
这些方法都是 public static 修饰的,因此使用这些方法可以通过“类名.方法”直接使用。例
如:
char key = Utility.readMenuSelection();
int amount = Utility.readNumber();
String desc = Utility.readString();
char confirm = Utility.readConfirmSelection();

 Tools:

import java.util.Scanner;
/**
Utility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class Utility {
    private static Scanner scanner = new Scanner(System.in);
    /**
	用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
	*/
	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("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
	/**
	用于收入和支出金额的输入。该方法从键盘读取一个不超过4位长度的整数,并将其作为方法的返回值。
	*/
    public static int readNumber() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(4);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
	/**
	用于收入和支出说明的输入。该方法从键盘读取一个不超过8位长度的字符串,并将其作为方法的返回值。
	*/
    public static String readString() {
        String str = readKeyBoard(8);
        return str;
    }
	
	/**
	用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
	*/
    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("选择错误,请重新输入:");
            }
        }
        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("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

 

Guess you like

Origin blog.csdn.net/qq_41048982/article/details/109304692
Recommended