Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:一个HashMap,其中包含一个键的ID和一个客户端作为值,需要访问客户端';s账户_Java_Hashmap - Fatal编程技术网

Java:一个HashMap,其中包含一个键的ID和一个客户端作为值,需要访问客户端';s账户

Java:一个HashMap,其中包含一个键的ID和一个客户端作为值,需要访问客户端';s账户,java,hashmap,Java,Hashmap,我有一个HashMap,它保存一个键的ID和一个客户端作为值。客户有一个或多个银行账户。我有一个单独的Account类,其中包含帐户的详细信息。我的客户机类可以访问Account类,这些帐户是从驱动程序类中的构造函数创建的。我想访问客户的帐户集合,但每次尝试访问时,只返回一个帐户,而不能访问其中的任何或所有帐户。我在driver类中创建帐户,并将每个帐户设置为客户端。你知道我如何访问任何或所有帐户吗 import java.util.ArrayList; public

我有一个HashMap,它保存一个键的ID和一个客户端作为值。客户有一个或多个银行账户。我有一个单独的Account类,其中包含帐户的详细信息。我的客户机类可以访问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.ArrayList;
公共级自动取款机{
专用输入阅读器;
私有字符串accountNumber;
私有字符串密码;
私有布尔定制;
私有字符串customerId;
私人银行;
私人银行;
私人客户;
高级私人期末考试成绩=65;
/**
*默认构造函数。调用initialize()方法为银行添加种子
*调用run()方法来执行主程序
*功能。
*/
公共ATM机(){
超级();
初始化();
run();
}
/**
*Main方法调用类的默认构造函数。
*@param args用于程序参数(未使用)
*/
公共静态void main(字符串[]args){
新ATM();
}
/**
*主应用程序处理器。所有应用程序功能都被调用
*从这里开始。使用循环提示用户执行银行交易。
*必须使用开关/案例选择来确定用途选择。
*/
公开募捐{
reader=新的InputReader();
布尔退出=假;
System.out.println(“欢迎来到布尔文克尔银行”);
当(!退出){
System.out.println(“选择以下选项之一:”);
System.out.println(“1-登录”);
System.out.println(“2-存款”);
System.out.println(“3-撤回”);
System.out.println(“4-显示帐户信息”);
System.out.println(“5-退出”);
系统输出打印(“>”);
int choice=reader.getIntInput();
开关(选择){
案例1:
选择=1;
验证客户();
打破
案例2:
选择=2;
交易存款();
打破
案例3:
选择=3;
transact-draw();
打破
案例4:
选择=4;
displayAccountInformation();
打破
案例5:
选择=5;
系统输出
.println(“感谢您在Bullwinkle银行的银行业务”);
系统输出
.println(“调试:显示银行中的所有帐户”);
Bank.displayAllCustomers();
系统出口(0);
}
}
}
/**
*通过调用重载构造函数创建客户对象。
*通过调用重载构造函数创建帐户对象。
*为客户设置帐户。
*将客户引用添加到Back HashMap作为测试的种子数据。
*/
公共无效初始化(){
theBank=新银行();
客户myia=新客户(“myia”、“Dog”、“456”、“123”和“65”);
if(myia.getAge()>=高级){
ArrayList accounts=新的ArrayList();
添加(新储蓄账户(“SA-123”,0.0));
添加(新的黄金账户(“GL-123”,0.0,.01,假));
对于(int j=0;j    
    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() + "]";
        }
        
        
        
        
        
    
    }
    
    
    -------

public class Client {
   ....
   private fields
   ....
   private Set<Account> clientAccount;
 
}
Map<Integer, Client> 
Map<String, client>