1.菜单与逻辑选择类部分(以及部分实现)
package Menu;
import ScreeenUtilize.ScreenUse;
public class MenuSelect {
private static void menuShow() {
System.out.println("**************************");
System.out.println("*****欢迎来到 谷日记账本*****");
System.out.println("*****1.显示收支 2.收入 *****");
System.out.println("*****3.支出 4.退出 *****");
System.out.println("**************************");
}
public static void main(String[] args) {
char select;
boolean flag = true;
String info = "";
double initial = 10000.0;
do {
menuShow();
select = ScreenUse.Select();
switch (select) {
case '1':
ScreenUse.Show(info);
break;
case '2':
double add = ScreenUse.RecordAdd();
initial += add;
String express1 = ScreenUse.Word();
info += "收入\t" + initial + "\t" + add + "\t" + express1 + "\n";
System.out.println("---------登记完成---------");
break;
case '3':
double sub = ScreenUse.RecordSub();
initial -= sub;
String express2 = ScreenUse.Word();
info += "支出\t" + initial + "\t" + sub + "\t" + express2 + "\n";
System.out.println("---------登记完成---------");
break;
case '4':
char yesorno = ScreenUse.Exit();
if (yesorno == 'Y') {
flag = false;
System.out.println("退出成功!");
}
break;
}
} while (flag);
}
}
2.主体逻辑以及输入管理类
package ScreeenUtilize;
import java.util.Scanner;
public class ScreenUse {
private static Scanner scanner = new Scanner(System.in);
public static char Select() {
System.out.println("请输入你的选择(1-4):");
char input = scanner.next().charAt(0);
while (input < '1' || input > '4') {
System.out.println("输入错误,请重新输入!");
input = scanner.next().charAt(0);
}
scanner.nextLine();
return input;
}
public static void Show(String info) {
info = (info.equals("")) ? "-----------<空>-----------\n" : info;
System.out.println("---------收支情况表---------");
System.out.println("收支\t账户金额\t收支金额\t说 明");
System.out.println(info);
System.out.println("--------------------------");
}
public static String Word() {
String express;
System.out.println("请添加明细说明(可为空)");
express = scanner.nextLine();
if (express.length() == 0) {
express = "<空>";
}
return express;
}
public static double RecordAdd() {
double add = 0.0;
System.out.println("请输入收入的金额数:");
do {
add = scanner.nextDouble();
if (add <= 0) {
System.out.println("输入金额有误,请重新输入:");
continue;
}
} while (add <= 0);
scanner.nextLine();
return add;
}
public static double RecordSub() {
double sub = 0.0;
System.out.println("请输入支出的金额数:");
do {
sub = scanner.nextDouble();
if (sub <= 0) {
System.out.println("输入金额有误,请重新输入:");
continue;
}
} while (sub <= 0);
scanner.nextLine();
return sub;
}
public static char Exit() {
char yn;
System.out.println("程序即将退出,请确认是否退出(y/n)");
do {
String up = readKeyBoard(1).toUpperCase();
yn = up.charAt(0);
if (yn == 'Y' || yn == 'N') {
break;
}
System.out.println("输入错误请重新输入");
} while (true);
return yn;
}
private static String readKeyBoard(int limit) {
String line = "";
while (scanner.hasNext()) {
line = scanner.nextLine();
if (line.length() < 1) {
System.out.println("长度为0");
continue;
}
if (line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}