Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a java program where we basically simulate a bunch of banks. My program throws a bunch of assertion errors and even a couple

I have a java program where we basically simulate a bunch of banks. My program throws a bunch of assertion errors and even a couple of casting errors and my brain is short circuiting. I have no idea what's going on despite trying to figure this out for a week. On top of that, the output is nowhere near what it's supposed to look like. Any help would be appreciated.

package testbank;

public class TestBank {

public static void main(String[] args) { // assert false; // System.out.println("If you see this message, then you DO NOT have assertions turned on!! Turn on assertions and then comment-out this line and the line above it."); // Create four customer objects Customer c1 = new Customer(new Account("P-123", 100), "Elvis Presley", "1 Nashville Way"); Customer c2 = new Customer(new Account("P-124", 50), "John Engler", "2 Sparty Drive"); Customer c3 = new Customer("Gov Snyder"); Customer c4 = new Customer(null, "Bob Robert", "3 Nickname Blvd"); // Create two manager objects Manager m1 = new Manager(33_500, 3, "Roy Rogers", "3333 Cactuses Row"); Manager m2 = new Manager(57_000, 10, "Lois Lane", "12 Manhattan St"); // Add a mixture of customer and manager objects to the Bank object. // The Bank class will need to have a private ArrayList of Person // references to be able to hold both Customers and Managers. Bank bank = new Bank("Bank of America"); bank.add(c1); bank.add(c2); bank.add(m1); bank.add(c3); bank.add(m2); bank.add(c4); // When the Bank's toString methoed is called, the Bank's toString // method should call the ArrayList's toString method; the ArrayList's // toString method will in turn call the toString method of each of // Customer and Manager objects held in the ArrayList. System.out.println(" ALL of the people at this bank: "); System.out.println(bank); // Polymorphism!!! // The Bank's "get" method will need to call some of the ArrayList's // methods to find the desired Person-based object. The "get" method // will probably need to take person's name to create a temporary // Person object with that name, then use that temporary Person // object to look for the person in the ArrayList. // This requires, in turn, that the Person class have an override // of the "equals" method -- setup the Person's "equals" method so // that it returns true if the two Person objects getting compared // have the same name. System.out.println("Find Roy Rogers: " + bank.get("Roy Rogers")); System.out.println("Find Bill Bixby: " + bank.get("Bill Bixby")); // We want to manipulate the account of one of the bank's customers. // First, get a reference to the customer object. Person found = bank.get("Elvis Presley"); if (found != null) { // The "get" method found the Customer "Elvis Presley", but the // "get" method only returns a Person reference. Cast it to // a Customer reference. Customer c = (Customer) found; // Take the Customer reference and retrieve the customer's account: c.getAccount().deposit(3011); c.getAccount().withdraw(10); System.out.println("After deposits and withdrawals: $" + c.getAccount().getBalance()); } // Delete a person from the bank: // First, verify that the person is there: assert bank.get("John Engler") != null; // Now remove the person bank.remove("John Engler"); // Verify that the person is gone assert bank.get("John Engler") == null; System.out.println("Note that Engler is now gone:"); System.out.println(bank); // The delete method should also return true or false, indicating // whether or not someone was really deleted: boolean b = bank.remove("Elvis Presley"); System.out.println("b: " + b); assert b == true; // Tom Brady is not at the bank, so cannot be removed assert bank.remove("Tom Brady") == false; System.out.println("Trying also to delete Tom Brady: " + bank.remove("Tom Brady")); System.out.println("The bank after the calls to remove:"); System.out.println(bank); Teller t1 = new Teller(11.50, 22.4, 300, "Clark Kent", "44 Riverview View"); bank.add(t1); assert bank.get("Clark Kent") != null; assert bank.toString().contains("Teller employee id 300, Clark Kent at 44 Riverview View, worked 22.4 hours at $11.50 per hour"); System.out.println(" After teller added: " + bank); System.out.println(" *** Trying Several Assertions: "); Customer cust1 = new Customer(new Account("P-567", 250), "Elmer Presley", "1 Nashville Way"); Customer cust2 = new Customer(new Account("P-568", 100), "Jack Engler", "2 Sparty Drive"); assert cust1.getName().equals("Elmer Presley"); assert cust1.getAccount().getNumber().equals("P-567"); assert cust1.toString().equals("Elmer Presley at 1 Nashville Way with account P-567, $250"); cust1.getAccount().deposit(50); cust1.getAccount().withdraw(3); assert cust1.getAccount().getBalance() == 297; Manager mgr1 = new Manager(150_000, 1, "Rob Rodgers", "1 Cactus St"); mgr1.setSalary(150_999); mgr1.setEmpId(2); assert mgr1.getEmpId() == 2; assert mgr1.toString().equals("Manager, employee id 2, Rob Rodgers at 1 Cactus St, with a salary of $150,999"); Teller teller1 = new Teller(12.50, 33.0, 123, "Stanley Kubrick", "10B Action Alley"); assert teller1.toString().equals("Teller employee id 123, Stanley Kubrick at 10B Action Alley, worked 33.0 hours at $12.50 per hour"); Bank keybank = new Bank("KeyBank"); keybank.add(cust1); keybank.add(mgr1); keybank.add(teller1); Customer cust = (Customer) keybank.get("Elmer Presley"); assert cust.getAccount().getBalance() == 297; System.out.println(keybank); String s = keybank.toString(); assert s.contains("KeyBank has:"); assert s.contains("Elmer Presley at 1 Nashville Way with account P-567, $297"); assert s.contains("Manager, employee id 2, Rob Rodgers at 1 Cactus St, with a salary of $150,999"); assert s.contains("Teller employee id 123, Stanley Kubrick at 10B Action Alley, worked 33.0 hours at $12.50 per hour"); keybank.remove("Elmer Presley"); s = keybank.toString(); assert !s.contains("Elmer Presley"); // You cannot add duplicate people to a bank. Two people are // consider the same if they have the same name. // // The Banks' "add" method should return true if the person was // successfully added, and return false if the person could not // be added. The person cannot be added if someone with that same // name is already at the bank. Therefore, the Bank's add method // will need to first search the ArrayList for the person who we are // trying to add, but only add them if there are not in the ArrayList // already. Bank chase = new Bank("Chase"); // No one is in the bank yet, so George will add successfully as // a Customer. The add method returns true. boolean addGeorge = chase.add(new Customer(new Account("P-999", 100), "George Washington", "1 President Lane")); assert addGeorge == true; // Here we are trying to add a manager named "George Washington". // A George Washington already is in the bank, so this manager object // will get rejected, and the add method will return false. Manager mgrGeorge = new Manager(33_500, 3, "George Washington", "2 Career Ave"); boolean addMgr = chase.add(mgrGeorge); assert addMgr == false; System.out.println(chase); String sChase = chase.toString(); assert sChase.contains("George Washington at 1 President Lane with account P-999, $100"); assert !sChase.contains("2 Career Ave"); System.out.println(" *** Assertion Testing Successfully Completed ***"); } }

package testbank;

import java.text.NumberFormat; import java.util.Locale;

public class Teller extends Employee{ private double rate; private double hours;

public Teller(double rate, double hours, int empID, String name, String address) { super(empID, name, address); this.rate = rate; this.hours = hours; }

public double getRate() { return rate; }

public void setRate(double rate) { this.rate = rate; }

public double getHours() { return hours; }

public void setHours(double hours) { this.hours = hours; }

@Override public String toString() { // return String.format("Teller %s, worked %d2 hours at $%.2f per hour", super.toString(), hours, rate); return "Teller " + super.toString() + ", worked " + hours + " hours at " + NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(rate) + " per hour"; } }

package testbank; //first

import java.util.Objects;

public class Person { public static final String DEFAULT_NAME = "*** NO NAME ***"; public static final String DEFAULT_ADDRESS = "*** NO ADDRESS ***"; private String name; private String address; public Person(String name, String address) {

this.name = name; this.address = address; } public Person(String name) { this.name = name; this.address = DEFAULT_ADDRESS; }

public Person() { this.name = DEFAULT_NAME; this.address = DEFAULT_ADDRESS; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getAddress() { return address; }

public void setAddress(String address) { this.address = address; }

@Override public String toString() { return String.format("%s at %s", name, address); }

@Override public int hashCode() { int hash = 3; return hash; }

@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof Person)) { return false; } final Person other = (Person) obj; if (!Objects.equals(this.name, other.name)) { return false; } if (!Objects.equals(this.address, other.address)) { return false; } return true; } }

package testbank; //fourth

import java.text.NumberFormat;

public class Manager extends Employee{ private int salary;

public Manager(int empID) { super(empID); }

public Manager(int salary, int empID, String name, String address) { super(empID, name, address); if(salary > 0) { this.salary = salary; } }

public int getSalary() { return salary; }

public void setSalary(int salary) { if(salary >= 0) { this.salary = salary; } }

@Override public String toString() { //String sal = NumberFormat.getIntegerInstance().format(salary); return String.format("Manager, %s, with a salary of $%,d", super.toString(), this.salary); }

void setEmpId(int id) { super.setEmpID(id); }

int getEmpId() { return super.getEmpID(); }

}

package testbank; //third public class Employee extends Person{ private int empID;

public Employee(int empID, String name, String address) { super(name, address); this.empID = empID; } public Employee(int empID) { this.empID = empID; }

public int getEmpID() { return empID; }

public void setEmpID(int empID) { this.empID = empID; }

@Override public String toString() { return String.format("employee id %d, %s", empID, super.toString()); } }

package testbank; //fifth public class Customer extends Person{ private Account account;

public Customer(Account account, String name, String address) { super(name, address); this.account = account; }

public Customer(String name ){ super(name); }

public Customer() { } public void setAccount(Account account) { if(!hasAnAccount()) { this.account = account; } } public Account getAccount() { return this.account; } public Account deleteAccount() { Account save = account; account = null; return save; } public boolean hasAnAccount() { return account != null; }

@Override public String toString() { if(hasAnAccount()) { return super.toString() + " with " + account.toString(); } return super.toString() + " - no current account"; } }

package testbank;

public class Bank extends Person{ private String name;

public Bank(String name) { this.name = name; }

public String getName() { return name; }

public void setName(String name) { this.name = name; } public boolean add(Person p) { if(super.equals(p)) { return false; } super.equals(p); return true; } public boolean remove(String name) { if(this.name == name) { this.name = null; } return true; } public Person get(String name) { Person p = new Person(name); return p; }

@Override public String toString() { return name + " has:" + super.toString(); } }

package testbank; //second public class Account extends Customer{ private String number; private int balance;

public Account(String number, int balance) { setBalance(balance); this.number = number; }

public Account(String number, int balance, Account account, String name, String address) { super(account, name, address); this.number = number; setBalance(balance); } public String getNumber() { return number; }

public void setNumber(String number) { this.number = number; }

public int getBalance() { return this.balance; }

private void setBalance(int balance) { if(balance >= 0) { this.balance = balance; } // else // { // this.balance = 0; // } }

public void deposit(int value) { if(value >= 0) { this.balance += value; } } public void withdraw(int value) { if(value >= 0) { this.balance -= value; }

} public boolean overdrawn() { return balance < 0; }

@Override public String toString() { return String.format("account %s, $%d", number, balance); } }

My output: (with errors commented out)

ALL of the people at this bank:

Bank of America has:*** NO NAME *** at *** NO ADDRESS *** Find Roy Rogers: Roy Rogers at *** NO ADDRESS *** Find Bill Bixby: Bill Bixby at *** NO ADDRESS *** Note that Engler is now gone: Bank of America has:*** NO NAME *** at *** NO ADDRESS *** b: true Trying also to delete Tom Brady: true The bank after the calls to remove: Bank of America has:*** NO NAME *** at *** NO ADDRESS ***

After teller added:

Bank of America has:*** NO NAME *** at *** NO ADDRESS ***

*** Trying Several Assertions:

KeyBank has:*** NO NAME *** at *** NO ADDRESS *** Chase has:*** NO NAME *** at *** NO ADDRESS ***

*** Assertion Testing Successfully Completed ***

The output it's supposed to have:

ALL of the people at this bank: Bank of America has: Elvis Presley at 1 Nashville Way with account P-123, $100 John Engler at 2 Sparty Drive with account P-124, $50 Manager, employee id 3, Roy Rogers at 3333 Cactuses Row, with a salary of $33,500 Gov Snyder at *** NO ADDRESS *** - no current account Manager, employee id 10, Lois Lane at 12 Manhattan St, with a salary of $57,000 Bob Robert at 3 Nickname Blvd - no current account Find Roy Rogers: Manager, employee id 3, Roy Rogers at 3333 Cactuses Row, with a salary of $33,500 Find Bill Bixby: null After deposits and withdrawals: $3101 Note that Engler is now gone: Bank of America has: Elvis Presley at 1 Nashville Way with account P-123, $3,101 Manager, employee id 3, Roy Rogers at 3333 Cactuses Row, with a salary of $33,500 Gov Snyder at *** NO ADDRESS *** - no current account Manager, employee id 10, Lois Lane at 12 Manhattan St, with a salary of $57,000 Bob Robert at 3 Nickname Blvd - no current account b: true Trying also to delete Tom Brady: false The bank after the calls to remove: Bank of America has: Manager, employee id 3, Roy Rogers at 3333 Cactuses Row, with a salary of $33,500 Gov Snyder at *** NO ADDRESS *** - no current account Manager, employee id 10, Lois Lane at 12 Manhattan St, with a salary of $57,000 Bob Robert at 3 Nickname Blvd - no current account After teller added: Bank of America has: Manager, employee id 3, Roy Rogers at 3333 Cactuses Row, with a salary of $33,500 Gov Snyder at *** NO ADDRESS *** - no current account Manager, employee id 10, Lois Lane at 12 Manhattan St, with a salary of $57,000 Bob Robert at 3 Nickname Blvd - no current account Teller employee id 300, Clark Kent at 44 Riverview View, worked 22.4 hours at $11.50 per hour *** Trying Several Assertions: KeyBank has: Elmer Presley at 1 Nashville Way with account P-567, $297 Manager, employee id 2, Rob Rodgers at 1 Cactus St, with a salary of $150,999 Teller employee id 123, Stanley Kubrick at 10B Action Alley, worked 33.0 hours at $12.50 per hour Chase has: George Washington at 1 President Lane with account P-999, $100 *** Assertion Testing Successfully Completed *** 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

More Books

Students also viewed these Databases questions