注:本项目是学完JavaSE的多态后写的纯文本项目
本项目的难点:
-
使用数组来存储金额收益/支出的详细信息,并且使用了数组的扩容避免了数组越界异常的情况。
-
另余额(balance)为静态(static)变量,避免了在金额收益/支出时出现冲突。
-
对日期的处理,将日期转化成了国内常用的形式。
yyyy–MM–dd HH:mm:ss -
使用了继承的关系,将金额收益和支出继承了父类Money,并且将父类Money定义为抽象(abstract)类,抽象类中有抽象方法printf(),用来打印出金额收益/支出的详细信息,提醒子类必须重写父类中的print()方法。
-
对于金额支出要有明细,做了具体的什么事,在初始化阶段默认为金额支出(否则输出的为null),后面再使用的时候再覆盖。而初始化则是再Consume类的无参构造器中进行初始化。
代码:
//父类Money
public abstract class Money {
private double money;
private Date date;
private double balance;
double[] moneys =new double[1];
String[] dates = new String[1];
double[] balances = new double[1];
Scanner scanner = new Scanner(System.in);
SystemTest systemTest = new SystemTest();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public Money() {
}
public Money(double money, Date date, double balance) {
this.money = money;
this.date = date;
this.balance = balance;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//数组扩容的方法
public void expand(int j){
double[] newMoneys = new double[j+1];
String[] newDates = new String[j+1];
double[] newBalances = new double[j+1];
for (int i = 0; i < j; i++) {
newMoneys[i] = moneys[i];
newDates[i] = dates[i];
newBalances[i] = balances[i];
}
moneys = newMoneys;
dates = newDates;
balances = newBalances;
}
//打印出收益/支出详情的方法,取决于是谁调用此方法,在相应的类中要重写该方法
public abstract void print();
}
//子类Income
public class Income extends Money{
public Income() {
}
public Income(double money, Date date, double balance) {
super(money, date, balance);
}
public static int j = 0;//使用静态变量用来检查当前数组是否需要扩容,并且还可以用来记录数组的下标
//收入的方法
public void add(){
System.out.print("请输入收入金额:");
double money = scanner.nextDouble();
if(money <= 0){
System.out.println("收入金额必须大于0");
return;
}
if(this.moneys.length<=j){
this.expand(j);
}
systemTest.balance += money;
this.balances[j] = systemTest.balance;
this.moneys[j] = money;
this.dates[j] = sdf.format(new Date());//转换时间的格式
j++;
}
@Override
public void print(){
System.out.println("-------------------------收益入账-------------------------");
for (int i = 0; i < this.moneys.length; i++) {
System.out.println( "收益入账 +" + this.moneys[i] + ", " + this.dates[i] + ", 余额:" + this.balances[i]);
}
}
}
//子类Consume
public class Consume extends Money{
String[] expandDetails = new String[1];
public Consume() {
this.expandDetails[0] = "金额支出";//初始化expandDetails[0],否则74行的this.expandDetails[i]为null
}
public Consume(double money, Date date, double balance, String[] expandDetails) {
super(money, date, balance);
this.expandDetails = expandDetails;
}
public static int j = 0;
@Override//重写expand,因为数组expandDetails也需要扩容,否则数组expandDetails会ArrayIndexOutOfBoundsException,数组越界异常
public void expand(int j){
double[] newMoneys = new double[j+1];
String[] newExpandDetails = new String[j+1];
String[] newDates = new String[j+1];
double[] newBalances = new double[j+1];
for (int i = 0; i < j; i++) {
newMoneys[i] = moneys[i];
newExpandDetails[i] = expandDetails[i];
newDates[i] = dates[i];
newBalances[i] = balances[i];
}
moneys = newMoneys;
expandDetails = newExpandDetails;
dates = newDates;
balances = newBalances;
}
//金额支出的方法
public void sub(){
System.out.print("请输入支出金额:");
double money = scanner.nextDouble();
System.out.print("请输入具体的消费:");
String expandDetail = scanner.next();
if(money > systemTest.balance) {
System.out.println("金额不足");
return;
}
if(money <= 0){
System.out.println("消费金额必须大于0");
return;
}
if(moneys.length<=j){
expand(j);
}
if(expandDetails.length<=j){
expand(j);
}
systemTest.balance -= money;
balances[j] = systemTest.balance;
moneys[j] = money;
expandDetails[j] = expandDetail;
dates[j] = sdf.format(new Date());
j++;
}
@Override
public void print(){
System.out.println("-------------------------金额支出-------------------------");
for (int i = 0; i < this.moneys.length; i++) {
System.out.println( this.expandDetails[i]+", -" + this.moneys[i] + ", " + this.dates[i] + ", 余额:" + this.balances[i]);
}
}
}
//主界面Menu类
public class Menu {
Income income = new Income();
Consume consume = new Consume();
Over over = new Over();
Scanner scanner = new Scanner(System.in);
int i = 0;
public void change() {
System.out.println("-------------------------零钱通菜单-------------------------");
System.out.println("1.零钱通明细");
System.out.println("2.收益入账");
System.out.println("3.金额支出");
System.out.println("4.退出");
System.out.print("请选择(1-4):");
i = scanner.nextInt();
while (i < 1 || i > 4) {
System.out.println("选择有误!请选择1~4");
i = scanner.nextInt();
}
while (true) {
switch (i) {
case 1:
income.print();
consume.print();
change();
break;
case 2:
income.add();
income.print();
change();
break;
case 3:
consume.sub();
consume.print();
change();
break;
case 4:
exit();
break;
}
}
}
public void exit() {
System.out.println("确定要退出吗?");
System.out.println("请输入Yes/No");
String ispass = scanner.next();
while (!("Yes".equals(ispass)) && !("No".equals(ispass))
&& !("yes".equals(ispass)) && !("no".equals(ispass))) {
System.out.println("输入有误!请重新弄输入!");
ispass = scanner.next();
}
if(ispass.equals("No")||ispass.equals("no")){
change();
}
if (ispass.equals("Yes") || ispass.equals("yes")) {
System.out.println("退出系统");
over.over();
}
}
}
//退出系统Over类
public class Over {
public void over(){
System.exit(0);
}
}
//系统测试类SystemTest
public class SystemTest {
public static double balance = 0.0;//将余额设为static,这样在输入或者在支付时就会保存下来当前的值,下次再用时就是上一次的值
public static void main(String[] args) {
Menu menu = new Menu();
menu.change();
}
}