小项目-零钱通

一、面向过程版本

package com.hspedu.smallChange;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeSys {


    public static void main(String[] args) {
        //化繁为简
        //1.先完成显示菜单,并可以选择菜单,给出对应提示


        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key="";
        //2.完成零钱通明细
        String details = "------------------------零钱通明细---------------------";

        //3.完成收益入账
        double money = 0;
        double balance = 0;
        Date date = null;
        //可以用于日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        //4.消费
        String note;

        //5.退出模块加确认
        String  choice;
        do {
            //打印菜单
            System.out.println("========================零钱通菜单=====================");
            System.out.println("\t\t\t1  零钱通明细");
            System.out.println("\t\t\t2  收益入账");
            System.out.println("\t\t\t3  消   费");
            System.out.println("\t\t\t4  退   出");

            System.out.println("请输入1-4选择功能:");
            key = scanner.next();

            switch (key){
                case "1":
//                    System.out.println("零钱通明细");
                    System.out.println(details);
                    break;
                case "2":
                    System.out.print("请输入入账金额:");
                    money = scanner.nextDouble();
                    //找出不正确金额条件,然后给出提示,就直接break
                    if(money <= 0){
                        System.out.println("消费金额需要大于等于0元!");
                        continue;
                    }
                    balance += money;
                    date = new Date();
                    details += "\n收益入账:\t+" +money + "\t" +sdf.format(date) + "\t余额:\t"+balance;
//                    System.out.println(details);
                    break;
                case "3":
                    System.out.print("消费金额:");
                    money = scanner.nextDouble();
                    //money的值需要被校验
                    if(money <=0 || money >balance){
                        System.out.println("消费金额应该在0-"+balance);
                        continue;
                    }
                    System.out.print("消费说明:");
                    note = scanner.next();
                    balance -=money;
                    //拼接消费信息到details
                    date = new Date();//获取当前日期
                    details +="\n"+ note + "\t\t-"+money +"\t"+sdf.format(date) + "\t余额:\t"+balance;


                    break;
                case "4":
//                    System.out.println("退   出");
                    while (true) {
                        System.out.print("你确定要退出吗?y/n");
                        choice = scanner.next();
                        if("y".equals(choice) || "n".equals(choice)) {
                            break;
                        }
                    }
                    if(choice.equals("y")){
                        loop = false;
                    }
                    break;
                default:
                    System.out.println("请检查输入!你的输入有误!");
            }
        }while (loop);
    }

}

二、面向对象版本

2.1SmallChangeSysOOP类
package com.hspedu.smallChange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*
* 使用面对对象编程思想
* */
public class SmallChangeSysOOP {
    //化繁为简
    //1.先完成显示菜单,并可以选择菜单,给出对应提示


    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key="";
    //2.完成零钱通明细
    String details = "------------------------零钱通明细---------------------";

    //3.完成收益入账
    double money = 0;
    double balance = 0;
    Date date = null;
    //可以用于日期格式化
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    //4.消费
    String note;

    //5.退出模块加确认
    String  choice;

    //完成显示菜单,并可以选择
    public void mainMenu() {
        do {
            //打印菜单
            System.out.println("========================零钱通菜单=====================");
            System.out.println("\t\t\t1  零钱通明细");
            System.out.println("\t\t\t2  收益入账");
            System.out.println("\t\t\t3  消   费");
            System.out.println("\t\t\t4  退   出");

            System.out.println("请输入1-4选择功能:");
            key = scanner.next();

            switch (key){
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.pay();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("请检查输入!你的输入有误!");
            }
        }while (loop);
    }
    //完成零钱通明细
    public void detail() {
        System.out.println(details);
    }

    //完成收益入账
    public void  income() {
        System.out.print("请输入入账金额:");
        money = scanner.nextDouble();
        //找出不正确金额条件,然后给出提示,就直接break
        if(money <= 0){
            System.out.println("消费金额需要大于等于0元!");
        }else {
            balance += money;
            date = new Date();
            details += "\n收益入账:\t+" +money + "\t" +sdf.format(date) + "\t余额:\t"+balance;
        }
    }

    //完成消费
    public void pay() {
        System.out.print("消费金额:");
        money = scanner.nextDouble();
        //money的值需要被校验
        if(money <=0 || money >balance){
            System.out.println("消费金额应该在0-"+balance);
        }else {
            System.out.print("消费说明:");
            note = scanner.next();
            balance -=money;
            //拼接消费信息到details
            date = new Date();//获取当前日期
            details +="\n"+ note + "\t\t-"+money +"\t"+sdf.format(date) + "\t余额:\t"+balance;
        }
    }

    public void exit() {
        while (true) {
            System.out.print("你确定要退出吗?y/n");
            choice = scanner.next();
            if("y".equals(choice) || "n".equals(choice)) {
                break;
            }
        }
        if(choice.equals("y")){
            loop = false;
        }
    }
}

2.2 SmallChangeSysAPP类
package com.hspedu.smallChange;

public class SmallChangeSysAPP {
    public static void main(String[] args) {
        SmallChangeSysOOP smallChangeSysOOP =  new SmallChangeSysOOP();
        smallChangeSysOOP.mainMenu();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值