zoukankan      html  css  js  c++  java
  • Java 零钱通小项目练习,感谢韩老师

    感谢韩顺平大神!!!请大家多多关注转发!!!

    Java视频基础教程:https://www.bilibili.com/video/BV1fh411y7R8

    本项目练习地址:https://www.bilibili.com/video/BV1fh411y7R8?p=335

    package com.joshua317;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            ChangeSystem changeSystem = new ChangeSystem();
            Scanner scanner = new Scanner(System.in);
            String number;
            do {
                System.out.println("1.零钱明细");
                System.out.println("2.收入");
                System.out.println("3.消费");
                System.out.println("4.退出");
                System.out.println("请输入要处理的选项:");
                try {
                    number = scanner.next();
                    switch (number) {
                        case "1":
                            changeSystem.detail();
                            break;
                        case "2":
                            changeSystem.increment();
                            break;
                        case "3":
                            changeSystem.expend();
                            break;
                        case "4":
                            changeSystem.exit();
                            break;
                        default:
                            System.out.println("===============重新输入==============\n");
                    }
                }catch (Exception exception) {
                    System.out.println("警告:请输入正确的内容\n");
                }
    
            }while (changeSystem.flag);
    
        }
    
        /**
         * 面向过程
         */
        public static void procedureOriented()
        {
            System.out.println("===============零钱包管理小demo===============");
            String number;
            boolean flag = true;
            Scanner scanner = new Scanner(System.in);
            String change = "";
            double totalMoney = 0.0;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            do {
                System.out.println("1.零钱明细");
                System.out.println("2.收入");
                System.out.println("3.消费");
                System.out.println("4.退出");
                System.out.println("请输入要处理的选项:");
    
                try {
                    number = scanner.next();
                    switch (number) {
                        case "1":
                            System.out.println("===============1.零钱明细===============\n");
                            System.out.println("收入\t\t金额\t\t\t余额\t\t\t日期\n");
                            System.out.println(change);
                            break;
                        case "2":
                            System.out.println("===============2.收入===============");
                            System.out.println("请输入收入的金额:");
                            double increment = scanner.nextDouble();
                            if (0 >= increment) {
                                System.out.println("输入金额不能小于0,请重新输入");
                                continue;
                            }
                            totalMoney += increment;
                            Date date = new Date();
                            change += "收入\t\t" + "+" + increment +"\t\t"+ totalMoney + "\t\t" + simpleDateFormat.format(date) +"\n";
                            break;
                        case "3":
                            System.out.println("===============3.消费===============\n");
                            System.out.println("请输入消费的金额:");
                            double increment2 = scanner.nextDouble();
                            if (0 >= increment2) {
                                System.out.println("输入消费金额不能小于0,请重新输入");
                                continue;
                            }
                            if (increment2 > totalMoney) {
                                System.out.println("余额不足,请及时挣钱补充收入");
                                continue;
                            }
                            System.out.println("请输入消费事项:");
                            String consume = scanner.next();
                            totalMoney -= increment2;
                            Date date2 = new Date();
                            change += consume+"\t\t" + "-" + increment2 +"\t\t"+ totalMoney + "\t\t" + simpleDateFormat.format(date2) +"\n";
                            break;
                        case "4":
                            System.out.println("===============4.退出===============\n");
                            flag = false;
                            break;
                        default:
                            System.out.println("===============重新输入==============\n");
                    }
                }catch (Exception exception) {
                    System.out.println("警告:请输入正确的内容\n");
                }
    
            }while (flag);
        }
    }
    
    /**
     * 面向对象
     */
    class ChangeSystem {
        boolean flag = true;
        Scanner scanner = new Scanner(System.in);
        String change = "";
        double totalMoney = 0.0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        //收入
        public void increment()
        {
            System.out.println("===============2.收入===============");
            System.out.println("请输入收入的金额:");
            double increment = scanner.nextDouble();
            if (0 >= increment) {
                System.out.println("输入金额不能小于0,请重新输入");
                return;
            }
            totalMoney += increment;
            Date date = new Date();
            change += "收入\t\t" + "+" + increment +"\t\t"+ totalMoney + "\t\t" + simpleDateFormat.format(date) +"\n";
        }
        //支出
        public void expend()
        {
            System.out.println("===============3.消费===============\n");
            System.out.println("请输入消费的金额:");
            double increment2 = scanner.nextDouble();
            if (0 >= increment2) {
                System.out.println("输入消费金额不能小于0,请重新输入");
                return;
            }
            if (increment2 > totalMoney) {
                System.out.println("余额不足,请及时挣钱补充收入");
                return;
            }
            System.out.println("请输入消费事项:");
            String consume = scanner.next();
            totalMoney -= increment2;
            Date date2 = new Date();
            change += consume+"\t\t" + "-" + increment2 +"\t\t"+ totalMoney + "\t\t" + simpleDateFormat.format(date2) +"\n";
        }
        //明细
        public void detail()
        {
            System.out.println("===============1.零钱明细===============\n");
            System.out.println("收入\t\t金额\t\t\t余额\t\t\t日期\n");
            System.out.println(change);
        }
        //退出
        public void exit()
        {
            System.out.println("===============4.退出===============\n");
            flag = false;
        }
    }

  • 相关阅读:
    vue axios接口封装、Promise封装、简单的axios方法封装、vue接口方法封装、vue post、get、patch、put方法封装
    vue-router 报错、:Avoided redundant navigation to current location 错误、路由重复
    微信小程序支付、小程序支付功能、小程序支付方法、微信小程序支付方法
    微信小程序热更新,小程序提示版本更新,版本迭代,强制更新,微信小程序版本迭代
    微信小程序动态修改title,动态配置title,动态配置头部,微信小程序动态配置头部
    响应式布局rem、rem方法封装、移动端响应式布局
    jquery 选项卡切换、选项卡封装、简单的jquery选项卡封装、tab切换效果
    js获取url并截取相应的字段,js解决url获取中文字段乱码问题
    微信小程序接口封装、原生接口封装、request、promise封装
    20193327《Python程序设计》实验报告三
  • 原文地址:https://www.cnblogs.com/joshua317/p/15571392.html
Copyright © 2011-2022 走看看