10-03-2013

posted Oct 3, 2013, 8:43 AM by Samuel Konstantinovich   [ updated Oct 3, 2013, 9:00 AM ]
Today is just a day to look over the class to see what works/doesn't and fix leftover issues.

Sample solution to the lab:

public class BankAccount{
    private String name,password;
    private double balance;
    private int PIN,account;

    //BankAccount(name,account,PIN,password,balance)
    public BankAccount(String name, int account, int PIN, String password, double balance){
setPIN(PIN);
setAccount(account);
setName(name);
setPassword(password);
setBalance(balance);
    }

    //default constructor to mess around with, you don't need this.
    public BankAccount(){
//Ignore this little java syntax hack i put in... you don't need it.
this("INVALID",999999999,9999,"XXXXX",0.0);
    }

    public String toString(){
return "Account Holder: " + name +
   "\nAccount #: " + account +
   "\nPIN: " + PIN +
   "\npassword: " + password +
   "\nbalance: " + balance + "\n";

    }

    public void setBalance(double balance){
if(balance < 0){
   balance = 0.0;
}
this.balance = balance;
    }

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

    public void setPassword(String password){
this.password = password;
    }

    public void setPIN(int PIN){
if(PIN < 1000 || PIN>9998){
   PIN = 9999;
   System.out.println("ERROR cannot set invalid PIN, using 9999");
}
this.PIN = PIN;
    }
    public void setAccount(int account){
if(account < 100000000 || account > 999999998){
    account = 999999999;
   System.out.println("ERROR cannot set invalid Account number, using 999999999");
}
this.account = account;
    }
    

    public boolean deposit(double n){
if(n < 0){
   System.out.println("ERROR Deposit, negative value not permitted");
   return false;
}else{
   balance += n;
   return true;
}
    }

    public boolean withdraw(double n){
if(n < 0){
   System.out.println("ERROR Withdraw, negative value not permitted");
   return false;
}else if(n > balance){
   System.out.println("ERROR Withdraw, not enough funds");
   return false;
}else{
   balance -= n;
   return true;
}
    }

    public boolean authenticate(int account, String password){
return this.password.equals(password) && this.account==account;
    }

    public static void main(String[]args){
BankAccount A = new BankAccount();
BankAccount B = new BankAccount("Bob",284252119,1234,"bobbyPiggy",0.0);

//test some bad values
System.out.println(A);
A.setPIN(345);

System.out.println(B);
//Test set valid numbers
B.setAccount(1);
B.setAccount(123456789);
//Test deposit/withdraw
B.deposit(-10.0);
B.deposit(123.44);
B.withdraw(3.44);
B.withdraw(300.00);
//reprint the account
System.out.println(B);

//test authenticate
if( B.authenticate(333444555,"bobbyPiggy")){
   System.out.println("login credentials match");
}else{
   System.out.println("login credentials do not  match");
}

if( B.authenticate(123456789,"bobbyPiggy")){
   System.out.println("login credentials match");
}else{
   System.out.println("login credentials do not  match");
}

if( B.authenticate(123456789,"bobby")){
   System.out.println("login credentials match");
}else{
   System.out.println("login credentials do not  match");
}
    }

}

Comments