模拟Atm机的一些简单功能
UserInfo类
public class UserInfo {private int id; //idprivate String name; //姓名private String password; //密码private String idCard; //卡号private String bank; //银行名称private double money; //余额@Overridepublic String toString() {return "UserInfo{" +"id=" + id +", name='" + name + '\'' +", password='" + password + '\'' +", idCard='" + idCard + '\'' +", bank='" + bank + '\'' +", money=" + money +'}';}public UserInfo() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getIdCard() {return idCard;}public void setIdCard(String idCard) {this.idCard = idCard;}public String getBank() {return bank;}public void setBank(String bank) {this.bank = bank;}public double getMoney() {return money;}public void setMoney(double money) {this.money = money;}public UserInfo(int id, String name, String password, String idCard, String bank, double money) {this.id = id;this.name = name;this.password = password;this.idCard = idCard;this.bank = bank;this.money = money;}
}
方法接口:
public interface controllerDemo {//登录方法public void login(UserInfo tom);//主页面public void showMenu(UserInfo tom);//查询余额public void Balance(UserInfo tom);//取款public void Withdraw(UserInfo tom);//存款public void deposit(UserInfo tom);//转账public void Transfer(UserInfo tom);//打印public void prints() throws IOException;
}
后台方法实现类:
public class controllerDemoImpl implements controllerDemo {static Scanner sc = new Scanner(System.in);static UserInfo tom = new UserInfo(1, "汤姆", "1", "1", "邮政", 10000);static UserInfo jack = new UserInfo(2, "张三", "1", "1", "招商", 10000);@Overridepublic void login(UserInfo tom) {System.out.println("请输入您的卡号:");String idCard = sc.next();System.out.println("请输入您的密码:");String password = sc.next();if (idCard.equals(tom.getIdCard()) && password.equals(tom.getPassword())) {//如果账号密码正确,则进入主页面showMenu(tom);} else {System.out.println("请使用指定账号");login(tom);}}/*** 主界面;* 判断用户进行的何种操作** @param tom*/@Overridepublic void showMenu(UserInfo tom) {System.out.println("***********************************");System.out.println("*******请输入你想要操作类型*************");System.out.println("******1.余额查询 2.取款*******");System.out.println("******3.存款 4.转账*******");System.out.println("******5.打印 6.退卡*******");System.out.println("*****************7.查询转账信息******************");System.out.println("***********************************");int next = sc.nextInt();switch (next) {case 1:System.out.println("查询余额" + next);Balance(tom);showMenu(tom);break;case 2:System.out.println("取款" + next);Withdraw(tom);showMenu(tom);break;case 3:System.out.println("存款" + next);deposit(tom);showMenu(tom);break;case 4:System.out.println("转账" + next);Transfer(tom);showMenu(tom);break;case 5:System.out.println("打印" + next);prints();showMenu(tom);break;case 6:System.out.println("退卡" + next);System.exit(0);break;case 7:System.out.println("查询转账信息" + next);prints();showMenu(tom);break;default:break;}}/*** 查询余额*/@Overridepublic void Balance(UserInfo tom) {System.out.println("您当前的余额为:" + tom.getMoney() + "元");}/*** 取款** @param tom*/@Overridepublic void Withdraw(UserInfo tom) {System.out.println("*****请选择你要操作菜单的序号*****");System.out.println("*****1.100 2.200****");System.out.println("*****3.500 4.800****");System.out.println("*****5.1000 6.2000***");System.out.println("*****7.其它 8.返回****");String next = sc.next();switch (next) {case "1":if (tom.getMoney() < 100) {System.out.println("卡余额不足,请先充值");}tom.setMoney(tom.getMoney() - 100);Balance(tom);break;case "2":if (tom.getMoney() < 200) {System.out.println("卡余额不足,请先充值");}tom.setMoney(tom.getMoney() - 200);Balance(tom);showMenu(tom);break;case "3":if (tom.getMoney() < 500) {System.out.println("卡余额不足,请先充值");}tom.setMoney(tom.getMoney() - 500);Balance(tom);showMenu(tom);break;case "4":if (tom.getMoney() < 800) {System.out.println("卡余额不足,请先充值");}tom.setMoney(tom.getMoney() - 800);Balance(tom);showMenu(tom);break;case "5":if (tom.getMoney() < 1000) {System.out.println("卡余额不足,请先充值");}tom.setMoney(tom.getMoney() - 1000);Balance(tom);showMenu(tom);break;case "6":if (tom.getMoney() < 2000) {System.out.println("卡余额不足,请先充值");}tom.setMoney(tom.getMoney() - 2000);Balance(tom);showMenu(tom);break;case "7":System.out.println("请输入您要操作的金额:");double i = sc.nextDouble();if (i > tom.getMoney()) {System.out.println("卡余额不足,请先充值");} else {tom.setMoney(tom.getMoney() - i);Balance(tom);showMenu(tom);}break;case "8":showMenu(tom);break;default:System.out.println("请输入正确数字");showMenu(tom);break;}}/*** 存钱** @param tom*/@Overridepublic void deposit(UserInfo tom) {System.out.println("请输入您需要存入的金额:");double i = sc.nextDouble();tom.setMoney(tom.getMoney() + i);}@Overridepublic void Transfer(UserInfo tom) {System.out.println("输入如对方卡号:");String next = sc.next();//验证卡号是否存在if (jack.getIdCard().equals(next)) {System.out.println("请输入您要转账的金额:");double i = sc.nextDouble();if (tom.getMoney() < i) {System.out.println("卡余额不足,请先充值!!!");} else {tom.setMoney(tom.getMoney() - i);jack.setMoney(jack.getMoney() + i);System.out.println("您当前的余额为:" + tom.getMoney() + "元");System.out.println("jack当前的余额为:" + jack.getMoney() + "元");}} else {System.out.println("对方卡号不存在,请核对后重输;");}}/*** 打印*/@Overridepublic void prints(){System.out.println("文件存放路径:G:\\桌面\\Java复习\\111.txt");File fi = new File("G:\\桌面\\Java复习\\111.txt");//判断该路径下是否有该文件if (fi.exists()) {//如果有则删除fi.delete();} else {try {//没有就新建一个,并写入tom的个人信息FileWriter fw = new FileWriter(fi);fw.write(tom.toString());//flush:Java中的IO流中的输出流一般都有flush这个操作,这个操作的作用是强制将缓存中的输出流(字节流,字符流等)强制输出。fw.flush();} catch (Exception e) {e.printStackTrace();}}}
测试类:
public class AtmOperation {static UserInfo tom = new UserInfo(1, "汤姆", "1", "1", "邮政", 10000);static UserInfo jack = new UserInfo(2, "张三", "1", "1", "招商", 10000);static List<UserInfo> users = new ArrayList<>();static controllerDemoImpl con = new controllerDemoImpl();public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("*****************");System.out.println("****欢迎使用ATM****");System.out.println("*****************");con.login(tom);}