请开发一个家庭收支记账软件
界面及操作过程如下所示:
-----------------家庭收支记账软件-----------------
1 收支明细
2 登记收入
3 登记支出
4 退 出
请选择(1-4):
选择1:
-----------------当前收支明细记录-----------------
收支 账户金额 收支金额 说 明
收入 11000 1000 劳务费
支出 10200 800 物业费
选择2:
本次收入金额:1000
本次收入说明:劳务费_
选择3:
本次支出金额:800
本次支出说明:物业费_
根据需求提取出类,属性和方法,完成上述功能
定义两个类,1实体,属性类2.测试执行方法类
Account实体类
public class Account {
private int type;//收支类型: 1 收入 2 支出
private double money;//本次记录收支的金额
private static double balance=10000;//所有对象共享 表示总余额
private double newBalance=10000;//最新余额
private String note;//说明,
public Account() {}
public Account(int type,double money,String note) {
this.type=type;
this.money=money;
this.note=note;
if(this.type==1) {
balance+=money;
}else if(this.type==2) {
if (balance < money) {
System.out.println("余额不足");
} else {
balance -= money;
}
}
//
newBalance=balance;
}
@Override
public String toString() {
String str="";
if(this.type==1) {
str="收入\t"+this.newBalance+" \t"+this.money+" \t"+this.note;
}else if(this.type==2) {
str="支出\t"+this.newBalance+" \t"+this.money+" \t"+this.note;
}
return str;
}
}
测试类:
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
menu();
int op=input.nextInt();
boolean bool=true;
Account[] accounts=new Account[50];
int size=0;//真实的收支记录个数
while(bool) {
switch (op) {
case 1:
list(accounts, size);
menu();
op=input.nextInt();
break;
case 2:
size=addAccount(accounts, op, size);
menu();
op=input.nextInt();
break;
case 3:
size=addAccount(accounts, op, size);
menu();
op=input.nextInt();
break;
case 4:
bool=false;
break;
default:
System.out.println("输入有误,请重新输入");
menu();
op=input.nextInt();
break;
}
}
}
public static void menu() {
System.out.println("-----------------家庭收支记账软件----------------- ");
System.out.println("1 收支明细 ");
System.out.println("2 登记收入 ");
System.out.println("3 登记支出 ");
System.out.println("4 退 出 ");
System.out.println("请选择(1-4):");
}
public static void list(Account[] accounts,int size) {
System.out.println("收支\t账户金额 \t收支金额 \t说明 \t");
//遍历收支记录的数组
for(int i=0;i<size;i++) {
System.out.println(accounts[i]);//自动toString 在Account类中 写了自定义的toString方法
}
}
public static int addAccount(Account[] accounts,int op,int size) {
Scanner input=new Scanner(System.in);
if(op==2) {//收入
System.out.println("本次收入金额:");
double money=input.nextDouble();
System.out.println("说明:");
String note=input.next();
accounts[size++]=new Account(1, money, note);
}else if(op==3) {
System.out.println("本次支出金额:");
double money=input.nextDouble();
System.out.println("说明:");
String note=input.next();
accounts[size++]=new Account(2, money, note);
}
System.out.println("已记录!");
return size;
}
}
本人菜鸟一枚,博客记录学习笔记,学习日常,分享学习心得,欢迎相互学习交流