工具類: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;
}
}
FamilyAccount.java
class FamilyAccount{
public static void main(String[] args){
boolean isFlag = true;
//記錄用戶的收入和支出的詳情
String details = "類型 \t 賬號余額 \t\t 收支金額 \t\t 說明 \n";
//初始金額
int balance = 10000;
while(isFlag){
System.out.println("----------------家庭收支記賬軟件----------------\n");
System.out.println(" 1.收支明細");
System.out.println(" 2.登記收入");
System.out.println(" 3.登記支出");
System.out.println(" 4.退 出\n");
System.out.print(" 請選擇(1-4): ");
//獲取用戶的選擇: 1-4
char selection = Utility.readMenuSelection();
switch(selection){
case '1':
System.out.println("----------------當期收支明細記錄----------------");
System.out.println(details);
System.out.println("--------------------------------------------------------");
break;
case '2':
System.out.print("本次收入金額: ");
int addMoney = Utility.readNumber();
System.out.print("本次收入說明: ");
String addInfo = Utility.readString();
//處理 balance
balance += addMoney;
//處理details
details += ("收入\t" + balance + "\t\t\t" + addMoney + "\t\t\t" + addInfo + "\n");
System.out.println("----------------登記完成----------------\n");
break;
case '3':
System.out.print("本次支出金額: ");
int minusMoney = Utility.readNumber();
System.out.print("本次支出說明: ");
String minusInfo = Utility.readString();
//處理 balance
if(balance >= minusMoney){
balance -= minusMoney;
//處理details
details += ("支出\t" + balance + "\t\t\t" + minusMoney + "\t\t\t" + minusInfo + "\n");
}else{
System.out.println("支出超出賬戶額度,支付失敗");
}
System.out.println("----------------登記完成----------------\n");
break;
case '4':
System.out.print("確認是否退出(Y/N): ");
char isExit = Utility.readConfirmSelection();
if(isExit == 'Y'){
isFlag = false;
}
}
}
}
}
效果: