java技能

package cn.atm; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class AtmDemo { public static void main(String[] args) { new AtmDemo(); } Scanner sc = new Scanner(System.in); String name; String pwd; int money= 0; String history = ""; public AtmDemo() { while (true) { System.out.println("1:注册\n2:登录\n0:退出"); String op = sc.nextLine(); switch (op) { case "1": reg(); break; case "2": login(); break; case "0": System.out.println("再见"); default: System.out.println("请输入正确的选择"); break; } } } public void reg() { System.out.println("请设置你的登录名:"); name = sc.nextLine(); System.out.println("请设置登录的密码:"); pwd = sc.nextLine(); System.out.println("欢迎:注册成功!"); } public void login() { System.out.println("请输入你的登录名:"); String nu = sc.nextLine(); System.out.println("请登输入登录密码:"); String pd = sc.nextLine(); if (name.equals(nu) && pwd.equals(pd)) { System.out.println("登录成功!"); operation(); } else { System.out.println("输入的姓名或者密码有误"); } } public void operation() { one:while (true) { System.out.println("1:存钱\n2:取钱\n3:显示余额\n4:交易记录\n0:退出"); String op = sc.nextLine(); if (op.equals("1")) { savemoney(); } else if (op.equals("2")) { withdraw(); } else if (op.equals("0")) { break one; } else if (op.equals("4")) { history(); } else if (op.equals("3")) { balance(); } } } private void balance() { // TODO Auto-generated method stub System.out.println("余额:\t " + this.money); } private void history() { // TODO Auto-generated method stub System.out.println("\t\t时间\t\t操作\t\t金额\t\t余额\t\t"); System.out.println(history); } private void withdraw() { // TODO Auto-generated method stub System.out.println("请输入你的取款金额:"); String m = sc.nextLine(); // 判断是否输入的是int类型 if (m.matches("\\d+")) {// 使用正则表达式1~N个 // 转成 int money = Integer.parseInt(m);    if (this.money >= money) { this.money -= money; System.out.println("取款成功"); history += "\n" + dt() + "\t\t取钱\t\t" + money + "\t\t" + this.money; } else { System.out.println("余额不足,请重新操作"); operation(); } } } private void savemoney() { // TODO Auto-generated method stub System.out.println("请输入你的存款金额:"); String m = sc.nextLine(); // 判断是否输入的是int类型 if (m.matches("\\d+")) {// 使用正则表达式1~N个 // 转成 int money = Integer.parseInt(m); if(money%100==0) this.money += money; System.out.println("保存成功了"); history += "\n" + dt() + "\t\t存钱\t\t" + money + "\t\t" + this.money; } else { System.out.println("请输入正确的金额"); } } public String dt() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss"); String str = sdf.format(new Date()); return str; } }

你可能感兴趣的:(java技能)