/* Problem #4: Create the class Account. The class will contain the private properties balance type double, id type byte, and owner type Owner. The class will implement Comparable (comparisons based upon the id of the account), Comparator (comparisons based upon the balance of the account), and Cloneable. The class will contain methods modelling operations of an account, including withdraw(int amount) and deposit(int amount). Problem #5: Create the class Owner. The class will contain the private properties name type String and address type String. The class will implement Comparable (comparisons based upon the name of the owner), Comparator (comparisons based upon the address of the owner), and Cloneable. */ import java.io.*; import java.util.*; //***************************************************CLASS Owner**************************************************************** class Owner implements Comparable, Comparator, Cloneable{ private String name; private String address; public Owner(){ name = new String(); address = new String(); } public Owner( String name, String address){ this.name = new String(name); this.address = new String(address); } public String getName() { return name;} public String getAddress() { return address;} public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } public String toString(){ return ("Name: " + name + " Address " + address); } public int compareTo(Object obj){ if(obj instanceof Owner){ Owner owner2 = (Owner) obj; return name.compareTo(owner2.name); //compare names lexiographically } else{ //error! return -99; } } public int compare(Object object1, Object object2){ if(object1 instanceof Owner && object2 instanceof Owner){ Owner owner1 = (Owner) object1; Owner owner2 = (Owner) object2; return owner1.address.compareTo(owner2.address); //compare addresses lexiographically } else{ //Error! return -99; } } public boolean equals(Object obj){ Owner owner1 = null; if(obj instanceof Owner){ owner1 = (Owner) obj; if(this.name.equals(owner1.name)){ //compare names if(this.address.equals(owner1.address)){ //compare address return true; } } } return false; } public Object clone(){ Object clone = null; String cloneName = new String(name); //makes new string objects String cloneAddress = new String(address); clone = new Owner(cloneName, cloneAddress); //creates a new Owner with the same values return clone; } } //********************************************CLASS Account************************************************************ class Account implements Comparable, Comparator, Cloneable{ //id must be bound between -128 and 127 gives us a total of 256 different ids private double balance; private byte id; private Owner owner; public Account(){ balance = 0; owner = new Owner(); id = 0; } public Account(double balance, byte id, Owner owner){ this.balance = balance; this.id = id; this.owner = owner; } public double getBalance() { return balance; } public byte getID() { return id; } public Owner getOwner() { return owner; } public void setBalance(double newBalance) { balance = newBalance; } public void setID(byte newID){ id = newID; } public void setOwner(Owner newOwner){ owner = newOwner; } public Object clone(){ Object clone = null; double cloneBalance = this.balance; byte cloneID = this.id; Owner cloneOwner = (Owner) this.owner.clone(); //Deep copy of the Owner clone = new Account(cloneBalance, cloneID, cloneOwner); return clone; } public int compareTo(Object obj){ Account account = null; if(obj instanceof Account){ account = (Account) obj; //cast the Account out of the object if(this.id > account.id) return 1; else if(this.id < account.id) return -1; else{ return 0; } } else{ //Error! return -99; } } public int compare(Object object1, Object object2){ Account account1 = null; Account account2 = null; if(object1 instanceof Account && object2 instanceof Account){ account1 = (Account) object1; account2 = (Account) object2; if(account1.balance < account2.balance) return -1; else if(account1.balance> account2.balance) return 1; else return 0; } else{ //ERROR return -99; } } public boolean equals(Object obj){ Account account1 = null; boolean equal = false; if(obj instanceof Account){ account1 = (Account) obj; if(compare(this, account1)==0){ if(this.id == account1.id){ if(this.owner.equals(account1.getOwner())){ equal = true; } } } return equal; } else{ //Error return false; } } public void Deposit(double amount){ balance += amount; } public void Withdraw(double amount){ balance -=amount; } public boolean multipleAccounts(Owner owner){ return this.owner.equals(owner); } public String toString(){ return ("Account " + id + " Owner: " + owner.toString() + " Balance: " + balance); } } //**********************************************************CLASS driver******************************************* class driver{ public static void main(String args[]){ char choice; Account accounts[] = new Account[0]; int nAccounts = 0; do{ choice = menu(); switch (choice){ case '1': accounts = addAccount(accounts); nAccounts = accounts.length; break; case '2': listAccounts(accounts); break; case '3': accounts = removeAccount(accounts); break; case '4': accounts = updateAccount(accounts); break; } }while(choice != '5'); } public static char menu(){ char choice; System.out.println("\n\n CIS 3023 Account System"); System.out.println(" 1. Create an Account"); System.out.println(" 2. View Accounts"); System.out.println(" 3. Delete Accounts"); System.out.println(" 4. Update Accounts"); System.out.println(" 5. Quit"); do{ System.out.print(" Enter your selection: "); choice = UserInput.readChar(); if(choice < '1' || choice > '5'){ System.out.println("ERROR INVALID CHOICE"); } }while(choice < '1' || choice > '5'); return choice; } public static Account[] addAccount(Account accounts[]){ String name = new String(); String address = new String(); double Balance = 0; byte ID = 0; char choice ='Y'; System.out.println("Enter the name of the Owner"); name = UserInput.readString(); System.out.println("Enter the address of the Owner"); address = UserInput.readString(); Owner newOwner = new Owner(name, address); for(int i=0; i