Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.80 KB | None | 0 0
  1. AccountAtBank.JAVA***********************************
  2. import java.util.*;
  3.  
  4. /**
  5.  * AccountAtBank – The class AccountAtBank represents simplification of a bank account like one you would see at any financial institution.
  6.  *
  7.  * @author: A. Gutierrez
  8.  * @version 2007.02.18
  9.  */
  10.  
  11. public class AccountAtBank
  12. {
  13.   // Fields of this class
  14.  
  15. // Interest rate
  16.  
  17. private static  double m_dCurrentInterestRate;
  18.  
  19. private static int m_nNextAccountNumber;
  20.  
  21. //  The current account balance
  22.  
  23.    private double m_dBalance;
  24.  
  25. // The customer name
  26.  
  27.    private String customer;
  28.  
  29. // The customer Account number
  30.  
  31.   private int custAccountNumber;
  32.  
  33. // Constructors
  34.  
  35.      public AccountAtBank (String customer, double dInitialDeposit)
  36.          {
  37.             this.customer = new String(customer.toUpperCase());
  38.             m_dBalance = dInitialDeposit;
  39.            
  40.            
  41.          }
  42.  
  43.        public AccountAtBank(String customer)
  44.           {
  45.             this.customer = new String(customer.toUpperCase());
  46.             m_dBalance = 0.0;
  47.                      
  48.          }
  49.  
  50. // Methods
  51.  
  52. //  Mutator Methods
  53.  
  54. // Setting a new rate for the account:
  55.  
  56. public static void rate(double newRate)
  57. {
  58. // Check for a good interest rate
  59.  if (newRate > 0.0 && newRate < 20.0)
  60.    {
  61.       m_dCurrentInterestRate = newRate;
  62.     }
  63.      else
  64.          {
  65.            System.out.println(newRate + " is is not a valid interest rate");
  66.           }
  67.    }
  68.  
  69. //  Depositing on the account
  70.  
  71. public void  newDeposit(double amount)
  72. {
  73.     // Checking first for a good deposit
  74.  
  75.     if ( amount > 0)
  76.       {
  77.         m_dBalance += amount;
  78.        }
  79.      else
  80.          {
  81.            System.out.println(" Your deposit should be positive");
  82.           }
  83.   }
  84.  
  85. // Withdrawing from an account
  86.  
  87. public void newWithdrawal(double withdraw)
  88. {
  89.    // Checking first for a good withdraw
  90.  
  91.     if ( withdraw > 0)
  92.       {
  93.         // Checking that this amount can be withdrawn
  94.  
  95.          if (m_dBalance >= withdraw)
  96.             {
  97.                 m_dBalance -= withdraw;
  98.               }
  99.            else
  100.              {
  101.                 System.out.println(" You don’t have enough money");            
  102.              }
  103.         }
  104.      else
  105.          {
  106.            System.out.println(" Your withdraw should be positive");
  107.           }
  108.   }
  109.  
  110.    public void monthlyChanges()
  111.  {
  112.      monthlyFee();
  113.      monthlyInterest();
  114.    }
  115.  
  116. // Private methods to implement the monthlyChanges
  117.  
  118. private void monthlyFee()
  119.    {
  120.       m_dBalance += 5.0;    // $5.00 monthly maintenance fee
  121.     }
  122.  
  123. private void  monthlyInterest()
  124.     {
  125.        m_dBalance +=   m_dBalance*( m_dCurrentInterestRate/1200.0);
  126.  
  127. // Try to figure out why 1200.0
  128.      }
  129.  
  130.  public static void putInitialAccountNumber(int newNumber)
  131.     {
  132.      m_nNextAccountNumber = newNumber;
  133.     }
  134.    
  135.  public void insertAccountNumber()
  136.    {
  137.       custAccountNumber = m_nNextAccountNumber;
  138.       m_nNextAccountNumber++;
  139.    }
  140.  
  141. //   Accessor Methods
  142.  
  143. public static double rate()
  144.  
  145. {
  146.    return  m_dCurrentInterestRate;
  147. }
  148.  
  149. public double getBalance()
  150.  {
  151.  //  We have here a private variable nCents
  152.  
  153.    int nCents = (int) (  m_dBalance*100 +0.5);
  154.    return nCents/100.0;
  155. }
  156.  public int getAccountNumber()
  157.  {
  158.   return custAccountNumber;
  159.   }
  160.  public String getCustomer()
  161.   {
  162.   return customer;
  163.   }
  164.   public static int getNextAccountNumber()
  165.  {
  166.   return m_nNextAccountNumber;
  167.   }
  168. }  // End the class definition of AccountAtBank
  169.  
  170. Bank.JAVA***********************************
  171.  
  172. import java.util.*;
  173.  
  174. /**
  175.  * Bank is a collection of AccountAtBank objects.
  176.  * It uses the ArrayList class, so be sure to import the java.util.ArrayList, or the java.util.* package
  177.  
  178.  *
  179.  * @author: A. Gutierrez
  180.  * @version 2007.02.07
  181.  */
  182.  
  183. public class Bank
  184. {
  185.  // Storage for an arbitrary number of AccountAtBank objects
  186.  
  187.     private ArrayList<AccountAtBank> bankAccounts;
  188.  
  189.  // Storage for an arbitrary number of Client objects
  190.  
  191.    private ArrayList<Client> clients;
  192.  
  193.  
  194.   public Scanner myScanner = new Scanner(System.in);
  195.  
  196.   // Constructor
  197.  
  198.    public Bank()
  199.   {
  200.     bankAccounts = new ArrayList<AccountAtBank>();
  201.     clients = new ArrayList<Client>();
  202.    }
  203.  
  204.   // Mutators
  205.    
  206.     public static void initializeBank()
  207.  
  208.    {// Setting the interest rate for the customers in the bank
  209.      AccountAtBank.rate(5);
  210.  
  211.   // Setting the nex Account number in the bank
  212.    AccountAtBank.putInitialAccountNumber(1);
  213.    }
  214.  
  215.  public void storeBankAccount(AccountAtBank nAccount)
  216.     {
  217.    /* Check that the account does not exist, before inserting it into the
  218.     * ArrayList.
  219.     */
  220.       boolean exists = existName(nAccount);
  221.       if (exists)
  222.           System.out.println("This client already has an account");
  223.       else
  224.         {
  225.         // If the client does not exist, include it, after inserting account number
  226.          nAccount.insertAccountNumber();
  227.          bankAccounts.add(nAccount);
  228.         // Because this is a new client, increment the next account number
  229.       }  
  230.     }
  231.  
  232.     public void storeClient(Client nClient)
  233.     {
  234.        clients.add(nClient);
  235.     }
  236.  
  237.  
  238.     public void removeBankAccount(int accountNumber)
  239.     {
  240.      AccountAtBank accountToDelete = retrieveAccount(accountNumber);
  241.      String custName = accountToDelete.getCustomer();
  242.      System.out.println("Is the account of " + custName +" the one you want to delete?");
  243.      System.out.println("Please enter y/n.");
  244.      System.out.print(">");
  245.      
  246.       //Gets the next String of data from the keyboard up to a "\n" and stores it in name
  247.       String answer = myScanner.nextLine();
  248.       // Convert the answer to upper case to establish a canonical form for the answer
  249.       answer = new String(answer.trim().toUpperCase());
  250.          if (answer.equals("Y"))
  251.             {
  252.               bankAccounts.remove(accountToDelete);
  253.             }
  254.           else
  255.             {
  256.               System.out.println("You should verify the account you want to delete");
  257.             }
  258.       }
  259.      
  260.    // Accessors
  261.  
  262.    //  For the bank accounts
  263.  
  264.    public int numberOfAccounts()
  265.    {
  266.     return bankAccounts.size();
  267.    }
  268.    
  269.    public boolean existName(AccountAtBank nAccount)
  270.    {
  271.       Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
  272.      boolean found = false;
  273.      AccountAtBank checkAccount;
  274.      String nName = nAccount.getCustomer();
  275.      while (iterAccounts.hasNext() && !found)
  276.        {
  277.          checkAccount = iterAccounts.next();
  278.         if (checkAccount.getCustomer().equals(nName) )
  279.             found = true;
  280.         }
  281.      return found;  
  282.    }
  283.  
  284.  
  285. public void showAccount(int accountNumber)
  286.    {
  287.     AccountAtBank accountToShow =  retrieveAccount(accountNumber);
  288.     if (accountToShow != null)
  289.     {
  290.       System.out.println("The account belong to " + accountToShow.getCustomer());
  291.      System.out.println("The balance in the account is $" + accountToShow.getBalance());
  292.     }
  293.     else
  294.     {
  295.      System.out.print ("The account number " + accountNumber);
  296.      System.out.println(" is not a valid account number");
  297.      }
  298.    } // End of showAccount
  299.  
  300. public AccountAtBank retrieveAccount(int accountNumber)
  301.  {
  302.    Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
  303.    boolean found = false;
  304.      AccountAtBank checkAccount = null;
  305.      while (iterAccounts.hasNext() && !found)
  306.        {
  307.          checkAccount = iterAccounts.next();
  308.         if (checkAccount.getAccountNumber() == accountNumber )
  309.             found = true;
  310.         }
  311.      if (found)
  312.         return checkAccount;
  313.      else
  314.         return null;
  315.    }
  316. public Client retrieveClient(String custName, int custAccount)
  317. {
  318.  Iterator<Client> iterClients =  clients.iterator();
  319.  boolean found = false;
  320.  Client checkClient = null;
  321.  while (iterClients.hasNext() && !found)
  322.    {
  323.     checkClient = iterClients.next();
  324.     if(checkClient.getOwner().equals(custName) &&
  325.           (checkClient.getAccountNumber() == custAccount))
  326.               {
  327.                found = true;
  328.               }
  329.    }
  330.   if(found)
  331.     return checkClient;
  332.   else
  333.     return null;
  334. }
  335.  
  336.  
  337.   public AccountAtBank getBankAccount(String custName)
  338.     {
  339.      Iterator<AccountAtBank> iterAccounts = bankAccounts.iterator();
  340.     boolean found = false;
  341.      AccountAtBank checkAccount = null;
  342.      while (iterAccounts.hasNext() && !found)
  343.        {
  344.          checkAccount = iterAccounts.next();
  345.          if (checkAccount.getCustomer().equals(custName) )
  346.             found = true;
  347.         }
  348.      if (found)
  349.         return checkAccount;
  350.      else
  351.         return null;
  352.    } // End of getBankAccount
  353.  
  354.   } // End of Bank class
  355. CLIENT.JAVA***********************************
  356.  
  357. /*
  358. Now we define the class of owner of the Account at the Bank
  359. **/
  360.  
  361. public class Client
  362. {
  363.   // First field is the name of the client
  364.    private String owner;
  365.  
  366.   // Second field is the amount to be deposited each month
  367.  
  368.    private double monDeposit;
  369.  
  370.   // Third field is the account number assigned to his/her account
  371.    private int accountNumber;
  372.  
  373. // Constructors
  374.   public Client(String name)
  375.   {
  376.     owner = new String(name);
  377.     accountNumber = 0;
  378.     monDeposit = 0.0;
  379.    }
  380.  
  381.   public Client(String name, double deposit)
  382.   {
  383.     owner = new String(name);
  384.     accountNumber = 0;
  385.     monDeposit = deposit;
  386.    }
  387.  
  388.   // Mutator
  389.  
  390.   public void putAccountNumber(int myNumber)
  391.   {
  392.     accountNumber = myNumber;
  393.    }
  394.   public void  newDeposit(double newDepo)
  395.   {
  396.        monDeposit = newDepo;
  397.    }
  398.  
  399.   // Accessors
  400.  
  401.    public String getOwner()
  402.    {
  403.      return owner;
  404.     }
  405.   public int getAccountNumber()
  406.   {
  407.    return accountNumber;
  408.   }
  409.   public double getMDeposit()
  410.   {
  411.    return monDeposit;
  412.    }
  413. } // End of the Client class
  414.  
  415. RUNNINGABANK.JAVA***********************************
  416.    
  417. /**
  418.  * RunningABank is a program to create, remove, display accounts.
  419.  *
  420.  * @author: A. Gutierrez
  421.  * @version 2007.02.28
  422.  */
  423.  
  424. import java.util.*;
  425.  
  426. public class RunningABank
  427. {
  428. private static Scanner myInputs = new Scanner(System.in);
  429.  // Creating the ArrayLists !!
  430. private static Bank fedBank = new Bank();
  431.  
  432.   public static void main(String args[])
  433.   {
  434.     // Set the rate for the bank, and the next account number
  435.  
  436.    fedBank.initializeBank();
  437.  
  438.     // Menu
  439.     int choice = 0;
  440.  
  441.    while (choice != 4)
  442.    {  
  443.      System.out.println();
  444.      System.out.println("Choose the number for your option:");
  445.      System.out.println();
  446.      System.out.println("To create an account: ..........1");
  447.      System.out.println();
  448.      System.out.println("To remove an account: ..........2");
  449.      System.out.println();
  450.      System.out.println("To display an account: .........3");
  451.      System.out.println();
  452.      System.out.println("To finish: .....................4");
  453.      System.out.println();
  454.      System.out.println("Please enter your choice.");
  455.      System.out.print(">");
  456.      choice = myInputs.nextInt();
  457.      myInputs.nextLine();
  458.      switch (choice)
  459.      {
  460.       case 1:  createAccount();
  461.                break;
  462.       case 2:  removeAccount();
  463.                break;
  464.       case 3:  displayAccount();
  465.                break;
  466.      case 4:  break;
  467.       default: System.out.print("Your choice should be a number between");
  468.                System.out.println(" 1 and 4. You entered " + choice);
  469.       } // End switch
  470.      } // End while
  471.     } // End main
  472.  
  473.      public static void createAccount()
  474.      {
  475.       String name = getCustomerName("create");
  476.       double initialDeposit = -1;
  477.       do
  478.       {
  479.         System.out.print("Enter the initial deposit on the account:  ");
  480.  
  481.         initialDeposit = myInputs.nextDouble();
  482.         if (initialDeposit < 0.0)
  483.           {
  484.            System.out.println("The Deposit should be a positive number. Try again");
  485.            System.out.println();
  486.            }
  487.  
  488.        } while (initialDeposit < 0.0);
  489.  
  490.       System.out.println();
  491.       AccountAtBank nBankAccount = new AccountAtBank(name, initialDeposit);
  492.       Client nClient = new Client(name,initialDeposit);
  493.       nClient.putAccountNumber(nBankAccount.getAccountNumber());
  494.       fedBank.storeBankAccount(nBankAccount);
  495.       fedBank.storeClient(nClient);
  496.       }
  497.  
  498.       public static void removeAccount()
  499.        {
  500.         String name = getCustomerName("remove");
  501.         AccountAtBank accountToRemove = fedBank.getBankAccount(name);
  502.          if (accountToRemove != null)
  503.          {
  504.          int clientAccount = accountToRemove.getAccountNumber();
  505.    //       System.out.println("The account you want to remove has number "+clientAccount);
  506.           System.out.println();
  507.          fedBank.removeBankAccount(clientAccount);
  508.          }
  509.         else
  510.           System.out.println("We could not find the account of " + name);  
  511.        } // End of removeAccount
  512.  
  513.       public static void displayAccount()
  514.       {
  515.         String name = getCustomerName("display");
  516.         AccountAtBank accountToShow = fedBank.getBankAccount(name);
  517.         if (accountToShow != null)
  518.          {
  519.           int clientAccount = accountToShow.getAccountNumber();
  520.      //     System.out.println("The account you want to display has number "+ clientAccount);
  521.           System.out.println();
  522.          fedBank.showAccount(clientAccount);
  523.          }
  524.         else
  525.           System.out.println(name +" does not have an account" );  
  526.        } //. End of displayAccount
  527.      
  528.       public static String getCustomerName(String action)
  529.       {
  530.         System.out.println();
  531.         System.out.print("Enter the name of the customer whose account");
  532.         System.out.print(" you want to " + action+ ":  ");
  533.         String name;
  534.        do
  535.          {
  536.            name = myInputs.nextLine();
  537.            name = new String(name.trim().toUpperCase());
  538.          }
  539.         while (name.equals(""));
  540.         System.out.println();
  541.         return name;
  542.        }
  543.      
  544.  }  // End of Running a Bank
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement