eclipse编写的Java家庭收支记账软件
界面及操作过程如下所示:
-----------------家庭收支记账软件-----------------
1 收支明细
2 登记收入
3 登记支出
4 退 出
请选择(1-4):
选择1:
-----------------当前收支明细记录-----------------
收支 账户金额 收支金额 说 明
收入 11000 1000 劳务费
支出 10200 800 物业费
选择2:
本次收入金额:1000
本次收入说明:劳务费_
选择3:
本次支出金额:800
本次支出说明:物业费_
根据需求提取出类,属性和方法,完成上述功能
运行结果
测试类Client
package com.gem.记账;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Home home = new Home();//实例化
Scanner scanner = new Scanner(System.in);
while (true) {
//循环界面功能
menue(scanner, home); //界面
}
}
private static void menue(Scanner scanner, Home home) {
System.out.println("-----------家庭收支记账软件---------------");
System.out.println("1.收支明细");
System.out.println("2.登记收入");
System.out.println("3.登记支出");
System.out.println("4.退出");
System.out.println("选择(1-4)");
int choice = Integer.parseInt(scanner.nextLine().trim());
//选择
switch (choice) {
case 1://收支明细
shouzhi(home);
break;
case 2://登记收入
dengji(scanner, home);
break;
case 3://登记支出
zhichu(scanner, home);
break;
case 4://退出
System.exit(0);
break;
default:
System.out.println("输入出错!");
break;
}
}
//等级收入
private static void dengji(Scanner scanner, Home home) {
// TODO Auto-generated method stub
System.out.println("选择2:");
System.out.println(" 本次收入金额为:");
double statemoney = Double.parseDouble(scanner.nextLine().trim());
System.out.println("本次收入说明");
String shuoming = scanner.nextLine().trim();
String state = "收入";
Recond a1 = new Recond(statemoney, shuoming, state);
//传递收入金额,收入说明,和判断是"收入"
home.dengjishouru(a1);
}
//等级支出
private static void zhichu(Scanner scanner, Home home) {
// TODO Auto-generated method stub
System.out.println("选择3:");
System.out.println(" 本次支出金额为:");
double statemoney = Double.parseDouble(scanner.nextLine().trim());
System.out.println("本次收入说明");
String shuoming = scanner.nextLine().trim();
String state = "支出";
Recond a1 = new Recond(statemoney, shuoming, state);
home.dengjizhichu(a1);
}
//全部收支明细
private static void shouzhi(Home home) {
home.shouzhimingxi();
}
}
类Home
package com.gem.记账;
public class Home {
Recond recond[] = new Recond[100];//对象数组
static int count = 0;//记录长度
//显示所有信息
public void shouzhimingxi() {
System.out.println("--------当前收支明细记录------------------");
System.out.println("id\t收支\t账户金额\t收支金额\t说 明");
if (count == 0) {
System.out.println("暂无信息显示,请重新录入");
return;
}
for (int i = 0; i < count; i++) {
recond[i].showlist();
}
System.out.println("-------------------------------------------");
}
//登记收入
public void dengjishouru(Recond recond) {
this.recond[this.count++] = recond; //先把键盘录入的数据放到recond[count]里面
if ((count - 1) == 0) {
//如果是第一次等级 那么在初始化money的基础上进行累加
this.recond[this.count - 1]
.setMoney(this.recond[this.count - 1].getMoney() + this.recond[this.count - 1].getStatemoney());
} else {
//如果是第二次之后,那么则在recond对象数组上一次的money基础上进行累加,再赋值给当前的
this.recond[this.count - 1]
.setMoney(this.recond[this.count - 2].getMoney() + this.recond[this.count - 1].getStatemoney());
}
System.out.println("登记收入成功");
}
//登记支出
public void dengjizhichu(Recond recond) {
//支出原理同收入原理
this.recond[this.count++] = recond;
if ((count - 1) == 0) {
this.recond[this.count - 1]
.setMoney(this.recond[this.count - 1].getMoney() - this.recond[this.count - 1].getStatemoney());
} else {
this.recond[this.count - 1]
.setMoney(this.recond[this.count - 2].getMoney() - this.recond[this.count - 1].getStatemoney());
}
System.out.println("登记支出成功");
}
}
Recond类
package com.gem.记账;
public class Recond {
private static int bianhao = 1;//利用静态变量实现每次创建id+1
private int id;//收支的编号
private String state; //用于判断是收入/支出状态
private double money = 10000; //账户余额初始值
private double statemoney; //收支金额
private String shuoming; //说明
{
this.id = bianhao++;
}
//自定义构造方法
public Recond(double money2, String shuoming2, String state2) {
// TODO Auto-generated constructor stub
this.statemoney = money2;
this.shuoming = shuoming2;
this.state = state2;
}
public void showlist() {
System.out.println(
this.id + "\t" + this.state + "\t" + this.money + "\t" + this.statemoney + "\t" + this.shuoming);
}
public static int getBianhao() {
return bianhao;
}
public static void setBianhao(int bianhao) {
Recond.bianhao = bianhao;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getStatemoney() {
return statemoney;
}
public void setStatemoney(double statemoney) {
this.statemoney = statemoney;
}
public String getShuoming() {
return shuoming;
}
public void setShuoming(String shuoming) {
this.shuoming = shuoming;
}
}
转载:https://blog.csdn.net/weixin_43951778/article/details/109150116
查看评论