java模拟银行ATM机操作(基础版),供大家参考,具体内容如下
实现的功能需求:
修改密码之后,就会自动退出登录,再重新登录,若登录成功才能验证修改密码成功,这里用到 【跳出指定循环】, 其他功能都是基本操作,作为入门入手程序。
准备两个实体类(一个银行类,一个用户类),一个测试类,注意,这里暂且存储了两个用户,这里可以优化,暂且不优化了
Blank.java
package com.demo2;
public class Blank {
private User[] users = new User[20];
int size;
User user;
public User getUser() {
return user;
}
public Blank (){ }
public void init(){
User user1= new User("890012","43042419990201","刘军","7519","15116497809","18000");
User user2= new User("890013","43042419990821","一凡","1314","118842320","13000");
users[0] = user1;
users[1] =user2;
size=2;
}
public boolean login(String cardNo, String passwd){
for(int i=0; i<size; i++){
if(users[i].getCardNo().equals(cardNo) && users[i].getPasswd().equals(passwd) ){
user = users[i];
return true;
}
}
return false;
}
public void save(String balance){
int money =Integer.parseInt(user.getBalance());
money += Integer.parseInt(balance);
user.setBalance((money+""));
System.out.println("存款成功");
}
public void widthDraw(String balance){
int money =Integer.parseInt(user.getBalance());
money -= Integer.parseInt(balance);
user.setBalance((money+""));
}
public void trans(String cardNo,String balance){
int i=0;
for(i=0; i<size;i++){
if(users[i].getCardNo().equals(cardNo)){
int oldmoney =Integer.parseInt(user.getBalance()) ;
int oldanothermoney = Integer.parseInt(users[i].getBalance());
widthDraw(balance);
int money =Integer.parseInt(users[i].getBalance());
money += Integer.parseInt(balance);
users[i].setBalance((money+""));
int userseflmoney =oldmoney - Integer.parseInt(user.getBalance());
int useraothermoney = Integer.parseInt(users[i].getBalance())-oldanothermoney;
if(userseflmoney == useraothermoney){
System.out.println("转账成功");
}
break;
}
}
if(i>=size){
System.out.println("输入的卡号有误");
}
}
public void query(){
System.out.println("【账户余额】: "+user.getBalance());
}
public void modifyPassword(String passwd){
user.setPasswd(passwd);
System.out.println("修改密码成功,请重新登录");
}
}
User.java
package com.demo2;
public class User {
private String cardNo ;
private String identity;
private String username;
private String passwd;
private String phone;
private String balance;
public User(){}
public User(String cardNo, String identity, String username, String passwd, String phone, String balance) {
this.cardNo = cardNo;
this.identity = identity;
this.username = username;
this.passwd = passwd;
this.phone = phone;
this.balance = balance;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
}
BlankTest.java
package com.demo2;
import java.util.Scanner;
public class BankTest {
public static void main(String[] args) {
Blank blank = new Blank();
blank.init();
Scanner sc =new Scanner(System.in);
while(true) {
System.out.println("========== 中国银行欢迎你 ========= ");
System.out.println(" 【请先登录】 ");
System.out.print("【请输入卡号】:");
String carNo = sc.next();
System.out.print("【请输入密码】:");
String passwd = sc.next();
if (blank.login(carNo, passwd)) {
System.out.println("登录成功: 【账号:欢迎】:"+ blank.getUser().getUsername());
// User user = blank.getUser();
prof: while(true) {
System.out.println("========== 1,【存款】 2,【取款】 3,【转账】 ========= ");
System.out.println("========== 4,【查询余额】 5,【修改密码】 6,【退出】 ========= ");
System.out.print("【请选择业务功能】:");
int num = sc.nextInt();
if (num == 1) {
System.out.println("【进入存款操作】");
System.out.print("【输入存款金额】:");
int money = sc.nextInt();
while (true) {
if (money <= 0) {
System.out.println("【请输入正确的存款金额】");
} else {
break;
}
}
blank.save(money + "");
} else if (num == 2) {
System.out.println("【进入取款操作】");
System.out.print("【输入取款金额】:");
int money = sc.nextInt();
blank.widthDraw(money+"");
System.out.println("取款成功");
} else if (num == 3) {
System.out.println("【进入转账操作】");
System.out.print("【输入转账卡号】:");
String cardNo =sc.next();
System.out.print("【输入转款金额】");
int money = sc.nextInt();
blank.trans(cardNo, money+"");
} else if (num == 4) {
blank.query();
} else if (num == 5) {
System.out.println("【进入改密操作】");
System.out.print("【输入重置密码】:");
String newpasswd =sc.next();
blank.modifyPassword(newpasswd);
break prof;
} else if (num == 6) {
System.out.println("【账号】: 退出成功");
break prof;
}
}
} else {
System.out.println("卡号或者密码输入不正确");
}
}
}
}
程序运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。