溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java實現(xiàn)銀行ATM功能示例

發(fā)布時間:2020-10-28 14:42:46 來源:億速云 閱讀:278 作者:Leah 欄目:開發(fā)技術(shù)

Java實現(xiàn)銀行ATM功能示例?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

實現(xiàn)功能

1、用戶需要通過輸入銀行卡號和密碼才能進入ATM系統(tǒng)
2、用戶可以在ATM中實現(xiàn)取款、存款、轉(zhuǎn)賬、余額查詢、退出系統(tǒng)等功能

簡單分析

1、創(chuàng)建User類(cardNo,identity,phone,username,password,balance(余額))
2、創(chuàng)建Bank類,主要實現(xiàn)初始化用戶、用戶登錄、顯示菜單、取款、存款、轉(zhuǎn)賬、余額查詢、退出系統(tǒng)等功能。

代碼實現(xiàn)

User.java

public class User {
   private String username;
   private int password;
   private String cardNo;
   private String identity;
   private String phone;
   private double blance;

  public User() {
  }

  public User(String username, int password, String cardNo, String identity, String phone, double blance) {
    this.username = username;
    this.password = password;
    this.cardNo = cardNo;
    this.identity = identity;
    this.phone = phone;
    this.blance = blance;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public int getPassword() {
    return password;
  }

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

  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 getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public double getBlance() {
    return blance;
  }

  public void setBlance(double blance) {
    this.blance = blance;
  }
}

Bank.java

public class Bank {
  //創(chuàng)建用戶數(shù)組
  private User[] users=new User[100];
  //用戶個數(shù)
  private int size=0;
  //當(dāng)前登錄的用戶
  private User loginuser;

  public Bank() {
    initial();//初始化用戶
  }

  /**
  * 初始化用戶
  */
  public void initial(){
    User user1=new User("鐘愛",12346,"6226789234023434","130324192309123074","13133565435",2312313);
    User user2=new User("冷冬",12354,"6226789234023567","1305472309123074","13446745675",2333);
    User user3=new User("小龍",9893,"6226789234023564","13032414575467457","13145745435",255553);
    User user4=new User("趙麗穎",6342,"6226789234029324","130324192647456774","13145675435",288883);
    User user5=new User("徐三哥",8445,"6226789234025487","1303241923456744","1457785435",28989);
    users[0]=user1;
    users[1]=user2;
    users[2]=user3;
    users[3]=user4;
    users[4]=user5;
    size=5;
  }
  /**
  * 用戶登錄
  */
  public User login(String cardNo,int password){
    for (int i = 0; i <size ; i++) {
  if((users[i].getCardNo().equals(cardNo))&&(users[i].getPassword()==password)){
        loginuser=users[i];
        return users[i];
      }
    }
    return null;
  }
  /**
  * 顯示菜單
  */
  public void showMenus(){
    Scanner input =new Scanner(System.in);
    do { System.out.println("**********************************歡迎進入ATM系統(tǒng)*****************************************");
    System.out.println("*********************1 存款 2 取錢 3 轉(zhuǎn)賬 4 查詢余額 5 修改密碼 0 退出***********************");


      int choice=input.nextInt();
      switch (choice){
        case 1:
          //取款
          save();
          break;
        case 2:
          //存錢
          withdraw();
          break;
        case 3:
          //轉(zhuǎn)賬
          trans();
          break;
        case 4:
          //查詢余額
          query();
          break;
        case 5:
          //修改密碼
          revise();
          break;
        case 0:
          //退出ATM系統(tǒng)
          System.out.println("歡迎下次使用本系統(tǒng)");
         return;
      }
    } while (true);
  }
 /**
   * 存款操作
   */
  public void save(){
    System.out.println("請輸入您的存款金額:");
    Scanner input=new Scanner(System.in);
    int money=input.nextInt();
    if(loginuser!=null){
      if(money>0){
        loginuser.setBlance(loginuser.getBlance()+money);
        System.out.println("存款成功");
        System.out.println("您的當(dāng)前余額為"+loginuser.getBlance());
      }else{
        System.out.println("輸入錯誤!");
      }
    }else{
      System.out.println("您還未進行賬戶登錄");
    }
  }
  /**
   * 取款操作
   */
  public void withdraw(){
    Scanner input=new Scanner(System.in);
    System.out.println("請輸入你要取出的金額為:");
    int money=input.nextInt();
    if(loginuser!=null){
      if(money>0&&money<=loginuser.getBlance()){
        loginuser.setBlance(loginuser.getBlance()-money);
        System.out.println("取款成功");
        System.out.println("您的當(dāng)前余額為:"+loginuser.getBlance());
      }else{
        System.out.println("輸入錯誤");
      }
    }else{
      System.out.println("您還未進行賬戶登錄");
    }
  }
  /**
   * 轉(zhuǎn)賬操作
   */
  public void trans(){
    Scanner input=new Scanner(System.in);
    System.out.println("請輸入要轉(zhuǎn)入賬戶的卡號:");
    String cardNo=input.next();
    User nuser=null;//對方賬戶
    boolean IsExit=false;//判斷對方賬戶是否存在
    for (int i = 0; i <size; i++) {
      if(users[i].getCardNo().equals(cardNo)){
        IsExit=true;//檢索到對方賬戶
        nuser=users[i];
      }
    }
    if(loginuser==nuser){
      System.out.println("不可以自己給自己賬戶進行轉(zhuǎn)賬");
      return;
    }
    if(loginuser!=null){
      System.out.println("請輸入要轉(zhuǎn)賬的金額:");
      int money=input.nextInt();
      if(money>0&&money<=loginuser.getBlance()){
        loginuser.setBlance(loginuser.getBlance()-money);
        nuser.setBlance(nuser.getBlance()+money);
        System.out.println("轉(zhuǎn)賬成功");
        System.out.println("您當(dāng)前余額為"+loginuser.getBlance());
        System.out.println(nuser.getBlance());
      }else{
        System.out.println("輸入錯誤");
      }
    }else{
      System.out.println("請登錄賬戶");
    }
  }
  /**
   * 查詢余額
   */
  public void query(){

    System.out.println("您的余額為:"+loginuser.getBlance());
  }
  /**
   * 修改密碼
   */
  public void revise(){
    Scanner input=new Scanner(System.in);
    System.out.println("請輸入您的新密碼");
    int newpassword=input.nextInt();
    for (int i = 0; i < size; i++) {
      if(users[i]==loginuser){
        users[i].setPassword(newpassword); //進行修改
      }
    }
    System.out.println("密碼修改成功");
  }
}

BankSystem.java

public class BankSystem {
  public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    Bank bank=new Bank();
      System.out.println("請輸入您的卡號:");
      String cardNo=input.next();
      System.out.println("請輸入您的密碼:");
      int password=input.nextInt();
      User user = bank.login(cardNo, password);//用戶登錄
      if(user!=null){
        System.out.println(user.getUsername()+"登陸成功");
        bank.showMenus();
      }else{
        System.out.println("登陸失敗");
      }
  }
}

看完上述內(nèi)容,你們掌握Java實現(xiàn)銀行ATM功能示例的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI