Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Rob H
- //Date: 05/03/2011
- //Desc: FINAL
- import java.text.DateFormat;
- import java.text.SimpleDateFormat; //remove? no, bugs out.
- import java.util.*;
- import javax.swing.*;
- public class AtmHerman
- {
- public static void main(String[] args)
- {
- int cleared = 0;
- //Have to use two arrays for this.
- int[] acc = {0,11112222, 22223333, 33334444};
- int[] pin = {0,1111, 2222, 3333};
- //Information on accounts
- String[] name = {"0", "Curly Brace", "Adam Sandler", "Kvothe"};
- double[] balance = {0, 100.01, 200.02, 300.03};
- //Ask user for account num "insert card"
- String input1 = JOptionPane.showInputDialog("Please insert card (8 numbers): ");
- int convert1 = Integer.parseInt(input1);
- //Now that we have a number, compare it to acc[]
- int position = 0;
- for (int i = 0; i < 4; i++)
- {
- if (convert1 == acc[i])
- {
- position = i;
- }
- else
- {
- //nothing
- }
- }
- //If position = 0, end program.
- if (position == 0)
- {
- System.out.println("Account number invalid. Please contact your bank.");
- System.exit(0);
- }
- //Ask for pin.
- String input2 = JOptionPane.showInputDialog("Please enter pin (4 numbers): ");
- int convert2 = Integer.parseInt(input2);
- //Loop. If it fails, close the program.
- int Three_Strikes = 0;
- for (int lol = 0; cleared < 1; lol++)
- {
- if (convert2 == pin[position])
- {
- cleared = 1;
- }
- else
- {
- Three_Strikes = Three_Strikes + 1;
- if (Three_Strikes == 3)
- {
- System.exit(0);
- }
- input2=JOptionPane.showInputDialog("PIN does not match. Please try again.");
- convert2 = Integer.parseInt(input2);
- }
- }
- int c = pin[position];
- int d = acc[position];
- //Create the object
- CheckingAccountCustomer cust = new CheckingAccountCustomer(name[position], c, balance[position], d);
- //interface
- String loop = "";
- while (loop != "Q")
- {
- //Greet, give name and date. What do you want to do? Take input here.
- loop = JOptionPane.showInputDialog(AtmHerman.getDateTime() + "\nWelcome " + cust.getName() + ".\n(D)eposit Money\n(W)ithdraw Money\n(C)heck Account Balance\n(Q)uit");
- loop = loop.toUpperCase();
- if (loop.equals("D"))
- {
- cust.deposit();
- }
- else if (loop.equals("W"))
- {
- cust.withdraw();
- }
- else if (loop.equals("C"))
- {
- cust.checkBalance();
- }
- else if (loop.equals("Q"))
- {
- System.out.println("Goodbye!");
- cust.quit();
- } //Exit the program.
- else
- System.out.println("Input Error");
- }
- }
- //Date and time.
- private static String getDateTime()
- {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
- return sdf.format(cal.getTime());
- }
- private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm";
- }
- abstract class Customer
- {
- //Declare variables.
- String name = "";
- int pin = 0;
- double balance = 0.0;
- //Constructor
- 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'
- {
- name = n;
- pin = p;
- balance = b;
- }
- Customer()
- {
- name = "";
- pin = 0;
- balance = 0.0;
- }
- //Accessors
- String getName()
- {
- return name;
- }
- int getPin()
- {
- return pin;
- }
- double getBalance()
- {
- return balance;
- }
- //These don't do anything until they get inherited.
- abstract void deposit(); // no access modifiers, because the desired protection level is between protected and private: thus, the default 'package private'
- abstract void withdraw();
- abstract void checkBalance();
- abstract void quit();
- }
- class CheckingAccountCustomer extends Customer
- {
- int accountNo = 0;
- //Constructor
- 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'
- {
- super(n, p, b);
- accountNo = a;
- }
- void deposit()
- {
- String input3 = JOptionPane.showInputDialog("How much are you depositing? ");
- double convert3 = Double.parseDouble(input3);
- if (convert3 > 0)
- {
- balance = balance + convert3;
- System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
- }
- else
- {
- System.out.println("Invalid entry");
- }
- }
- void withdraw()
- {
- String input4 = JOptionPane.showInputDialog("How much are you withdrawing? ");
- double convert4 = Double.parseDouble(input4);
- if(convert4 > 0)
- {
- balance = balance - convert4;
- if(balance < 0)
- {
- balance = balance + convert4;
- System.out.println("Insufficient funds.");
- }
- else
- {
- System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
- }
- }
- else
- {
- System.out.println("Invalid entry");
- }
- }
- void checkBalance()
- {
- System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance);
- }
- void quit()
- {
- System.exit(0);
- }
- //Date and time.
- private static String getDateTime()
- {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
- return sdf.format(cal.getTime());
- }
- private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement