自己写的第一个java项目!
项目名为“零钱通”
细节参考:
【零基础 快速学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(" 1 零钱通明细"); 29 System.out.println(" 2 收益入账"); 30 System.out.println(" 3 消费"); 31 System.out.println(" 4 退 出"); 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 += " 收益入账 +" + money + " " + sdf.format(date) + " " + 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 += " " + note + " -" + money + " " + sdf.format(date) + " " + 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 }