****Please check sample output first THANK YOU**** Design a class named Account
ID: 3712346 • Letter: #
Question
****Please check sample output first THANK YOU****
Design a class named Account that contains:
·A private int data field named id for the account (default 0).
·A private double data field named balance for the account (default 0).
·A private double data field named annualInterestRate that stores the current interest rate (default 0). All accounts have the same interest rate (static variable).
·A private Date data field named dateCreated that stores the date when the account was created. (import java.util.Date)
·A no-argument constructor that creates a default account.
·A constructor that creates an account with the specified id and initial balance.
·The accessor and mutator methods for id, balance, and annualInterestRate.
·A method named getMonthlyInterest() that returns the monthly interest that is accrued on the current balance.
·A method named withdraw that withdraws a specified amount from the account.
·A method named deposit that deposits a specified amount to the account.
·A toString method which will print the current Account Id, the balance, the monthly interest and the date when the account was created. (FYI. The Date class also contains a
toString() method which will provide a String of the date created)
Write a test program that creates an array of Account objects, using a constant
MAX_ACCOUNTS to define the size of the Account array.
.Prompt the user if they would like to create an account. If so, create an Account in the array and prompt the user for the Account Id and the initial balance.
.Prompt the user for the interest rate, a withdrawal amount and a deposit amount
.Add an option to display all Accounts so you can see how changing the interest rate for one account will change the interest rate for all accounts.
Sample Output
1. Add new Account
2. Display Accounts
3. Exit
Choose: 1
Enter Account ID: 2211
Enter Account Balance: 20000
Enter Account Interest Rate: 4.5
Enter Amount to Withdrawal: 2500
Enter Amount to Deposit: 3000
Account : 2211
Balance : $20500.00
Interest Rate : 4.50
Monthly Interest: $76.88
Date Created : Sat Oct 28 11:41:10 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 2
Account : 2211
Balance : $20500.00
Interest Rate : 4.50
Monthly Interest: $76.88
Date Created : Sat Oct 28 11:41:10 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 1
Enter Account ID: 394922
Enter Account Balance: 10000
Enter Account Interest Rate: 12.25
Enter Amount to Withdrawal: 100
Enter Amount to Deposit: 3000
Account : 394922
Balance : $12900.00
Interest Rate : 12.25
Monthly Interest: $131.69
Date Created : Sat Oct 28 11:41:35 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 2
Account : 2211
Balance : $20500.00
Interest Rate : 12.25
Monthly Interest: $209.27
Date Created : Sat Oct 28 11:41:10 PDT 2017
Account : 394922
Balance : $12900.00
Interest Rate : 12.25
Monthly Interest: $131.69
Date Created : Sat Oct 28 11:41:35 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 3
Explanation / Answer
Solution:
Please find below the code
Account.java
package ds;
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private static double annualInterestRate = 0;
private Date dateCreated;
/**
* Default constructor
*/
public Account() {
this.dateCreated = new Date(System.currentTimeMillis());
}
/**
* Parameterized constructor
* @param id
* @param initialBalance
*/
public Account(int id, double initialBalance) {
this.id = id;
this.balance = initialBalance;
this.dateCreated = new Date(System.currentTimeMillis());
}
/**
* accessor and mutator methods
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public static void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
/**
* A method named getMonthlyInterest()
* that returns the monthly interest that
* is accrued on the current balance.
*/
public double getMonthlyInterest(){
return (this.getBalance() * (Account.getAnnualInterestRate()/100))/12;
}
/**
* A method named withdraw that
* withdraws a specified amount
* from the account.
* @param amount
*/
public void deposit(double amount){
this.setBalance(this.getBalance() + amount);
}
/**
* A method named deposit that deposits
* a specified amount to the account.
*/
public void withdraw(double amount){
this.setBalance(this.getBalance() - amount);
}
/**
* toString() method
*/
@Override
public String toString() {
return "Account :" + id + " " +
"Balance :" + balance + " " +
"Interest Rate :" + Account.annualInterestRate + " " +
"Monthly Interest :" + this.getMonthlyInterest() + " " +
"Date Created :" + dateCreated.toGMTString() + " ";
}
}
AccountRunner.java
package ds;
import java.util.Arrays;
import java.util.Scanner;
public class AccountRunner {
public static void main(String [] args){
Scanner scanner = new Scanner(System.in);
final int MAX_ACCOUNTS = 3;
Account[] accounts = new Account[MAX_ACCOUNTS];
int userChoice;
int i = 0;
do{
System.out.println("1. Add new Account");
System.out.println("2. Display Accounts");
System.out.println("3. Exit");
System.out.print("Choose:");
userChoice = -1;
if (scanner.hasNextInt()){
userChoice = scanner.nextInt();
switch (userChoice) {
case 1:
System.out.print("Enter Account ID: ");int id = scanner.nextInt();
System.out.print("Enter Account Balance:");double balance = scanner.nextDouble();
System.out.print("Enter Account Interest Rate:");double interest = scanner.nextDouble();
System.out.print("Enter Amount to Withdrawal:");double withrawal = scanner.nextDouble();
System.out.print("Enter Amount to Deposit:");double deposit = scanner.nextDouble();
Account account = new Account(id, balance);
Account.setAnnualInterestRate(interest);
account.deposit(deposit);
account.withdraw(withrawal);
Arrays.asList(accounts).add(account);
break;
case 2:
for(Account accountObject : accounts){
accountObject.toString();
}
break;
case 3:
System.exit(0);
break;
default: System.out.println("Invalid choice");
}
}
}while (userChoice != 3);
scanner.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.