Guest User

Debug Assignment (0/1)

a guest
Jul 29th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.27 KB | None | 0 0
  1. /* This program allows the user to create an account (provided the
  2.    bank has space), deposit money in the account, and
  3.  withdraw money from the account. The bank has only space for
  4.  five accounts, and each account holder's name must be unique.  
  5. */
  6.  
  7. import javax.swing.*; // Imported to use JOptionPane
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. public class SimpleAccountTeller
  12. {
  13.   public static boolean hasCustomer(SimpleAccount[] bank, String bap)
  14.   {
  15.     boolean customerExists = false;// No customer exists with given name.  
  16.   for(int index = 0; index < bank.length; index++)
  17.   {
  18.   if(bank[index]!= null && bap.equalsIgnoreCase(bank[index].getOwner()))
  19.   {
  20.     customerExists = true;  
  21.   }
  22.   }
  23.   return customerExists; // Return user's status.
  24.   }
  25.  
  26.   /* This method returns true if the bank is at full capacity
  27.      and false if the banl is not full and new customers can be added.
  28.   */
  29.   public static boolean bankIsFull(SimpleAccount[] bank)
  30.   {
  31.     boolean full = true;
  32.     // Searches the bank for any available space in the form of
  33.   // null values.  
  34.   for(SimpleAccount bap: bank)
  35.   {
  36.     if(bap == null)
  37.   {  
  38.     full = false;
  39.   }
  40.   }
  41.   return full;// Returns the true/false status of bank's capacity.
  42.   }
  43.  
  44.   /* This method adds customers to the bank if there is space
  45.      and if the customer does not already exist.
  46.   */
  47.   public static void addCustomer(SimpleAccount[] bank, SimpleAccount newCus)
  48.   {
  49.   for(int index = 0; index < bank.length; index++)
  50.   {
  51.     if(bank[index]== null && !hasCustomer(bank, newCus.getOwner()))
  52.     {
  53.     bank[index] = newCus;
  54.   }
  55.   }
  56.   }
  57.  
  58.   /* This method returns the index of a customer's account
  59.      in the bank array.
  60.   */
  61.   public static int getCustomerIndex(SimpleAccount[] bank, String bap)
  62.   {  
  63.   int iCustomer = 0; // Initializes customer index to 0.
  64.   // Searches the entire array, finds the location of the bank account,
  65.   // and resets the customer's acount index to the found index.  
  66.   for(int index = 0; index < bank.length; index++)
  67.   {
  68.   if(bank[index]!= null && bap.equalsIgnoreCase(bank[index].getOwner()))
  69.   {
  70.     iCustomer = index;
  71.   }
  72.   }
  73.   return iCustomer;// Returns the customer's account index
  74.   }
  75.  
  76.   // Saves customer data in customers array to file.
  77.   public static void save(SimpleAccount[] bank, String givenFile)throws IOException
  78.   {  
  79.     String file = givenFile;
  80.   FileWriter outfile = new FileWriter(file);
  81.   PrintWriter out = new PrintWriter(outfile);
  82.   for(SimpleAccount a : bank)
  83.   {
  84.     if(a != null)  
  85.   {
  86.     out.println(a.getOwner() + "," + a.getBalance());
  87.   }
  88.   }
  89.   out.close();
  90.   }
  91.  
  92.   /* Reads customer data from file to start bank session and returns array of
  93.      BankAccountP... objects    */
  94.   public static SimpleAccount[] startUp(SimpleAccount[] bank,
  95.                                         String givenFile)throws IOException
  96.   {
  97.       String file = givenFile;
  98.   String content = "", givenName = "";
  99.   double givenDeposit = 0;
  100.   int space = -1; // index of space between owner name and balance
  101.   SimpleAccount[] newBank = new SimpleAccount[bank.length];
  102.  
  103.   // Catch IO errors when opening file
  104.   try
  105.   {
  106.     // if the file is found do this block of code
  107.     FileReader freader = new FileReader(file);
  108.     Scanner fileScanner = new Scanner(freader);
  109.  
  110.       int i =0;
  111.     // Create array of BankAccountP... objects from file data
  112.     // (What if there is no file?)
  113.     while(fileScanner.hasNext() && i < bank.length)
  114.       {
  115.       content = fileScanner.nextLine();
  116.       space = content.indexOf(",");
  117.       givenName = content.substring(0, space);
  118.       givenDeposit = Double.parseDouble(content.substring(space+1));
  119.       SimpleAccount b = new SimpleAccount(givenName, givenDeposit);
  120.       newBank[i] = b;
  121.         i++;
  122.     }
  123.    }
  124.    catch(IOException e)
  125.    {
  126.      JOptionPane.showMessageDialog(null, "Bank Now Open");
  127.    }
  128.   return newBank;
  129.   }
  130.  
  131.   // main method: Creates Bank (which = array of customers)
  132.   public static void main(String[] args)
  133.   {
  134.     final int BANK_SIZE = 5; // Maximum bank size  
  135.   SimpleAccount[] customers
  136.      = new SimpleAccount[BANK_SIZE]; //Initializes maximum bank
  137.                   //size to 5 customers
  138.   String response = ""; // Holds the user's input
  139.   String name = ""; // Holds the user's name
  140.   double iDeposit = 0.0; // Holds user's deposit amount
  141.   double iWithdraw = 0.0; // Holds customer's withdraw amount
  142.   int iTemp = 0; // Holds index of customer's account
  143.   String menu = "Press o to open an account.\n" +
  144.        "Press d to deposit.\n" +
  145.       "Press w to withdraw.\n" +
  146.       "Press q to exit.\n"; // The menu display
  147.   //Shows the menu and takes in input as long as the user does not quit.
  148.   do
  149.   {
  150.   for(int j = 0; j < customers.length; j++)
  151.   {
  152.     try
  153.     {
  154.       customers[j] = startUp(customers, "CustomerData.txt")[j];
  155.     }
  156.     catch(IOException e)
  157.     {
  158.      // Do nothing, just continue with program
  159.     }
  160.   }
  161.  
  162.   response = JOptionPane.showInputDialog(menu);
  163.  
  164.   // If the user chooses to open an account
  165.   if(response.equalsIgnoreCase("O"))
  166.   {
  167.     // If the bank is already full
  168.     if(bankIsFull(customers))
  169.     {
  170.       JOptionPane.showMessageDialog(null,"Unfortunately, the bank has already\n"
  171.           + " reached the maximum number of accounts allowed.");
  172.     }
  173.  
  174.     // Else, if the bank has available account space
  175.     else
  176.     {
  177.       // Gets the user's name and intial deposit amount  
  178.     name = JOptionPane.showInputDialog("Please input your name:");
  179.       iDeposit = Double.parseDouble(JOptionPane.showInputDialog
  180.                                      ("Initial Deposit:"));
  181.    
  182.     // If the deposit is positive and the customer does not
  183.     // already have an account
  184.       if(iDeposit > 0 && !hasCustomer(customers, name))
  185.       {
  186.         // Creates a new bank account,
  187.     // shows the customer's account status,
  188.     // and adds the account to the bank.    
  189.     SimpleAccount nAccount = new SimpleAccount(name, iDeposit);
  190.       JOptionPane.showMessageDialog(null, nAccount);
  191.       addCustomer(customers, nAccount);
  192.     }
  193.    
  194.       // If the deposit is 0 and
  195.       else if (iDeposit == 0 && !hasCustomer(customers, name))
  196.       {
  197.         SimpleAccount nAccount = new SimpleAccount(name);
  198.       addCustomer(customers, nAccount);
  199.       }
  200.       else
  201.       {
  202.         JOptionPane.showMessageDialog(null, "Cannot deposit: "
  203.              + "Negative deposit or no account!");
  204.       }
  205.     }  
  206.   }// End of 'O'
  207.  
  208.   // If user chooses to make a deposit
  209.   else if(response.equalsIgnoreCase("D"))
  210.   {
  211.     // Gets the user's name
  212.     name = JOptionPane.showInputDialog("Please input your name:");
  213.    
  214.     // If the user is a customer
  215.     if(hasCustomer(customers, name))
  216.     {
  217.       // asks how much the user wants to deposit.  
  218.     iDeposit = Double.parseDouble(JOptionPane.showInputDialog("How much " +
  219.                    "would you like to deposit?"));
  220.     //If the deposit is greater than 0.
  221.     if(iDeposit > 0)
  222.     {
  223.       // adjusts the customer's account and displays the status.  
  224.     iTemp = getCustomerIndex(customers, name);
  225.       customers[iTemp].deposit(iDeposit); customers[iTemp].incrementDeposits();
  226.       JOptionPane.showMessageDialog(null, customers[iTemp]);
  227.     }
  228.    
  229.     // If the deposit is not greater than 0
  230.     else
  231.     {
  232.       JOptionPane.showMessageDialog(null, "Cannot deposit.");
  233.     }
  234.     }
  235.     // If the user in not a customer
  236.     else
  237.     {
  238.       String noAccount = "Error! You do not have an account with us.\n" +
  239.            " Press ok to return to the menu and open an account.";  
  240.     JOptionPane.showMessageDialog(null, noAccount);
  241.     }
  242.   }
  243.  
  244.   // If the user chooses to withdraw money from an account
  245.   else if(response.equalsIgnoreCase("W"))
  246.   {
  247.     // gets the user's name.
  248.     name = JOptionPane.showInputDialog("Please input your name:");
  249.      
  250.     // If the user is a customer
  251.     if(hasCustomer(customers, name))
  252.     {
  253.       // gets withdraw amount,  
  254.     iWithdraw = Double.parseDouble(JOptionPane.showInputDialog("How much "
  255.                      + "would you like to withdraw?"));
  256.       iTemp = getCustomerIndex(customers, name);
  257.    
  258.     // checks the withdraw amount, adjusts the customer's account.
  259.     if(iWithdraw >= 0 && iWithdraw <= customers[iTemp].getBalance())
  260.     {  
  261.     customers[iTemp].withdraw(iWithdraw); customers[iTemp].incrementWithdrawals();
  262.       JOptionPane.showMessageDialog(null, customers[iTemp]);
  263.     }
  264.    
  265.     // if an overdraft will occur or the withdraw amount is negative
  266.     else
  267.     {
  268.       JOptionPane.showMessageDialog(null, "Overdraft/Negative Withdraw Warning!\n" +
  269.               "Cannot withdraw amount.");
  270.     }
  271.     }
  272.    
  273.     // If the user does not have an account with the bank
  274.     else
  275.     {
  276.       String noAccount = "Error! You do not have an account with us.\n" +
  277.            " Press ok to return to the menu and open an account.";  
  278.     JOptionPane.showMessageDialog(null, noAccount);
  279.     }
  280.   }
  281.   }
  282.   while(!response.equalsIgnoreCase("Q"));// Repeat cycle until the user quits.
  283.  
  284.   // Save the data from this session to a text file
  285.   try
  286.   {
  287.     save(customers, "CustomerData.txt");
  288.   }
  289.   catch (IOException e)
  290.   {
  291.     JOptionPane.showMessageDialog(null, "File not Found.");
  292.   }
  293.  
  294.   System.exit(0);
  295.   }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment