1. 程式人生 > >模擬銀行存取款業務 簡要程式碼

模擬銀行存取款業務 簡要程式碼

需要驗證使用者輸入的銀行卡號和銀行卡密碼,是否存在於銀行系統中,如果是提示登入成功,否則提示登入失敗,讓使用者重新登入。登入成功後,需根據提示的四種操作型別,輸入相應的數字,對當前賬戶存款進行存取和查詢餘額操作。操作完成後,輸入數字0將退出整個系統。

在這個程式中,我們分成兩個類,一個是介面ATM類,一個是Account儲存客戶資訊

ATM類

public class ATM {
    private static ArrayList<Account>  accounts=new ArrayList<Account>();

    private static Scanner scanner;  
//    private static boolean isLogin1 = false;  
//    private static boolean isLogin2 = false;  
    private static boolean isLogin = false;  
    public static void showMenu() {  
        System.out.println("請選擇操作");  
        System.out.println("1.存錢");  
        System.out.println("2.取錢");  
        System.out.println("3.查詢餘額");  
        System.out.println("0.取卡");  
       
 
    }  
    public static void processMenu(int id, int command) {  //執行指定的命令
        switch (command) {  
        case 1:  
             System.out.println("請輸入存入的錢數");  
               double depositMoney = scanner.nextDouble();  
               accounts.get(id).deposit(depositMoney);  
               break;  
//             double withdrawMoney = scanner.nextDouble();  
//             accounts.get(id).withDraw(withdrawMoney);  
//             break;  
//            System.out.println("您賬戶裡的餘額式" + accounts.get(id).getBalance());  
//            break;  
        case 2:  
            System.out.println("請輸入取出的錢數");  
            double withdrawMoney = scanner.nextDouble();  
            accounts.get(id).withDraw(withdrawMoney);  
            break;  
        case 3:  
          System.out.println("您賬戶裡的餘額式" + accounts.get(id).getBalance());  
          break;  
        case 0:  
            isLogin = false;  
            System.out.println("取卡並退出");
            break;  
        default:  
            System.out.println("請輸入正確的指令:");  
            break;
        }
        }  
      public static void welcome(){//一定要定義為靜態方法
          System.out.println("************************************************");
           System.out.println("*************歡迎光臨*************************");
           System.out.println("***********請插入銀行卡進行操作************");
           System.out.println("*************************************************");
      }
        
    public static void main(String[] args) throws Exception {  
        // TODO Auto-generated method stub  
       
            Account acc1 = new Account(001, 100,100);  
            accounts.add(acc1);  
            Account acc2 = new Account(002, 200,150);  
            accounts.add(acc2);  
            Account acc3 = new Account(003, 300,200);  
            accounts.add(acc3);  
            Account acc4 = new Account(004, 400,250);  
            accounts.add(acc4);  
//    
          while(true) {
           ATM.welcome();
           scanner = new Scanner(System.in);  
            int id = scanner.nextInt();
//  
//            for (int i = 0; i < 3; i++) {
            for (Account acc : accounts) {  //檢查賬戶和密碼是否相符
                if ( id == acc.getId()) {  
//                    isLogin1 = true;   
                    System.out.println("請輸入密碼");
                    int password= scanner.nextInt();
                    if ( password == acc.getPassword()) {  //)&&(password==acc.password)
                        isLogin = true;   
                      break;
                }
                  
                }
//                else {
//                    System.out.println("請輸入正確的賬戶");
//                }
                
            }
//                    else {
//                        System.out.println("請輸入正確的密碼");
//                    }
//                        
//                    //此處不能直接用acc.id 因為id 是私有變數
//                }  
//                else {
//                
//                     int id2= scanner.nextInt();
//                     break;
//                }
            //檢測賬號和密碼是否正確
                
            
//            }//三次機會
            
            
            
            
            
            
//                else {
//                    System.out.println("請輸入正確的ID");
//                     int id2 = scanner.nextInt();
//                   continue;
//                }
//            }
//                
//          System.out.println("請輸入密碼");
//         int password = scanner.nextInt();
       
//       
//      else {
//            System.out.println("請輸入正確的ID");
//             int password2= scanner.nextInt();
//             break;
//        }
 
 
//          
//               
//       
//       
//     while (isLogin1==true||isLogin2==true)
//     {
//         isLogin=true;
//     }
         
            while (isLogin) {  
                showMenu();  
                scanner = new Scanner(System.in);  
                int command = scanner.nextInt();  
                processMenu(id, command);  
 
            }  
       


          }   
    }
    
    
}




再設定一個儲存客戶資訊的,在這裡我們用連結串列來引用 

     Account acc1 = new Account(001, 100,100);  
            accounts.add(acc1);  
            Account acc2 = new Account(002, 200,150);  
            accounts.add(acc2);  
            Account acc3 = new Account(003, 300,200);  
            accounts.add(acc3);  
            Account acc4 = new Account(004, 400,250);  
            accounts.add(acc4); 


Account.java 

import java.util.Date;

/**
 * ATM
 * @author mac
 *
 */
public class Account {
        int id;
        private int password;
        private double balance;
        
 
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        
        public double getBalance() {
            return balance;
        }
        public void setBalance(double balance) {
            this.balance = balance;
        }
    
    
        public Account() {
            // TODO Auto-generated constructor stub
        }
       
        public Account(int id, int password, double balance) {
            super();
            this.id = id;
            this.password = password;
            this.balance = balance;
        }
        
        public void withDraw(double money) {  
            balance -= money;  
        }  
      
        public void deposit(double money) {  
            balance += money;  
          
        }  
//        
        public int getPassword() {
            return password;
        }
        public void setPassword(int password) {
            this.password = password;
        }
       
        }







以上程式均為個人作業程式碼 ,如有錯誤,請加微信macforyou1