- 模拟实现基于文本界面的《家庭记账软件》
- 该软件能够记录家庭的收入、支出,并能够打印收支明细表。
- 项目采用分级菜单方式。主菜单如下:?
?
分析
- 需要使用Scanner输入语句,if-else判断语句,while、for循环;
- 使用ArraysList容器来存储支出或收入的说明和金额;
代码实现
目录
分析
代码实现
运行结果
package hw;
import java.util.ArrayList;
import java.util.List;
public class FamilyAccount {
private int sum;
private List<String> sout = new ArrayList<String>();
private List<String> sin = new ArrayList<String>();
private List<Integer> iout = new ArrayList<Integer>();
private List<Integer> iin = new ArrayList<Integer>();
public FamilyAccount() {
this.sum=10000;//默认基本金
}
public void PrintMainMenu(){//打印主菜单
System.out.println("-------------家庭收支记账软件---------------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记支出");
System.out.println(" 4.查询本基金");
System.out.println(" 5.退 出");
System.out.println("-----------------------------------------");
}
public void DetailOfIncomeAndExpenditure(){//收支明细
for(int i=0;i<this.iout.size();i++){
System.out.println(" 支出金额:"+this.iout.get(i)+" 支出说明:"+this.sout.get(i));
}
for(int i=0;i<this.iin.size();i++){
System.out.println(" 收入金额:"+this.iin.get(i)+" 收入说明:"+this.sin.get(i));
}
}
public void input(int income,String explain){//收入操作
this.iin.add(income);//存储收入金额
this.sin.add(explain);//存储收入说明
// System.out.println("本次收入金额:" + income);
// System.out.println("本次收入说明:" + explain);
this.sum += income;//把收入加到基本金里
GetGrossIncome();//输出基本金
System.out.println("结束登记请按1,或按任意键继续登记");
}
public void expend(int expend,String oexplain){//支出操作
this.iout.add(expend);
this.sout.add(oexplain);
// System.out.println("本次支出金额:"+expend);
// System.out.println("本次支出说明:"+oexplain);
this.sum-=expend;
GetGrossIncome();
System.out.println("结束登记请按1,或按任意键继续登记");
}
public void GetGrossIncome(){//获得基本金
System.out.println("剩余本基金:"+this.sum);
}
}
package hw;
import java.util.Scanner;
public class hw5 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
FamilyAccount fa = new FamilyAccount();
System.out.println("欢迎来到家庭收支记账软件");
System.out.println("按任意键进入主菜单");
scan.nextInt();
fa.PrintMainMenu();
over: while (true) {
System.out.print("请选择(1-5):");
switch (scan.nextInt()) {
case 1:
fa.DetailOfIncomeAndExpenditure();
fa.GetGrossIncome();
break;
case 2:
while(true) {
System.out.print("本次收入金额:");
int income = scan.nextInt();
System.out.print("本次收入说明:");
String iexplain = scan.next();
fa.input(income, iexplain);
if(scan.nextInt()==1) break;
}
break;
case 3:
while(true) {
System.out.print("本次支出金额:");
int expend = scan.nextInt();
System.out.print("本次支出说明:");
String oexplain = scan.next();
fa.expend(expend, oexplain);
if(scan.nextInt()==1) break;
}
break;
case 4:
fa.GetGrossIncome();
System.out.print("按任意键返回主菜单:");
scan.nextInt();
fa.PrintMainMenu();
break;
case 5:
break over;//直接结束while外循环
}
}
System.out.println("已退出!");
}
}
运行结果
|