小挑戰:
You job is to create a simple banking application.
There should be a Bank class
It should have an arraylist of Branches
Each Branch should have an arraylist of Customers
The Customer class should have an arraylist of Doubles (transactions)
Customer:
Name, and the ArrayList of doubles.
Branch:
Need to be able to add a new customer and initial transaction amount.
Also needs to add additional transactions for that customer/branch
Bank:
Add a new branch
Add a customer to that branch with initial transaction
Add a transaction for an existing customer for that branch
Show a list of customers for a particular branch and optionally a list
of their transactions
Demonstration autoboxing and unboxing in your code
Hint: Transactions
Add data validation.
e.g. check if exists, or does not exist, etc.
Think about where you are adding the code to perform certain actions
解答:
我的版本
Customer.java
import java.util.ArrayList;public class Customer {
private ArrayList<Double> transactions;
private String name; public Customer(String name) {
this.name = name;
transactions = new ArrayList<Double>();
} public static Customer createCustomer(String name) {
return new Customer(name);
} public String getName() {
return name;
} public ArrayList<Double> getTransactions() {
return transactions;
} public void transaction_record() {
for (int i = 0; i < transactions.size(); i++) {
System.out.println((i + 1) + " : " + transactions.get(i));
}
} public double countBalance() {
double balance = 0;
for (int i = 0; i < transactions.size(); i++) {
balance += transactions.get(i).doubleValue();
}
return balance;
}
}
Branch.java
import java.util.ArrayList;public class Branch {
private ArrayList<Customer> customers;
private String BranchCode; public Branch(String BranchCode) {
this.BranchCode = BranchCode;
customers = new ArrayList<Customer>();
} public static Branch createBranch(String BranchCode) {
return new Branch(BranchCode);
} public ArrayList<Customer> getCustomers() {
return customers;
} public String getBranchCode() {
return BranchCode;
}
public void customersTransaction(Customer customerA, Customer customerB, Double amount) {
customerA.getTransactions().add(-amount);
customerB.getTransactions().add(amount);
} public boolean addCustomer(Customer customer) {
if (findCustomer(customer.getName()) >= 0) {
System.out.println("customer is already on file.");
return false;
}
customers.add(customer);
double initial_amount = 10000;
customer.getTransactions().add(initial_amount);
return true;
} public int findCustomer(String customerName) {
for (int i = 0; i < customers.size(); i++) {
Customer customer = customers.get(i);
if (customer.getName().equals(customerName)) {
return i;
}
}
return -1;
} public Customer queryCustomer(String customerName) {
int position = findCustomer(customerName);
if (position >= 0) {
return customers.get(position);
}
return null;
} public double checkBalance(String customerName) {
Customer customer = queryCustomer(customerName);
return customer.countBalance();
} public void printCustomer() {
System.out.println("Branch code(" + getBranchCode() + "), list of customer: ");
if (customers.size() != 0) {
for (int i = 0; i < customers.size(); i++) {
System.out.println("No." + (i + 1) + " Customer: " + customers.get(i).getName());
System.out.println("Transaction record: ");
customers.get(i).transaction_record();
System.out.println("Total balance: \n$" + customers.get(i).countBalance());
}
} else {
System.out.println("Please add customer to branch.\nDon't have any customer now!");
}
}
}
Bank.java
import java.util.ArrayList;public class Bank {
private static String BankName;
private static ArrayList<Branch> branches; public Bank(String bankName) {
BankName = bankName;
branches = new ArrayList<Branch>();
} public static String getBankName() {
return BankName;
} public static ArrayList<Branch> getBranches() {
return branches;
} public boolean printBranches() {
if (branches.size() == 0) {
System.out.println("Please add branch first!\nDon't have any branch now.");
return false;
}
System.out.println(getBankName() + " had " + branches.size() + " branches.");
for (int i = 0; i < branches.size(); i++) {
System.out.println(getBankName() + " No." + (i + 1) +
" branch code (" + branches.get(i).getBranchCode() + ")"); }
return true;
} public boolean addNewCustomer(Branch branch, String customerName) {
Customer customer = new Customer(customerName);
return branch.addCustomer(customer);
} public boolean addNewBranch(Branch branch) {
if (findBranch(branch.getBranchCode()) >= 0) {
System.out.println("That branch is already on file.");
return false;
}
branches.add(branch);
return true;
} public int findBranch(String branchName) {
for (int i = 0; i < branches.size(); i++) {
Branch branch = branches.get(i);
if (branch.getBranchCode().equals(branchName)) {
return i;
}
}
return -1;
}
public void printBranchCustomer(String BranchName) {
if (queryBranch(BranchName) != null) {
queryBranch(BranchName).printCustomer();
} else {
System.out.println("Couldn't find branch code (" + BranchName + ")");
}
} public Branch queryBranch(String BranchName) {
int position = findBranch(BranchName);
if (position >= 0) {
return branches.get(position);
}
return null;
} public boolean customersTransaction(Branch branchA, String customerA, Branch branchB, String customerB, Double amount) {
if (branchA.queryCustomer(customerA) == null || branchB.queryCustomer(customerB) == null) {
System.out.println("Customer didn't exist that branch.");
return false;
} Customer customer_A = branchA.queryCustomer(customerA);
Customer customer_B = branchB.queryCustomer(customerB); if (branchA.checkBalance(customerA) >= amount) {
branchA.customersTransaction(customer_A, customer_B, amount);
return true;
} System.out.println("Customer amount isn't enough!");
return false;
}
}
MainBank.java
import java.util.Scanner;public class MainBank {
private static Scanner scanner = new Scanner(System.in);
private static Bank bank = new Bank("CityBank"); public static void main(String[] args) {
boolean quit = false;
startBank();
printActions();
while (!quit) {
System.out.println("------------------------------------");
System.out.println("Enter number to action: ");
int choice = scanner.nextInt();
scanner.nextLine(); switch (choice) {
case 0:
quit = true;
System.out.println("Thank you, Good bye!");
break;
case 1:
bank.printBranches();
break;
case 2:
addNewBranch();
break;
case 3:
addNewCustomer();
break;
case 4:
addTransaction();
break;
case 5:
printCustomer();
break;
case 6:
printActions();
break; }
} } private static void addNewBranch() {
System.out.println("This function is addNewBranch");
System.out.println("Enter new branch code: ");
Branch branch = Branch.createBranch(scanner.nextLine());
if (bank.addNewBranch(branch)) {
System.out.println("Successful! New branch code (" + branch.getBranchCode() + ") be added");
} else {
System.out.println("Error! Cannot add branch code (" + branch.getBranchCode() + ") already had...");
}
System.out.println("function out!");
} private static void addNewCustomer() {
System.out.println("This function is addNewCustomer");
if (bank.printBranches()) {
System.out.println("Which branch need to add new customer?\nEnter branch code: ");
String branchName = scanner.nextLine();
Branch branch = bank.queryBranch(branchName);
if (bank.findBranch(branchName) >= 0) {
System.out.println("Enter new customer name: ");
String customerName = scanner.nextLine();
if (bank.addNewCustomer(branch, customerName)) {
System.out.println("Successful! New customer: " + customerName + " be added to branch code: " + branchName);
} else {
System.out.println("Cannot add new customer " + customerName + " to branch code " + branchName);
}
} else {
System.out.println("Error! Branch(" + branchName + ") doesn't exist!");
}
}
System.out.println("function out!");
} private static void addTransaction() {
System.out.println("This function is addTransaction");
if (bank.printBranches()) {
System.out.println("A pay $amount to B");
System.out.println("Enter A customer branch code: ");
String branchName = scanner.nextLine();
Branch branchA = bank.queryBranch(branchName);
System.out.println("Enter A customer name: ");
String customerA = scanner.nextLine();
System.out.println("Pay how much?");
Double amount = scanner.nextDouble();
scanner.nextLine();
System.out.println("Enter B customer branch code: ");
String branchNameB = scanner.nextLine();
Branch branchB = bank.queryBranch(branchNameB);
System.out.println("Enter B customer name: ");
String customerB = scanner.nextLine();
if (bank.customersTransaction(branchA, customerA, branchB, customerB, amount)) {
System.out.println("Successful!");
} else {
System.out.println("Error");
}
}
System.out.println("function out!");
} private static void printCustomer() {
System.out.println("This function is printCustomer");
if (bank.printBranches()) {
System.out.println("Which branch need to print all customer?\nEnter branch code: ");
String branchName = scanner.nextLine();
bank.printBranchCustomer(branchName);
}
System.out.println("function out!");
} private static void printActions() {
System.out.println("\nAvailable actions:\nPress number of list.");
System.out.println("0 - to shutdown\n" +
"1 - to print branches\n" +
"2 - to add a new branch\n" +
"3 - to add a new customer\n" +
"4 - to add a transaction\n" +
"5 - to print customer list of branch\n" +
"6 - to print action.");
} private static void startBank() {
System.out.println("Start Bank app ------");
}
}
這道題我沒有先看 Tim 的解答,靠自己修改前面的練習所寫,所以不一定符合題目要求
足足花了我 5 天來寫而且還是在我有參考 Mobile project 的情況
從剛開始光理解題目就很頭痛,到理解完題目開始寫一點
到寫完大部分開始調適代碼,卻發現數字竟然對不上
又開始東調西調代碼,到輸出結果終於能滿足自己的想法
這題目比較難的是 Bank 要有 Branch的Arraylis,而 Branch 又要有 Customer的Arraylis
要非常清楚自己寫的 method 要起什麼作用,要給哪個 Class 調用,要放入跟傳回哪種數據類型
寫完之後就會對數據的傳遞方式的理解,印象深入了許多
開始寫才發現,前面光用看的或照抄Tim的代碼,雖然當時覺得自己理解大部份內容了
但真的要寫的時候就一片空白,只好回去翻前面的練習,邊抄邊改成自己要的
補充一下,寫不出來也不用太在意,我看網友說連大公司的programmer也是會抄代碼來改
Tim version
Customer_TimVersion.java
import java.util.ArrayList;public class Customer_TimVersion {
private String name;
private ArrayList<Double> transactions; public Customer_TimVersion(String name, double initialAmount) {
this.name = name;
transactions = new ArrayList<Double>();
addTransaction(initialAmount);
} public void addTransaction(double amount) {
transactions.add(amount);
} public String getName() {
return name;
} public ArrayList<Double> getTransactions() {
return transactions;
}
}
Branch_TimVersion.java
import java.util.ArrayList;public class Branch_TimVersion {
private String name;
private ArrayList<Customer_TimVersion> customers; public Branch_TimVersion(String name) {
this.name = name;
customers = new ArrayList<Customer_TimVersion>();
} public String getName() {
return name;
} public ArrayList<Customer_TimVersion> getCustomers() {
return customers;
} public boolean newCustomer(String customerName, double initialAmount) {
if (findCustomer(customerName) == null) {
customers.add(new Customer_TimVersion(customerName, initialAmount));
return true;
}
return false;
} public boolean addCustomerTransaction(String customerName, double amount) {
Customer_TimVersion existingCustomer = findCustomer(customerName);
if (existingCustomer != null) {
existingCustomer.addTransaction(amount);
return true;
}
return false;
} private Customer_TimVersion findCustomer(String customerName) {
for (int i = 0; i < customers.size(); i++) {
Customer_TimVersion checkCustomer = customers.get(i);
if (checkCustomer.getName().equals(customerName)) {
return checkCustomer;
}
}
return null;
}
}
Bank_TimVersion.java
import java.util.ArrayList;public class Bank_TimVersion {
private String name;
private ArrayList<Branch_TimVersion> branches; public Bank_TimVersion(String name) {
this.name = name;
branches = new ArrayList<Branch_TimVersion>();
} public boolean addBranch(String branchName) {
if (findBranch(branchName) == null) {
branches.add(new Branch_TimVersion(branchName));
return true;
}
return false;
} public boolean addCustomer(String branchName, String customerName, double initialAmount) {
Branch_TimVersion branch = findBranch(branchName);
if (branchName != null && branch != null) {
return branch.newCustomer(customerName, initialAmount);
}
return false;
} public boolean addCustomerTransaction(String branchName, String customerName, double amount) {
Branch_TimVersion branch = findBranch(branchName);
if (branch != null) {
return branch.addCustomerTransaction(customerName, amount);
}
return false;
} private Branch_TimVersion findBranch(String branchName) {
for (int i = 0; i < branches.size(); i++) {
Branch_TimVersion Branch = branches.get(i);
if (Branch.getName().equals(branchName)) {
return Branch;
}
}
return null;
} public boolean listCustomer(String branchName, boolean showTransaction) {
Branch_TimVersion branch = findBranch(branchName);
if (branch != null) {
System.out.println("Customer details for branch " + branch.getName()); ArrayList<Customer_TimVersion> branchCustomers = branch.getCustomers();
for (int i = 0; i < branchCustomers.size(); i++) {
Customer_TimVersion branchCustomer = branchCustomers.get(i);
System.out.println("Customer: " + branchCustomer.getName() + "[" + (i + 1) + "]");
if (showTransaction) {
System.out.println("Transactions");
ArrayList<Double> transactions = branchCustomer.getTransactions();
for (int j = 0; j < transactions.size(); j++) {
System.out.println("[" + (j + 1) + "]" + "Amount " + transactions.get(j)); }
}
}
return true;
}
return false;
}
}
MainBank_TimVersion.java
public class MainBank_TimVersion { public static void main(String[] args) {
Bank_TimVersion bank = new Bank_TimVersion("City Bank");
bank.addBranch("New York"); bank.addCustomer("New York", "Tim", 500.05);
if(!bank.addCustomer("New York", "Tim", 500)){
System.out.println("Already had");
}
bank.addCustomer("New York", "Tim", 500);
bank.addCustomer("New York", "Jemmy", 170.56);
bank.addCustomer("New York", "Zic", 320.22); bank.addBranch("Los Angel");
bank.addCustomer("Los Angel", "Bonny", 126.54); bank.addCustomerTransaction("New York", "Tim", 36.66);
bank.addCustomerTransaction("Los Angel", "Bonny", 36.66); bank.listCustomer("Los Angel", true);
bank.listCustomer("New York", false); if(!bank.addCustomer("Sydney", "Brian", 6.66)){
System.out.println("Error Sydney branch doesn't exist");
}
}
}
結果 Tim 做的功能比我想的少很多
但是寫的真漂亮,看起來就很舒服
於是我又把自己寫的修改了一下
修正版
Customer.java
import java.util.ArrayList;public class Customer {
private ArrayList<Double> transactions;
private String name; public Customer(String name, double initialAmount) {
this.name = name;
transactions = new ArrayList<Double>();
addTransaction(initialAmount);
} public static Customer createCustomer(String name, double initialAmount) {
return new Customer(name, initialAmount);
} public String getName() {
return name;
} public ArrayList<Double> getTransactions() {
return transactions;
} public void addTransaction(double amount) {
transactions.add(amount);
} public void transaction_record() {
for (int i = 0; i < transactions.size(); i++) {
System.out.println((i + 1) + " : " + transactions.get(i));
}
} public double getBalance() {
double balance = 0;
for (Double transaction : transactions) {
balance += transaction;
}
return balance;
}
}
Branch.java
import java.util.ArrayList;public class Branch {
private ArrayList<Customer> customers;
private String name; public Branch(String name) {
this.name = name;
customers = new ArrayList<Customer>();
} public static Branch createBranch(String BranchCode) {
return new Branch(BranchCode);
} public String getName() {
return name;
} public boolean addCustomer(String customerName, double initialAmount) {
if (findCustomer(customerName) >= 0) {
return false;
}
customers.add(Customer.createCustomer(customerName, initialAmount));
return true;
} private int findCustomer(String customerName) {
for (int i = 0; i < customers.size(); i++) {
Customer customer = customers.get(i);
if (customer.getName().equals(customerName)) {
return i;
}
}
return -1;
} public Customer queryCustomer(String customerName) {
int position = findCustomer(customerName);
if (position >= 0) {
return customers.get(position);
}
return null;
} public void printCustomer() {
System.out.println("Branch code [" + getName() + "] --- list of customer: ");
if (customers.size() != 0) {
for (int i = 0; i < customers.size(); i++) {
System.out.println("No." + (i + 1) + " Customer: " + customers.get(i).getName());
System.out.println("Transaction record: ");
customers.get(i).transaction_record();
System.out.println("Total balance: \n$" + customers.get(i).getBalance());
}
} else {
System.out.println("Please add customer to branch.\nDon't have any customer now!");
}
}
}
Bank.java
import java.util.ArrayList;public class Bank {
private String name;
private ArrayList<Branch> branches; public Bank(String name) {
this.name = name;
branches = new ArrayList<Branch>();
} public String getName() {
return name;
} public boolean printBranches() {
if (branches.size() == 0) {
System.out.println("Please add branch first!\nDon't have any branch now.");
return false;
}
System.out.println(getName() + " had " + branches.size() + " branches.");
for (int i = 0; i < branches.size(); i++) {
System.out.println(getName() + " No." + (i + 1) +
" branch code (" + branches.get(i).getName() + ")"); }
return true;
} public boolean addNewCustomer(String branchName, String customerName, double initialAmount) {
if (findBranch(branchName) >= 0) {
Branch branch = queryBranch(branchName);
if (branch.addCustomer(customerName, initialAmount)) {
return true;
}
}
return false;
} public boolean addNewBranch(String branchName) {
if (findBranch(branchName) >= 0) {
return false;
}
branches.add(Branch.createBranch(branchName));
return true;
} private int findBranch(String branchName) {
for (int i = 0; i < branches.size(); i++) {
Branch branch = branches.get(i);
if (branch.getName().equals(branchName)) {
return i;
}
}
return -1;
} public Branch queryBranch(String BranchName) {
int position = findBranch(BranchName);
if (position >= 0) {
return branches.get(position);
}
return null;
} public void printBranchCustomer(String BranchName) {
queryBranch(BranchName).printCustomer(); } public boolean customersTransaction(String branchA, String customerA, String branchB, String customerB, Double amount) {
if (findBranch(branchA) >= 0 && findBranch(branchB) >= 0) {
Branch branch_A = queryBranch(branchA);
Branch branch_B = queryBranch(branchB);
if (branch_A.queryCustomer(customerA) != null && branch_B.queryCustomer(customerB) != null) {
Customer customerPay = branch_A.queryCustomer(customerA);
if (customerPay.getBalance() >= amount) {
customerPay.getTransactions().add(-amount);
Customer customerReceive = branch_B.queryCustomer(customerB);
customerReceive.getTransactions().add(amount);
return true;
}
}
}
return false;
}
}
MainBank.java
import java.util.Scanner;public class MainBank {
private static Scanner scanner = new Scanner(System.in);
private static Bank bank = new Bank("CityBank"); public static void main(String[] args) {
boolean quit = false;
startBank();
printActions();
while (!quit) {
System.out.println("------------------------------------");
System.out.println("Enter number to action: ");
int choice = scanner.nextInt();
scanner.nextLine(); switch (choice) {
case 0:
quit = true;
System.out.println("Thank you, Good bye!");
break;
case 1:
bank.printBranches();
break;
case 2:
addNewBranch();
break;
case 3:
addNewCustomer();
break;
case 4:
transaction();
break;
case 5:
printCustomer();
break;
case 6:
printActions();
break; }
} } private static void addNewBranch() {
System.out.println("This function is addNewBranch");
System.out.println("Enter new branch code: ");
String branchName = scanner.nextLine();
if (bank.addNewBranch(branchName)) {
System.out.println("Successful! New branch code [" + branchName + "] be added to " + bank.getName());
} else {
System.out.println("Error! Cannot add branch code [" + branchName + "] to \" + bank.getName())");
System.out.println("Branch code [" + branchName + "] already in " + bank.getName());
}
System.out.println("addNewBranch function out!");
} private static void addNewCustomer() {
System.out.println("This function is addNewCustomer");
if (bank.printBranches()) {
System.out.println("Which branch need to add new customer?\nEnter branch code: ");
String branchName = scanner.nextLine();
if (bank.queryBranch(branchName) != null) {
System.out.println("Enter new customer name: ");
String customerName = scanner.nextLine();
System.out.println("How much initial amount do you want to set?");
double initialAmount = scanner.nextDouble();
scanner.nextLine();
if (bank.addNewCustomer(branchName, customerName, initialAmount)) {
System.out.println("Successful! New customer: " + customerName + " be added to branch code [" + branchName + "]");
} else {
System.out.println("Error! Cannot add " + customerName + " to branch code [" + branchName + "].");
System.out.println("Customer already in that branch.");
}
} else {
System.out.println("Error! Branch code [" + branchName + "] doesn't exist!");
}
}
System.out.println("addNewCustomer function out!");
} private static void transaction() {
System.out.println("This function is transaction");
if (bank.printBranches()) {
System.out.println("A pay $amount to B");
System.out.println("Enter customer A -> branch code: ");
String branchA = scanner.nextLine();
if (bank.queryBranch(branchA) != null) {
Branch branch_A = bank.queryBranch(branchA);
System.out.println("Enter customer A -> name: ");
String customerA = scanner.nextLine();
if (branch_A.queryCustomer(customerA) != null) {
Customer customer_A = branch_A.queryCustomer(customerA);
System.out.println("Pay how much?");
double amount = scanner.nextDouble();
scanner.nextLine();
if (customer_A.getBalance() >= amount) {
System.out.println("Enter customer B -> branch code: ");
String branchB = scanner.nextLine();
if (bank.queryBranch(branchB) != null) {
Branch branch_B = bank.queryBranch(branchB);
System.out.println("Enter customer B -> name: ");
String customerB = scanner.nextLine();
if (branch_B.queryCustomer(customerB) != null) {
if (bank.customersTransaction(branchA, customerA, branchB, customerB, amount)) {
System.out.println("Successful!");
System.out.println(customerA + " pay $" + amount + " to " + customerB);
} else {
System.out.println("Error, transaction failed!");
}
} else {
System.out.println("Error! " + customerB + " doesn't exist in branch code [" + branchB + "]");
}
} else {
System.out.println("Error! Branch code [" + branchB + "] doesn't exist!");
}
} else {
System.out.println("Error! " + customerA + " balance isn't enough!");
}
} else {
System.out.println("Error! " + customerA + " doesn't exist in branch code [" + branchA + "]");
}
} else {
System.out.println("Error! Branch code [" + branchA + "] doesn't exist!");
}
}
System.out.println("transaction function out!");
} private static void printCustomer() {
System.out.println("This function is printCustomer");
if (bank.printBranches()) {
System.out.println("Which branch need to print all customer?\nEnter branch code: ");
String branchName = scanner.nextLine();
if (bank.queryBranch(branchName) != null) {
bank.printBranchCustomer(branchName);
} else {
System.out.println("Error! Branch code [" + branchName + "] doesn't exist!");
}
}
System.out.println("printCustomer function out!");
} private static void printActions() {
System.out.println("\nAvailable actions:\nPress number of list.");
System.out.println("0 - to shutdown\n" +
"1 - to print branches\n" +
"2 - to add a new branch\n" +
"3 - to add a new customer\n" +
"4 - to add a transaction\n" +
"5 - to print customer list of branch\n" +
"6 - to print action.");
} private static void startBank() {
System.out.println("Start Bank app ------");
}
}
此為看完 Tim 的代碼,把最上方 我的代碼 進行修正
大部分都有照著單一功能,簡潔化的方向去修改
但是在 MainBank.java 的 Method transaction()
就弄得很醜
我一時間也不曉得要怎麼修改
等以後看更多案例學習吧
主程式運行方式:
運行 MainBank.java
功能會由 printActions()
直接提示出來
照著要使用的功能,輸入數字操作
0.關閉程序
1.顯示現有分行
2.增加分行
3.增加客戶及開戶金額到現有分行
4.轉帳(顧客A 轉帳給 顧客B)
5.顯示特定分行裡所有客戶名單跟交易過程
代碼邏輯:
功能寫在三個 Class 裡,分別為 Customer、Branch、Bank
主要就是用 Arraylist 紀錄創建的 Customer 跟 Branch 的資料
Bank 裡有 Branch 的資料,Branch 裡有 Customer 的資料
大致功能就是寫 新增 跟 查詢,參考前面的 MobilePhone project 即可
轉帳的寫法就是 先檢查 A 客戶要付的金額是否大於帳戶內餘額
若有的話就回傳失敗,沒有的話就進行轉帳
A 客戶 新增一筆 付款(負數)
B 客戶 新增一筆 收款
算餘額就用 for 把第一筆加到最後一筆就得出
要注意的是不要把餘額這個變量寫到全局
把變量寫在 Method 裡,調用的時候在算就好
我一開始寫到 Class 裡當全局變量用,結果調用 Method 的過程中數字重複計算
有時候跑出正確餘額,有時候跑出錯誤餘額,害我找問題找半天
改寫到 Method 裡才解決問題