2016-09-28

posted Sep 28, 2016, 6:24 AM by Samuel Konstantinovich   [ updated Sep 29, 2016, 7:00 AM ]
Goal: Ways to test your work

Things you want to discuss on the google group:

Your Triangle has:
3 constructors
toString()
getPerimeter()
getArea()
isCloseEnough(a,b)
isRight()

Come up with a Main that would test each method. Note that some methods require other methods to test!
You can collaborate on a main, but you should write the rest of the assignment on your own as much as possible.


Forget about Triangle for now:
We will write a bank account class as follows:

class name: 
        BankAccount
Instance variables required:
      balance 
      accountID [the account number, an integer]
      password 
Methods Required:
      1. A constructor: Should accept values for EACH of the instance variables
      2. A toString method that returns (tab separated): "Account#\tBalance"
      3. get methods for: balance, accountID
      4. set method for: password
      5a. A method to deposit money into the account. It should accept an amount of money, and increase the account balance by that much and return true. This should not apply a negative deposit, instead leaving the balance unchanged and returning false. 
      5b. Method to withdraw money from the account and return true if it was successful, false otherwise. This will not be successful if the balance is too low, in which case the action does not change the balance. 
      6. A main method (included in the same file) to test your code. 
         Since withdraw and deposit return booleans. We can use them as follows:
       hint:
       if( var.withdraw(1000) ){
            println("Withdrawal success!");
       }else{
            println("Withdrawal failure");
       }
      

EXAMPLE SOLUTION:

QUICK REFERENCE:

    /*Sole constructor requires all 5 values*/
    public BankAccount( int accountID,  double balance, String password){...}

    /*Return a String to be used to display the account. */
    public String toString(){...}

    /*standard accessor methods*/
    public double getBalance(){...}
    public int getAccountID(){...}

    /*Change the value of password to the specified value
     *@param newPass The value to replace the old password with*/
    public void setPassword(String newPass){...}

    /*When amount is positive: Increase balance by amount and return true. 
     *When amount is not positive: Return false.
     *@param amount The amount is how much to change the balance by. */
    public boolean deposit(double amount){...}

    /*When amount is positive and the amount is at least as much as the balance: 
     *Decrease balance by amount and return true. Otherwise: Return false.
     *@param amount The amount is how much to change the balance by. */
    public boolean withdraw(double amount){... }

Here is a functional version of BankAccount. You don't have to memorize the code, since you will get the
reference, but you should know how it works so you understand how to add new features.





public class BankAccount{
    private double balance;
    private int accountID;
    private String password;
    
    /**
     *Create a new BankAccount, with all parameters provided
     */
    public BankAccount(
                       double balance,
int accountID,
		       String password
){ this.accountID = accountID; this.balance = balance; this.password = password; } /** *Print the bank account for testing purposes *private method should not show up in javadoc */ private void print(){ System.out.println(accountID+":"+password); System.out.println("$"+balance); } /** *Return a String consisting of non-password/non-pin information *to be used to display the account. */ public String toString(){ return accountID+",\t$"+balance; } /** *Return the value of balance */ public double getBalance(){ return balance; } /** *Return the value of accountID */ public int getID(){ return accountID; } /** *Change the value of password to the specified value *@param newPass The value to replace the old password with */ public void setPassword(String newPass){ password = newPass; } /** *When amount is positive: Increase balance by amount and return 
     *true. When amount is not positive: Return false.
     *@param amount The amount is how much to change the balance by.
     */
    public boolean deposit(double amount){
	if(amount >= 0.0){
	    balance += amount;
	    return true;
	}
	return false;
    }
    /**
     *When amount is positive and the amount is at least as much as the balance: 
     *decrease balance by amount and return true. Otherwise: Return false.
     *@param amount The amount is how much to change the balance by.
     */
    public boolean withdraw(double amount){
	if(balance >= amount && amount >= 0){
	    balance -= amount;
	    return true;
	}
	return false;
    }


    public static void main(String[]args){
	BankAccount b1 = new BankAccount(1000.01000001"abc123");
	BankAccount b2 = new BankAccount(1234.01000231, "abcasdg23");
	
	System.out.println(b1);
	System.out.println(b2);

        

	double cashAmount = 300.0;
	if(b1.withdraw(cashAmount) ){
	    if(b2.deposit(cashAmount)){
		System.out.println("Success");
	    }else{
		//This should never happen. 
		//Error message provided to make sure that is the case
		System.out.println(cashAmount + " withdrawn from" +
				   b1 + "Failed to add to "+b2);
	    }
	}else{
	    System.out.println("Failed to withdraw "+cashAmount+" from "+b1);
	}
	System.out.println(b1);
	System.out.println(b2);

    }
}
Comments