java寫一個簡單的銀行存取款系統
阿新 • • 發佈:2019-02-05
package com.coding.study09; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("------歡迎使用自助存取款系統------"); System.out.println("請輸入賬戶密碼(首次使用請按#註冊):"); String account = scanner.nextLine(); if (account.equals("#")) { System.out.println("請輸入新的賬戶:"); String account1 = scanner.nextLine(); System.out.println("請輸入密碼:"); String passwd1 = scanner.nextLine(); System.out.println("請輸入使用者姓名:"); String name1 = scanner.nextLine(); BankOperate.regist(account1, passwd1, name1); continue; } System.out.println("請輸入密碼:"); String passwd2 = scanner.nextLine(); // BankOperate.logine(account, passwd2); boolean result = BankOperate.logine(account, passwd2); if (result) { System.out.println("登入成功,歡迎" + BankTool.account[BankTool.index].getName() + "光臨"); operate(); } else { System.out.println("輸入錯誤,請重新輸入。"); } } } public static void operate() { Scanner scanner1 = new Scanner(System.in); while (true) { System.out.println("請選擇服務專案:"); System.out.println("1.存款"); System.out.println("2.取款"); System.out.println("3.查詢"); System.out.println("4.退出"); int n = scanner1.nextInt(); switch (n) { case 1: System.out.println("請輸入存款金額:"); double money = scanner1.nextDouble(); BankOperate.save(money); break; case 2: System.out.println("請輸入取款金額:"); double money1 = scanner1.nextDouble(); BankOperate.take(money1); break; case 3: BankOperate.check(); break; case 4: return; default: System.out.println("輸入錯誤,請重新輸入。"); break; } } } } package com.coding.study09; public class Account { String account; String passwd; String name; double money; public Account(String account, String passwd, String name) { this.account = account; this.passwd = passwd; this.name = name; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } } package com.coding.study09; public class BankOperate { public static void regist(String account, String passwd, String name) { Account account1 = new Account(account, passwd, name); for (int i = 0; i < BankTool.account.length; i++) { if (BankTool.account[i] == null) { BankTool.account[i] = account1; System.out.println("註冊成功"); break; } } } public static boolean logine(String account, String passwd) { for (int i = 0; i < BankTool.account.length; i++) { if (BankTool.account[i] != null) { if (account.equals(BankTool.account[i].getAccount()) && passwd.equals(BankTool.account[i].getPasswd())) { BankTool.index = i; return true; } } } return false; } public static void save(double money) { BankTool.account[BankTool.index].setMoney(BankTool.account[BankTool.index].getMoney() + money); System.out.println("存錢成功"); } public static void take(double money) { BankTool.account[BankTool.index].setMoney(BankTool.account[BankTool.index].getMoney() - money); System.out.println("取錢成功"); } public static void check() { System.out.println("當前賬戶餘額為:"+BankTool.account[BankTool.index].getMoney()); } } package com.coding.study09; public class BankTool { public static Account[] account=new Account[100]; public static int index=0; }
Demo用來進行測試,Account用來建立賬戶的set和get方法,BankOperate用來存放註冊/登入/存錢/取錢/查詢的操作,BankTool定義工具類。