Java:一个将密钥和客户端的ID作为值的哈希映射,需要访问客户的帐户



我有一个HashMap,它保存一个键的ID和一个客户端作为Value。客户有一个或多个银行账户。我有一个单独的Account类,它包含帐户的详细信息。My Client类可以访问Account类,并且这些帐户是从Driver类中的构造函数创建的。我想访问客户的帐户集合,但每次尝试时,我只返回一个帐户,而不能访问其中的任何或全部帐户。我在驱动程序类中创建帐户,并将每个帐户设置为客户端。有什么想法我可以访问任何或所有帐户吗?

import java.util.ArrayList;

public class ATM {

private InputReader reader;
private String accountNumber;
private String passcode;
private boolean customerVerified;
private String customerId;
private Bank theBank;
private Bank theBanks;
private Customer currentCustomer;
private final int senior = 65;


/**
* Default constructor. Calls the initialize() method to seed the Bank with
* some Customers. Calls the run() method to perform the primary program
* functions.
*/
public ATM() {
super();
initialize();
run();

}

/**
* Main method calls the class default constructor.
* @param args for program arguments (not used)
*/
public static void main(String[] args) {

new ATM();

}

/**
* The primary application processor. All application functions are c         alled
* from here. Uses a loop to prompt users to perform banking transactions. 
* Must use switch/case selection to determine uses choices.
*/
public void run() {

reader = new InputReader();
boolean exit = false;

System.out.println("Welcome to Bullwinkle's Bank.");

while (!exit) {



System.out.println("Choose one of the following options:");
System.out.println("1 - Sign In");
System.out.println("2 - Deposit");
System.out.println("3 - Withdraw");
System.out.println("4 - Display Account Info");
System.out.println("5 - Exit");
System.out.print("> ");
int choice = reader.getIntInput();



switch (choice) {

case 1:
choice = 1;
verifyCustomer();
break;
case 2:
choice = 2;

transactDeposit();
break;
case 3:
choice = 3;
transactWithdraw();
break;
case 4:
choice = 4;
displayAccountInformation();
break;
case 5:
choice = 5;
System.out
.println("Thank you for banking at Bullwinkle's Bank");
System.out
.println("DEBUG: Displaying all the accounts in the bank.");
Bank.displayAllCustomers();
System.exit(0);

}

}

}

/**
* Creates Customer objects by calling overloaded constructor.
* creates Account objects by calling overloaded constructor.
* Sets Accounts to Customers.
* Adds Customer references to the Back HashMap as seed data for testing.
*/
public void initialize() {

theBank = new Bank();


Customer myia = new Customer("Myia", "Dog", "456", "123", 65);



if (myia.getAge() >= senior) {

ArrayList<Account> accounts = new ArrayList<Account>();
accounts.add(new SavingsAccount("SA-123", 0.0));
accounts.add(new GoldAccount("GL-123", 0.0, .01, false));
for (int j = 0; j < accounts.size(); j++) {
Account act = accounts.get(j);
myia.setAccount(act);
theBank.addCustomer(myia);


System.out.println(myia);

}



}/*

Customer freckle = new Customer("Freckle", "Cat", "789", "3", 22);
freckle.setAccount(new SavingsAccount("SA-789", 0.0));
theBank.addCustomer(freckle);


*/



/*Customer[] cust = {new Customer("Freckle", "Cat", "123", "1", 22), new Customer("Myia", "Dog", "456", "2", 65)};
Account[] acct = { new SavingsAccount("SA-123", 0.0), new ChequingAccount("CH-123", 0.0, 3)};


for(int i = 0; i < cust.length; i++) {
cust[i].setAccount(acct[i]);
}

for (Customer customer : cust) {
theBank.addCustomer(customer);
}*/



}

/**
* Performs a deposit into a Customer's account. Checks to see if the
* customer has signed in. If not, then verifyCustomer() is called and the menu
* is displayed again.
*/
public void transactDeposit() {

if (customerVerified) {

System.out.println("Enter the amount to deposit: ");
currentCustomer.getAccount().addToBalance(reader.getDoubleInput());

} else {

System.out
.println("ERROR: You must LOGIN before you can perform a transaction.");
verifyCustomer();
}
}

/**
* Performs a withdrawal from a Customer's account. Checks to see if the
* customer has signed in. If not, then verifyCustomer() is called and the menu
* is displayed again.
*/
public void transactWithdraw() {

if (customerVerified) {
System.out.println("Enter the amount to withdraw: ");
double amount = reader.getDoubleInput();

if (amount <= currentCustomer.getAccount().getBalance()) {
currentCustomer.getAccount().subtractFromBalance(amount);
} else {
System.out
.println("ERROR: You have insufficinet funds to withdraw that amount.");
}
} else {

System.out
.println("ERROR: You must LOGIN before you can perform a transaction.");
verifyCustomer();
}

}

/**
* Displays a Customer's information if the customer has been
* previously verified.
*/
public void displayAccountInformation() {

if (customerVerified) {
System.out.println("Here is your information.");
Bank.displayCustomerInformation(currentCustomer);
} else {

System.out
.println("ERROR: You must LOGIN before you can perform a transaction.");
verifyCustomer();
}

}

/**
* Confirms a Customer's account number and passcode. Called when the
* user is required to sign in to the application. Will set a boolean so the
* user does not have to sign in again during the session.
*/
public void verifyCustomer() {

System.out.println("Enter CustomerID: ");
customerId = reader.getStringInput();
System.out.println("Enter Passcode: ");
passcode = reader.getStringInput();
System.out.println("Enter AccountNumber");
accountNumber = reader.getStringInput();
currentCustomer = Bank.theBank.get(customerId);

if (currentCustomer != null) {
if (passcode.equals(currentCustomer.getPasscode())) {

customerVerified = true;
} else{
System.out.println("ERROR: Either account number or passcode is not correct.");
run();
}

} else {
System.out.println("ERROR: Either account number or passcode is not correct.");
run();
}




}
}



import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class Bank {
private ATM atm; 

/**
* The bank collection to hold all Customer data.
* Uses a customer's account number as key and the Customer reference as the value.
*/
public static  HashMap<String, Customer > theBank;
public static  HashMap<String, HashMap<String, Customer>> theBanks;
public static ArrayList<Customer> bankster;
/**
* Default constructor for Bank class.
* Initializes the HashMap
*/
public Bank() {
super();
theBank = new HashMap<String, Customer>();
bankster = new ArrayList<Customer>();
theBanks = new HashMap<String, HashMap<String, Customer>>();
}

/**
* Add a new Customer to the HashMap.
* @param newCustomer The new element to add to the HashpMap using 
* the account number as the key and the new Customer as the value.
* @param string 
*/
public void addCustomer(Customer newCustomer) {

if (newCustomer != null) {


theBank.put(newCustomer.getCustomerId(), newCustomer);
}
}




/**
* Removes an Customer from the HashMap.
* @param accountNumber The key of the element to remove from the HashMap.
*/
public void closeAccount(String customerId, String accountNumber) {

if (accountNumber != null && customerId != null) {

theBank.get(customerId).getAccount().setActive(false);
}
}

/**
* Displays the details of a Customer element in the HshMap.
* Uses Customer.toString() implementation.
* @param customer the Customer chosen to display.
*/
public static void displayCustomerInformation(Customer customer){

if(customer != null){
System.out.println(customer.getAccount().getAccountNumber());
System.out.println(customer.toString());
System.out.println(customer);
}
}

/**
* Displays all elements of the HashMap by using Customer.toString() 
* implementation of each.
*/
public static void displayAllCustomers(){

for(Customer customer : theBank.values()){

System.out.println(customer);
for (HashMap<String, Customer> custy : theBanks.values()) {
System.out.println(custy);
}


for (Customer custer : bankster) {
System.out.println(custer);

}



}
}

}


import java.util.*;
public class Customer {

private String firstName;
private String lastName;
private String passcode;
private Account account;
private int age; 
private String customerId;





/**
* Default constructor for a Customer. Sets the fields to the default values
* for each type.
*/
public Customer() {
super();

}

/**
* @param firstName
*            String to initialize the firstName field
* @param lastName
*            String to initialize the lastName field
* @param passcode
*            String to initialize the passcode field
*/
public Customer(String firstName, String lastName, String passcode,           String customerId, int age) {
super();

setFirstName(firstName);
setLastName(lastName);
setPasscode(passcode);
setAge(age);
setCustomerId(customerId);
account = new Account();
setAccount(account);

}

/**
* Accessor for a Cutomer's collection of Accounts
* 
* @return a customer account as an Account
*/
public Account getAccount() {

return account;
}

public int getAge() {
return age;
}

public void setAge(int age) {
if (age > 17 && age <= 120) {
this.age = age;
}
}

public String getCustomerId() {
return customerId;
}

public void setCustomerId(String customerId) {
if (customerId != null) {
this.customerId = customerId;
}
}

/**
* Mutator for the Account filed
* 
* @param account
*            the Account to set
*/
public void setAccount(Account account) {

if (account != null) {

this.account = account;
}
}




/**
* Accessor method for the firstName field
* 
* @return the firstName as a String
*/
public String getFirstName() {
return firstName;
}

/**
* Mutator for the firstName field
* 
* @param firstName
*            the firstName to set
*/
public void setFirstName(String firstName) {

if (firstName != null && !firstName.trim().isEmpty()) {
this.firstName = firstName;
}
}

/**
* Accessor method for the lastName
* 
* @return the lastName as a String
*/
public String getLastName() {
return lastName;
}

/**
* Mutator for the lastName field
* 
* @param lastName
*            the lastName to set
*/
public void setLastName(String lastName) {

if (lastName != null && !lastName.trim().isEmpty()) {
this.lastName = lastName;
}
}

/**
* Accessor method for the passcode field
* 
* @return the passcode as a String
*/
public String getPasscode() {
return passcode;
}

/**
* Mutator for the passcode field
* 
* @param passcode
*            the passcode to set
*/
public void setPasscode(String passcode) {

if (passcode != null && !passcode.trim().isEmpty()) {
this.passcode = passcode;
}
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/


@Override
public String toString() {
return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", passcode=" + passcode + ", account="
+ account + ", age=" + age + ", customerId=" + customerId + ", toString()=" + super.toString() + "]";
}



}

------


import java.util.ArrayList;
import java.util.Date;
public class Account {

private String accountNumber;
private double balance;
private boolean active;
protected ArrayList<String>transactions;



/**
* Default constructor to create Account objects
*/
public Account() {
super();
transactions = new ArrayList<String>();

}

/**
* Overloaded Account constructor
* 
* @param accountNumber
*            to set the accountNumber field
* @param balance
*            to set the balance field
*/
public Account(String accountNumber, double balance) {
super();

setAccountNumber(accountNumber);
setBalance(balance);
transactions = new ArrayList<String>();
active = true;
}



public void setAccountNumber(String accountNumber) {
if (accountNumber != null) {
this.accountNumber = accountNumber;
}
}



/**
* accessor for the accountNumber field
* 
* @return the accountNumber as a String
*/
public String getAccountNumber() {

return accountNumber;
}

/**
* accessor for the balance field
* 
* @return the balance as a double
*/
public double getBalance() {
return balance;
}

/**
* accessor for the active field
* 
* @return the active boolean
*/
public boolean isActive() {
return active;
}

/**
* mutator fir the balance field
* 
* @param balance
*            the balance to set
*/
public void setBalance(double balance) {
if(balance >= 0){
this.balance = balance;

}
}

/**
* mutator for the active field
* 
* @param active
*            the active to set
*/
public void setActive(boolean active) {
this.active = active;
}   

/**
* Adds to an Account balance
* 
* @param amount
*            a double to add to the existing balance field
*/
public void addToBalance(double amount) {

if (amount > 0) {
balance += amount;
addTransactionInfo(String.format("%s - deposit: $%.2f", new Date(), amount));
}
}

/**
* Subtracts from an Account balance
* 
* @param amount
*            a double to subtract from the balance field
*/
public void subtractFromBalance(double amount) {

if (amount > 0 && balance - amount > 0) {
balance -= amount;
addTransactionInfo(String.format("%s - deposit: $%.2f", new Date(), amount));

}
}



/**
* method to add to transaction information 
* @param transaction
*/
public void addTransactionInfo(String addTransaction) {
if (addTransaction != null) {
transactions.add(addTransaction);
}
}

/**
* Method to display the transactions
*/
public void displayAccountRecords() {

if (transactions.size() > 0) {
for (String transacts : transactions) {

System.out.println(transacts);
}
}
}

@Override
public String toString() {
return "Account [accountNumber=" + accountNumber + ", balance=" + balance + ", active=" + active
+ ", transactions=" + transactions + "]";
}

------

import java.util.Date;

public class ChequingAccount extends Account {

private final double FEE = 5.0;
private int numberOfCheques;

public ChequingAccount() {
super();

}
public ChequingAccount(String accountNumber, double balance, int numberOfCheques) {
super(accountNumber, balance);
setNumberOfCheques(numberOfCheques);

}

public int getNumberOfCheques() {
return numberOfCheques;
}

public void setNumberOfCheques(int numberOfCheques) {
if (numberOfCheques >= 0) {
this.numberOfCheques = numberOfCheques;
}
}

public double getFEE() {
return FEE;
}

/**
* Adds to an Account balance
* 
* @param amount
*            a double to add to the existing balance field
*/
public void addToBalance(double amount) {

if (amount > 0) {
super.addToBalance(amount);
addTransactionInfo(new Date() + " + Deposit: $" + amount);
}
}

/**
* Subtracts from an Account balance
* 
* @param amount
*            a double to subtract from the balance field
*/
public void subtractFromBalance(double amount) {

if (super.getBalance() - amount - FEE > 0) {
super.subtractFromBalance(amount + FEE);
}else if (super.getBalance() - amount - FEE < 0) {
if (super.getBalance() - FEE > 0) {
super.subtractFromBalance(0+FEE);
}
}

}




@Override
public String toString() {
return "ChequingAccount [FEE=" + FEE + ", numberOfCheques=" + numberOfCheques + ", toString()="
+ super.toString() + "]";
}






}


-------

基本上,您需要做的是,Client类应该有一个Set,用于保存客户端的帐户详细信息。

public class Client {
....
private fields
....
private Set<Account> clientAccount;

}

地图看起来是这样的:

Map<Integer, Client> 

或者更优选地使用随机生成的散列客户端ID,[就像我们在区块链世界中一样]

Map<String, client>

一旦你有了这种安排,你就可以很容易地获得你想要的任何cliend ID的账户。

希望能有所帮助。。

最新更新