项目-零钱通

时间:2022-10-01 17:59:40

好久没更新了,最近学了Java语言,下面简单写个项目-零钱通:


化繁为简: 1. 先完成显示菜单,并可以选择菜单,给出对应显示 2. 完成零钱通明细 3. 完成收益入账 4. 消费 5. 退出 6. 代码优化 方法1(面向过程):

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

public class SmallChangeSys {
    public static void main(String[] args) {
        boolean loop = true;
        double money = 0;
        double balance = 0;
        String note = "";
        String choice = "";
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Scanner scanner = new Scanner(System.in);
        String key = "";
        String detail = "---------------零钱通明细--------------";

        do{
            System.out.println("\n--------------零钱通菜单--------------");
            System.out.println("\t\t\t1 零钱通明细");
            System.out.println("\t\t\t2 收益入账");
            System.out.println("\t\t\t3 消费");
            System.out.println("\t\t\t1" + "4 退     出");
            System.out.print("请选择(1-4):");
            key = scanner.next();
            //菜单
            switch(key) {
                case "1":
                    System.out.println(detail);
                    break;
                case "2":
                    System.out.print("请输入入账的金额:");
                    money = scanner.nextDouble();
                    if(money <= 0) {
                        System.out.println("你的入账金额应该在 大于 0");
                        break;
                    }
                    balance += money;
                    detail += "\n收益入账\t+" + money + "\t" + sdf.format(date) +
                            "\t余额:" + balance;
                    break;
                case "3":
                    System.out.print("输入消费项目:");
                    note = scanner.next();
                    System.out.print("输入消费金额:");
                    money = scanner.nextDouble();
                    if(money <= 0 || money > balance) {
                        System.out.println("你的消费金额 应该在 0-" + balance);
                        break;
                    }
                    balance -= money;
                    detail += "\n"+ note + "\t-" + money + "\t" + sdf.format(date) +
                            "\t余额:" + balance;
                    break;
                case "4":
                    System.out.print("您是否要退出");
                    while(true) {
                        System.out.print("\t输入(y/n):");
                        choice = scanner.next();
                        if(choice.equals("y") || choice.equals(("n"))) {
                         break;
                        }
                    }
                    if(choice.equals("y")) {
                        loop = false;
                    }
                    break;
            }
        }while(loop);
        System.out.println("退出了零钱菜单");
    }
}

方法2(面向对象-OOP):

package OOP;
//main类
public class SmallChangeSysApp {
    public static void main(String[] args) {
        new SmallChangeSysOOP().mainMenu();
    }
}
//菜单类
package OOP;
package OOP;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSysOOP {
    boolean loop = true;
    double money = 0;
    double balance = 0;
    String note = "";
    String choice = "";
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Scanner scanner = new Scanner(System.in);
    String key = "";
    String detail = "---------------零钱通明细--------------";
    public void mainMenu() {
        do{
            System.out.println("\n--------------选择零钱通菜单--------------");
            System.out.println("\t\t\t1 零钱通明细");
            System.out.println("\t\t\t2 收益入账");
            System.out.println("\t\t\t3 消费");
            System.out.println("\t\t\t1" + "4 退     出");
            System.out.print("请选择(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;
            }
        }while(loop);
        System.out.println("退出了零钱菜单");
    }
    public void detail() {
        System.out.println(detail);
        return;
    }
    public void income() {
        System.out.print("请输入入账的金额:");
        money = scanner.nextDouble();
        if(money <= 0) {
            System.out.println("你的入账金额应该在 大于 0");
            return;		//返回方法
        }
        balance += money;
        detail += "\n收益入账\t+" + money + "\t" + sdf.format(date) +
                "\t余额:" + balance;
        return;
    }
    public void pay() {
        System.out.print("输入消费项目:");
        note = scanner.next();
        System.out.print("输入消费金额:");
        money = scanner.nextDouble();
        if(money <= 0 || money > balance) {
            System.out.println("你的消费金额 应该在 0-" + balance);
            return;
        }
        balance -= money;
        detail += "\n"+ note + "\t-" + money + "\t" + sdf.format(date) +
                "\t余额:" + balance;
        return;
    }
    public void exit() {
        System.out.print("您是否要退出");
        while(true) {
            System.out.print("\t输入(y/n):");
            choice = scanner.next();
            if(choice.equals("y") || choice.equals(("n"))) {
                break;
            }
        }
        if(choice.equals("y")) {
            loop = false;
        }
        return;
    }
}

目标大厂,冲冲冲!!