首页 > 其他分享 >零钱通系统

零钱通系统

时间:2022-10-29 11:12:07浏览次数:42  
标签:sc record money 系统 System 零钱 println out

零钱通系统

分析:

1.选择菜单,用到基本的switch语句

2.最好做的功能即是退出

3.创建两个类,账户类和记录类,其中界面所展示的零钱通明细中的金额,时间,余额都放于账户类创建的记录类数组中

4.明细即是对创建的数组进行遍历

5.再对剩下的功能进行完善

代码展示

public class App {
    public static void main(String[] args) {
        Account account = new Account();
        boolean isBreak = true;
        while (isBreak) {
            Scanner sc = new Scanner(System.in);
            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 option = sc.nextInt();
            switch (option) {
                case 1:
                    System.out.println("------------零钱通-----------");
                    account.print();
                    break;
                case 2:
                    System.out.println("---------------收益入账---------------");
                    System.out.print("收入来源:");
                    String name = sc.next();
                    System.out.print("金额:");
                    double money = sc.nextDouble();
                    System.out.print("日期:");
                    String date = sc.next();
                    Record record = new Record(name,money,date,0);
                    account.accounting(record);
                    break;
                case 3:
                    System.out.println("---------------消费---------------");
                    System.out.print("消费内容:");
                    String c = sc.next();
                    System.out.print("金额:");
                    double m = sc.nextDouble();
                    System.out.print("日期:");
                    String d = sc.next();
                    Record r = new Record(c,m,d,1);
                    account.accounting(r);
                    break;
                case 4:
                    System.out.println("确认退出吗?(y/n)");
                    if (sc.next().equals("y")) {
                        isBreak = false;
                    }
                    break;
                default:
                    break;
            }
        }

    }
}

账户类和记录类

public class Account {
    double total;
    Record[] records;
    int count ;
    public void accounting(Record record) {
        if (records == null) {
            records = new Record[10];
        } else if (count == records.length) {
            records = Arrays.copyOf(records, records.length + 10);
        }

        boolean isOutcome = true;
        if (record.type==0){
            income(record.money);
        }else if (record.type==1){
            outcome(record.money);
        }

        if (isOutcome){
            record.balance=total;
            records[count]=record;
            count++;
        }

    }

    public void  income(double money){
        total += money;
    }

    public boolean outcome(double money){
        if (total-money<0){
            System.out.println("余额不足,请及时充值");
            return false;
        }else {
            total -= money;
            return true;
        }
    }

    public  void  print(){
        for (int i = 0; i < count; i++) {
            Record item = records[i];
            String money = "";
            if (item.type==0){
                money="+"+item.money;
            }else if (item.type==1) {
                money="-"+item.money;
            }
            System.out.printf("%s \t\t %s \t\t %s \t\t 余额:%.2f \n",item.name,money,item.dateTime,item.balance);
        }
    }


}

 

标签:sc,record,money,系统,System,零钱,println,out
From: https://www.cnblogs.com/ZhangJingkai/p/16838277.html

相关文章