Advertisement
inferno719

Java finish candidate (edition 3)

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