目录
1.项目的界面如下
2.说明及思路
本次编程通过idea完成。本人也正在进行java的学习,此文章用来记录这次完成的项目,代码之中颇有不足之处,皆因本人水平所限。
思路:
1.先新建一个包。建立一个主方法,先打印出这个整体的菜单,并通过Switch语句,进行功能选择,输入1-4实现对应的功能。
2.由于选择4是退出,这里整体运用for循环,如果选择退出,则return。
3.1-3中各个功能都需要记录当时的入账或者消费,所以我选择继承,直接创建一个Money父类,然后1继承Money,2继承1,3继承2,这样,创建一个子类对象,能调用各个类的方法,数据都存放在父类中,便于后续遍历输出。
4.对于消费,记录消费项目及金额到数组中,并判断消费金额是否小于余额,否则不能消费,然后记录下当时所剩的金额。收入入账也是这样。
3.代码
1.菜单部分
package LingQianTong;//菜单
import java.util.Scanner;
public class YeMian {
public static void main(String[] args) {
Money m = new choose3();
Scanner scanner = new Scanner(System.in);
for (int i = 1; i > 0; i++) {
System.out.println("----------零钱通菜单----------");
System.out.println("\t\t1 零钱通明细");
System.out.println("\t\t2 消 费");
System.out.println("\t\t3 收益入账");
System.out.println("\t\t4 退 出");
System.out.println("请选择(1-4):");
int key = scanner.nextInt();
switch (key) {
case 1:
choose1 m1 = (choose1) m;
m1.choose1();
break;
case 2:
choose2 m2 = (choose2) m;
m2.choose2();
break;
case 3:
choose3 m3 = (choose3) m;
m3.choose3();
break;
case 4:
choose4 m4 = new choose4();
if (m4.quit() == 'y') {
return;
}
break;
default:
System.out.println("请输入1-4");
break;
}
}
}
}
2.Money部分
package LingQianTong;
//import com.sun.org.apache.xpath.internal.operations.String;
public class Money {
private double[] arr = {0.0};//记录每次收益入账金额
private double nowMoney;//记录当前余额
private String[] arr1 = new String[0];//记录消费项目名称
private double[] arr2 = {0};//记录每次入账或者消费时,当时的余额
//给每个属性设置getset方法
public double[] getArr() {
return arr;
}
public void setArr(double[] arr) {
this.arr = arr;
}
public String[] getArr1() {
return arr1;
}
public void setArr1(String[] arr1) {
this.arr1 = arr1;
}
public double getNowMoney() {
return nowMoney;
}
public double setNowMoney(double nowMoney) {//当入账时+,消费时-。实时的余额
if (nowMoney < 0) {
return this.nowMoney += nowMoney;
} else {
return this.nowMoney += nowMoney;
}
}
public double[] getArr2() {
return arr2;
}
public void setArr2(double[] arr2) {
this.arr2 = arr2;
}
}
3.功能1
package LingQianTong;
public class choose1 extends Money {//明细
public void choose1() {//明细方法
System.out.println("----------零钱通明细----------");
for (int i = 0, j = 0; i < getArr().length; i++) {//遍历输出入账或者消费的金额,及当时的余额
if (i < getArr().length - 1) {
if (getArr()[i + 1] > 0) {
System.out.println("收益到账\t" + getArr()[i + 1] + "\t余额:" + getArr2()[i + 1]);
} else if (getArr()[i + 1] < 0) {
System.out.println(getArr1()[j] + "\t" + getArr()[i + 1] + "\t余额:" + getArr2()[i + 1]);
j++;
}
}
}
}
}
4.功能2
package LingQianTong;
//import com.sun.org.apache.xpath.internal.operations.String;
import java.util.Scanner;
public class choose2 extends choose1 {//消费
private String name;//项目名字
private double money;//项目金额
Scanner scanner = new Scanner(System.in);
public void choose2() {//记录消费名字,并存入数组中
System.out.println("本次消费项目为:");
name = scanner.next();
String[] temp = new String[getArr1().length + 1];
for (int i = 0; i < getArr1().length; i++) {
temp[i] = getArr1()[i];
}
temp[getArr1().length] = name;
setArr1(temp);//传入
for (int j = 1; j > 0; j++) {//记录消费金额,并存入数组中,并对消费金额判断,余额能不能支撑此次消费
System.out.println("本次消费金额为:");
money = scanner.nextDouble();
if (money > getNowMoney()) { //消费金额>现金时候,不能消费
System.out.println("余额不足,支付失败");
} else { //能消费时,消费金额存入一个数组中,每消费一次,扩容数组并记录在尾部
double[] temp1 = new double[getArr().length + 1];
for (int i = 0; i < getArr().length; i++) {
temp1[i] = getArr()[i];
}
temp1[getArr().length] = -money;
setArr(temp1);
setNowMoney((-money));//更新现金额度
j = -100;//结束记录金额过程
double[] temp111 = new double[getArr2().length + 1];//记录消费后此时的现金
for (int i = 0; i < getArr2().length; i++) {
temp111[i] = getArr2()[i];
}
temp111[getArr2().length] = getNowMoney();
setArr2(temp111);
}
}
}
}
5.功能3
package LingQianTong;
import java.util.Scanner;
public class choose3 extends choose2 {//收益入账
private double getMoney;//入账金额
Scanner scanner = new Scanner(System.in);
public void choose3() {//对入账金额记录并存入数组,金额<=0时,输出输入有误
System.out.println("本次到账金额为:");
getMoney = scanner.nextDouble();
if (getMoney <= 0) {
System.out.println("金额输入有误");
} else { //对入账金额进行记录
double[] temp = new double[getArr().length + 1];
for (int i = 0; i < getArr().length; i++) {
temp[i] = getArr()[i];
}
setNowMoney(getMoney);
temp[temp.length - 1] = getMoney;
setArr(temp);
double[] temp11 = new double[getArr2().length + 1];
for (int i = 0; i < getArr2().length; i++) {
temp11[i] = getArr2()[i];
}
temp11[getArr2().length] = getNowMoney();
setArr2(temp11);
}
}
}
6.功能4
package LingQianTong;
import java.util.Scanner;
public class choose4 {//退出程序
public char quit() {
System.out.println("确定退出吗?y/n");
Scanner scanner = new Scanner(System.in);
char answer = scanner.next().charAt(0);
if (answer == 'y') {
return 'y';
} else {
return 'n';
}
}
}
4.运行操作
1.运行代码
2.存入100
3.存入1500
4.消费568
5.查询明细
6.退出
5.尾语
如果有正在学习的小伙伴欢迎留言
版权声明:本文为qq_45873372原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。