This is the first part of a 2 part project.
Person Class
Constructor
-
public Person( String _name , int _age )
instance Variables
-
private String name
-
private int age
Methods
-
public String getName()
-
public int compareTo(Person other) { return this.age - other.age; }
Account – Abstract Super Class
Static Variable(s)
-
private static int nextAccountNum
-
private static int parentCompanyCode = 12810 ;
instance Variables
-
private int accountNumber// first account number should be 1
-
protected double balance
-
protected ArrayList<Person> owners
Constructor
-
public Account( double _balance, Person _owner) // add _owner to ArrayList owners
Methods
-
public static int getParentCode()
-
public int getAccountNumber()
-
public void deposit(double amount)
-
public double getCurrentBalance()
-
public abstract boolean withdraw(double amount)
-
public boolean equals(Account other) // are the unique account numbers equals ?
-
public String toString() Output should follow the conventions we have discussed. I will be testing for formatting like this :
- public int comparesTo(Account other)
{ return this.accountNumber- other.accountNumber } (Note that this method has an ‘s‘ )
-
public void addOwner(Person p) //adds P to the ArrayList of owners
-
public ArrayList<Person> getOwners() // returns the ArrayList of owners
-
public Person remove( Person p) // this emulates the ArrayList’s remove() method (link) . Remember that that method removes the object and then returns the removed object. In this case, you should remove
Person P from the ArrayList of owners
- CheckingAccount extends Account
- This class has to do certain things. You can decide the best way to do it. This class should have a way to
- attempt to withdraw money by writing a check. Every check should have a ‘check number’, which should be an integer. The lowest permissible value for a check number is
100 .
-
public int writeCheck(double amount) // @ returns the check number of the written check
-
public boolean cancelCheck(int checkNumber)
- be able to cancel checks and keep track of the check numbers that have been cancelled
- when a check is cancelled, the amount associated with that check is added back onto the balance.
- return
true if
checkNumber is a valid check number;
false otherwise.
- One solution
- every time you write a check, you need a new check “#”
-
writeCheck( double amount) - > tries to call
this.withdraw(double amount) . You can then use ArrayLists to store check numbers, associated check amounts , things like that
SavingsAccount extends Account
-
public double getInterestRate()
-
public String toString() Output should follow the conventions we have discussed. I will be testing for formatting like this :
CertificateOfDeposit extends SavingsAccount
instance Variables
-
public static final double EARLY_WITHDRAW_PENALTY= 200 .
Methods
-
public boolean withdraw(double amount , boolean isEarly ) //@override the withdraw() and applies penalty to balance
Tester file :
import java.util.ArrayList;
public class Tester {
boolean verbose = false; //set to true for some expanded occasional error messages
public Tester() {
p2("Begin Testing");
p2("Version 4");
p2("set variable verbose to true if you want some occasional extra tips on errors");
p2("********************************************************************") ;
p2("********************************************************************") ;
doTest();
}
public void doTest(){
int correct = 0;
int wrong = 0;
Person tyler = new Person("tyler", 18);
Person joe = new Person("joe", 18);
Person jim = new Person("jim", 17);
if(tyler.compareTo(joe) == 0)
correct++;
else{
p2("error @person, compareTo()");
wrong++;
}
if(jim.compareTo(joe) == -1)
correct++;
else{
p2("error @person, compareTo()");
wrong++;
}
if(joe.compareTo(jim) == 1)
correct++;
else{
p2("error @person, compareTo()");
wrong++;
}
if( joe.getName().equals("joe"))
correct++;
else{
p2("error @person, getName()");
wrong++;
}
CertificateOfDeposit a = new CertificateOfDeposit(tyler, 120);
double a_original_bal = a.getCurrentBalance();
a.addOwner(joe);
int a_num1 = a.getAccountNumber() ;
if (a_num1 != 1 ){
wrong++ ;
p2("First account 's id should be '1' ");
}
else
correct++;
CertificateOfDeposit a2 = new CertificateOfDeposit(jim, 120);
if(a2.getAccountNumber() <= a_num1){
wrong++ ;
p2("Unique account numbers not working correctly ");
}
else
correct++;
if (a.getOwners().size() != 2)
{
p2("addOwner() not working ");
wrong++ ;
}
else
correct++;
a.remove(joe);
if (a.getOwners().size() != 1)
{
wrong++ ;
p2("error , probably removeOwner() not working ");
}
else
{
Person owner = a.getOwners().get(0);
if(owner.getName().toLowerCase().equals("tyler"))
correct++;
else
{
wrong++ ;
p2("error , probably removeOwner() not working ");
}
}
if(Math.abs(a_original_bal - 120) > 0.000001)
{
p2 ("foo problem with setting balance");
wrong++;
}
else
correct++;
a.deposit(25);
if(Math.abs(a.getCurrentBalance() - 145) > 0.000001)
{
wrong++ ;
p2("problem with setting balance");
}
else
correct++;
Person rob = new Person("rob", 18);
CheckingAccount b = new CheckingAccount(rob, 200);
b.deposit(500);
b.withdraw(450);
if( b.equals(a))
{
wrong++;
p2("Failed equals() test (Many things could cause this)");
}
else
correct++;
if( a.equals(a) == false)
{
wrong++;
p2("Failed equals() test (Many things could cause this)");
}
else
correct++;
double balanceNow = b.getCurrentBalance();
int checkNum = b.writeCheck(4);
// p2("checkNum : " + checkNum);
if(checkNum >= 100)
correct++;
else
{
wrong++;
p2("error, Check Numbers cannot be lower than 100") ;
}
int check2Num = b.writeCheck(4);
if(check2Num - checkNum == 1)
correct++;
else
{
wrong++;
p2("error involving checknumbers. Should increment by 1") ;
}
if( balanceNow - b.getCurrentBalance() == 8)
correct++;
else
{
wrong++;
p2("Error involving writing checks and balance...could be various problems");
if(verbose)
p2("\t most likely your writeCheck() or withdraw() are the problem");
}
balanceNow = b.getCurrentBalance() ;
int check3Num = b.writeCheck(242.1);
if(check3Num - checkNum == 2)
correct++;
else
{
wrong++;
p2("error involving checknumbers. Should increment by 1,even if the check bounces!") ;
}
if(balanceNow == b.getCurrentBalance())
correct++;
else
{
wrong++;
p2("Error involving bounced checks. You cannot withdrawal more than your current balance") ;
p2("exiting testing program");
return;
}
boolean badChkNum = b.cancelCheck(50);
if(badChkNum == false)
correct++;
else
{
wrong++;
p2("Error. cancelCheck on non existent check. Should return false, but didn't") ;
p2("exiting testing program");
return;
}
int check4Num = b.writeCheck(100);
b.cancelCheck(check4Num);
double balanceAferCancel = b.getCurrentBalance();
if( balanceAferCancel == 242)
correct++;
else{
wrong++;
if(balanceAferCancel == 142)
p2("cancelCheck() did not update balance. You still have deducted the amount for the check that is now cancelled");
else
p2("error , could be various things but most likely relateing to cancelCheck()/deposit() ");
}
// if(balanceNow == b.getCurrentBalance())
// correct++;
// else
// {
// wrong++;
// p2("Error involving bounced checks. You cannot withdrawal more than your current balance") ;
// }
Person roochi = new Person("roochi", 12);
SavingsAccount c = new SavingsAccount(roochi, 110);
boolean allGood = true;
if(c.getCurrentBalance()== 110)
correct++;
else
{
allGood = false;
wrong++;
p2("error, could be constructor or getCurrentBalance()") ;
}
c.withdraw(5);
if(c.getCurrentBalance()== 105)
correct++;
else
{
if(allGood )
p2("error, probably withdraw()");
else
p2("error, could be constructor or getCurrentBalance() ") ;
wrong++;
}
boolean isNull = false;
try{
c.addOwner(tyler);
}
catch( NullPointerException e){
isNull = true;
}
if(isNull)
{
p2("Looks like owners arraylist is null");
p2("exiting program now, fix nullpointer first");
wrong++;
return;
}
else
correct++;
//here
// public String toString()
Person james = new Person("Jim Adams II", 12);
SavingsAccount jamesAcct = new SavingsAccount(james , 666 );
jamesAcct.addOwner(new Person("Jane Adams ", 34) );
jamesAcct.addOwner(new Person("Jim Adams Sr", 35) );
ArrayList<Person> owners = jamesAcct.getOwners();
boolean anotherNull = false;
try{
jamesAcct.recordDailyBalance();
}
catch(NullPointerException e){
anotherNull = true ;
wrong++;
}
if(anotherNull)
{
wrong++;
p2("NullPointer Exception Error, looks like you never initialized dailyBalances in SavingsAccount");
p2("Exiting testing program");
return;
}
else
correct++;
jamesAcct.updateInterestRate(3.3);
if(jamesAcct.getInterestRate() == 3.3 )
correct++;
else{
wrong++;
p2("error, probably re: updateInterestRate() or possibly getInterestRate()");
}
jamesAcct.recordDailyBalance();
jamesAcct.deposit(22);
jamesAcct.recordDailyBalance();
jamesAcct.withdraw(100);
jamesAcct.recordDailyBalance();
jamesAcct.deposit(330);
jamesAcct.recordDailyBalance();
ArrayList<Double> dailyBalances = jamesAcct.getDailyBalances();
if(dailyBalances.get(0)== 666)
correct++;
else
{
wrong++;
p2("error @ dailyBalances index 0");
if(verbose)
p2("\t\t 1st element of dailyBalances should be 666.00");
p2("exiting testing program, fix daily balances first");
return;
}
if(dailyBalances.get(1)== 666)
correct++;
else
{
wrong++;
p2("error @ dailyBalances index 1");
if(verbose)
p2("\t\t 2nd element of dailyBalances should be 666.00");
}
if(dailyBalances.get(2)== 688)
correct++;
else
{
wrong++;
p2("error @ dailyBalances index 2");
if(verbose)
p2("\t\t 3rd element of dailyBalances should be 666.00") ;
}
if(dailyBalances.get(3)== 588)
correct++;
else
{
wrong++;
p2("error @ dailyBalances index 3");
if(verbose)
p2("\t\t 4th element of dailyBalances should be 588.00") ;
}
if(dailyBalances.get(4)== 918)
correct++;
else
{
wrong++;
p2("error @ dailyBalances index 4");
if(verbose)
p2("\t\t 5th element of dailyBalances should be 918.00") ;
}
if( Account.getParentCode()== 12810)
correct++;
else{
wrong++;
p2("Account.getParentCode() wrong ");
}
//toString Tests
String str = jamesAcct.toString();
if(str.indexOf("SavingsAccount") == 0 )
correct++;
else{
wrong++;
p2("toString() should start w/classname. i.e. \"SavingsAccount\" ");
}
str = str.toLowerCase();
if(str.contains("number") )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, Account number");
}
if(str.contains(jamesAcct.getAccountNumber() +"") )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, Account number");
}
if(str.contains("balance") )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, re: account balance");
}
if(str.contains(jamesAcct.getCurrentBalance() +"") )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, re: account balance");
}
// ArrayList<Person> getOwners()
if(str.contains("owner") )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, re: Account Owners");
}
ArrayList<Person> jamesAcctOwners = jamesAcct.getOwners();
for(Person p : jamesAcctOwners){
String strName = p.getName().toLowerCase();
if(str.contains(strName) )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, re: Account Owners");
if(verbose)
p2("\t\t " + str + "\n \t\t\t Missing " + strName);
}
}//end of for-each
if(str.contains("interest rate") )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, re: interest rate ");
}
if(str.contains(jamesAcct.getInterestRate() + "") )
correct++;
else{
wrong++;
p2("toString() incorrectly formatted, re: interest rate ");
}
Person jorge = new Person("jorge", 17) ;
CertificateOfDeposit cd_foo = new CertificateOfDeposit(jorge, 500);
cd_foo.withdraw(100, true);
double jorgesMoney = cd_foo.getCurrentBalance();
if(jorgesMoney == 200 )
correct++;
else{
wrong++;
if( jorgesMoney == 400)
p2("You did not apply the EARLY_WITHDRAW_PENALTY for a CD's early withdrawal");
else
p2("Certificate of Deposit has wrong balance after early withdrawal");
}
Account sb = new SavingsAccount( jorge, 100 );
Account sb2 = new SavingsAccount( joe, 100);
if( sb2.comparesTo(sb) == 1)
correct++ ;
else
{
wrong++;
p2("Account's comparesTo not implemented correctly");
}
SavingsAccount sb33 = new SavingsAccount( jorge, 100 );
sb33.updateInterestRate(3.3);
if( sb33.getInterestRate()== 3.3)
correct++;
else
{
wrong++;
p2("Savings account error involving interestRate");
p2("\t either getInterestRate() or , more likely, updateInterestRate() is wrong ");
}
double ball_yr1 = sb33.projectBalance(1);
if( Math.abs(ball_yr1 - 103.350372874721)< 0.000001)
correct++ ;
else
{
wrong++;
p2("SavingAccount's projectBalance() incorrect");
}
p2("********************************************************************") ;
p2("********************************************************************") ;
p2(" SCORE ") ;
p2("********************************************************************") ;
p2("********************************************************************") ;
p2("********************************************************************") ;
p2("-------------------------------");
p2("Correct : " + correct);
p2("Wrong : " + wrong);
}
void p2(String s){
System.out.println(s);
}
void p2(int s){
System.out.println(s);
}
void p2(boolean s){
System.out.println(s);
}
}
Tester.java
When you are done, you can begin the Bank project .