L06
BankAccount.java
/**
* Parent BankAccount class
*
* @author Brandon
* @version 1/1/1990
*/
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
public double getBalance() {
return balance;
}
}
CheckingAccount.java
/**
* Child CheckingAccount class
*
* @author Brandon
* @version 1/1/1990
*/
public class CheckingAccount extends BankAccount {
private int transactionCount;
public CheckingAccount() {
transactionCount = 0;
}
public CheckingAccount(double initialBalance) {
super(initialBalance);
// use the super class constructor that's already coded
transactionCount = 0;
}
public void deposit(double amount) {
super.deposit(amount);
transactionCount = transactionCount + 1;
}
public void withdraw(double amount) {
super.withdraw(amount);
transactionCount = transactionCount + 1;
}
public void deductFees() {
if (transactionCount > 3) {
double fee = 2.0*(transactionCount – 3);
super.withdraw(fee);
}
transactionCount = 0;
}
}
BankAccountTest.java
import java.util.*;
/**
* BankAccount test class
*
* @author Brandon
* @version 1/1/1990
*/
public class BankAccountTest {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.print("Enter initial checking account balance: ");
double amount = Double.parseDouble(myScanner.nextLine());
CheckingAccount myChecking = new CheckingAccount(amount);
System.out.println("D - Deposit into checking");
System.out.println("W - Withdraw from checking");
System.out.println("P - End of month processing");
System.out.println("S - Show account balance");
System.out.println("E - Exit the program");
boolean done = false;
while (!done) {
System.out.print("> ");
String choice = myScanner.nextLine();
if (choice.equalsIgnoreCase("D")) {
System.out.print("Enter amount to deposit: ");
amount = Double.parseDouble(myScanner.nextLine());
myChecking.deposit(amount);
} else if (choice.equalsIgnoreCase("W")) {
System.out.print("Enter amount to withdraw: ");
amount = Double.parseDouble(myScanner.nextLine());
myChecking.withdraw(amount);
} else if (choice.equalsIgnoreCase("P"))
myChecking.deductFees();
else if (choice.equalsIgnoreCase("S"))
System.out.println("Checking balance: " + myChecking.getBalance());
else if (choice.equalsIgnoreCase("E"))
done = true;
else
System.out.println("Invalid menu choice - please re-enter");
}
System.out.println("Goodbye!");
}
}
No Comments