项目名为“零钱通”
细节参考:
【零基础 快速学Java】韩顺平 零基础30天学会Java
基本版:
1 package project; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.Scanner; 6 7 public class SmallChangeSys { 8 public static void main(String[] args) { 9 //1.零钱通菜单 10 boolean loop = true; 11 Scanner scanner = new Scanner(System.in); 12 String key = \"\"; 13 //2.零钱通明细 14 String details = \"==============零钱通明细==============\"; 15 //3.收益入账 16 double money = 0; 17 double balance = 0; 18 Date date = null; 19 SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd HH:mm\");//用于日期格式化的 20 //4.零钱通消费 21 String note = \"\"; 22 //5.零钱通退出确认 23 String choice = \"\"; 24 //6.判断金额入账和消费时金额是否合理 25 26 do { 27 System.out.println(\"==============零钱通菜单==============\"); 28 System.out.println(\"\\t\\t\\t1 零钱通明细\"); 29 System.out.println(\"\\t\\t\\t2 收益入账\"); 30 System.out.println(\"\\t\\t\\t3 消费\"); 31 System.out.println(\"\\t\\t\\t4 退 出\"); 32 33 System.out.println(\"请选择1-4\"); 34 key = scanner.next(); 35 36 //switch 控制 37 switch (key) { 38 case \"1\": 39 System.out.println(details); 40 break; 41 case \"2\": 42 System.out.println(\"收益入账金额\"); 43 money = scanner.nextDouble(); 44 //money范围校验 45 if (money <= 0) { 46 System.out.println(\"收益入账金额范围 需要 大于 0\"); 47 break; 48 } 49 balance += money; 50 //拼接信息到收益入账 51 date = new Date();//获取当前日期 52 details += \"\\n收益入账\\t+\" + money + \"\\t\" + sdf.format(date) + \"\\t\" + balance; 53 break; 54 case \"3\": 55 System.out.println(\"3 消费\"); 56 money = scanner.nextDouble(); 57 //money范围校验 58 if (money <= 0 || money > balance) { 59 if (money <= 0){ 60 System.out.println(\"您的消费金额应该 大于 0\"); 61 break; 62 }else{ 63 System.out.println(\"消费不能透支余额!\"); 64 break; 65 } 66 } 67 68 System.out.println(\"消费说明\"); 69 note = scanner.next(); 70 balance -= money; 71 date = new Date();//获取当前日期 72 details += \"\\n\" + note + \"\\t-\" + money + \"\\t\" + sdf.format(date) + \"\\t\" + balance; 73 break; 74 case \"4\": 75 System.out.println(\"4 退 出\"); 76 77 while (true) { 78 System.out.println(\"你确定要退出吗?y/n\"); 79 choice = scanner.next(); 80 if (\"y\".equals(choice) || \"n\".equals(choice)) { 81 break; 82 } 83 84 } 85 //当用户退出while后再判断choice是y还是n,就可以决定是否退出 86 if (\"y\".equals(choice)) { 87 loop = false; 88 } 89 90 break; 91 default: 92 System.out.println(\"选择有误,请重新选择\"); 93 break; 94 } 95 } while (loop); 96 System.out.println(\"==============退出了零钱通项目==============\"); 97 98 99 } 100 }
OOP版:
SmallChangeSysOOP:
1 package project; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import java.util.Scanner; 6 7 /** 8 * 该类完成零钱通的各个功能 9 * 要求使用OOP(面向对象) 10 */ 11 public class SmallChangeSysOOP { 12 //属性 13 //1.零钱通菜单 14 boolean loop = true; 15 Scanner scanner = new Scanner(System.in); 16 String key = \"\"; 17 //2.零钱通明细 18 String details = \"==============零钱通明细==============\"; 19 //3.收益入账 20 double money = 0; 21 double balance = 0; 22 Date date = null; 23 SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd HH:mm\");//用于日期格式化的 24 //4.零钱通消费 25 String note = \"\"; 26 //5.零钱通退出确认 27 String choice = \"\"; 28 //6.判断金额入账和消费时金额是否合理 29 30 //完成零钱通菜单 31 public void mainMenu(){ 32 do { 33 System.out.println(\"============零钱通菜单(OOP)============\"); 34 System.out.println(\"\\t\\t\\t1 零钱通明细\"); 35 System.out.println(\"\\t\\t\\t2 收益入账\"); 36 System.out.println(\"\\t\\t\\t3 消费\"); 37 System.out.println(\"\\t\\t\\t4 退 出\"); 38 39 System.out.println(\"请选择1-4\"); 40 key = scanner.next(); 41 42 //switch 控制 43 switch (key) { 44 case \"1\": 45 this.detail(); 46 break; 47 case \"2\": 48 this.income(); 49 break; 50 case \"3\": 51 this.pay(); 52 break; 53 case \"4\": 54 this.exit(); 55 break; 56 default: 57 System.out.println(\"选择有误,请重新选择\"); 58 break; 59 } 60 } while (loop); 61 } 62 //完成零钱通明细 63 public void detail(){ 64 System.out.println(details); 65 } 66 //完成收益入账 67 public void income(){ 68 System.out.println(\"收益入账金额\"); 69 money = scanner.nextDouble(); 70 //money范围校验 71 if (money <= 0) { 72 System.out.println(\"收益入账金额范围 需要 大于 0\"); 73 return;//退出方法,不再执行后面的代码 74 } 75 balance += money; 76 //拼接信息到收益入账 77 date = new Date();//获取当前日期 78 details += \"\\n收益入账\\t+\" + money + \"\\t\" + sdf.format(date) + \"\\t\" + balance; 79 } 80 //完成消费 81 public void pay(){ 82 System.out.println(\"3 消费\"); 83 money = scanner.nextDouble(); 84 //money范围校验 85 if (money <= 0 || money > balance) { 86 if (money <= 0){ 87 System.out.println(\"您的消费金额应该 大于 0\"); 88 return; 89 }else{ 90 System.out.println(\"消费不能透支余额!\"); 91 return; 92 } 93 } 94 95 System.out.println(\"消费说明\"); 96 note = scanner.next(); 97 balance -= money; 98 date = new Date();//获取当前日期 99 details += \"\\n\" + note + \"\\t-\" + money + \"\\t\" + sdf.format(date) + \"\\t\" + balance; 100 } 101 //退出 102 public void exit(){ 103 System.out.println(\"4 退 出\"); 104 105 while (true) { 106 System.out.println(\"你确定要退出吗?y/n\"); 107 choice = scanner.next(); 108 if (\"y\".equals(choice) || \"n\".equals(choice)) { 109 break; 110 } 111 112 } 113 //当用户退出while后再判断choice是y还是n,就可以决定是否退出 114 if (\"y\".equals(choice)) { 115 loop = false; 116 } 117 } 118 119 }
SmallChangeSysApp:
1 package project; 2 3 4 /** 5 * 直接调用SmallChangeSysOOP对象,显示主菜单 6 */ 7 public class SmallChangeSysApp { 8 public static void main(String[] args) { 9 new SmallChangeSysOOP().mainMenu(); 10 } 11 }
感谢韩老师精细的视频讲解,推荐新入行的小伙伴学习
链接:【零基础 快速学Java】韩顺平 零基础30天学会Java_哔哩哔哩_bilibili
来源:https://www.cnblogs.com/Mmbhcyt/p/16184594.html
本站部分图文来源于网络,如有侵权请联系删除。