The first small project ATM

Preface: Finished the first small project written in object-oriented: ATM. The code in this article is all based on object-oriented writing.

Project requirements:

  1. Registration: user name, mobile phone number, ID number, card number, password.
  2. Query: The account must exist and the password (three chances, lock the card if it is wrong).
  3. Withdrawal: The account must exist, the password (three chances, lock the card if it is wrong), and the withdrawal amount cannot be greater than the deposit.
  4. Deposit: The account must exist and the deposit amount cannot be less than 0.
  5. Transfer: Both your account and transfer account must exist, the password (three chances, lock the card if it is wrong), and the transfer amount cannot exceed the balance.
  6. Card lock: The account must exist, it can be frozen with a password, or it can be frozen with an ID number.
  7. Card unlock: The account must exist, and only the ID number can be used to unlock it.
  8. Card renewal: renew the card by verifying the ID card, and the previous card becomes invalid after the renewal.
  9. Change password: I would like to change the password, or use the ID card to change the password.
  10. Exit: save data

The finished look:

After entering, let the user register first, and enter the user name, ID card, mobile phone number, and password once and then confirm it once. After successful registration, enter the user operation interface. There are functions required by the subject on the interface. Users can operate according to their needs to achieve user needs.

Registration interface

void logon() {
    
    
//储存的身份证、用户名、手机号都存储在一个叫Card的类里面,体现面向对象
的思想。
		System.out.println("欢迎进入注册系统!");
		Scanner input = new Scanner(System.in);
		System.out.println("请输入你的账户:");
		super.setAccount(input.next());
		System.out.println("请输入你的手机号:");
		super.setPhone(input.nextInt());
		System.out.println("请输入你的身份证号:");
		super.setIdcard(input.nextInt());
		while (true) {
    
    
			System.out.println("请输入你的密码:");
			int passWord1 = input.nextInt();
			System.out.println("请再输入一次你的密码:");
			int passWord2 = input.nextInt();
			//判断两次的输入是否一致
			if(passWord1 == passWord2 ){
    
    
				super.setPassWord(passWord1);
				System.out.println(super.getPassWord());
				break;
			}
			System.out.println("两次输入的密码不相同,请重新开始!");
		}
		Random random = new Random();
		//随机生成卡号
		//因为要的是一个16位数的卡号,我分成了2个8位数的long类型的数
		long a;
		long b;
		while(true) {
    
    
			a = random.nextInt(99999999);
			if(a>10000000) {
    
    //判断一下随机生成的数,是否是一个8位数的
				while(true) {
    
    
					b = random.nextInt(99999999);
					if(b>10000000) {
    
    
					//让a*100000000+b,组成一个16位的数。
						super.setCardid(a*100000000+b);
						break;
					}
				}
			}
			break;
		}
		System.out.println("你的卡号是:"+super.getCardid());
	}

Login interface

The login interface is to match the account and password entered by the user with the stored account and password.

void regiser() {
    
    
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎进入登录界面");
		//在登陆时判断卡是否被锁,用super()来调用父类的数据
		if (super.isIslock()==false) {
    
    
			System.out.println("你的卡已被锁,请解锁");
		}
		while(true) {
    
    
			int count = 0;
			System.out.println("请输入你的卡号:");
			long cardid2 = input.nextLong();
			if (cardid2==super.getCardid()) {
    
    
				System.out.println("请输入你的密码:");
				int passWord2 = input.nextInt();
				System.out.println(super.getPassWord());
				if (super.getPassWord()==passWord2) {
    
    
					System.out.println("登录成功");
					break;
				}else {
    
    
					System.out.println("密码错误,请重新输入");
					count++;
					if(count==3) {
    
    
						super.setIslock(false);
					}
				}
			}else {
    
    
				System.out.println("账号错误,请重新输入");
				count++;
				//用变量count来计数,当为3的时候锁卡下次登陆就要先去解卡
				if(count==3) {
    
    
					super.setIslock(false);
				}
			}
		}
	}

Deposit interface

Because there is no real ATM to count money, so the deposit here is for the user to input by himself, and have the addiction of the rich (although only for a short while)

void addMoney() {
    
    
		Scanner ipnut = new Scanner(System.in);
		System.out.println("欢迎进入存款界面");
		Scanner input = new Scanner(System.in);
		System.out.println("请先登录");
		this.regiser();
		if (super.isIslock()==false) {
    
    
			System.out.println("你的卡已被锁");
		}else {
    
    
			System.out.println("请输入你的存款的金额:");
			int add = input.nextInt();
			super.setMoney (super.getMoney() + add);
			System.out.println("你的账户有" + super.getMoney());
		}
	}

Withdrawal interface

void gainMoney() {
    
    //取款
		System.out.println("欢迎进入取款界面");
		Scanner input = new Scanner(System.in);
		System.out.println("请先登录");
		this.regiser();
		if (super.isIslock()==false) {
    
    
			System.out.println("你的卡已被锁,请解锁");
		}else {
    
    
			System.out.println("请输入你的取款的金额:");
			int gain = input.nextInt();
			//判断取款的金额是否大于存款数
			if (gain>super.getMoney()) {
    
    
				System.out.println("账户余额不足");
			}else {
    
    
				super.setMoney (super.getMoney()-gain);
				System.out.println("你的账户有"+super.getMoney());
			}
		}
	}

Query interface

void query() {
    
    
		System.out.println("欢迎进入查询界面");
		System.out.println("请先登录");
		this.regiser();
		if (super.isIslock()==false) {
    
    
		//判断是否锁卡
			System.out.println("你的卡已被锁,请解锁");
		}else {
    
    
			System.out.println("卡内余额:" + super.getMoney());
		}
	}

Lock card interface

void lock() {
    
    
		System.out.println("欢迎进入锁卡界面");
		Scanner input = new Scanner(System.in);
		System.out.println("请先登录");
		this.regiser();
		if (super.isIslock()==false) {
    
    
			System.out.println("你的卡已被锁");
		}else {
    
    
			System.out.println("是否要执行锁卡操作  1.是  2.否");
			int choice3 = input.nextInt();
			if (choice3==1) {
    
    
				super.setIslock(false);
				System.out.println("锁卡成功");
			}else {
    
    
				super.setIslock(true);
				System.out.println("锁卡失败");
			}
		}
	}

Unlock interface

void unlock() {
    
    
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎进入解锁界面");
		while(true) {
    
    
			System.out.println("请输入你解锁卡的身份证号");
			int idcard2 = input.nextInt();
			if(idcard2 == super.getIdcard()) {
    
    
				System.out.println("验证成功");
				System.out.println("你的卡已解锁");
				super.setIslock(true);
				break;
			}else {
    
    
				System.out.println("验证失败,请重新输入");
			}
		}
	}

Card replacement interface

void newCard() {
    
    
		System.out.println("欢迎进入补卡界面");
		Scanner input = new Scanner(System.in);
		Random random = new Random();
		while (true) {
    
    
		//调用数据对比身份证信息
			System.out.println("请输入你的身份证进行验证:");
			int idcard1 = input.nextInt();
			if (super.getIdcard() != idcard1) {
    
    
				System.out.println("请重新输入你的身份证进行验证:");
				idcard1 = input.nextInt();
			}
			break;
		}
		while(true) {
    
    
		//生成一个新的卡号
			int a = random.nextInt(99999999);
			int b = random.nextInt(99999999);
			if(a>10000000 && b>10000000) {
    
    
				super.setCardid(a*10000000+b);
				break;
			}
		System.out.println("请重新输入你的身份证进行验证:" + super.getCardid());
		}
	}

Modify password interface

void changePwd() {
    
    
		System.out.println("欢迎进入修改密码界面");
		Scanner input = new Scanner(System.in);
		while (true) {
    
    
			System.out.println("请输入你的身份证进行验证:");
			int idcard1 = input.nextInt();
			if (super.getIdcard() != idcard1) {
    
    
				System.out.println("请重新输入你的身份证进行验证:");
				idcard1 = input.nextInt();
			}
			break;
		}
		System.out.println("请输入你的新密码");
		super.setPassWord(input.nextInt());
		System.out.println("修改密码成功");
	}

Parent class for storing data

public class Card {
    
    //把数据都存放到这个里面
	static int money;
	static boolean islock = true;
	static int idcard;
	static int phone ;
	static String account;
	static long cardid;
	static int passWord;
	
	public Card(int money, boolean islock, int idcard, int phone, String account, long cardid, int passWord) {
    
    
		super();
		this.money = money;
		this.islock = islock;
		this.idcard = idcard;
		this.phone = phone;
		this.account = account;
		this.cardid = cardid;
		this.passWord = passWord;
	}
	public Card() {
    
    
		super();
	}
	public int getMoney() {
    
    
		return money;
	}
	public void setMoney(int money) {
    
    
		this.money = money;
	}
	public boolean isIslock() {
    
    
		return islock;
	}
	public void setIslock(boolean islock) {
    
    
		this.islock = islock;
	}
	public int getIdcard() {
    
    
		return idcard;
	}
	public void setIdcard(int idcard) {
    
    
		this.idcard = idcard;
	}
	public int getPhone() {
    
    
		return phone;
	}
	public void setPhone(int phone) {
    
    
		this.phone = phone;
	}
	public String getAccount() {
    
    
		return account;
	}
	public void setAccount(String account) {
    
    
		this.account = account;
	}
	public long getCardid() {
    
    
		return cardid;
	}
	public void setCardid(long cardid) {
    
    
		this.cardid = cardid;
	}
	public int getPassWord() {
    
    
		return passWord;
	}
	public void setPassWord(int passWord) {
    
    
		this.passWord = passWord;
	}
}

Guess you like

Origin blog.csdn.net/weixin_46687295/article/details/106006609