项目来自于: 尚硅谷Java入门视频教程(在线答疑+Java面试真题)
1. 需求说明
2. 代码实现
2.1 Utility.java类
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;
}
}
2. 项目具体实现
import java.util.Scanner;
import java.io.*;
public class FamilyAccount {
private static Scanner scanner = new Scanner(System.in);
public int base_Money = 10000;
public String details_menu = "income/outcome" + '\t' + "Account Money" + '\t' + "Money" + '\t' + "statement" + '\n';
// main function
public static void main(String[] args) {
// base money
FamilyAccount familyAccount = new FamilyAccount();
while (true) {
familyAccount.printMenu();
char choice = Utility.readMenuSelection();
if (choice == '1') {
familyAccount.detailsInAndOut();
} else if (choice == '2') {
familyAccount.registeredIncome();
} else if (choice == '3') {
familyAccount.registeredExpenditure();
} else {
familyAccount.quitMenu();
}
}
}
// print the menu
public void printMenu() {
System.out.println("-------------------------------------------------------------------------- ");
System.out.println("---------Family income and expenditure accounting software---------------- ");
System.out.println(" 1.Income and expenditure details ");
System.out.println(" 2.Registered income ");
System.out.println(" 3.Registered expenditure ");
System.out.println(" 4.Quit ");
System.out.print("---------Please choose your choice(1-4): ");
// System.out.println("--------------------------------------------------------------------------
// ");
}
// Income and expenditure details
public void detailsInAndOut() {
// String income_detail = registeredIncome();
// String outcome_detail = registeredOutcome();
System.out.println(details_menu);
// System.out.println(income_detail);
// System.out.println(outcome_detail);
}
// Registered income
public void registeredIncome() {
System.out.println("Amount of income this time: ");
int income = Utility.readNumber();
System.out.println("This income statement: ");
String statement = Utility.readString();
base_Money += income;
String string_line = "income" + "\t\t\t" + base_Money + '\t' + income + '\t' + statement + '\n';
details_menu = details_menu + string_line;
System.out.println("***************** ");
System.out.println("Record success!");
System.out.println("***************** ");
}
// Registered expenditure
public void registeredExpenditure() {
System.out.println("Amount of outcome this time: ");
int outcome = Utility.readNumber();
System.out.println("This outcome statement: ");
String statement = Utility.readString();
base_Money -= outcome;
String string_line = "outcome" + "\t\t\t" + base_Money + '\t' + outcome + '\t' + statement + '\n';
details_menu = details_menu + string_line;
System.out.println("***************** ");
System.out.println("Record success!");
System.out.println("***************** ");
}
// quit
public void quitMenu() {
System.out.println("Determine whether to exit(Y/N): ");
char c = Utility.readConfirmSelection();
if (c == 'Y') {
System.exit(0);
}
}
}
2.3实现效果如下:
-
主菜单
-
登记收入
-
登记支出
-
收支明细
-
退出