简单的ATM系统

来源:互联网 发布:湖南广电网络官网 编辑:程序博客网 时间:2024/04/30 23:17

创建的第一个类

 

public class UserBean {
 
 private String cardNum;//卡号
 
 private String password;//密码
 
 private double account;//余额
 
 public UserBean(){
  
 }
 
 public UserBean(String cardNum, String password, double account) {
  this.cardNum = cardNum;
  this.password = password;
  this.account = account;
 }

 public String getCardNum() {
  return cardNum;
 }

 public void setCardNum(String cardNum) {
  this.cardNum = cardNum;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public double getAccount() {
  return account;
 }

 public void setAccount(double account) {
  this.account = account;
 }
}

 

创建的第二个类

 

import java.util.Scanner;

public class Atm {

 private UserBean user;// 用户

 private int cash;// ATM中的现金

 private String bankName;// 所属银行

 public final int MAX_CASH = 100000;

 public Atm() {
  // 初始化数据
  this.cash = 50000;
  this.bankName = "ICBC爱存不存银行";
  this.user = new UserBean("001003002132", "132231", 520);
 }

 // 运行方法---流程控制
 public void run() {
  this.wlecome();
  boolean flag = this.login();
  if (flag) {
   while (true) {
    int choice = this.chooseMenu();
    switch (choice) {
    case 1:
     this.query();
     break;
    case 2:
     this.getMoney();
     break;
    case 3:
     this.storeMoney();
     break;
    case 4:
     this.changePwd();
     break;
    case 5:
     this.exit();
     break;
    default:
     System.out.println("没有该选项,请重新选择");
     break;
    }
   }
  } else {
   this.exit();
  }

 }

 // 欢迎
 private void wlecome() {
  System.out.println("**********************");
  System.out.println("**欢迎来到***************");
  System.out.println("********" + this.bankName + "*");
  System.out.println("**********************");
  System.out.println("*************verson 1.0");
 }

 // 登录
 private boolean login() {
  Scanner scan = new Scanner(System.in);
  for (int i = 0; i < 3; i++) {
   System.out.println("请输入卡号:");
   String inputCardNum = scan.next();
   System.out.println("请输入密码:");
   String inputPwd = scan.next();

   if (inputCardNum.equals(this.user.getCardNum()) && inputPwd.equals(this.user.getPassword())) {
    System.out.println("卡号:" + this.user.getCardNum() + "用户,登录成功!");
    return true;
   } else {
    System.out.println("卡号或密码有误,请查证。");
   }

  }
  System.out.println("三次机会使用完毕,您的卡被没收,请到柜台处理!");
  return false;

 }

 // 菜单选择
 private int chooseMenu() {
  Scanner scan = new Scanner(System.in);
  System.out.println("请选择你要执行的操作:");
  System.out.println("1、查询;2、取款;3、存款;4、修改密码;5、退出;");
  int choice = scan.nextInt();
  return choice;
 }

 // 查询余额
 private void query() {
  System.out.println("您当前余额为:" + this.user.getAccount());
 }

 // 取钱
 private void getMoney() {
  Scanner input = new Scanner(System.in);
  System.out.println("请输入您想提取的金额");
  int money = input.nextInt();
  if (money % 100 == 0) {
   if (money <= this.MAX_CASH) {
    if (money <= this.cash) {
     if (money < this.user.getAccount()) {
      this.user.setAccount(this.user.getAccount()-money);
      this.cash-=money;
      System.out.println("操作成功,请拿取现金");
     } else {
      System.out.println("您的余额不足");
     }
    } else {
     System.out.println("ATM机中现金不足");
    }
   } else {
    System.out.println("超过额度");
   }
  } else {
   System.out.println("操作失败,请输入100的倍数");
  }
 }

 // 存钱
 private void storeMoney() {
  Scanner input = new Scanner(System.in);
  System.out.println("请放入您想存的金额");
  int money = input.nextInt();
  if (money % 100 == 0) {
   if (money <= (this.MAX_CASH - this.cash)) {
    System.out.println("请确认存入金额");
    int money1 = input.nextInt();
    if (money == money1) {
     this.cash=this.cash+money;
     System.out.println("已存入" + money + "当前余额为" + (this.user.getAccount() + money));
    }
   } else {
    System.out.println("存放不下,请取回存入金额转到柜台办理");
   }
  } else {
   System.out.println("操作失败,请存入100的倍数");
  }
 }

 // 修改密码
 private void changePwd() {
  Scanner input = new Scanner(System.in);
  for(int i = 0; i < 3; i++){
  System.out.println("请输入原先的密码");
  String pwd = input.nextLine();
  if (this.user.getPassword().equals(pwd)) {
   
   for (int j = 0; j < 3; j++) {
    System.out.println("请输入新的6位数密码");
    String pwd1 = input.nextLine();
    System.out.println("请再输入一次新的密码");
    String pwd2 = input.nextLine();
    if (pwd1.equals(pwd2)) {
     this.user.setPassword(pwd2);
     System.out.println("操作成功");
     System.out.println("新的密码为" + this.user.getPassword());
     break;
    } else {
     System.out.println("操作失败,两次输入的密码不匹配");
    }
   }
   break;
  }else{
   System.out.println("原密码错误,请查证");
  }
  }
 }

 // 退出
 private void exit() {
  System.out.println("谢谢您的光顾,请下次再来!");
  System.exit(1);// 关闭虚拟机
 }

}

在建立一个main的就可以了

0 0
原创粉丝点击