Advertisement
inferno719

Java Finished!

May 3rd, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.37 KB | None | 0 0
  1. //Name: Rob H
  2. //Date: 05/03/2011
  3. //Desc: FINAL
  4.  
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;  //remove? no, bugs out.
  7. import java.util.*;
  8. import javax.swing.*;
  9.  
  10.  
  11. public class AtmHerman
  12. {
  13.     public static void main(String[] args)
  14.     {
  15.         int cleared = 0;
  16.  
  17.         //Have to use two arrays for this.
  18.         int[] acc = {0,11112222, 22223333, 33334444};
  19.         int[] pin = {0,1111, 2222, 3333};
  20.        
  21.         //Information on accounts
  22.         String[] name = {"0", "Curly Brace", "Adam Sandler", "Kvothe"};
  23.         double[] balance = {0, 100.01, 200.02, 300.03};
  24.        
  25.         //Ask user for account num "insert card"
  26.         String input1 = JOptionPane.showInputDialog("Please insert card (8 numbers): ");
  27.         int convert1 = Integer.parseInt(input1);
  28.        
  29.         //Now that we have a number, compare it to acc[]
  30.         int position = 0;
  31.         for (int i = 0; i < 4; i++)
  32.         {
  33.             if (convert1 == acc[i])
  34.             {
  35.                 position = i;
  36.             }
  37.             else
  38.             {
  39.                 //nothing
  40.             }
  41.         }
  42.        
  43.         //If position = 0, end program.
  44.         if (position == 0)
  45.         {
  46.             System.out.println("Account number invalid.  Please contact your bank.");
  47.             System.exit(0);
  48.         }
  49.        
  50.         //Ask for pin.  
  51.         String input2 = JOptionPane.showInputDialog("Please enter pin (4 numbers): ");
  52.         int convert2 = Integer.parseInt(input2);
  53.        
  54.         //Loop.  If it fails, close the program.  
  55.         int Three_Strikes = 0;
  56.         for (int lol = 0; cleared < 1; lol++)
  57.         {
  58.             if (convert2 == pin[position])
  59.             {
  60.                 cleared = 1;
  61.             }
  62.             else
  63.             {
  64.                 Three_Strikes = Three_Strikes + 1;
  65.  
  66.                 if (Three_Strikes == 3)
  67.                 {
  68.                     System.exit(0);
  69.                 }
  70.                 input2=JOptionPane.showInputDialog("PIN does not match.  Please try again.");
  71.                 convert2 = Integer.parseInt(input2);
  72.             }
  73.         }
  74.        
  75.  
  76.         int c = pin[position];
  77.         int d = acc[position];
  78.        
  79.         //Create the object
  80.         CheckingAccountCustomer cust = new CheckingAccountCustomer(name[position], c, balance[position], d);
  81.  
  82.         //interface
  83.         String loop = "";
  84.         while (loop != "Q")
  85.         {
  86.             //Greet, give name and date.  What do you want to do?  Take input here.
  87.             loop = JOptionPane.showInputDialog(AtmHerman.getDateTime() + "\nWelcome " + cust.getName() + ".\n(D)eposit Money\n(W)ithdraw Money\n(C)heck Account Balance\n(Q)uit");
  88.             loop = loop.toUpperCase();
  89.             if (loop.equals("D"))
  90.             {
  91.                 cust.deposit();
  92.             }
  93.             else if (loop.equals("W"))
  94.             {
  95.                 cust.withdraw();
  96.             }
  97.             else if (loop.equals("C"))
  98.             {
  99.                 cust.checkBalance();
  100.             }
  101.             else if (loop.equals("Q"))
  102.             {
  103.                 System.out.println("Goodbye!");
  104.                 cust.quit();
  105.             }    //Exit the program.
  106.             else
  107.                 System.out.println("Input Error");     
  108.         }
  109.     }
  110.    
  111.     //Date and time.
  112.     private static String getDateTime()
  113.     {
  114.         Calendar cal = Calendar.getInstance();
  115.         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  116.         return sdf.format(cal.getTime());
  117.    }
  118.         private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm";
  119. }
  120.  
  121.  
  122. abstract class Customer
  123. {
  124.     //Declare variables.
  125.     String name = "";
  126.     int pin = 0;
  127.     double balance = 0.0;
  128.     //Constructor
  129.     Customer(String n, int p, double b) // no access modifiers, because the desired protection level is between protected and private: thus, the default 'package private'
  130.     {
  131.         name = n;
  132.         pin = p;
  133.         balance = b;
  134.     }
  135.    
  136.      Customer()
  137.     {
  138.         name = "";
  139.         pin = 0;
  140.         balance = 0.0;
  141.     }
  142.    
  143.     //Accessors
  144.      String getName()
  145.     {
  146.         return name;
  147.     }
  148.      int getPin()
  149.     {
  150.         return pin;
  151.     }
  152.      double getBalance()
  153.     {
  154.         return balance;
  155.     }
  156.    
  157.     //These don't do anything until they get inherited.  
  158.     abstract void deposit(); // no access modifiers, because the desired protection level is between protected and private: thus, the default 'package private'
  159.  
  160.     abstract void withdraw();
  161.  
  162.     abstract void checkBalance();
  163.  
  164.     abstract void quit();
  165.  
  166. }
  167.  
  168. class CheckingAccountCustomer extends Customer
  169. {
  170.     int accountNo = 0;
  171.    
  172.     //Constructor
  173.     CheckingAccountCustomer(String n, int p, double b, int a)// no access modifiers, because the desired protection level is between protected and private: thus, the default 'package private'
  174.     {
  175.         super(n, p, b);
  176.         accountNo = a;
  177.     }
  178.    
  179.     void deposit()
  180.     {
  181.         String input3 = JOptionPane.showInputDialog("How much are you depositing?  ");
  182.         double convert3 = Double.parseDouble(input3);
  183.         if (convert3 > 0)
  184.         {
  185.             balance = balance + convert3;
  186.             System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  187.         }
  188.         else
  189.         {
  190.             System.out.println("Invalid entry");
  191.         }
  192.     }
  193.    
  194.     void withdraw()
  195.     {
  196.         String input4 = JOptionPane.showInputDialog("How much are you withdrawing?  ");
  197.         double convert4 = Double.parseDouble(input4);
  198.         if(convert4 > 0)
  199.         {
  200.             balance = balance - convert4;
  201.             if(balance < 0)
  202.             {
  203.                 balance = balance + convert4;
  204.                 System.out.println("Insufficient funds.");
  205.             }
  206.             else
  207.             {
  208.                 System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  209.             }
  210.         }
  211.         else
  212.         {
  213.             System.out.println("Invalid entry");
  214.         }
  215.     }
  216.    
  217.     void checkBalance()
  218.     {
  219.         System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
  220.     }
  221.    
  222.     void quit()
  223.     {
  224.         System.exit(0);
  225.     }
  226.    
  227.         //Date and time.
  228.     private static String getDateTime()
  229.     {
  230.         Calendar cal = Calendar.getInstance();
  231.         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  232.         return sdf.format(cal.getTime());
  233.    }
  234.         private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm";
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement