InputReader.java
//package rex;
import java.util.Scanner;
/**
* Class InputReader reads user input from the keyboard.
*/
/**
* @version 2016.01.20
* @author Bullwinkle Moose
*/
public class InputReader {
private Scanner scanner;
/**
* Default constructor for InputReader class
* Create a new InputReader to read user input.
*/
public InputReader() {
scanner = new Scanner(System.in);
}
/**
* Retrieves a user's int input
* @return the user's input as an int
*/
public int getIntInput() {
try {
return scanner.nextInt();
} catch (java.util.InputMismatchException e) {
System.out.println("Not a number - treating as zero");
scanner.nextLine(); // clear the buffer
return 0;
}
}
/**
* Retrieves a user's double input
* @return the user's input as an double
*/
public double getDoubleInput() {
try {
return scanner.nextDouble();
} catch (java.util.InputMismatchException e) {
System.out.println("Not a number - treating as zero");
scanner.nextLine(); // clear the buffer
return 0.0;
}
}
/**
* Retrieves a user's String input
* @return the user's input as an String
*/
public String getStringInput() {
String input = scanner.next();
if (input.length() > 0) {
return input;
} else {
System.out.println("ERROR:Invalid input provided");
}
return null;
}
/*
public static void main(String[] args) {
InputReader rex = new InputReader();
System.out.println(rex.getStringInput());
}
*/
}
code example
Sunday, February 26, 2017
BankCustomer.java
BankCustomer.java
/**
* @author rex
* @version 1.0
* @date Feb 7 2017
* @param firstName, String to initialize the firstName field
* @param lastName, String to initialize the lastName field
* @param accountNumber, String to initialize the accountNumber field
* @param passcode, String to initialize the passcode field
* @param balance, a double to hold the balance of the account
*/
public class BankCustomer {
public java.lang.String firstName;
public java.lang.String lastName;
public java.lang.String accountNumber;
public java.lang.String passcode;
public double balance;
public InputReader scanner;
/**
* default constructor of BankCustomer class
* @param scanner, object created to obtain keyboard entry
*/
public BankCustomer() {
scanner = new InputReader();
}
/**
* overload constructor of BankCustomer class
*
* @param firstName, String to initialize the firstName field
* @param lastName, String to initialize the lastName field
* @param accountNumber, String to initialize the accountNumber field
* @param passcode, String to initialize the passcode field
*/
public BankCustomer(java.lang.String firstName, java.lang.String lastName,
java.lang.String accountNumber, java.lang.String passcode) {
this.firstName = firstName;
this.lastName = lastName;
this.accountNumber = accountNumber;
this.passcode = passcode;
}
/**
* Adds to a BankCustomer's balance
* @param amount, a double to add to the existing balance field
*/
public void addToBalance(double amount) {
this.balance += amount;
}
/**
* Accessor method for the the balance field
* @return the balance as a double
*/
public double getBalance() {
return this.balance;
}
/**
* Mutator for the accountNumber field
* @param accountNumber, the accountNumber to set
*/
public void setAccountNumber(java.lang.String accountNumber) {
this.accountNumber = accountNumber;
}
/**
* Mutator for the the balance field
* @param balance, the balance to set
*/
public void setBalance(double balance) {
this.balance = balance;
}
public void setFirstname(java.lang.String firstName) {
this.firstName = firstName;
}
/**
* Mutator for the lastName field
* @param lastName, the lastName to set
*/
public void setLastname(java.lang.String lastName) {
this.lastName = lastName;
}
/**
* Mutator for the passcode field
* @param passcode, the passcode to set
*/
public void setPasscode(java.lang.String passcode) {
this.passcode = passcode;
}
/**
* Subtracts from a BankCustomer's balance
* @param amount, a double to subtract from the balance field
*/
public void subtractFromBalance(double amount) {
this.balance -= amount;
}
/**
* Accessor method for the firstName field
* @return firstName as a String
*/
public java.lang.String getFirstName() {
return firstName;
}
/**
* Accessor method for the lastName
* @return the lastName as a String
*/
public java.lang.String getLastName() {
return lastName;
}
/**
* Accessor method for the accountNumber field
*
* @return the accountNumber as a String
*/
public java.lang.String getAccountNumber() {
return accountNumber;
}
/**
* Accessor method for the passcode field
*
* @return the passcode as a String
*/
public java.lang.String getPasscode() {
return passcode;
}
/**
* overides toString in class java.lang.Object
*/
public java.lang.String toString() {
return String.format("first name=%s, last name=%s,"
+ "account number=%s, passcode=%s, balance=%s",
firstName, lastName, accountNumber, passcode, balance);
}
}
/**
* @author rex
* @version 1.0
* @date Feb 7 2017
* @param firstName, String to initialize the firstName field
* @param lastName, String to initialize the lastName field
* @param accountNumber, String to initialize the accountNumber field
* @param passcode, String to initialize the passcode field
* @param balance, a double to hold the balance of the account
*/
public class BankCustomer {
public java.lang.String firstName;
public java.lang.String lastName;
public java.lang.String accountNumber;
public java.lang.String passcode;
public double balance;
public InputReader scanner;
/**
* default constructor of BankCustomer class
* @param scanner, object created to obtain keyboard entry
*/
public BankCustomer() {
scanner = new InputReader();
}
/**
* overload constructor of BankCustomer class
*
* @param firstName, String to initialize the firstName field
* @param lastName, String to initialize the lastName field
* @param accountNumber, String to initialize the accountNumber field
* @param passcode, String to initialize the passcode field
*/
public BankCustomer(java.lang.String firstName, java.lang.String lastName,
java.lang.String accountNumber, java.lang.String passcode) {
this.firstName = firstName;
this.lastName = lastName;
this.accountNumber = accountNumber;
this.passcode = passcode;
}
/**
* Adds to a BankCustomer's balance
* @param amount, a double to add to the existing balance field
*/
public void addToBalance(double amount) {
this.balance += amount;
}
/**
* Accessor method for the the balance field
* @return the balance as a double
*/
public double getBalance() {
return this.balance;
}
/**
* Mutator for the accountNumber field
* @param accountNumber, the accountNumber to set
*/
public void setAccountNumber(java.lang.String accountNumber) {
this.accountNumber = accountNumber;
}
/**
* Mutator for the the balance field
* @param balance, the balance to set
*/
public void setBalance(double balance) {
this.balance = balance;
}
public void setFirstname(java.lang.String firstName) {
this.firstName = firstName;
}
/**
* Mutator for the lastName field
* @param lastName, the lastName to set
*/
public void setLastname(java.lang.String lastName) {
this.lastName = lastName;
}
/**
* Mutator for the passcode field
* @param passcode, the passcode to set
*/
public void setPasscode(java.lang.String passcode) {
this.passcode = passcode;
}
/**
* Subtracts from a BankCustomer's balance
* @param amount, a double to subtract from the balance field
*/
public void subtractFromBalance(double amount) {
this.balance -= amount;
}
/**
* Accessor method for the firstName field
* @return firstName as a String
*/
public java.lang.String getFirstName() {
return firstName;
}
/**
* Accessor method for the lastName
* @return the lastName as a String
*/
public java.lang.String getLastName() {
return lastName;
}
/**
* Accessor method for the accountNumber field
*
* @return the accountNumber as a String
*/
public java.lang.String getAccountNumber() {
return accountNumber;
}
/**
* Accessor method for the passcode field
*
* @return the passcode as a String
*/
public java.lang.String getPasscode() {
return passcode;
}
/**
* overides toString in class java.lang.Object
*/
public java.lang.String toString() {
return String.format("first name=%s, last name=%s,"
+ "account number=%s, passcode=%s, balance=%s",
firstName, lastName, accountNumber, passcode, balance);
}
}
Bank.java
Bank.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/**
*
* @author rex
* @version 1.0
* @date Feb 5 2017
*
*/
public class Bank {
public static HashMap<String, BankCustomer> theBank;
public InputReader scanner;
/**
* Default constructor for Bank class. Initializes the HashMap
*/
public Bank() {
theBank = new HashMap<String, BankCustomer>();
scanner = new InputReader();
}
/**
* Removes an BankCustomer from the HashMap.
*
* @param accountNumber, The key of the element to remove from the HashMap.
*/
public void closeAccount(String accountNumber) {
theBank.remove(accountNumber);
}
/**
* Add a new BankCustomer element to the HashMap.
*
* @param newCustomer, The new element to add to the HashpMap using the
* account number as the key and the new BankCustomer as the value.
*/
public void createAccount(BankCustomer newCustomer) {
String userAccountNumber = newCustomer.getAccountNumber();
theBank.put(userAccountNumber, newCustomer);
}
/**
* Gets the BankCustomer from the HashMap and adds a double amount to a
* BankCustomer's balance.
*
* @param accountNumber, The account number of the BankCustomer.
* @param amount, The amount to deposit.
*/
public void deposit(java.lang.String accountNumber, double amount) {
theBank.get(accountNumber).addToBalance(amount);
}
/**
* Displays the details of a BankCustomer element in the HshMap. Uses
* BankCustomer.toString() implementation.
*
* @param customer, the BankCustomer chosen to display.
*/
public static void displayCustomerInformation(BankCustomer customer) {
ArrayList array = new ArrayList();
array.add(customer.toString());
System.out.println(array.toString());
}
public BankCustomer getCustomerDetail(String customer) {
return theBank.get(customer);
}
/**
* Displays all elements of the HashMap by using BankCustomer.toString()
* implementation of each.
*
*/
public static void displayAllCustomers() {
Set<String> keys = theBank.keySet();
for (String item : keys) {
System.out.println(theBank.get(item).toString());
}
}
/**
* Gets the BankCustomer from the HashMap and subtracts an amount from a
* BankCustomer's balance as long as it does not leave a negative balance.
*
*
* @param accountNumber, The account number of the BankCustomer.
* @param amout, The amount to subtract from a BankCustomer's balance.
*/
public void withdraw(java.lang.String accountNumber, double amount) {
double balance = theBank.get(accountNumber).getBalance();
if ((balance - amount) < 0) {
System.out.println("Invalid Transaction: This will result in a negative balance: "+ balance);
} else {
double newBalance = balance - amount;
theBank.get(accountNumber).setBalance(newBalance);
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/**
*
* @author rex
* @version 1.0
* @date Feb 5 2017
*
*/
public class Bank {
public static HashMap<String, BankCustomer> theBank;
public InputReader scanner;
/**
* Default constructor for Bank class. Initializes the HashMap
*/
public Bank() {
theBank = new HashMap<String, BankCustomer>();
scanner = new InputReader();
}
/**
* Removes an BankCustomer from the HashMap.
*
* @param accountNumber, The key of the element to remove from the HashMap.
*/
public void closeAccount(String accountNumber) {
theBank.remove(accountNumber);
}
/**
* Add a new BankCustomer element to the HashMap.
*
* @param newCustomer, The new element to add to the HashpMap using the
* account number as the key and the new BankCustomer as the value.
*/
public void createAccount(BankCustomer newCustomer) {
String userAccountNumber = newCustomer.getAccountNumber();
theBank.put(userAccountNumber, newCustomer);
}
/**
* Gets the BankCustomer from the HashMap and adds a double amount to a
* BankCustomer's balance.
*
* @param accountNumber, The account number of the BankCustomer.
* @param amount, The amount to deposit.
*/
public void deposit(java.lang.String accountNumber, double amount) {
theBank.get(accountNumber).addToBalance(amount);
}
/**
* Displays the details of a BankCustomer element in the HshMap. Uses
* BankCustomer.toString() implementation.
*
* @param customer, the BankCustomer chosen to display.
*/
public static void displayCustomerInformation(BankCustomer customer) {
ArrayList array = new ArrayList();
array.add(customer.toString());
System.out.println(array.toString());
}
public BankCustomer getCustomerDetail(String customer) {
return theBank.get(customer);
}
/**
* Displays all elements of the HashMap by using BankCustomer.toString()
* implementation of each.
*
*/
public static void displayAllCustomers() {
Set<String> keys = theBank.keySet();
for (String item : keys) {
System.out.println(theBank.get(item).toString());
}
}
/**
* Gets the BankCustomer from the HashMap and subtracts an amount from a
* BankCustomer's balance as long as it does not leave a negative balance.
*
*
* @param accountNumber, The account number of the BankCustomer.
* @param amout, The amount to subtract from a BankCustomer's balance.
*/
public void withdraw(java.lang.String accountNumber, double amount) {
double balance = theBank.get(accountNumber).getBalance();
if ((balance - amount) < 0) {
System.out.println("Invalid Transaction: This will result in a negative balance: "+ balance);
} else {
double newBalance = balance - amount;
theBank.get(accountNumber).setBalance(newBalance);
}
}
}
ATM.java
ATM.java
/**
* @author rex
* @version 1.0
* @date Feb 8 2017
* @bool loggedIn, boolean for the state of user log in
* @param accountNumber, String, a placeholder initialize for test purpose
*/
public class ATM {
private InputReader scanner;
Bank firstBank;
boolean loggedIn = false;
String accountNumber;
/**
* default constructor. Calls the initialize() method to seed the Bank with
* some BankCustomers. Calls the run() method to perform the primary program
* functions.
*
*/
public ATM() {
scanner = new InputReader();
initialize();
}
/*
public ATM(InputReader scanner) {
super();
this.scanner = scanner;
}
*/
/**
* Displays a BankCustomer's account information if the customer has been
* previously verified.
*
* @param customer
*/
public void displayAccountInformation(BankCustomer customer) {
BankCustomer temp = firstBank.getCustomerDetail(accountNumber);
System.out.println(temp);
}
/**
* Adds Customer references to the Bank HashMap as seed data for testing.
*/
public void initialize() {
firstBank = new Bank();
BankCustomer dog = new BankCustomer("Darby", "Dog", "ST-123", "123");
BankCustomer cat = new BankCustomer("Freckle", "Cat", "ST-780", "789");
BankCustomer dog2 = new BankCustomer("Myia", "Dog", "ST-456", "456");
firstBank.createAccount(dog);
firstBank.createAccount(cat);
firstBank.createAccount(dog2);
System.out.println("Just for verification, "
+ "This are all the customers in the list");
firstBank.displayAllCustomers();
}
/**
* The primary application processor. All application functions are called
* from here. Uses a loop to prompt users to perform banking transactions.
* Must use switch/case selection to determine uses choices.
*/
public void run() {
int choice;
//char quit = 'n';
boolean quit = true;
System.out.println("\n\n");
if (!loggedIn) {
System.out.println("Welcome to Bullwinkle's Bank");
}
System.out.println("Choose one of the following options:\n"
+ "1 - Sign In\n"
+ "2 - Deposit\n"
+ "3 - Withdraw\n"
+ "4 - Display Account\n"
+ "5 - Exit");
while (quit) {
choice = scanner.getIntInput();
if (choice < 1 || choice > 5) {
choice = 0;
}
switch (choice) {
case 0:
System.out.println("ERROR: Choose from 1 ~ 5");
break;
case 1:
verifyCustomer();
break;
case 2:
transactDeposit();
break;
case 3:
transactWithdraw();
break;
case 4:
displayAccountInfo();
break;
case 5:
System.out.println("DEBUG: Displaying all the accounts"
+ " in the bank.");
listAllCustomers();
quit = false;
loggedIn = false;
break;
}
}
}
public void displayAccountInfo() {
if (!loggedIn) {
System.out.println("ERROR: You must login before your"
+ " perform a transaction");
run();
return;
}
firstBank.displayCustomerInformation(firstBank.getCustomerDetail(accountNumber));
run();
}
public void listAllCustomers()
{
System.out.println("Thank you for banking at BullWinkle's Bank");
Bank.displayAllCustomers();
}
/**
* Performs a deposit into a BankCustomer's account. Checks to see if the
* user has signed in. If not, then verifyCustomer() is called and the menu
* is displayed again.
*/
public void transactDeposit() {
if (!loggedIn) {
System.out.println("ERROR: You must login before "
+ "your perform a transaction");
run();
return;
}
System.out.println("Enter Amount to deposit");
double deposit = scanner.getDoubleInput();
firstBank.deposit(accountNumber, deposit);
run();
}
/**
* To confirm a BankCustomer's account number and passcode. Called when the
* user is required to sign in to the application. Will set a boolean so the
* user does not have to sign in again during the session.
*/
public void verifyCustomer() {
System.out.println("Enter Account Number");
String account = scanner.getStringInput();
System.out.println("Enter Passcode");
String password = scanner.getStringInput();
BankCustomer auth = firstBank.getCustomerDetail(account);
System.out.println("\n\n");
if (auth.getPasscode().equals(password)) {
System.out.println("<!---- Welcome to Bullwinkle's Bank ---> Dear " + auth.getFirstName());
loggedIn = true;
accountNumber = account;
} else {
System.out.println("ERROR: Either account number or password is not correct");
}
run();
}
/**
* Performs a withdrawal from a BankCustomer's account. Checks to see if the
* user has signed in. If not, then verifyCustomer() is called and the menu
* is displayed again.
*/
public void transactWithdraw() {
if (!loggedIn) {
System.out.println("ERROR: You must login before your perform a transaction");
run();
return;
}
System.out.println("Enter the amount to withdraw");
double amount = scanner.getDoubleInput();
firstBank.withdraw(accountNumber, amount);
run();
}
/**
* Main method calls the class default constructor.
*
* @param args, for program arguments (not used)
*/
public static void main(java.lang.String[] args) {
ATM test = new ATM();
test.run();
}
}
/**
* @author rex
* @version 1.0
* @date Feb 8 2017
* @bool loggedIn, boolean for the state of user log in
* @param accountNumber, String, a placeholder initialize for test purpose
*/
public class ATM {
private InputReader scanner;
Bank firstBank;
boolean loggedIn = false;
String accountNumber;
/**
* default constructor. Calls the initialize() method to seed the Bank with
* some BankCustomers. Calls the run() method to perform the primary program
* functions.
*
*/
public ATM() {
scanner = new InputReader();
initialize();
}
/*
public ATM(InputReader scanner) {
super();
this.scanner = scanner;
}
*/
/**
* Displays a BankCustomer's account information if the customer has been
* previously verified.
*
* @param customer
*/
public void displayAccountInformation(BankCustomer customer) {
BankCustomer temp = firstBank.getCustomerDetail(accountNumber);
System.out.println(temp);
}
/**
* Adds Customer references to the Bank HashMap as seed data for testing.
*/
public void initialize() {
firstBank = new Bank();
BankCustomer dog = new BankCustomer("Darby", "Dog", "ST-123", "123");
BankCustomer cat = new BankCustomer("Freckle", "Cat", "ST-780", "789");
BankCustomer dog2 = new BankCustomer("Myia", "Dog", "ST-456", "456");
firstBank.createAccount(dog);
firstBank.createAccount(cat);
firstBank.createAccount(dog2);
System.out.println("Just for verification, "
+ "This are all the customers in the list");
firstBank.displayAllCustomers();
}
/**
* The primary application processor. All application functions are called
* from here. Uses a loop to prompt users to perform banking transactions.
* Must use switch/case selection to determine uses choices.
*/
public void run() {
int choice;
//char quit = 'n';
boolean quit = true;
System.out.println("\n\n");
if (!loggedIn) {
System.out.println("Welcome to Bullwinkle's Bank");
}
System.out.println("Choose one of the following options:\n"
+ "1 - Sign In\n"
+ "2 - Deposit\n"
+ "3 - Withdraw\n"
+ "4 - Display Account\n"
+ "5 - Exit");
while (quit) {
choice = scanner.getIntInput();
if (choice < 1 || choice > 5) {
choice = 0;
}
switch (choice) {
case 0:
System.out.println("ERROR: Choose from 1 ~ 5");
break;
case 1:
verifyCustomer();
break;
case 2:
transactDeposit();
break;
case 3:
transactWithdraw();
break;
case 4:
displayAccountInfo();
break;
case 5:
System.out.println("DEBUG: Displaying all the accounts"
+ " in the bank.");
listAllCustomers();
quit = false;
loggedIn = false;
break;
}
}
}
public void displayAccountInfo() {
if (!loggedIn) {
System.out.println("ERROR: You must login before your"
+ " perform a transaction");
run();
return;
}
firstBank.displayCustomerInformation(firstBank.getCustomerDetail(accountNumber));
run();
}
public void listAllCustomers()
{
System.out.println("Thank you for banking at BullWinkle's Bank");
Bank.displayAllCustomers();
}
/**
* Performs a deposit into a BankCustomer's account. Checks to see if the
* user has signed in. If not, then verifyCustomer() is called and the menu
* is displayed again.
*/
public void transactDeposit() {
if (!loggedIn) {
System.out.println("ERROR: You must login before "
+ "your perform a transaction");
run();
return;
}
System.out.println("Enter Amount to deposit");
double deposit = scanner.getDoubleInput();
firstBank.deposit(accountNumber, deposit);
run();
}
/**
* To confirm a BankCustomer's account number and passcode. Called when the
* user is required to sign in to the application. Will set a boolean so the
* user does not have to sign in again during the session.
*/
public void verifyCustomer() {
System.out.println("Enter Account Number");
String account = scanner.getStringInput();
System.out.println("Enter Passcode");
String password = scanner.getStringInput();
BankCustomer auth = firstBank.getCustomerDetail(account);
System.out.println("\n\n");
if (auth.getPasscode().equals(password)) {
System.out.println("<!---- Welcome to Bullwinkle's Bank ---> Dear " + auth.getFirstName());
loggedIn = true;
accountNumber = account;
} else {
System.out.println("ERROR: Either account number or password is not correct");
}
run();
}
/**
* Performs a withdrawal from a BankCustomer's account. Checks to see if the
* user has signed in. If not, then verifyCustomer() is called and the menu
* is displayed again.
*/
public void transactWithdraw() {
if (!loggedIn) {
System.out.println("ERROR: You must login before your perform a transaction");
run();
return;
}
System.out.println("Enter the amount to withdraw");
double amount = scanner.getDoubleInput();
firstBank.withdraw(accountNumber, amount);
run();
}
/**
* Main method calls the class default constructor.
*
* @param args, for program arguments (not used)
*/
public static void main(java.lang.String[] args) {
ATM test = new ATM();
test.run();
}
}
Subscribe to:
Posts (Atom)