Advertisement
inferno719

Java code so far (edition 2)

May 3rd, 2011
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.82 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() + "(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.                 System.out.println("Function Deposit");
  115.                 cust.deposit();
  116.             }
  117.             else if (loop.equals("W"))
  118.             {
  119.                 System.out.println("Function Withdraw");
  120.                 cust.withdraw();
  121.             }
  122.             else if (loop.equals("C"))
  123.             {
  124.                 System.out.println("Function Check");
  125.                 cust.checkBalance();
  126.             }
  127.             else if (loop.equals("Q"))
  128.             {
  129.                 System.out.println("Quit");
  130.                 //REMOVE THIS ONCE OTHER FUNCTIONALITY TAKES OVER
  131.                 System.exit(0);
  132.             }    //Exit the program.
  133.             else
  134.                 System.out.println("Input Error");     
  135.         }
  136.     }
  137.    
  138.    
  139.  
  140.    
  141.  
  142.  
  143.     public static void quit()
  144.  
  145.    
  146.     //Date and time.
  147.     private static String getDateTime()
  148.     {
  149.         Calendar cal = Calendar.getInstance();
  150.         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  151.         return sdf.format(cal.getTime());
  152.    }
  153.         public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm\n";
  154. }
  155.  
  156.  
  157.  
  158. abstract class Customer
  159. {
  160. /*
  161. CHECKLIST
  162. [x]data fields name pin balance
  163. [x]3 accessor methods (return) getName() returns <i>name</i>
  164. [x]1 constructor method, name pin balance
  165. [x]4 abstract (deposit ... ... exit)
  166. */
  167.     //Declare variables.
  168.     String name = "";
  169.     int pin = 0;
  170.     double balance = 0.0;
  171.     //Constructor
  172.     public Customer(String n, int p, double b)
  173.     {
  174.         name = n;
  175.         pin = p;
  176.         balance = b;
  177.     }
  178.    
  179.     public Customer()
  180.     {
  181.         name = "";
  182.         pin = 0;
  183.         balance = 0.0;
  184.     }
  185.    
  186.     //Accessors
  187.     public String getName()
  188.     {
  189.         return name;
  190.     }
  191.     public int getPin()
  192.     {
  193.         return pin;
  194.     }
  195.     public double getBalance()
  196.     {
  197.         return balance;
  198.     }
  199.    
  200.     //These don't do anything until they get inherited.  
  201.     //deposit(), withdraw(), checkBalance(), and quit().
  202.     abstract public void deposit();
  203.  
  204.     abstract public void withdraw();
  205.  
  206.     abstract public void checkBalance();
  207.  
  208.     abstract public void quit();
  209.  
  210. }
  211.  
  212. class CheckingAccountCustomer extends Customer
  213. {
  214. /*
  215. CHECKLIST
  216. [x]Data field?  accountNo
  217. [ ]return getAccountNo method
  218. [x]constructor name pin balance accountNo
  219. [ ]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.
  220. [ ]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.
  221. [ ]checkBalance: This method just prints the current balance as of the current date/time to the screen.
  222. [ ]quit: duh.  System.exit(0);
  223.     */
  224.     int accountNo = 0;
  225.    
  226.     //Constructor
  227.     public CheckingAccountCustomer(String n, int p, double b, int a)
  228.     {
  229.         super(n, p, b);
  230.         accountNo = a;
  231.     }
  232.    
  233.     public void deposit()
  234.     {
  235.         String input3 = JOptionPane.showInputDialog("How much are you depositing?  ");
  236.         double convert3 = Double.parseDouble(input3);
  237.         balance = balance + convert3;
  238.         System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  239.     }
  240.    
  241.     public void withdraw()
  242.     {
  243.         String input4 = JOptionPane.showInputDialog("How much are you withdrawing?  ");
  244.         double convert4 = Double.parseDouble(input4);
  245.         balance = balance - convert4;
  246.         System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  247.     }
  248.    
  249.     public void checkBalance()
  250.     {
  251.         System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  252.     }
  253.    
  254.     public void quit()
  255.     {
  256.         System.exit(0);
  257.     }
  258.    
  259.    
  260.    
  261.    
  262.         //Date and time.
  263.     private static String getDateTime()
  264.     {
  265.         Calendar cal = Calendar.getInstance();
  266.         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  267.         return sdf.format(cal.getTime());
  268.    }
  269.         public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm\n";
  270.    
  271. //Possible reference but probably all garbage
  272.     /*
  273.     boolean active = true;
  274.     public SavingsAccount(double balance, double interest_rate) //constructor
  275.     {
  276.         super.BankAccount(balance, interest_rate);
  277.     }
  278.     public void withdraw(double withdrawal)
  279.     {
  280.         if (active == false) //Check if active.  If not, its a no go.
  281.             System.out.println("The account is inactive, because the balance is below $25.");
  282.         else // If it is active, do the withdrawal through the superclass _
  283.               // and check if its below 25 to make it inactive
  284.             super.withdraw(withdrawal, num_wd);
  285.             if (balance < 25)
  286.                 active = false;
  287.         System.out.println("Your new balance is: " + balance);
  288.     }
  289.     *//*
  290.     public void deposit(double deposit)
  291.     {
  292.         if (active == false)
  293.             if (balance + deposit >= 25)
  294.                 active = true;
  295.         super.deposit(deposit, num_wd);
  296.         System.out.println("Your new balance is: " + balance);
  297.     }
  298.     *//*
  299.     public void monthlyProcess()
  300.     {
  301.         if (num_wd > 4)
  302.             service_charge = service_charge + num_wd - 4;
  303.         super.monthlyProcess(num_dep, num_wd);
  304.        
  305.         if (balance < 25)
  306.             active = false;
  307.         System.out.println("Your new balance is: " + balance);
  308.     }
  309.     */
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement