Advertisement
inferno719

Java finish candidate (edition 3) again

May 3rd, 2011
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.59 KB | None | 0 0
  1. //Name: Rob H
  2. //Date: 05/03/2011
  3. //Desc: FINAL
  4. /*
  5. CHECKLIST
  6. do these in order.
  7. [x] Works!!
  8. [ ] Indentation.
  9. [x] Documentation everywhere.
  10. [ ] Fiddle around with the security for the prefixes
  11. */
  12. import java.util.Date;
  13. import java.text.DateFormat;
  14. import java.text.SimpleDateFormat;
  15. import java.util.*;
  16. import javax.swing.*;
  17.  
  18.  
  19. public class AtmHerman
  20. {
  21. /*
  22. CHECKLIST
  23. [x] array of objects
  24. [x] ask for account number (ask for "debit card") + convert to int.
  25. [x] compare ^ to CheckingAccountCustomer object's accountNo
  26. [x] ask for PIN
  27. [x] compare pin.
  28. [x] 3 chances then quit.   
  29. [x] interface, date + time, d/w/c/q
  30. [x] from this, call the appropriate CheckingAccountCustomer class
  31. [x] loop to let customer perform another action.
  32. [a little] innovation
  33. */
  34.     public static void main(String[] args)
  35.     {
  36.         int cleared = 0;
  37.  
  38.         //Have to use two arrays for this.
  39.         int[] acc = {0,11112222, 22223333, 33334444};
  40.         int[] pin = {0,1111, 2222, 3333};
  41.         //Ask user for account num "insert card"
  42.         String input1 = JOptionPane.showInputDialog("Please insert card (8 numbers): ");
  43.         int convert1 = Integer.parseInt(input1);
  44.         //Now that we have a number, compare it to acc[]
  45.         int match = 0;
  46.         int position = 0;
  47.         for (int i = 0; i < 4; i++)
  48.         {
  49.             if (convert1 == acc[i])
  50.             {
  51.                 position = i;
  52.             }
  53.             else
  54.             {
  55.                 //nothing
  56.             }
  57.         }
  58.         //REMOVE ONCE CONFIRMED WORKING (the two lines below)
  59.         System.out.println("position=" + position + "  acc[position]=" + acc[position]);
  60.         System.out.println("pin should be: " + pin[position]);
  61.         //If position = 0, end program.
  62.         if (position == 0)
  63.         {
  64.             System.out.println("Account number invalid.  Please contact your bank.");
  65.             System.exit(0);
  66.         }
  67.         //Ask for pin.  
  68.         String input2 = JOptionPane.showInputDialog("Please enter pin (4 numbers): ");
  69.         int convert2 = Integer.parseInt(input2);
  70.         //Loop.  If it fails, close the program.  
  71.         int Three_Strikes = 0;
  72.         for (int lol = 0; cleared < 1; lol++) //there's probably a better way to do this.
  73.         {
  74.             if (convert2 == pin[position])
  75.             {
  76.                 cleared = 1;
  77.             }
  78.             else
  79.             {
  80.                 Three_Strikes = Three_Strikes + 1;
  81.                 //REMOVE ONCE CONFIRMED WORKING (the one line below)
  82.                 System.out.println("Three_Strikes=" + Three_Strikes);
  83.                 if (Three_Strikes == 3)
  84.                 {
  85.                     System.exit(0);
  86.                 }
  87.                 input2=JOptionPane.showInputDialog("PIN does not match.  Please try again.");
  88.                 convert2 = Integer.parseInt(input2);
  89.             }
  90.         }
  91.         //REMOVE ONCE CONFIRMED WORKING (the one line below)
  92.         System.out.println("cleared = " + cleared);
  93.        
  94.         //bogus information for every account
  95.         String a = "Curly Brace";
  96.         double b = 100.01;
  97.        
  98.        
  99.        
  100.        
  101.         CheckingAccountCustomer cust = new CheckingAccountCustomer("Curly Brace", 2222, 100.01, 12341234);
  102.  
  103.        
  104.        
  105.         //interface
  106.         String loop = "";
  107.         while (loop != "Q")
  108.         {
  109.             loop = JOptionPane.showInputDialog(AtmHerman.getDateTime() + "\nWelcome " + cust.getName() + ".\n(D)eposit Money\n(W)ithdraw Money\n(C)heck Account Balance\n(Q)uit");
  110.             loop = loop.toUpperCase();
  111.             if (loop.equals("D"))
  112.             {
  113.                 cust.deposit();
  114.             }
  115.             else if (loop.equals("W"))
  116.             {
  117.                 cust.withdraw();
  118.             }
  119.             else if (loop.equals("C"))
  120.             {
  121.                 cust.checkBalance();
  122.             }
  123.             else if (loop.equals("Q"))
  124.             {
  125.                 System.out.println("Goodbye!");
  126.                 cust.quit();
  127.             }    //Exit the program.
  128.             else
  129.                 System.out.println("Input Error");     
  130.         }
  131.     }
  132.    
  133.    
  134.     //Date and time.
  135.     private static String getDateTime()
  136.     {
  137.         Calendar cal = Calendar.getInstance();
  138.         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  139.         return sdf.format(cal.getTime());
  140.    }
  141.         public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm";
  142. }
  143.  
  144.  
  145.  
  146. abstract class Customer
  147. {
  148. /*
  149. CHECKLIST
  150. [x]data fields name pin balance
  151. [x]3 accessor methods (return) getName() returns <i>name</i>
  152. [x]1 constructor method, name pin balance
  153. [x]4 abstract (deposit ... ... exit)
  154. */
  155.     //Declare variables.
  156.     String name = "";
  157.     int pin = 0;
  158.     double balance = 0.0;
  159.     //Constructor
  160.     public Customer(String n, int p, double b)
  161.     {
  162.         name = n;
  163.         pin = p;
  164.         balance = b;
  165.     }
  166.    
  167.     public Customer()
  168.     {
  169.         name = "";
  170.         pin = 0;
  171.         balance = 0.0;
  172.     }
  173.    
  174.     //Accessors
  175.     public String getName()
  176.     {
  177.         return name;
  178.     }
  179.     public int getPin()
  180.     {
  181.         return pin;
  182.     }
  183.     public double getBalance()
  184.     {
  185.         return balance;
  186.     }
  187.    
  188.     //These don't do anything until they get inherited.  
  189.     //deposit(), withdraw(), checkBalance(), and quit().
  190.     abstract public void deposit();
  191.  
  192.     abstract public void withdraw();
  193.  
  194.     abstract public void checkBalance();
  195.  
  196.     abstract public void quit();
  197.  
  198. }
  199.  
  200. class CheckingAccountCustomer extends Customer
  201. {
  202. /*
  203. CHECKLIST
  204. [x]Data field?  accountNo
  205. [x]return getAccountNo method
  206. [x]constructor name pin balance accountNo
  207. [x]deposit: Prompt a customer to enter an amount of money to be deposited into his/her account.  Add this amount to the balance and print the current balance as of the current date/time to the screen.
  208. [x]withdraw: Prompt a customer to enter an amount of money to withdraw from account.  Deduct this amount to the balance and print the current balance as of the current date/time to the screen.
  209. [x]checkBalance: This method just prints the current balance as of the current date/time to the screen.
  210. [x]quit: duh.  System.exit(0);
  211.     */
  212.     int accountNo = 0;
  213.    
  214.     //Constructor
  215.     public CheckingAccountCustomer(String n, int p, double b, int a)
  216.     {
  217.         super(n, p, b);
  218.         accountNo = a;
  219.     }
  220.    
  221.     public void deposit()
  222.     {
  223.         String input3 = JOptionPane.showInputDialog("How much are you depositing?  ");
  224.         double convert3 = Double.parseDouble(input3);
  225.         balance = balance + convert3;
  226.         System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  227.     }
  228.    
  229.     public void withdraw()
  230.     {
  231.         String input4 = JOptionPane.showInputDialog("How much are you withdrawing?  ");
  232.         double convert4 = Double.parseDouble(input4);
  233.         balance = balance - convert4;
  234.         if(balance < 0)
  235.         {
  236.             balance = balance + convert4;
  237.             System.out.println("Insufficient funds.");
  238.         }
  239.         else
  240.         {
  241.             System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  242.         }
  243.     }
  244.    
  245.     public void checkBalance()
  246.     {
  247.         System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  248.     }
  249.    
  250.     public void quit()
  251.     {
  252.         System.exit(0);
  253.     }
  254.    
  255.         //Date and time.
  256.     private static String getDateTime()
  257.     {
  258.         Calendar cal = Calendar.getInstance();
  259.         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  260.         return sdf.format(cal.getTime());
  261.    }
  262.         public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm";
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement