Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AccountAtBank.JAVA***********************************
- import java.util.*;
- /**
- * AccountAtBank β The class AccountAtBank represents simplification of a bank account like one you would see at any financial institution.
- *
- * @author: A. Gutierrez
- * @version 2007.02.18
- */
- public class AccountAtBank
- {
- // Fields of this class
- // Interest rate
- private static double m_dCurrentInterestRate;
- private static int m_nNextAccountNumber;
- // The current account balance
- private double m_dBalance;
- // The customer name
- private String customer;
- // The customer Account number
- private int custAccountNumber;
- // Constructors
- public AccountAtBank (String customer, double dInitialDeposit)
- {
- this.customer = new String(customer.toUpperCase());
- m_dBalance = dInitialDeposit;
- }
- public AccountAtBank(String customer)
- {
- this.customer = new String(customer.toUpperCase());
- m_dBalance = 0.0;
- }
- // Methods
- // Mutator Methods
- // Setting a new rate for the account:
- public static void rate(double newRate)
- {
- // Check for a good interest rate
- if (newRate > 0.0 && newRate < 20.0)
- {
- m_dCurrentInterestRate = newRate;
- }
- else
- {
- System.out.println(newRate + " is is not a valid interest rate");
- }
- }
- // Depositing on the account
- public void newDeposit(double amount)
- {
- // Checking first for a good deposit
- if ( amount > 0)
- {
- m_dBalance += amount;
- }
- else
- {
- System.out.println(" Your deposit should be positive");
- }
- }
- // Withdrawing from an account
- public void newWithdrawal(double withdraw)
- {
- // Checking first for a good withdraw
- if ( withdraw > 0)
- {
- // Checking that this amount can be withdrawn
- if (m_dBalance >= withdraw)
- {
- m_dBalance -= withdraw;
- }
- else
- {
- System.out.println(" You donβt have enough money");
- }
- }
- else
- {
- System.out.println(" Your withdraw should be positive");
- }
- }
- public void monthlyChanges()
- {
- monthlyFee();
- monthlyInterest();
- }
- // Private methods to implement the monthlyChanges
- private void monthlyFee()
- {
- m_dBalance += 5.0; // $5.00 monthly maintenance fee
- }
- private void monthlyInterest()
- {
- m_dBalance += m_dBalance*( m_dCurrentInterestRate/1200.0);
- // Try to figure out why 1200.0
- }
- public static void putInitialAccountNumber(int newNumber)
- {
- m_nNextAccountNumber = newNumber;
- }
- public void insertAccountNumber()
- {
- custAccountNumber = m_nNextAccountNumber;
- m_nNextAccountNumber++;
- }
- // Accessor Methods
- public static double rate()
- {
- return m_dCurrentInterestRate;
- }
- public double getBalance()
- {
- // We have here a private variable nCents
- int nCents = (int) ( m_dBalance*100 +0.5);
- return nCents/100.0;
- }
- public int getAccountNumber()
- {
- return custAccountNumber;
- }
- public String getCustomer()
- {
- return customer;
- }
- public static int getNextAccountNumber()
- {
- return m_nNextAccountNumber;
- }
- } // End the class definition of AccountAtBank
- Bank.JAVA***********************************
- import java.util.*;
- /**
- * Bank is a collection of AccountAtBank objects.
- * It uses the ArrayList class, so be sure to import the java.util.ArrayList, or the java.util.* package
- *
- * @author: A. Gutierrez
- * @version 2007.02.07
- */
- public class Bank
- {
- // Storage for an arbitrary number of AccountAtBank objects
- private ArrayList<AccountAtBank> bankAccounts;
- // Storage for an arbitrary number of Client objects
- private ArrayList<Client> clients;
- public Scanner myScanner = new Scanner(System.in);
- // Constructor
- public Bank()
- {
- bankAccounts = new ArrayList<AccountAtBank>();
- clients = new ArrayList<Client>();
- }
- // Mutators
- public static void initializeBank()
- {// Setting the interest rate for the customers in the bank
- AccountAtBank.rate(5);
- // Setting the nex Account number in the bank
- AccountAtBank.putInitialAccountNumber(1);
- }
- public void storeBankAccount(AccountAtBank nAccount)
- {
- /* Check that the account does not exist, before inserting it into the
- * ArrayList.
- */
- boolean exists = existName(nAccount);
- if (exists)
- System.out.println("This client already has an account");
- else
- {
- // If the client does not exist, include it, after inserting account number
- nAccount.insertAccountNumber();
- bankAccounts.add(nAccount);
- // Because this is a new client, increment the next account number
- }
- }
- public void storeClient(Client nClient)
- {
- clients.add(nClient);
- }
- public void removeBankAccount(int accountNumber)
- {
- AccountAtBank accountToDelete = retrieveAccount(accountNumber);
- String custName = accountToDelete.getCustomer();
- System.out.println("Is the account of " + custName +" the one you want to delete?");
- System.out.println("Please enter y/n.");
- System.out.print(">");
- //Gets the next String of data from the keyboard up to a "\n" and stores it in name
- String answer = myScanner.nextLine();
- // Convert the answer to upper case to establish a canonical form for the answer
- answer = new String(answer.trim().toUpperCase());
- if (answer.equals("Y"))
- {
- bankAccounts.remove(accountToDelete);
- }
- else
- {
- System.out.println("You should verify the account you want to delete");
- }
- }
- // Accessors
- // For the bank accounts
- public int numberOfAccounts()
- {
- return bankAccounts.size();
- }
- public boolean existName(AccountAtBank nAccount)
- {
- Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
- boolean found = false;
- AccountAtBank checkAccount;
- String nName = nAccount.getCustomer();
- while (iterAccounts.hasNext() && !found)
- {
- checkAccount = iterAccounts.next();
- if (checkAccount.getCustomer().equals(nName) )
- found = true;
- }
- return found;
- }
- public void showAccount(int accountNumber)
- {
- AccountAtBank accountToShow = retrieveAccount(accountNumber);
- if (accountToShow != null)
- {
- System.out.println("The account belong to " + accountToShow.getCustomer());
- System.out.println("The balance in the account is $" + accountToShow.getBalance());
- }
- else
- {
- System.out.print ("The account number " + accountNumber);
- System.out.println(" is not a valid account number");
- }
- } // End of showAccount
- public AccountAtBank retrieveAccount(int accountNumber)
- {
- Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
- boolean found = false;
- AccountAtBank checkAccount = null;
- while (iterAccounts.hasNext() && !found)
- {
- checkAccount = iterAccounts.next();
- if (checkAccount.getAccountNumber() == accountNumber )
- found = true;
- }
- if (found)
- return checkAccount;
- else
- return null;
- }
- public Client retrieveClient(String custName, int custAccount)
- {
- Iterator<Client> iterClients = clients.iterator();
- boolean found = false;
- Client checkClient = null;
- while (iterClients.hasNext() && !found)
- {
- checkClient = iterClients.next();
- if(checkClient.getOwner().equals(custName) &&
- (checkClient.getAccountNumber() == custAccount))
- {
- found = true;
- }
- }
- if(found)
- return checkClient;
- else
- return null;
- }
- public AccountAtBank getBankAccount(String custName)
- {
- Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
- boolean found = false;
- AccountAtBank checkAccount = null;
- while (iterAccounts.hasNext() && !found)
- {
- checkAccount = iterAccounts.next();
- if (checkAccount.getCustomer().equals(custName) )
- found = true;
- }
- if (found)
- return checkAccount;
- else
- return null;
- } // End of getBankAccount
- } // End of Bank class
- CLIENT.JAVA***********************************
- /*
- Now we define the class of owner of the Account at the Bank
- **/
- public class Client
- {
- // First field is the name of the client
- private String owner;
- // Second field is the amount to be deposited each month
- private double monDeposit;
- // Third field is the account number assigned to his/her account
- private int accountNumber;
- // Constructors
- public Client(String name)
- {
- owner = new String(name);
- accountNumber = 0;
- monDeposit = 0.0;
- }
- public Client(String name, double deposit)
- {
- owner = new String(name);
- accountNumber = 0;
- monDeposit = deposit;
- }
- // Mutator
- public void putAccountNumber(int myNumber)
- {
- accountNumber = myNumber;
- }
- public void newDeposit(double newDepo)
- {
- monDeposit = newDepo;
- }
- // Accessors
- public String getOwner()
- {
- return owner;
- }
- public int getAccountNumber()
- {
- return accountNumber;
- }
- public double getMDeposit()
- {
- return monDeposit;
- }
- } // End of the Client class
- RUNNINGABANK.JAVA***********************************
- /**
- * RunningABank is a program to create, remove, display accounts.
- *
- * @author: A. Gutierrez
- * @version 2007.02.28
- */
- import java.util.*;
- public class RunningABank
- {
- private static Scanner myInputs = new Scanner(System.in);
- // Creating the ArrayLists !!
- private static Bank fedBank = new Bank();
- public static void main(String args[])
- {
- // Set the rate for the bank, and the next account number
- fedBank.initializeBank();
- // Menu
- int choice = 0;
- while (choice != 4)
- {
- System.out.println();
- System.out.println("Choose the number for your option:");
- System.out.println();
- System.out.println("To create an account: ..........1");
- System.out.println();
- System.out.println("To remove an account: ..........2");
- System.out.println();
- System.out.println("To display an account: .........3");
- System.out.println();
- System.out.println("To finish: .....................4");
- System.out.println();
- System.out.println("Please enter your choice.");
- System.out.print(">");
- choice = myInputs.nextInt();
- myInputs.nextLine();
- switch (choice)
- {
- case 1: createAccount();
- break;
- case 2: removeAccount();
- break;
- case 3: displayAccount();
- break;
- case 4: break;
- default: System.out.print("Your choice should be a number between");
- System.out.println(" 1 and 4. You entered " + choice);
- } // End switch
- } // End while
- } // End main
- public static void createAccount()
- {
- String name = getCustomerName("create");
- double initialDeposit = -1;
- do
- {
- System.out.print("Enter the initial deposit on the account: ");
- initialDeposit = myInputs.nextDouble();
- if (initialDeposit < 0.0)
- {
- System.out.println("The Deposit should be a positive number. Try again");
- System.out.println();
- }
- } while (initialDeposit < 0.0);
- System.out.println();
- AccountAtBank nBankAccount = new AccountAtBank(name, initialDeposit);
- Client nClient = new Client(name,initialDeposit);
- nClient.putAccountNumber(nBankAccount.getAccountNumber());
- fedBank.storeBankAccount(nBankAccount);
- fedBank.storeClient(nClient);
- }
- public static void removeAccount()
- {
- String name = getCustomerName("remove");
- AccountAtBank accountToRemove = fedBank.getBankAccount(name);
- if (accountToRemove != null)
- {
- int clientAccount = accountToRemove.getAccountNumber();
- // System.out.println("The account you want to remove has number "+clientAccount);
- System.out.println();
- fedBank.removeBankAccount(clientAccount);
- }
- else
- System.out.println("We could not find the account of " + name);
- } // End of removeAccount
- public static void displayAccount()
- {
- String name = getCustomerName("display");
- AccountAtBank accountToShow = fedBank.getBankAccount(name);
- if (accountToShow != null)
- {
- int clientAccount = accountToShow.getAccountNumber();
- // System.out.println("The account you want to display has number "+ clientAccount);
- System.out.println();
- fedBank.showAccount(clientAccount);
- }
- else
- System.out.println(name +" does not have an account" );
- } //. End of displayAccount
- public static String getCustomerName(String action)
- {
- System.out.println();
- System.out.print("Enter the name of the customer whose account");
- System.out.print(" you want to " + action+ ": ");
- String name;
- do
- {
- name = myInputs.nextLine();
- name = new String(name.trim().toUpperCase());
- }
- while (name.equals(""));
- System.out.println();
- return name;
- }
- } // End of Running a Bank
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement