Skip to content

Instantly share code, notes, and snippets.

@Joseph-M
Created May 2, 2013 22:13
Show Gist options
  • Save Joseph-M/5505886 to your computer and use it in GitHub Desktop.
Save Joseph-M/5505886 to your computer and use it in GitHub Desktop.
Implementation of a pseudo bank ATM machine.
package assignment_04;
import java.util.ArrayList;
import java.util.Date;
// General account class
class Account {
int id;
double balance;
Date dateCreated = new Date();
ArrayList transactionLog = new ArrayList();
ArrayList transferLog = new ArrayList();
// Constructors
public Account() {
id = 0;
balance = 0;
}
public Account(int i, double b) {
id = i;
balance = b;
}
// Accessor and mutator methods
public int getId() {
return id;
}
public void setId(int x) {
id = x;
}
public double getBalance() {
return balance;
}
public void setBalance(double x) {
balance = x;
}
public Date getDateCreated() {
return dateCreated;
}
// Account methods
public void withdraw(double x) {
String str;
if (x <= 0) {
str = "Incorrect value entered. Withdrawal unsuccessful.";
}
else if (balance < x) {
str = "Insufficient funds. Withdrawal unsuccessful.";
}
else {
balance -= x;
str = String.format("Withdrawal of $%,.2f successful.", x);
}
System.out.println(str);
addEntry(transactionLog, str);
}
public void deposit(double x) {
String str;
if (x <= 0) {
str = "Incorrect value entered. Deposit unsuccessful.";
}
else {
balance += x;
str = String.format("Deposit of $%,.2f successful.", x);
}
System.out.println(str);
addEntry(transactionLog, str);
}
// Transfer methods
public void transferTo(Account acct, double x) {
String str;
if (x <= 0)
str = "Incorrect value entered. Transfer is unsuccessful.";
else if (this.balance < x)
str = "Insufficient funds. Transfer is unsuccessful.";
else {
this.balance -= x;
acct.balance += x;
str = String.format("Transfer of $%,.2f successful.", x);
}
System.out.println(str);
addEntry(transferLog, str);
}
public void transferFrom(Account acct, double x) {
String str;
if (x <= 0)
str = "Incorrect value entered. Transfer is unsuccessful.";
else if (acct.balance < x)
str = "Insufficient funds. Transfer is unsuccessful.";
else {
acct.balance -= x;
this.balance += x;
str = String.format("Transfer of $%,.2f successful.", x);
}
System.out.println(str);
addEntry(transferLog, str);
}
// Log methods
public void addEntry(ArrayList a, String str) {
a.add(new Date() + ": " + str);
}
public void printStatement(ArrayList a) {
if (a.isEmpty())
System.out.println("Log is empty.");
else {
for (int i = 0; i < a.size(); i++)
System.out.println(a.get(i));
}
}
}
// Class for savings accounts
class SavingsAccount extends Account {
private static double annualInterestRate;
public SavingsAccount() {
super();
}
public SavingsAccount(int i, double b) {
super(i, b);
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public static void setAnnualInterestRate(double x) {
annualInterestRate = x;
}
public double getMonthlyInterest() {
return annualInterestRate/12/100 * balance;
}
}
// Class for checking accounts
class CheckingAccount extends Account {
public CheckingAccount() {
super();
}
public CheckingAccount(int i, double b) {
super(i, b);
}
public void payBill(String s, double x) {
String str;
if (x <= 0) {
str = "Incorrect value entered. Payment unsuccessful.";
}
else if (balance < x) {
str = "Insufficient funds. Payment cannot be made.";
}
else {
balance -= x;
str = String.format("Payment of $%,.2f to " + s + " is successful.", x);
}
System.out.println(str);
addEntry(transactionLog, str);
}
}
//*****************************************
//* Joseph Mikhli Assignment 04 *
//* CISC 3150 MW2 03/22/2012 *
//*****************************************
package assignment_04;
import java.util.Scanner;
public class Assignment_04 {
public static void main(String[] args) {
boolean quit = false;
int option;
Scanner input = new Scanner(System.in);
BankingAccount ba = new BankingAccount("John Doe", "123-45-6789",
"1234 Main St, Brooklyn, NY", 1001, 20000, 1002, 100, 3.0);
do {
System.out.println("What would you like to do?");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Transfer");
System.out.println("4. View account values");
System.out.println("5. View account statements");
System.out.println("6. Pay a bill");
System.out.println("7. Quit");
do {
System.out.print("Enter an option from 1-7: ");
option = input.nextInt();
if (option != 1 && option != 2 && option != 3 && option != 4
&& option != 5 && option != 6 && option != 7)
System.out.println("Unknown value entered. Try again.");
} while (option != 1 && option != 2 && option != 3 && option != 4
&& option != 5 && option != 6 && option != 7);
switch (option) {
case 1:
ba.deposit();
break;
case 2:
ba.withdraw();
break;
case 3:
ba.transfer();
break;
case 4:
ba.values();
break;
case 5:
ba.statement();
break;
case 6:
ba.payBill();
break;
case 7:
quit = true;
break;
}
System.out.println();
} while (quit == false);
System.out.println("Goodbye!");
}
}
package assignment_04;
import java.util.Scanner;
// Aggregate class
class BankingAccount {
private String name, ssn, address;
private SavingsAccount sa;
private CheckingAccount ca;
private Scanner input = new Scanner(System.in);
// Constructors
public BankingAccount() {
name = "";
ssn = "";
address = "";
sa = new SavingsAccount();
ca = new CheckingAccount();
}
public BankingAccount(String x, String y, String z) {
name = x;
ssn = y;
address = z;
sa = new SavingsAccount();
ca = new CheckingAccount();
}
public BankingAccount(String x, String y, String z, int i1, double b1,
int i2, double b2, double intRate) {
name = x;
ssn = y;
address = z;
sa = new SavingsAccount(i1, b1);
ca = new CheckingAccount(i2, b2);
SavingsAccount.setAnnualInterestRate(intRate);
}
// Accessor and mutator methods
public String getName() { return name; }
public void setName(String x) { name = x; }
public String getSSN() { return ssn; }
public void setSSN(String x) { ssn = x; }
public String getAddress() { return address; }
public void setAddress(String x) { address = x; }
// Front-end methods
public void deposit() {
int option;
double amount;
System.out.println("Which account would you like to deposit to?");
System.out.println("1. Checking");
System.out.println("2. Savings");
do {
System.out.print("Enter option 1 or 2: ");
option = input.nextInt();
if (option != 1 && option != 2)
System.out.println("Unknown value entered. Try again.");
} while (option != 1 && option != 2);
do {
System.out.print("How much would you like to deposit? $");
amount = input.nextDouble();
if (amount <= 0)
System.out.println("Illogical value entered. Try again.");
} while (amount <= 0);
if (option == 1)
ca.deposit(amount);
else if (option == 2)
sa.deposit(amount);
}
public void withdraw() {
int option;
double amount;
System.out.println("Which account would you like to withdraw from?");
System.out.println("1. Checking");
System.out.println("2. Savings");
do {
System.out.print("Enter option 1 or 2: ");
option = input.nextInt();
if (option != 1 && option != 2)
System.out.println("Unknown value entered. Try again.");
} while (option != 1 && option != 2);
do {
System.out.print("How much would you like to withdraw? $");
amount = input.nextDouble();
if (amount <= 0)
System.out.println("Illogical value entered. Try again.");
} while (amount <= 0);
if (option == 1)
ca.withdraw(amount);
else if (option == 2)
sa.withdraw(amount);
}
public void transfer() {
int optionA, optionB;
double amount;
System.out.println("Which account would you like to perform a transfer"
+ " operation on?");
System.out.println("1. Checking");
System.out.println("2. Savings");
do {
System.out.print("Enter option 1 or 2: ");
optionA = input.nextInt();
if (optionA != 1 && optionA != 2)
System.out.println("Unknown value entered. Try again.");
} while (optionA != 1 && optionA != 2);
System.out.println("What would you like to do with this account?");
System.out.println("1. Transfer funds to other account");
System.out.println("2. Transfer funds from other account");
do {
System.out.print("Enter option 1 or 2: ");
optionB = input.nextInt();
if (optionB != 1 && optionB != 2)
System.out.println("Unknown value entered. Try again.");
} while (optionB != 1 && optionB != 2);
do {
System.out.print("How much would you like to transfer? $");
amount = input.nextDouble();
if (amount <= 0)
System.out.println("Illogical value entered. Try again.");
} while (amount <= 0);
if (optionA == 1) {
if(optionB == 1)
ca.transferTo(sa, amount);
else if (optionB == 2)
ca.transferFrom(sa, amount);
}
else if (optionA == 2) {
if(optionB == 1)
sa.transferTo(ca, amount);
else if (optionB == 2)
sa.transferFrom(ca, amount);
}
}
public void values() {
int option;
System.out.println("Which account would you like to know the value of?");
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("3. Both accounts");
do {
System.out.print("Enter option 1, 2, or 3: ");
option = input.nextInt();
if (option != 1 && option != 2 && option != 3)
System.out.println("Unknown value entered. Try again.");
} while (option != 1 && option != 2 && option != 3);
if (option == 1)
System.out.format("Checking: $%,.2f%n", ca.getBalance());
else if (option == 2)
System.out.format("Savings: $%,.2f%n", sa.getBalance());
else if (option == 3) {
System.out.format("Checking: $%,.2f%n", ca.getBalance());
System.out.format("Savings: $%,.2f%n", sa.getBalance());
}
}
public void statement() {
int option;
System.out.println("Which account statements would you like?");
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("3. Both accounts");
do {
System.out.print("Enter option 1, 2, or 3: ");
option = input.nextInt();
if (option != 1 && option != 2 && option != 3)
System.out.println("Unknown value entered. Try again.");
} while (option != 1 && option != 2 && option != 3);
if (option == 1) {
System.out.println("CHECKING ACCOUNT");
System.out.println("Transaction Log:");
ca.printStatement(ca.transactionLog);
System.out.println("Transfer Log:");
ca.printStatement(ca.transferLog);
}
else if (option == 2) {
System.out.println("SAVINGS ACCOUNT");
System.out.println("Transaction Log:");
sa.printStatement(sa.transactionLog);
System.out.println("Transfer Log:");
sa.printStatement(sa.transferLog);
}
else if (option == 3) { // Monthly Statement
System.out.println("CHECKING ACCOUNT");
System.out.println("Transaction Log:");
ca.printStatement(ca.transactionLog);
System.out.println("Transfer Log:");
ca.printStatement(ca.transferLog);
System.out.println();
System.out.println("SAVINGS ACCOUNT");
System.out.println("Transaction Log:");
sa.printStatement(sa.transactionLog);
System.out.println("Transfer Log:");
sa.printStatement(sa.transferLog);
System.out.println();
this.compoundInterest();
}
}
public void payBill() {
String payee;
double amount;
System.out.print("To whom would you like to pay a bill? ");
payee = input.next();
do {
System.out.print("How much would you like to pay? $");
amount = input.nextDouble();
if (amount <= 0)
System.out.println("Illogical value entered. Try again.");
} while (amount <= 0);
ca.payBill(payee, amount);
}
public void compoundInterest() {
sa.setBalance(sa.getBalance() + sa.getMonthlyInterest());
System.out.format("The new savings account balance is: $%,.2f%n", sa.getBalance());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment