import java.util.Scanner;
public class Utility {
private static Scanner scanner = new Scanner(System.in);
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;
}
public static int readNumber() {
int i;
while (true) {
String str = readKeyBoard(4);
try {
i = Integer.parseInt(str);
break;
} catch (NumberFormatException numberFormatException) {
System.out.print("数字输入错误,请重新输入");
}
}
return i;
}
public static String readString() {
return readKeyBoard(8);
}
public static char readConfirmSelection() {
char c;
while (true) {
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 paramInt) {
String str = "";
while (scanner.hasNext()) {
str = scanner.nextLine();
if (str.length() < 1 || str.length() > paramInt){
System.out.print("输入长度(不大于"+ paramInt + ")错误,请重新输入");}
else break;
}
return str;
}
}
public class familyaccount {
public static void main(String[] paramArrayOfString) {
String str = “收支\t账户金额\t收支金额\t说明”;
int i = 1000;
boolean bool = true;
do {
int j;
int k;
String str1;
String str2;
System.out.println("-------------------------家庭收支记账软件-------------------------");
System.out.println(" 1.收支明细");
System.out.println(" 2登记收入.");
System.out.println(" 3.登记支出");
System.out.println(" 4.退出");
System.out.println(" 请选择1-4");
char c1 = Utility.readMenuSelection();
System.out.println();
switch (c1) {
case ‘1’:
System.out.println("-----------------------当前收支明细------------------------");
System.out.println(str);
System.out.println("---------------------------------------------------------");
break;
case ‘2’:
System.out.println(“本次收入金额为:”);
j = Utility.readNumber();
System.out.println(“收入说明:”);
str1 = Utility.readString();
i += j;
str = str + “收入\t” + i + “\t\t” + j + “\t\t” + str1 + “\n”;
System.out.println("-------------------------登记完成-------------------------");
case ‘3’:
System.out.println(“请输入支出金额:”);
k = Utility.readNumber();
System.out.println(“支出说明:”);
str2 = Utility.readString();
i -= k;
str = str + “支出\t” + i + “\t\t” + k + “\t\t” + str2 + “\n”;
System.out.println("-------------------------登记完成-------------------------");
case ‘4’:
System.out.println(“是否退出?”);
char c2 = Utility.readConfirmSelection();
if (c2 == ‘y’) {
bool = false;
break;
}
}
}
while (bool);
}
}