Hello everyone, I'm Adams. My exam will be this week end. I'm doing some exercises given by the lecturer. This is a bank account program, a basic object oriented programming .
I
⦁ A SavingsBank offers both a basic bank account, which allows funds to be deposited and withdrawn, as well as an interest-bearing savings account, which pays interest monthly for accounts that have reached a specified deposit amount target for the month.
The SavingsBank program implementation will begin with the modelling of a basic bank account in the Account class. You are required to finish off this class definition by writing the code for the missing parts of the class, as described below:
// assume that the class definition below already exists
class Account
{
private String accountNumber; // id of account (e.g. “A1234”)
private String accountName; // name of account holder
private double balance; // current balance of the account
⦁ Write a constructor for the Account class which accepts the account ID, account name and balance as parameters and initialises the corresponding instance variables (shown above) accordingly.
⦁ Write the accessors (getters) for each of the instance variables defined above which return the current value of the corresponding instance variable to the caller.
⦁ Write a mutator public boolean withdraw(double amount), which begins by checking to see if the current balance is greater than or equal to the amount specified.
If so then the balance should be reduced by the specified amount and the method should return a true result, otherwise the balance should remain unchanged and a false result should be returned.
⦁ Write a method public boolean deposit(double amount), which begins by checking to see if the amount specified is a positive value. If so then this amount should be added to the balance and the method should return a true result, otherwise the balance should remain unchanged and a false result should be returned.
⦁ Write a method public void print() which prints the details for a Account to the screen in a neat format, one instance variable per line (you should print a “label” before each variable describing what the value is - eg. Account Number: A1234, Account Name: Bob The Builder, etc).
WRITE YOUR ANSWERS IN THE SCRIPT BOOKLET PROVIDED (ie. not in this exam paper)
(please turn the page for more exam questions)
⦁ A SavingsAccount is an account which pays interest at a rate of 5% of the current balance in any month where the account balance has increased overall by at least $500 over the final balance for the previous month. You should address this new requirement by following the steps outlined below:
⦁ Define a subclass of Account called SavingsAccount. This Account subclass should define a new instance variable previousBalance, representing the final balance for the SavingsAccount at the end of the previous month.
You should also define a class (static) constant DEPOSIT_TARGET representing the fixed deposit target a SavingsAccount needs to reach in order to earn interest for a given month ($500).
⦁ Define a constructor for this SavingsAccount class which accepts the account number, account name and balance for the SavingsAccount object being created.
This constructor should use the super() facility in an appropriate manner in order to pass on the account number, acount name and balance to the superclass constructor.
The previousBalance instance variable should be set to the specified initial balance to begin with.
I'm confused at this level, everything up there is ok.
⦁ Implement a new method public void addInterest(), which starts off by checking the current balance for the SavingsAccount against the previous month’s final balance to see if the standard $500 monthly deposit target for the SavingsAccount has been reached.
If so then interest of 5% of the current balance should be deposited to the account. After this the method should then reset the previousBalance instance variable to the current SavingsAccount balance.
Note that you should be demonstrating how to re-use accessors and mutators to working with the balance instance variable defined in the Account superclass, instead of trying to access and manipulate it directly.
⦁ Override the print() method in this SavingsAccount subclass so that it also displays the previous month’s balance to the screen. Note that this method should invoke the superclass print() method to print the basic details for this SavingsAccount.
⦁ Your final task in this SavingsBank program is to complete the implementation of a trial application which manages a collection of Account and SavingsAccount objects. Parts of this application class have already been completed so you only need to implement the code segments highlighted below to complete the program.
Important notes:
Do NOT repeat/rewrite any of the code segments that have already been provided below, as they do not form part of your answer and you will not get any marks for doing so (thus rewriting them in your script booklet will just be wasting precious time).
You only need to write the code segments for parts of the program highlighted in requirements A) to D) below - the points in the program where the code segments you need to write for these requirements should begin are noted with comments similar to the following:
// your answer for requirement A) would go here
// ...
import java.util.*; import java.io.*;
public class SavingsBank
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String accountNumber;
double amount;
boolean result;
Account acc;
// create array 20 Account references
Account [] accountList = new Account[20];
// fill array with objects
acountList[0] = new Account(“A001”, “Cliff”, 1000.0);
acountList[1] = new SavingsAccount(“S003”, “Martin”, 2000.0);
acountList[2] = new Account(“A006”, “Susan”, 2500.0);
acountList[3] = new SavingsAccount(“S011”, “Bob”, 1200.0);
acountList[4] = new SavingsAccount(“S024”, “Jill”, 5000.0);
acountList[5] = new Account(“A049”, “David”, 500.0);
// create remaining objects and store them in the array
// (you do NOT need to do this)
// ...
⦁ Open a text file called “deposit-log.txt” for writing.
try
{
// your answer for requirement A) would go here
// ...
PrintWriter pw = ... ; // complete this statement![/B][/B][/SIZE][/SIZE]
--- Update ---
Account Class
public class Account {
private String accountNumber;
private String accountName;
private double balance;
public Account(String accountNumber, String accountName, double balance) {
this.accountNumber = accountNumber;
this.accountName = accountName;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public String getAccountName() {
System.out.println("Your account name is " + accountName);
return accountName;
}
public double getBalance() {
System.out.println("Your current balance is " + balance);
return balance;
}
public boolean withdaw (double amount){
if (balance >= amount){
balance = balance - amount;
}
return true;
}
public boolean deposit (double amount){
if (amount >=0){
balance = balance + amount;
}
return true;
}
public void print(){
System.out.printf("%-20s %30s \n", "Account Number:", accountNumber);
System.out.printf("%-20s %30s \n", "Acccount Name:",accountName);
System.out.printf("%-20s %30s \n","Balance:", balance);
}
}