代码1
package lesson.project.p1_familyAccount;
import java.util.Scanner;
public class FamilyAccount {
public static int money = 10000;
public static String all = "";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
show();
String s = scanner.next();
switch (s) {
case "1":
show();
System.out.println("收支\t金额\t明细\t");
System.out.println(all);
System.out.println("余额:" + money);
break;
case "2"://收入
System.out.println("金额:");
String b = scanner.next();
System.out.println("明细:");
String d = scanner.next();
all+="收入"+"\t"+b+"\t"+d+"\t"+"\n";
money += Integer.parseInt(b);
break;
case "3"://支出
System.out.println("金额:");
String b1 = scanner.next();
System.out.println("明细:");
String d1 = scanner.next();
all+="支出"+"\t"+b1+"\t"+d1+"\t"+"\n";
money -= Integer.parseInt(b1);
break;
case "4":
while (true) {
System.out.println("是否要退出?(Y\\N)");
String choose = scanner.next();
if (choose.equals("Y") || choose.equals("y")) {
System.exit(0);
} else if (choose.equals("N") || choose.equals("n")) {
break;
} else {
System.out.println("输入不合法!请重新输入");
}
}
break;
default:
System.out.println("输入不合法!请重新输入");
show();
break;
}
}
}
public static void show() {
System.out.println("家庭收支账户:");
System.out.println("1.查看账户");
System.out.println("2.增加收入");
System.out.println("3.减少收入");
System.out.println("4.退出账户");
System.out.println("请选择功能:");
}
}
代码2
工具类
package lesson.project.p1_familyAccount.version2;
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 n;
for (; ; ) {
String str = readKeyBoard(4);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static String readString() {
String str = readKeyBoard(8);
return str;
}
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;
}
}
家庭收支
package lesson.project.p1_familyAccount.version2;
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);
}
}