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; } }