The Assignment
---------------------------
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
}
}
--------------------
What I've Got:
public class Name{
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() );
}
}
public class Account {
double balance;
double rate, deposit, withdrawal;
String acctName;
// constructor
public Account( String n, double b) {
acctName = n;
balance = b;
}
public void SetRate(double ratev){
rate=0.03;
rate = ratev;
balance += balance * rate;
}
public void deposit (double depositv){
deposit = depositv;
balance += balance + deposit;
}
public void withDrawal (double withdrawalv){
withdrawal = withdrawalv;
balance += balance - withdrawal;
}
public void calcInterest (double interestv){
balance += balance*rate;
balance += balance + balance;
}
public class Bank {
String bankName;
Account[] accts = new Account [3];
int max = 5;
int min = 0;
// constructor
public Bank( String n, int howmany) {
bankName = n;
accts = new Account[ howmany ];
}
}
public class CheckAcct extends Account{}
public class SavAcct extends Account{}
--------------------------
What is it that I'm missing to make the main class functionable?|||It needs a few tweaks:
1.Bank constructor has String, where main does not provide:
Bank CIBC = new Bank("TestBank", 10);
2. Implement Account.getBalance
public double getBalance ()
{
return (balance);
}
3.Implement Bank.addAcct and define a new member AccCount - Number of account added
int AccCount = 0;
public void addAcct (Account a)
{
accts [AccCount++] = a;
}
4. Implement Bank.totalBalance
public double totalBalance ()
{
double TotalBal = 0.0;
for (int i = 0; i %26lt; AccCount; i++)
{
TotalBal += accts[i].getBalance();
}
return (TotalBal);
}
5. Implement constructor for CheckAcct
public class CheckAcct extends Account
{
public CheckAcct( String n, double b)
{
super(n, b);
}
}
6. Implement constructor for SavAcct
public class SavAcct extends Account
{
public SavAcct( String n, double b)
{
super(n, b);
}
}
7. Fix Account.deposit
public void deposit (double depositv)
{
deposit = depositv;
balance += deposit;
}
8. Fix Account.withdrawal
public void withDrawal (double withdrawalv)
{
withdrawal = withdrawalv;
balance -= withdrawal;
}
9. Fix Account.calcInterest
public void calcInterest (double interestv)
{
balance += balance*rate;
//balance += balance + balance;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment