package com.lic; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class demoATM { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<Atm> at=new ArrayList<>(); while (true){ System.out.println("注册账户 登录账户"); String a = sc.next(); at.add(xuanxiang(a,at)); } } public static Atm xuanxiang(String a,ArrayList<Atm> at) { Atm atm=new Atm(); if (a.equals("注册账户")) { atm=zhuce(at); return atm; } else if (a.equals("登录账户")) { int c=denglu(at); caozuo(c,at); return atm; }else System.out.println("命令错误,请重新输入"); return atm; } public static Atm zhuce(ArrayList<Atm> at) { Random ra = new Random(); Scanner sc = new Scanner(System.in); int u=1; int o=0; while (u!=0){ o= ra.nextInt(10) + 89; u=0; for (int i = 0; i < at.size(); i++) { if (o==at.get(i).getCar()){ u++; } } } System.out.println("你的卡号是" +o); System.out.println("请输入你的姓名"); String name = sc.next(); String mima; while (true) { System.out.println("请输入你的密码"); mima = sc.next(); System.out.println("请你再次输入密码"); String mi = sc.next(); if (mi.equals(mima)) { break; } } Double c = 0.0; Atm use =new Atm(); use=(new Atm(o, name, c, mima)); System.out.println("恭喜你,注册成功"); return use; } public static int denglu(ArrayList<Atm> atms){ Scanner sc=new Scanner(System.in); System.out.println("请输入账号"); int a= sc.nextInt(); System.out.println("请输入密码"); String b=sc.next(); for (int i = 0; i < atms.size(); i++) { if (a==atms.get(i).getCar()){ // System.out.println("请输入密码"); // String b=sc.next(); if (b.equals(atms.get(i).getMima())){ System.out.println("登录成功"); return i; } } } System.out.println("登录失败"); return atms.size()+5; } public static int caozuo(int a,ArrayList<Atm> at){ Scanner sc=new Scanner(System.in); if (a<at.size()){ while (true){ System.out.println("请你选择序号进行操作"); System.out.println("1.查询余额"+"\t"+"2.存钱"+"\t"+"3.取钱"); System.out.println("4.转账"+"\t"+"5.修改密码"+"\t"+"6.返回"); System.out.println("7.注销账号"); int nmb=sc.nextInt(8); switch (nmb){ case 1: System.out.println(at.get(a).getNumber()); break; case 2: Double c=sc.nextDouble(); Double b; b=c+at.get(a).getNumber(); at.get(a).setNumber(b); System.out.println("你此次存了"+c+"元"+"账户余额还有"+b+"元"); break; case 3: qu(a,at); break; case 4: zhuan(a,at); break; case 5: xiu(a,at); break; case 6: return 0; case 7: int zhux=zhu(a,at); if (zhux==1){ return 0; } } } } return 0; } public static Double qu(int a,ArrayList<Atm> at){ Scanner sc=new Scanner(System.in); System.out.println("请输入取钱金额"); Double nmb=sc.nextDouble(); if (nmb<=at.get(a).getNumber()){ at.get(a).setNumber(at.get(a).getNumber()-nmb); Double o=at.get(a).getNumber(); System.out.println("你的余额还剩" + o + "元"); return o; } else {Double o=at.get(a).getNumber(); System.out.println("金额不足"); return o; } } public static Double zhuan(int a,ArrayList<Atm> at){ int c=0; while (true){ Scanner sc=new Scanner(System.in); System.out.println("请输入转账账户"); int nmb=sc.nextInt(); for (int i = 0; i <at.size() ; i++) { if (nmb==at.get(i).getCar()){ c=i; } else if(i==at.size()-1&&c==0) { System.out.println("不存在该账户请重新输入"); return 0.0; } } String name=(at.get(c).getName()); System.out.println("*" + name.substring(1)); for (int j = 3; j >0 ; j--) { System.out.println("请输入收款人姓氏,你有"+j+"次机会"); String na=sc.next(); if (na.equals(name.substring(0,1))){ Double nm=z(a,at); at.get(c).setNumber(at.get(c).getNumber()+nm); return 0.0; } } return 0.0; } } public static Double z(int a,ArrayList<Atm> at){ Scanner sc=new Scanner(System.in); System.out.println("请输入转账金额"); double nmb=sc.nextDouble(); if (nmb<=at.get(a).getNumber()){ at.get(a).setNumber(at.get(a).getNumber()-nmb); Double o=at.get(a).getNumber(); System.out.println("转账成功"); System.out.println("你的余额还剩" + o + "元"); return nmb; } else {Double o=at.get(a).getNumber(); System.out.println("金额不足"); System.out.println("转账失败"); return 0.0; } } public static void xiu(int a,ArrayList<Atm> at){ Scanner sc=new Scanner(System.in); int i=3; while (i>0) { i--; System.out.println("请输入你的密码新密码,你还有"+i+"次机会"); String mima = sc.next(); System.out.println("请你再次输入密码"); String mi = sc.next(); if (mi.equals(mima)) { at.get(a).setMima(mi); break; } } } public static int zhu(int a,ArrayList<Atm> at){ Scanner sc=new Scanner(System.in); System.out.println("请输入密码验证信息"); String mi=sc.next(); if (mi.equals(at.get(a).getMima())){ Atm z=new Atm(); z=at.remove(a); System.out.println(z.getName() + "先生,你的账户注销完成"); return 1; }else System.out.println("密码错误,注销失败"); return 0; } }
创建ATM机属性类
package com.lic; public class Atm { private int car; private String name; private Double number; private String mima; public Atm(int car, String name, Double number, String mima) { this.car = car; this.name = name; this.number = number; this.mima = mima; } public Atm() { } public int getCar() { return car; } public void setCar(int car) { this.car = car; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getNumber() { return number; } public void setNumber(Double number) { this.number = number; } public String getMima() { return mima; } public void setMima(String mima) { this.mima = mima; } }
程序实现一下功能