模拟实现一个基于文本界面的 模拟实现一个基于文本界面的 《家庭记账软件 家庭记账软件 》
主要涉及以下知识点:
- 变量的定义
- 基本数据类型的使用
- 循环语句
- 分支语句
- 方法声明、调用和返回值的接收
- 简单的屏幕输出格式控制
该软件能够记录家庭的收入、支出,并能够打印收支明细表。
需求说明
假设家庭起始的生活基本金为10000元。
每次登记收入(菜单2)后,收入的金额应累加到基本金上,并记录本次收入明细,以便后续的查询。
每次登记支出(菜单3)后,支出的金额应从基本金中扣除,并记录本次支出明细,以便后续的查询。
查询收支明细(菜单1)时,将显示所有的收入、支出名细列表。
"登记收入"的界面及操作过程如下所示:
"登记支出"的界面及操作过程如下所示:
"收支明细"的界面及操作过程如下所示:
// 提示:明细表格的对齐,可以简单使用制表符‘t’来实现"退出"的界面及操作过程如下所示:
package com.lingchen.pojo.familyaccount;
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 read/confirm/iSelection() {
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 com.lingchen.pojo.familyaccount;
public class FamilyAccount {
public static void main(String[] args) {
String details = "收 支tt账户金额tt收支金额tt说 明n";
// 初始金额
int balance = 10000;
do{
System.out.println("****************家庭记账本****************");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记支出");
System.out.println(" 4.退 出n");
System.out.print(" 请选择(1-4):");
char selection = Utility.readMenuSelection();
switch (selection){
case '1':
//System.out.println(1);
System.out.println("***************当前收支明细记录**************");
System.out.println(details);
System.out.println("******************************************n");
break;
case '2':
//System.out.println(2);
System.out.print("本次收入金额:");
int income = Utility.readNumber();
System.out.print("本次收入说明:");
String inInfo = Utility.readString();
// 处理 balance
balance += income;
// 处理 details
details += "收入ttt" + balance + "tt" + income + "ttt" + inInfo + "n";
System.out.println("*****************登记完成*****************n");
break;
case '3':
// System.out.println(3);
System.out.print("本次支出金额:");
int outcome = Utility.readNumber();
System.out.print("本次支出说明:");
String outInfo = Utility.readString();
// 处理 balance
if(balance >= outcome){
balance -= outcome;
// 处理 details
details += "支出ttt" + balance + "tt" + outcome + "ttt" + outInfo + "n";
}else{
System.out.println("支出超出账户余额,支付失败!");
}
System.out.println("*****************登记完成*****************n");
break;
case '4':
//System.out.println(4);
System.out.print("确认是否退出(Y/N):");
char isExit = Utility.read/confirm/iSelection();
if(isExit == 'Y'){
// System.out.println("退出成功!");
return ;
}
break;
}
}while(true);
}
}



