前言
适合初学者理解面向对象以及初步建立系统概念,体会面向对象OOP优点
需求
使用 Java 开发零钱通项目 , 可以完成收益入账,消费,查看明细,退出系统等功能。
界面
项目实现
过程实现
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class SmallChangeSys { public static void main(String[] args) { //定义变量并初始化 boolean loop = true; String details = "=============================零钱通明细============================"; double money = 0.0; double moneyin = 0.0; double moneyout = 0.0; String note = "null"; Date date = null; Date date_out= null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//格式化时间格式 do { System.out.println("=============================零钱通明细============================"); System.out.println("\t\t\t\t\t\t1 零钱通明细"); System.out.println("\t\t\t\t\t\t2 收益入账"); System.out.println("\t\t\t\t\t\t3 消 费"); System.out.println("\t\t\t\t\t\t4 退 出"); System.out.println("请选择(1-4):"); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); switch (i){ case 1 : System.out.println("1 零钱通明细"); System.out.println(details); break; case 2 : System.out.println("2 收益入账"); Scanner in = new Scanner(System.in); boolean flag = true; while (flag){ //对收益入账金额进行校验 >0 System.out.print("收益入账金额:"); moneyin = in.nextDouble(); if (moneyin <= 0){ System.out.println("收益入账金额必须大于0,请重新输入"); flag = true; }else { flag = false; } } money = money +moneyin; System.out.print("收益入账说明:"); note = in.next(); date = new Date(); details += "\n收益入账 \t+" + moneyin + "\t时间:" + sdf.format(date) + "\t余额:" + money + "\t说明:" + note; break; case 3 : System.out.println("3 消 费"); Scanner out = new Scanner(System.in); boolean flag_out = true; while (flag_out){ //对消费金额进行校验 小于余额 System.out.print("消费金额:"); moneyout = out.nextDouble(); if (moneyout > money){ System.out.println("消费金额大于余额("+money+"),请重新消费"); flag_out = true; }else { flag_out = false; } } money = money - moneyout; System.out.print("消费说明:"); note = out.next(); date_out = new Date(); System.out.print("消费金额:"); details += "\n消 费\t-" + moneyout + "\t时间:" + sdf.format(date_out) + "\t余额:" + money+ "\t说明:" + note; break; case 4 : System.out.println("4 退出"); String exit_loop = null; while (true){ //退出确认,并校验输入是否有效 System.out.println("是否确认退出,y/n"); Scanner exit = new Scanner(System.in); exit_loop = exit.next(); if ("y".equals(exit_loop) || "n".equals(exit_loop)){ break; } else { System.out.println("输入有误,请重新输入"); } } if (exit_loop.equals("y") ){ loop = false; }else { loop = true; } break; default: System.out.println("菜单选择有误,请重新输入(1-4)"); } }while(loop); System.out.println("零钱通已退出"); } }