介面及操作過程如下所示:
-----------------家庭收支記賬軟體-----------------
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;
}
}