income类
/**
*
*/
package com.gem.demo.home;
/**
*
* Description:
*
* @author HadwinLing
*
* @date 2020年1月15日下午4:27:40
*
* @version 0.0.1
*
*/
public class income {
static int all;
private int income;
private String incomeDetail;
income(){
all=0;
income =0;
incomeDetail="";
}
income(int all,int income,String incomeDetail){
this.all = all;
this.income =income;
this.incomeDetail = incomeDetail;
}
public static int getAll() {
return all;
}
public void setAll(int all,int income,int expend) {
this.all = all+income-expend;
}
public int getIncome() {
return income;
}
public void setIncome(int income) {
this.income = income;
}
public String getIncomeDetail() {
return incomeDetail;
}
public void setIncomeDetail(String incomeDetail) {
this.incomeDetail = incomeDetail;
}
}
expend类
/**
*
*/
package com.gem.demo.home;
/**
*
* Description:
*
* @author HadwinLing
*
* @date 2020年1月15日下午4:27:50
*
* @version 0.0.1
*
*/
public class expend {
static int all;
private int expend;
private String expendDetail;
expend(){
all=0;
expend =0;
expendDetail="";
}
expend(int all,int expend,String expendDetail){
this.all = all;
this.expend =expend;
this.expendDetail = expendDetail;
}
public static int getAll() {
return all;
}
public void setAll(int all,int income,int expend) {
this.all = all+income-expend;
}
public int getExpend() {
return expend;
}
public void setExpend(int expend) {
this.expend = expend;
}
public String getExpendDetail() {
return expendDetail;
}
public void setExpendDetail(String expendDetail) {
this.expendDetail = expendDetail;
}
}
incomeAndExpand类
/**
*
*/
package com.gem.demo.home;
import java.util.Scanner;
/**
*
* Description:
*
* @author HadwinLing
*
* @date 2020年1月15日下午3:57:52
*
* @version 0.0.1
*
*/
public class incomeAndExpend {
static income[] incomes = new income[10];
static expend[] expends = new expend[10];
static int size =0 ;
static int all = 0;
static Scanner input = new Scanner(System.in);
public static void setIncomeInfo() {
size++;
System.out.print("本次收入金额为:");
int income = input.nextInt();
System.out.print("本次收入说明为:");
String incomeDetail = input.next();
income in = new income();
in.setIncome(income);
in.setIncomeDetail(incomeDetail);
// for(int i=0;i<size;i++) {
// incomes[i-] = in;
// }
incomes[size-1] =in;
}
public static void setExpendInfo() {
size++;
System.out.print("支出金额为:");
int expend = input.nextInt();
System.out.print("本次支出说明为:");
String expendDetail = input.next();
expend ex = new expend();
ex.setExpend(expend);
ex.setExpendDetail(expendDetail);
// for(int i=0;i<size;i++) {
// expends[i] = ex;
// }
expends[size-1]=ex;
}
public static void print() {
System.out.println("-----------------当前收支明细记录-----------------");
System.out.println("收支 "+"\t"+"账户金额" +"\t"+"收支金额 "+"\t"+"说 明");
for(int i=0;i<size;i++) {
if(incomes[i]!=null) {
all =all+incomes[i].getIncome();
System.out.println("收入 \t"+all+"\t"+incomes[i].getIncome()+"\t"+incomes[i].getIncomeDetail());
}else if(expends[i]!=null) {
all =all-expends[i].getExpend();
System.out.print("支出 \t"+all+"\t"+expends[i].getExpend()+"\t"+expends[i].getExpendDetail());
}else {
continue;
}
}
}
}
incomeAndExpendTest 类
/**
*
*/
package com.gem.demo.home;
import java.util.Scanner;
import com.gem.demo.day05_practice.Home;
/**
*
* Description:
*
* @author HadwinLing
*
* @date 2020年1月15日下午3:58:14
*
* @version 0.0.1
*
*/
public class incomeAndExpendTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("-----------------家庭收支记账软件----------------- ");
System.out.println("1:收支明细");
System.out.println("2:登记收入");
System.out.println("3:登记支出");
System.out.println("4:退出");
Scanner input = new Scanner(System.in);
boolean flag =true;
incomeAndExpend h = new incomeAndExpend();
while(flag) {
System.out.print("请选择(1~4)");
int num = input .nextInt();
switch(num) {
case 1:
incomeAndExpend.print();
break;
case 2:
incomeAndExpend.setIncomeInfo();
break;
case 3:
incomeAndExpend.setExpendInfo();
break;
case 4:
flag = false;
break;
default:
System.out.println("输入有误");
}
System.out.println();
}
}
}
2.0还有个小bug,就是我这里设置的是10个长度,没有处理数组长度溢出,