Java實現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏
系統(tǒng)準備內(nèi)容分析
每個用戶的賬戶信息都是一個對象,需要提供賬戶類
需要準備一個容器,用戶存儲系統(tǒng)全部賬戶對象信息
首頁值需要包含:登入和注冊2個功能
1.??系統(tǒng)準備,首頁,用戶開戶功能
系統(tǒng)準備,首頁設計
系統(tǒng)準備內(nèi)容分析:
- 每個用戶的賬戶信息都是一個對象,需要提供賬戶類
- 需要準備一個容器,用于存儲和系統(tǒng)全部賬戶對象信息
- 首頁只需要包含:登入和注冊2個功能
實現(xiàn)步驟:
- 定義賬戶類,用于后期創(chuàng)建賬戶對象封裝用戶的賬戶信息
- 賬戶類中的信息至少需要包含(卡號,姓名,密碼,余額,取現(xiàn)額度)
package com.wangxinhua;
import java.util.ArrayList;
import java.util.Scanner;
public class ATMSystem
{
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
{
System.out.println("========歡迎進入首頁=====");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登入
break;
case 2:
//開戶
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
}

總結(jié)
- 用戶的賬戶信息,系統(tǒng)如何表示的?
定義賬戶類Account,定義系統(tǒng)關(guān)心的屬性信息
- 系統(tǒng)采用什么來儲存全部用戶的賬戶對象信息?
ArrayList<Account> accounts = new ArrayList<> ();
用戶開戶功能實現(xiàn)
- 分析
開戶功能其實就是往系統(tǒng)的集合容器中存入一個新的賬戶對象的信息
- 開戶功能實現(xiàn)步驟
定義方法完成開戶:
public static void register(ArrayList<Account> accounts){...}
- 鍵盤錄入姓名,秘密,確認密碼(需保證兩次密碼一致)
- 生成賬戶卡號,卡號必須由系統(tǒng)自動生成8位數(shù)字(必須保證卡號的唯一)
- 創(chuàng)建Account賬戶類對象用戶封裝賬戶信息(姓名,密碼,卡號)
- 把Account賬戶類對象存入到集合accounts中去。
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem
{
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登入
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;//查無此賬戶,說明卡號沒有重復了!
}
}

總結(jié)
開戶功能的實現(xiàn)需要哪幾步操作,需要注意什么問題?
- 開戶功能應該獨立定義成方法,并傳入當前集合對象給該方法。
- 錄入開戶信息(姓名,密碼)
- 卡號要自動生成且唯一
- 把開戶的信息封裝成Account對象,存入到集合中去。
2.??用戶登入,操作頁展示,查詢賬戶,退出賬戶
用戶登入功能實現(xiàn)
分析
- 定義方法:
public static void login(ArrayList<Account>accounts){...}
- 讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象。
- 如果沒有找到了賬戶對象,說明卡號不存在,繼續(xù)輸入卡號
- 如果找到了賬戶對象,說明卡號存在,繼續(xù)輸入密碼
- 如果密碼不正確,提示繼續(xù)輸入密碼
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem
{
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登錄
login(accounts,sc);
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 完成用戶登錄
* @param accounts
*/
private static void login(ArrayList<Account> accounts,Scanner sc)
{
//必須系統(tǒng)中存在賬戶才可以登錄
if (accounts.size() == 0)
{
//沒有任何賬戶
System.out.println("當前系統(tǒng)中無任何賬戶,您需要先注冊!");
return;//直接結(jié)束方法的執(zhí)行!
}
//2.讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象
while (true) {
System.out.println("請您輸入登錄的卡號:");
String cardId = sc.next();
//根據(jù)卡號查詢賬戶對象
Account acc = getAccountBycardId(cardId,accounts);
// 3.判斷賬戶對象是否存在,存在說明卡號沒問題
if (acc != null)
{
while (true)
{
// 4.讓用戶繼續(xù)輸入密碼
System.out.println("請您輸入登錄的密碼:");
String password = sc.next();
// 5.判斷密碼是否正確
if (acc.getPassWord().equals(password))
{
//密碼正確,登入成功
//展示系統(tǒng)登錄后的操作界面
System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號是:" + acc.getCardId());
//
}
else
{
System.out.println("您的密碼有誤,請確認!");
}
}
}
else
{
System.out.println("對不起,不存在該卡號的賬戶!");
}
}
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;//查無此賬戶,說明卡號沒有重復了!
}
}

總結(jié)
登錄功能如何實現(xiàn)的?
- 根據(jù)卡號去集合中查詢對應的賬戶對象
- 如果找到了賬戶對象,說明卡號存在,繼續(xù)輸入密碼
- 如果密碼正確,則登錄成功
用戶操作頁設計,查詢賬戶,退出賬戶功能
用戶操作頁,查詢賬戶,退出賬戶功能分析
- 用戶登錄成功后,需要進入用戶操作頁、
- 查詢就是直接展示當前登錄成功的賬戶對象的信息
- 退出賬戶是需要回到首頁的
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
//showMain 開戶首頁的意思
// ArrayList<Account> accounts 使用方法定義功能傳入容器中 accounts是傳參
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登錄
login(accounts,sc);
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 完成用戶登錄
* @param accounts
*/
private static void login(ArrayList<Account> accounts,Scanner sc)
{
//必須系統(tǒng)中存在賬戶才可以登錄
if (accounts.size() == 0)
{
//沒有任何賬戶
System.out.println("當前系統(tǒng)中無任何賬戶,您需要先注冊!");
return;//直接結(jié)束方法的執(zhí)行!
}
//2.讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象
while (true) {
System.out.println("請您輸入登錄的卡號:");
String cardId = sc.next();
//根據(jù)卡號查詢賬戶對象
Account acc = getAccountBycardId(cardId,accounts);
// 3.判斷賬戶對象是否存在,存在說明卡號沒問題
if (acc != null)
{
while (true)
{
// 4.讓用戶繼續(xù)輸入密碼
System.out.println("請您輸入登錄的密碼:");
String password = sc.next();
// 5.判斷密碼是否正確
if (acc.getPassWord().equals(password))
{
//密碼正確,登入成功
//展示系統(tǒng)登錄后的操作界面
System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號是:" + acc.getCardId());
//展示操作頁面
showUserCommand(sc,acc);
return;//繼續(xù)結(jié)束登錄方法
}
else
{
System.out.println("您的密碼有誤,請確認!");
}
}
}
else
{
System.out.println("對不起,不存在該卡號的賬戶!");
}
}
}
private static void showUserCommand(Scanner sc,Account acc) {
System.out.println("=========用戶操作頁面========");
System.out.println("1.查詢賬戶");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.轉(zhuǎn)賬");
System.out.println("5.修改密碼");
System.out.println("6.退出");
System.out.println("7.注銷賬戶");
while (true)
{
System.out.println("請您輸入操作命令:");
int command = sc.nextInt();
switch (command)
{
case 1:
//查詢賬戶
showAccount(acc);
break;
case 2:
//存款
break;
case 3:
//取款
break;
case 4:
//轉(zhuǎn)賬
break;
case 5:
//修改密碼
break;
case 6:
//退出
System.out.println("歡迎下次光臨??!");
return; //結(jié)束當前showUserCommand(Scanner sc,Account acc)的方法
case 7:
//注銷賬戶
break;
default:
System.out.println("您輸入有誤!");
}
}
}
private static void showAccount(Account acc)
{
System.out.println("===========當前賬戶詳情=========");
System.out.println("卡號" + acc.getCardId());
System.out.println("姓名" + acc.getUserWord());
System.out.println("余額" + acc.getMoney());
System.out.println("當次限額:" + acc.getQuotaMoney());
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;//查無此賬戶,說明卡號沒有重復了!
}
}

總結(jié)
用戶操作頁設計,查詢賬戶,退出賬戶功能注意事項
- 我們應該注意接入登錄界面后應該注意用戶操作頁面有哪些
- 設置好操作界面后需要接入退出接口
3.??用戶存款與取款
用戶存款
存款分析
- 存款就是拿到當前賬戶對象
- 然后讓用戶輸入存款金額
- 調(diào)用賬戶對象的setMoney方法將戰(zhàn)虎余額修改成存錢后的余額
- 存錢后需要查新一下賬戶信息,確認是否存錢成功了!
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
//showMain 開戶首頁的意思
// ArrayList<Account> accounts 使用方法定義功能傳入容器中 accounts是傳參
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登錄
login(accounts,sc);
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 完成用戶登錄
* @param accounts
*/
private static void login(ArrayList<Account> accounts,Scanner sc)
{
//必須系統(tǒng)中存在賬戶才可以登錄
if (accounts.size() == 0)
{
//沒有任何賬戶
System.out.println("當前系統(tǒng)中無任何賬戶,您需要先注冊!");
return;//直接結(jié)束方法的執(zhí)行!
}
//2.讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象
while (true) {
System.out.println("請您輸入登錄的卡號:");
String cardId = sc.next();
//根據(jù)卡號查詢賬戶對象
Account acc = getAccountBycardId(cardId,accounts);
// 3.判斷賬戶對象是否存在,存在說明卡號沒問題
if (acc != null)
{
while (true)
{
// 4.讓用戶繼續(xù)輸入密碼
System.out.println("請您輸入登錄的密碼:");
String password = sc.next();
// 5.判斷密碼是否正確
if (acc.getPassWord().equals(password))
{
//密碼正確,登入成功
//展示系統(tǒng)登錄后的操作界面
System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號是:" + acc.getCardId());
//展示操作頁面
showUserCommand(sc,acc);
return;//繼續(xù)結(jié)束登錄方法
}
else
{
System.out.println("您的密碼有誤,請確認!");
}
}
}
else
{
System.out.println("對不起,不存在該卡號的賬戶!");
}
}
}
private static void showUserCommand(Scanner sc,Account acc) {
while (true)
{
System.out.println("=========用戶操作頁面========");
System.out.println("1.查詢賬戶");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.轉(zhuǎn)賬");
System.out.println("5.修改密碼");
System.out.println("6.退出");
System.out.println("7.注銷賬戶");
System.out.println("請您輸入操作命令:");
int command = sc.nextInt();
switch (command)
{
case 1:
//查詢賬戶
showAccount(acc);
break;
case 2:
//存款
depositMoney(acc,sc);
break;
case 3:
//取款
break;
case 4:
//轉(zhuǎn)賬
break;
case 5:
//修改密碼
break;
case 6:
//退出
System.out.println("歡迎下次光臨!!");
return; //結(jié)束當前showUserCommand(Scanner sc,Account acc)的方法
case 7:
//注銷賬戶
break;
default:
System.out.println("您輸入有誤!");
}
}
}
/**
* 專門存錢的
* @param acc
*/
private static void depositMoney(Account acc,Scanner sc) {
System.out.println("===========存錢操作=========");
System.out.println("請您輸入存款的金額:");
double money = sc.nextDouble();
//直接把金額修改到賬戶對象的money屬性中去
acc.setMoney(acc.getMoney() + money);
System.out.println("存款完成!");
showAccount(acc);
}
private static void showAccount(Account acc)
{
System.out.println("===========當前賬戶詳情=========");
System.out.println("卡號" + acc.getCardId());
System.out.println("姓名" + acc.getUserWord());
System.out.println("余額" + acc.getMoney());
System.out.println("當次限額:" + acc.getQuotaMoney());
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;
//查無此賬戶,說明卡號沒有重復了!
}
}

總結(jié)
存款分析
- 存款就是拿到當前賬戶對象
- 然后讓用戶輸入存款的金額
- 調(diào)用賬戶對象的setMoney方法將賬戶余額修改成存錢后的余額
- 存錢后需要查詢一下賬戶信息,確認是否存錢成功了!
取款分析
- 取款需要先判斷賬戶是否有錢
- 有錢則拿到自己賬戶對象
- 然后讓用戶輸入取款金額
- 判斷取款金額是否超過了當次限額,以及金額是否足夠
- 滿足要求則調(diào)用賬戶對象的setMoney方法完成金額的修改
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
//showMain 開戶首頁的意思
// ArrayList<Account> accounts 使用方法定義功能傳入容器中 accounts是傳參
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登錄
login(accounts,sc);
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 完成用戶登錄
* @param accounts
*/
private static void login(ArrayList<Account> accounts,Scanner sc)
{
//必須系統(tǒng)中存在賬戶才可以登錄
if (accounts.size() == 0)
{
//沒有任何賬戶
System.out.println("當前系統(tǒng)中無任何賬戶,您需要先注冊!");
return;//直接結(jié)束方法的執(zhí)行!
}
//2.讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象
while (true) {
System.out.println("請您輸入登錄的卡號:");
String cardId = sc.next();
//根據(jù)卡號查詢賬戶對象
Account acc = getAccountBycardId(cardId,accounts);
// 3.判斷賬戶對象是否存在,存在說明卡號沒問題
if (acc != null)
{
while (true)
{
// 4.讓用戶繼續(xù)輸入密碼
System.out.println("請您輸入登錄的密碼:");
String password = sc.next();
// 5.判斷密碼是否正確
if (acc.getPassWord().equals(password))
{
//密碼正確,登入成功
//展示系統(tǒng)登錄后的操作界面
System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號是:" + acc.getCardId());
//展示操作頁面
showUserCommand(sc,acc);
return;//繼續(xù)結(jié)束登錄方法
}
else
{
System.out.println("您的密碼有誤,請確認!");
}
}
}
else
{
System.out.println("對不起,不存在該卡號的賬戶!");
}
}
}
private static void showUserCommand(Scanner sc,Account acc) {
while (true)
{
System.out.println("=========用戶操作頁面========");
System.out.println("1.查詢賬戶");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.轉(zhuǎn)賬");
System.out.println("5.修改密碼");
System.out.println("6.退出");
System.out.println("7.注銷賬戶");
System.out.println("請您輸入操作命令:");
int command = sc.nextInt();
switch (command)
{
case 1:
//查詢賬戶
showAccount(acc);
break;
case 2:
//存款
depositMoney(acc,sc);
break;
case 3:
//取款
drawMoney(acc,sc);
break;
case 4:
//轉(zhuǎn)賬
break;
case 5:
//修改密碼
break;
case 6:
//退出
System.out.println("歡迎下次光臨?。?);
return; //結(jié)束當前showUserCommand(Scanner sc,Account acc)的方法
case 7:
//注銷賬戶
break;
default:
System.out.println("您輸入有誤!");
}
}
}
/**
* 取款操作
* @param acc
* @param sc
*/
private static void drawMoney(Account acc, Scanner sc) {
System.out.println("==========取款操作=========");
//1.判斷它的賬戶是否足夠100元
if (acc.getMoney() >= 100)
{
while (true) {
System.out.println("請您輸入取款的金額:");
double money = sc.nextDouble();
//2.判斷整個金額有沒有超過當次限額
if (money > acc.getQuotaMoney())
{
System.out.println("您當次取款金額超過每次限額,不要取那么多,每次最多可以?。? + acc.getQuotaMoney());
}
else
{
//3.判斷當前余額是否足夠你取錢
if (acc.getMoney() >= money)
{
//夠了,可以取錢了
acc.setMoney(acc.getMoney() - money);
System.out.println("恭喜您,取錢" + money + "成功了!當前賬戶還剩余:" + acc.getMoney());
return;//取錢后干掉了取錢方法
}
else
{
System.out.println("余額不足?。?!");
}
}
}
}
else
{
System.out.println("您自己的金額沒有超過100元,該努力工作了~~~");
}
}
/**
* 專門存錢的
* @param acc
*/
private static void depositMoney(Account acc,Scanner sc) {
System.out.println("===========存錢操作=========");
System.out.println("請您輸入存款的金額:");
double money = sc.nextDouble();
//直接把金額修改到賬戶對象的money屬性中去
acc.setMoney(acc.getMoney() + money);
System.out.println("存款完成!");
showAccount(acc);
}
private static void showAccount(Account acc)
{
System.out.println("===========當前賬戶詳情=========");
System.out.println("卡號" + acc.getCardId());
System.out.println("姓名" + acc.getUserWord());
System.out.println("余額" + acc.getMoney());
System.out.println("當次限額:" + acc.getQuotaMoney());
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;
//查無此賬戶,說明卡號沒有重復了!
}
}

總結(jié)溫習
- 取款需要先判斷賬戶是否有錢
- 有錢則拿到自己賬戶對象
- 然后讓用戶輸入取款金額
- 判斷取款金額是否超過了當次限額,以及金額是否足夠
- 滿足要求則調(diào)用賬戶對象的setMoney方法完成金額的修改
4.??用戶轉(zhuǎn)賬,修改密碼,銷戶
用戶轉(zhuǎn)賬功能
分析
- 轉(zhuǎn)賬功能需要判斷系統(tǒng)中是否有2個賬戶對象及以上
- 同時還要判斷總結(jié)賬戶是否有錢
- 接下來需要輸入對方卡號,判斷對方賬戶是否存在
- 對方賬戶存在還需要認證對方戶主的姓氏
- 滿足要求則可以把自己賬戶對象的金額修改到對方賬戶對象中去
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
//showMain 開戶首頁的意思
// ArrayList<Account> accounts 使用方法定義功能傳入容器中 accounts是傳參
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登錄
login(accounts,sc);
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 完成用戶登錄
* @param accounts
*/
private static void login(ArrayList<Account> accounts,Scanner sc)
{
//必須系統(tǒng)中存在賬戶才可以登錄
if (accounts.size() == 0)
{
//沒有任何賬戶
System.out.println("當前系統(tǒng)中無任何賬戶,您需要先注冊!");
return;//直接結(jié)束方法的執(zhí)行!
}
//2.讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象
while (true) {
System.out.println("請您輸入登錄的卡號:");
String cardId = sc.next();
//根據(jù)卡號查詢賬戶對象
Account acc = getAccountBycardId(cardId,accounts);
// 3.判斷賬戶對象是否存在,存在說明卡號沒問題
if (acc != null)
{
while (true)
{
// 4.讓用戶繼續(xù)輸入密碼
System.out.println("請您輸入登錄的密碼:");
String password = sc.next();
// 5.判斷密碼是否正確
if (acc.getPassWord().equals(password))
{
//密碼正確,登入成功
//展示系統(tǒng)登錄后的操作界面
System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號是:" + acc.getCardId());
//展示操作頁面
showUserCommand(sc,acc,accounts);
return;//繼續(xù)結(jié)束登錄方法
}
else
{
System.out.println("您的密碼有誤,請確認!");
}
}
}
else
{
System.out.println("對不起,不存在該卡號的賬戶!");
}
}
}
private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts)
{
while (true)
{
System.out.println("=========用戶操作頁面========");
System.out.println("1.查詢賬戶");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.轉(zhuǎn)賬");
System.out.println("5.修改密碼");
System.out.println("6.退出");
System.out.println("7.注銷賬戶");
System.out.println("請您輸入操作命令:");
int command = sc.nextInt();
switch (command)
{
case 1:
//查詢賬戶
showAccount(acc);
break;
case 2:
//存款
depositMoney(acc,sc);
break;
case 3:
//取款
drawMoney(acc,sc);
break;
case 4:
//轉(zhuǎn)賬
transferMoney(accounts,acc ,sc);
break;
case 5:
//修改密碼
break;
case 6:
//退出
System.out.println("歡迎下次光臨!!");
return; //結(jié)束當前showUserCommand(Scanner sc,Account acc)的方法
case 7:
//注銷賬戶
break;
default:
System.out.println("您輸入有誤!");
}
}
}
/**
* 轉(zhuǎn)賬功能
* @param accounts
* @param acc
* @param sc
*/
private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
{
//1.判斷系統(tǒng)中是否有2個賬戶及以上
if (accounts.size() < 2)
{
System.out.println("對不起,系統(tǒng)中無其他賬戶,您不可以轉(zhuǎn)賬?。?);
return;
}
//2.判斷自己的賬戶對象中是否有錢
if (acc.getMoney() == 0)
{
System.out.println("對不起,您自己都快吃土了,就別裝逼了??!");
return;
}
//3.開始轉(zhuǎn)賬邏輯
while (true)
{
System.out.println("請您輸入對方賬戶的卡號:");
String cardId = sc.next();
Account account = getAccountBycardId(cardId,accounts);
//判斷整個賬戶對象是否存在,存在說明對方卡號輸入正確
if (account != null)
{
//判斷這個賬戶對象是否是當前自己登錄的賬戶
if (account.getCardId().equals(acc.getCardId()))
{
//也就是這里企圖想給自己轉(zhuǎn)賬
System.out.println("您不能給自己轉(zhuǎn)賬!");
}
else
{
//確認對方的姓氏
String name = "*" + account.getUserWord().substring(1);
System.out.println("請您確認【" + name + "】的姓氏:");
String preName = sc.next();
//判斷
if (account.getUserWord().startsWith(preName))
{
//真正的轉(zhuǎn)賬才剛剛開始
System.out.println("請您輸入轉(zhuǎn)賬的金額:");
double money = sc.nextDouble();
//判斷這個金額是否超過了自己的金額
if (money > acc.getMoney())
{
System.out.println("對不起,您要轉(zhuǎn)賬的金額太多,您最多可以轉(zhuǎn)賬:" + acc.getMoney());
}
else
{
//開始了
acc.setMoney(acc.getMoney() - money);
account.setMoney(account.getMoney() + money);
System.out.println("恭喜您,轉(zhuǎn)賬成功了,已經(jīng)為" + account.getUserWord() + "轉(zhuǎn)賬了:" + money);
showAccount(acc);
return;
}
}
else
{
System.out.println("對不起,您認證的信息有誤~~~");
}
}
}
else
{
System.out.println("對不起,您輸入的轉(zhuǎn)賬卡號有問題!!");
}
}
}
/**
* 取款操作
* @param acc
* @param sc
*/
private static void drawMoney(Account acc, Scanner sc)
{
System.out.println("==========取款操作=========");
//1.判斷它的賬戶是否足夠100元
if (acc.getMoney() >= 100)
{
while (true) {
System.out.println("請您輸入取款的金額:");
double money = sc.nextDouble();
//2.判斷整個金額有沒有超過當次限額
if (money > acc.getQuotaMoney())
{
System.out.println("您當次取款金額超過每次限額,不要取那么多,每次最多可以取:" + acc.getQuotaMoney());
}
else
{
//3.判斷當前余額是否足夠你取錢
if (acc.getMoney() >= money)
{
//夠了,可以取錢了
acc.setMoney(acc.getMoney() - money);
System.out.println("恭喜您,取錢" + money + "成功了!當前賬戶還剩余:" + acc.getMoney());
return;//取錢后干掉了取錢方法
}
else
{
System.out.println("余額不足?。。?);
}
}
}
}
else
{
System.out.println("您自己的金額沒有超過100元,該努力工作了~~~");
}
}
/**
* 專門存錢的
* @param acc
*/
private static void depositMoney(Account acc,Scanner sc) {
System.out.println("===========存錢操作=========");
System.out.println("請您輸入存款的金額:");
double money = sc.nextDouble();
//直接把金額修改到賬戶對象的money屬性中去
acc.setMoney(acc.getMoney() + money);
System.out.println("存款完成!");
showAccount(acc);
}
private static void showAccount(Account acc)
{
System.out.println("===========當前賬戶詳情=========");
System.out.println("卡號" + acc.getCardId());
System.out.println("姓名" + acc.getUserWord());
System.out.println("余額" + acc.getMoney());
System.out.println("當次限額:" + acc.getQuotaMoney());
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;
//查無此賬戶,說明卡號沒有重復了!
}
}

總結(jié)溫習
- 轉(zhuǎn)賬功能需要判斷系統(tǒng)中是否有2個賬戶對象及以上
- 同時還要判斷總結(jié)賬戶是否有錢
- 接下來需要輸入對方卡號,判斷對方賬戶是否存在
- 對方賬戶存在還需要認證對方戶主的姓氏
- 滿足要求則可以把自己賬戶對象的金額修改到對方賬戶對象中去
- 修改密碼與銷戶
分析
- 修改密碼就是把當前對現(xiàn)象的密碼屬性使用set方法進行更新
- 銷戶是從集合對象中刪除當前對象,并回到首頁
這里為止所有的ATM系統(tǒng)的操作代碼就已經(jīng)完成
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
//showMain 開戶首頁的意思
// ArrayList<Account> accounts 使用方法定義功能傳入容器中 accounts是傳參
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登錄
login(accounts,sc);
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 完成用戶登錄
* @param accounts
*/
private static void login(ArrayList<Account> accounts,Scanner sc)
{
//必須系統(tǒng)中存在賬戶才可以登錄
if (accounts.size() == 0)
{
//沒有任何賬戶
System.out.println("當前系統(tǒng)中無任何賬戶,您需要先注冊!");
return;//直接結(jié)束方法的執(zhí)行!
}
//2.讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象
while (true)
{
System.out.println("請您輸入登錄的卡號:");
String cardId = sc.next();
//根據(jù)卡號查詢賬戶對象
Account acc = getAccountBycardId(cardId,accounts);
// 3.判斷賬戶對象是否存在,存在說明卡號沒問題
if (acc != null)
{
while (true)
{
// 4.讓用戶繼續(xù)輸入密碼
System.out.println("請您輸入登錄的密碼:");
String password = sc.next();
// 5.判斷密碼是否正確
if (acc.getPassWord().equals(password))
{
//密碼正確,登入成功
//展示系統(tǒng)登錄后的操作界面
System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號是:" + acc.getCardId());
//展示操作頁面
showUserCommand(sc,acc,accounts);
return;//繼續(xù)結(jié)束登錄方法
}
else
{
System.out.println("您的密碼有誤,請確認!");
}
}
}
else
{
System.out.println("對不起,不存在該卡號的賬戶!");
}
}
}
private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts)
{
while (true)
{
System.out.println("=========用戶操作頁面========");
System.out.println("1.查詢賬戶");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.轉(zhuǎn)賬");
System.out.println("5.修改密碼");
System.out.println("6.退出");
System.out.println("7.注銷賬戶");
System.out.println("請您輸入操作命令:");
int command = sc.nextInt();
switch (command)
{
case 1:
//查詢賬戶
showAccount(acc);
break;
case 2:
//存款
depositMoney(acc,sc);
break;
case 3:
//取款
drawMoney(acc,sc);
break;
case 4:
//轉(zhuǎn)賬
transferMoney(accounts,acc ,sc);
break;
case 5:
//修改密碼
updataPassWord(acc,sc);
return;//結(jié)束當前…………
case 6:
//退出
System.out.println("歡迎下次光臨?。?);
return; //結(jié)束當前showUserCommand(Scanner sc,Account acc)的方法
case 7:
//注銷賬戶
//從當前集合中抹掉當前賬戶對象即可
accounts.remove(acc);
System.out.println("銷戶成功了??!");
return;
default:
System.out.println("您輸入有誤!");
}
}
}
/**
* 修改密碼
* @param acc
*/
private static void updataPassWord(Account acc,Scanner sc)
{
System.out.println("===========修改密碼=========");
while (true)
{
System.out.println("請您輸入正確的密碼:");
String okPassWord = sc.next();
//判斷密碼是否正確
if (acc.getPassWord().equals(okPassWord))
{
//可以輸入新密碼
System.out.println("請您輸入新的密碼:");
String newPassWord = sc.next();
System.out.println("請您輸入確認密碼:");
String okNewPassWord = sc.next();
if (newPassWord.equals(okNewPassWord))
{
//修改賬戶對象的密碼為新密碼
acc.setPassWord(newPassWord);
return;//直接結(jié)束方法!
}
else
{
System.out.println("您兩次輸入的密碼不一致~~");
}
}
else
{
System.out.println("當前輸入的密碼不正確~~~");
}
}
}
/**
* 轉(zhuǎn)賬功能
* @param accounts
* @param acc
* @param sc
*/
private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
{
//1.判斷系統(tǒng)中是否有2個賬戶及以上
if (accounts.size() < 2)
{
System.out.println("對不起,系統(tǒng)中無其他賬戶,您不可以轉(zhuǎn)賬?。?);
return;
}
//2.判斷自己的賬戶對象中是否有錢
if (acc.getMoney() == 0)
{
System.out.println("對不起,您自己都快吃土了,就別裝逼了?。?);
return;
}
//3.開始轉(zhuǎn)賬邏輯
while (true)
{
System.out.println("請您輸入對方賬戶的卡號:");
String cardId = sc.next();
Account account = getAccountBycardId(cardId,accounts);
//判斷整個賬戶對象是否存在,存在說明對方卡號輸入正確
if (account != null)
{
//判斷這個賬戶對象是否是當前自己登錄的賬戶
if (account.getCardId().equals(acc.getCardId()))
{
//也就是這里企圖想給自己轉(zhuǎn)賬
System.out.println("您不能給自己轉(zhuǎn)賬!");
}
else
{
//確認對方的姓氏
String name = "*" + account.getUserWord().substring(1);
System.out.println("請您確認【" + name + "】的姓氏:");
String preName = sc.next();
//判斷
if (account.getUserWord().startsWith(preName))
{
//真正的轉(zhuǎn)賬才剛剛開始
System.out.println("請您輸入轉(zhuǎn)賬的金額:");
double money = sc.nextDouble();
//判斷這個金額是否超過了自己的金額
if (money > acc.getMoney())
{
System.out.println("對不起,您要轉(zhuǎn)賬的金額太多,您最多可以轉(zhuǎn)賬:" + acc.getMoney());
}
else
{
//開始了
acc.setMoney(acc.getMoney() - money);
account.setMoney(account.getMoney() + money);
System.out.println("恭喜您,轉(zhuǎn)賬成功了,已經(jīng)為" + account.getUserWord() + "轉(zhuǎn)賬了:" + money);
showAccount(acc);
return;
}
}
else
{
System.out.println("對不起,您認證的信息有誤~~~");
}
}
}
else
{
System.out.println("對不起,您輸入的轉(zhuǎn)賬卡號有問題!!");
}
}
}
/**
* 取款操作
* @param acc
* @param sc
*/
private static void drawMoney(Account acc, Scanner sc)
{
System.out.println("==========取款操作=========");
//1.判斷它的賬戶是否足夠100元
if (acc.getMoney() >= 100)
{
while (true) {
System.out.println("請您輸入取款的金額:");
double money = sc.nextDouble();
//2.判斷整個金額有沒有超過當次限額
if (money > acc.getQuotaMoney())
{
System.out.println("您當次取款金額超過每次限額,不要取那么多,每次最多可以?。? + acc.getQuotaMoney());
}
else
{
//3.判斷當前余額是否足夠你取錢
if (acc.getMoney() >= money)
{
//夠了,可以取錢了
acc.setMoney(acc.getMoney() - money);
System.out.println("恭喜您,取錢" + money + "成功了!當前賬戶還剩余:" + acc.getMoney());
return;//取錢后干掉了取錢方法
}
else
{
System.out.println("余額不足?。?!");
}
}
}
}
else
{
System.out.println("您自己的金額沒有超過100元,該努力工作了~~~");
}
}
/**
* 專門存錢的
* @param acc
*/
private static void depositMoney(Account acc,Scanner sc)
{
System.out.println("===========存錢操作=========");
System.out.println("請您輸入存款的金額:");
double money = sc.nextDouble();
//直接把金額修改到賬戶對象的money屬性中去
acc.setMoney(acc.getMoney() + money);
System.out.println("存款完成!");
showAccount(acc);
}
private static void showAccount(Account acc)
{
System.out.println("===========當前賬戶詳情=========");
System.out.println("卡號" + acc.getCardId());
System.out.println("姓名" + acc.getUserWord());
System.out.println("余額" + acc.getMoney());
System.out.println("當次限額:" + acc.getQuotaMoney());
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;
//查無此賬戶,說明卡號沒有重復了!
}
}

5. ??源代碼在這里這里拿
package com.wangxinhua;
/**
賬戶類
*/
public class Account {
private String CardId;//卡號
private String UserWord;//客戶名稱
private String PassWord;//密碼
private double Money;//余額
private double QuotaMoney;//當次取現(xiàn)限額
//無參函數(shù)
public Account() {
}
// 構(gòu)造好了有參函數(shù),那么就會有無參函數(shù)
// 有參函數(shù)
public Account(String cardId, String userWord, String passWord, double quotaMoney) {
CardId = cardId;
UserWord = userWord;
PassWord = passWord;
QuotaMoney = quotaMoney;
}
public String getCardId() {
return CardId;
}
public void setCardId(String cardId) {
CardId = cardId;
}
public String getUserWord() {
return UserWord;
}
public void setUserWord(String userWord) {
UserWord = userWord;
}
public String getPassWord() {
return PassWord;
}
public void setPassWord(String passWord) {
PassWord = passWord;
}
public double getMoney() {
return Money;
}
public void setMoney(double money) {
Money = money;
}
public double getQuotaMoney() {
return QuotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
QuotaMoney = quotaMoney;
}
}
這里是第一個類用于構(gòu)造函數(shù) 下面這個是第二個類
package com.wangxinhua;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMSystem {
public static void main(String[] args)
{
// 1.準備系統(tǒng)需要的容器對象,用戶存儲賬戶對象
ArrayList<Account> accounts = new ArrayList();
// 2.準備系統(tǒng)的首頁,登入,開戶
showMain(accounts);
}
public static void showMain(ArrayList<Account> accounts)
//showMain 開戶首頁的意思
// ArrayList<Account> accounts 使用方法定義功能傳入容器中 accounts是傳參
{
System.out.println("=============歡迎進入首頁===========");
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("請您輸入您想做的操作:");
System.out.println("1.登錄");
System.out.println("2.開戶");
System.out.println("您可以輸入命令了:");
int command = sc.nextInt();
switch(command)
{
case 1:
//登錄
login(accounts,sc);
break;
case 2:
//開戶
register(accounts,sc);
break;
default:
System.out.println("您當前輸入的操作命令不被支持!");
}
}
}
/**
* 完成用戶登錄
* @param accounts
*/
private static void login(ArrayList<Account> accounts,Scanner sc)
{
//必須系統(tǒng)中存在賬戶才可以登錄
if (accounts.size() == 0)
{
//沒有任何賬戶
System.out.println("當前系統(tǒng)中無任何賬戶,您需要先注冊!");
return;//直接結(jié)束方法的執(zhí)行!
}
//2.讓用戶鍵盤錄入卡號,根據(jù)卡號查詢賬戶對象
while (true)
{
System.out.println("請您輸入登錄的卡號:");
String cardId = sc.next();
//根據(jù)卡號查詢賬戶對象
Account acc = getAccountBycardId(cardId,accounts);
// 3.判斷賬戶對象是否存在,存在說明卡號沒問題
if (acc != null)
{
while (true)
{
// 4.讓用戶繼續(xù)輸入密碼
System.out.println("請您輸入登錄的密碼:");
String password = sc.next();
// 5.判斷密碼是否正確
if (acc.getPassWord().equals(password))
{
//密碼正確,登入成功
//展示系統(tǒng)登錄后的操作界面
System.out.println("恭喜您," + acc.getUserWord() +",成功登入系統(tǒng),您的卡號是:" + acc.getCardId());
//展示操作頁面
showUserCommand(sc,acc,accounts);
return;//繼續(xù)結(jié)束登錄方法
}
else
{
System.out.println("您的密碼有誤,請確認!");
}
}
}
else
{
System.out.println("對不起,不存在該卡號的賬戶!");
}
}
}
private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts)
{
while (true)
{
System.out.println("=========用戶操作頁面========");
System.out.println("1.查詢賬戶");
System.out.println("2.存款");
System.out.println("3.取款");
System.out.println("4.轉(zhuǎn)賬");
System.out.println("5.修改密碼");
System.out.println("6.退出");
System.out.println("7.注銷賬戶");
System.out.println("請您輸入操作命令:");
int command = sc.nextInt();
switch (command)
{
case 1:
//查詢賬戶
showAccount(acc);
break;
case 2:
//存款
depositMoney(acc,sc);
break;
case 3:
//取款
drawMoney(acc,sc);
break;
case 4:
//轉(zhuǎn)賬
transferMoney(accounts,acc ,sc);
break;
case 5:
//修改密碼
updataPassWord(acc,sc);
return;//結(jié)束當前…………
case 6:
//退出
System.out.println("歡迎下次光臨??!");
return; //結(jié)束當前showUserCommand(Scanner sc,Account acc)的方法
case 7:
//注銷賬戶
//從當前集合中抹掉當前賬戶對象即可
accounts.remove(acc);
System.out.println("銷戶成功了??!");
return;
default:
System.out.println("您輸入有誤!");
}
}
}
/**
* 修改密碼
* @param acc
*/
private static void updataPassWord(Account acc,Scanner sc)
{
System.out.println("===========修改密碼=========");
while (true)
{
System.out.println("請您輸入正確的密碼:");
String okPassWord = sc.next();
//判斷密碼是否正確
if (acc.getPassWord().equals(okPassWord))
{
//可以輸入新密碼
System.out.println("請您輸入新的密碼:");
String newPassWord = sc.next();
System.out.println("請您輸入確認密碼:");
String okNewPassWord = sc.next();
if (newPassWord.equals(okNewPassWord))
{
//修改賬戶對象的密碼為新密碼
acc.setPassWord(newPassWord);
return;//直接結(jié)束方法!
}
else
{
System.out.println("您兩次輸入的密碼不一致~~");
}
}
else
{
System.out.println("當前輸入的密碼不正確~~~");
}
}
}
/**
* 轉(zhuǎn)賬功能
* @param accounts
* @param acc
* @param sc
*/
private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
{
//1.判斷系統(tǒng)中是否有2個賬戶及以上
if (accounts.size() < 2)
{
System.out.println("對不起,系統(tǒng)中無其他賬戶,您不可以轉(zhuǎn)賬??!");
return;
}
//2.判斷自己的賬戶對象中是否有錢
if (acc.getMoney() == 0)
{
System.out.println("對不起,您自己都快吃土了,就別裝逼了??!");
return;
}
//3.開始轉(zhuǎn)賬邏輯
while (true)
{
System.out.println("請您輸入對方賬戶的卡號:");
String cardId = sc.next();
Account account = getAccountBycardId(cardId,accounts);
//判斷整個賬戶對象是否存在,存在說明對方卡號輸入正確
if (account != null)
{
//判斷這個賬戶對象是否是當前自己登錄的賬戶
if (account.getCardId().equals(acc.getCardId()))
{
//也就是這里企圖想給自己轉(zhuǎn)賬
System.out.println("您不能給自己轉(zhuǎn)賬!");
}
else
{
//確認對方的姓氏
String name = "*" + account.getUserWord().substring(1);
System.out.println("請您確認【" + name + "】的姓氏:");
String preName = sc.next();
//判斷
if (account.getUserWord().startsWith(preName))
{
//真正的轉(zhuǎn)賬才剛剛開始
System.out.println("請您輸入轉(zhuǎn)賬的金額:");
double money = sc.nextDouble();
//判斷這個金額是否超過了自己的金額
if (money > acc.getMoney())
{
System.out.println("對不起,您要轉(zhuǎn)賬的金額太多,您最多可以轉(zhuǎn)賬:" + acc.getMoney());
}
else
{
//開始了
acc.setMoney(acc.getMoney() - money);
account.setMoney(account.getMoney() + money);
System.out.println("恭喜您,轉(zhuǎn)賬成功了,已經(jīng)為" + account.getUserWord() + "轉(zhuǎn)賬了:" + money);
showAccount(acc);
return;
}
}
else
{
System.out.println("對不起,您認證的信息有誤~~~");
}
}
}
else
{
System.out.println("對不起,您輸入的轉(zhuǎn)賬卡號有問題??!");
}
}
}
/**
* 取款操作
* @param acc
* @param sc
*/
private static void drawMoney(Account acc, Scanner sc)
{
System.out.println("==========取款操作=========");
//1.判斷它的賬戶是否足夠100元
if (acc.getMoney() >= 100)
{
while (true) {
System.out.println("請您輸入取款的金額:");
double money = sc.nextDouble();
//2.判斷整個金額有沒有超過當次限額
if (money > acc.getQuotaMoney())
{
System.out.println("您當次取款金額超過每次限額,不要取那么多,每次最多可以取:" + acc.getQuotaMoney());
}
else
{
//3.判斷當前余額是否足夠你取錢
if (acc.getMoney() >= money)
{
//夠了,可以取錢了
acc.setMoney(acc.getMoney() - money);
System.out.println("恭喜您,取錢" + money + "成功了!當前賬戶還剩余:" + acc.getMoney());
return;//取錢后干掉了取錢方法
}
else
{
System.out.println("余額不足?。?!");
}
}
}
}
else
{
System.out.println("您自己的金額沒有超過100元,該努力工作了~~~");
}
}
/**
* 專門存錢的
* @param acc
*/
private static void depositMoney(Account acc,Scanner sc)
{
System.out.println("===========存錢操作=========");
System.out.println("請您輸入存款的金額:");
double money = sc.nextDouble();
//直接把金額修改到賬戶對象的money屬性中去
acc.setMoney(acc.getMoney() + money);
System.out.println("存款完成!");
showAccount(acc);
}
private static void showAccount(Account acc)
{
System.out.println("===========當前賬戶詳情=========");
System.out.println("卡號" + acc.getCardId());
System.out.println("姓名" + acc.getUserWord());
System.out.println("余額" + acc.getMoney());
System.out.println("當次限額:" + acc.getQuotaMoney());
}
/**
* 用戶開戶功能
* @param accounts 賬戶的集合對象
*/
private static void register(ArrayList<Account> accounts, Scanner sc)
{
System.out.println("=========用戶開戶功能==========");
//鍵盤錄入 姓名 密碼 確認密碼
System.out.println("請您輸入開戶名稱:");
String name = sc.next();
String password = "";
while (true)
{
System.out.println("請您輸入開戶密碼:");
password = sc.next();
System.out.println("請您輸入確認密碼:");
String okPassword = sc.next();
// 判斷兩次輸入的密碼是否一致
if (okPassword.equals(password))
//字符串比較用equals
{
break;
}
else
{
System.out.println("兩次密碼必須一致~~~");
}
}
System.out.println("請您輸入當次限額:");
double quotaMoney = sc.nextDouble();
// 3.生成賬戶的卡號,卡號是8位,而且不能與其他賬戶卡號重復。
String cardId = creatCardId(accounts);
// 4.創(chuàng)建一個賬戶對象封裝賬戶的信息
// public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
Account account = new Account(cardId,name,password,quotaMoney);
// 5.把賬戶對象添加到集合中去
accounts.add(account);
System.out.println("恭喜您,您開戶成功,您的卡號是:" + account.getCardId() + ",請您妥善保管");
}
public static String creatCardId(ArrayList<Account> accouts)
{
while (true) {
// 生成8位隨機的數(shù)字代表卡號
String cardId = "";
Random r = new Random();
for (int i = 0; i < 8; i++)
{
cardId += r.nextInt(10);
}
// 判斷卡號是否重復了
Account acc = getAccountBycardId(cardId,accouts);
if (acc == null)
{
// 說明當前卡號沒有重復
return cardId;
}
}
}
public static Account getAccountBycardId(String cardId, ArrayList<Account> accounts)
{
// 根據(jù)卡號查詢對象
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId))
{
return acc;
}
}
return null;
//查無此賬戶,說明卡號沒有重復了!
}
}

??到這里我們的ATM系統(tǒng)就已經(jīng)實現(xiàn)完成了,肝了兩天兩夜,喜歡的小伙伴可以復制源代碼去試試看,挺有趣的噢
到此這篇關(guān)于Java實現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏的文章就介紹到這了,更多相關(guān)Java ATM系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何修改HttpServletRequest中header中的信息
這篇文章主要介紹了如何修改HttpServletRequest中header中的信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringSecurity之SecurityContextHolder使用解讀
這篇文章主要介紹了SpringSecurity之SecurityContextHolder使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

