Thursday, December 8, 2011

Some help with Java programming!?

Bank Accounts








We are getting to the good stuff. Now that we have added methods to our arsenal it's time to add classes. Classes allow us to create objects and that's exactly what we are going to do. For this assignment you will create a class Account that will represent a Bank Account. You will also create a class Bank which will hold various Accounts in an array. There will be two types of Accounts: Checking and Savings, and they will operate under different constraints as you will see below. *Hint: Checking and Savings accounts are types of Accounts.





Types of Accounts:


When you create a new Account you should give it the holder's name as well as a starting balance. If one is not given there should be a default value. Any type of Account you create must be able to do the following (in other words must include the following methods)





void setRate (double v) //sets the rate


void deposit (double v)//deposits a given amount into the account


void withDrawal (double v)//subtracts a given amount from the account


void calcInterest ()// calculates yearly interest and adds it to the balance


double getBalance () // that returns the current balance


String toString() // returns a String with all the information of the acct








//CheckAcct: a Checking account only receives interest if balance %26gt; $1000





//SavAcct: A Savings account, can only withdraw up to $200 at a time





The Bank should be able to calculate interest for all the Accounts it holds.


(calcInterestAll) and it should get the total balance in the Bank (totalBalance())


It will also have to have a methods that add Accounts to the Bank.





Your main class should operate as follows:





public class YourNameAss3{





public static void main (String [] args) throws Exception {





Account a = new Account("Donald Duck", 100.00);


CheckAcct chk = new CheckAcct("Scrooge McDuck", 5000.0);


SavAcct sav = new SavAcct("Condoleeza Rice", 320.0);





chk.deposit(100.00);


sav.deposit(100.00);


a.deposit(100.00);





sav.withDrawal(300.0);


chk.withDrawal(200.0);





Bank CIBC = new Bank(10);





a.deposit(100.00);


a.withDrawal(50.00);





System.out.println("Account a : " + a.getBalance() );


System.out.println("Account chk : " + chk.getBalance() );





CIBC.addAcct(a);


CIBC.addAcct(sav);


CIBC.addAcct(new Account("Scrooge McDuck", 1000.00));





System.out.println(a.toString()); //This could be used to get info for //account a or any other account





System.out.println("Total Balance for CIBC : " + CIBC.totalBalance() );


System.in.read(); //This is not necessary in BlueJ


}








}





Does anybody have the know-how for how this works?





Some heads-ups would also be appreciated.|||look, newbies do get a mental block on Objects and they are not hard, they are cool, in fact. You know arrays. With an array you can put all kinds of data under one name and go fetch it with an index number. myData[ 21 ]. Think of an Object as a fancy array. Instead of an index number, you go fetch data with a name.





If this were me, I would use an





abstract class Accounts {


// no constructor





//sets the rate


abstract void setRate (double v);





//deposits a given amount into the account


abstract void deposit (double v);





//subtracts a given amount from the account


abstract void withDrawal (double v);





// calculates yearly interest and adds it to the balance


abstract void calcInterest ();





// that returns the current balance


abstract double getBalance ();





// returns a String with all the information of the acct


abstract String toString();


}





class CheckAcct extends Accounts {


}





class SavAcct extends Accounts {


}





But, I bet you haven't covered abstract class just yet. Do what I did. Take off abstract, fill in methods and add a constructor. I see in your instructions Donald Duck gets an Account.





class Account {


double balance;


String acctName;





// constructor


public Account( String n, doubleb) {


acctName = n;


balance = b;


}





class Bank {


String bankName;


Account[] accts;





// constructor


public Bank( String n, int howmany) {


bankName = n;


accts = new Accout[ howman ];


}

No comments:

Post a Comment