Account Class Definition: Class Account Private Data Members: int accountId doub
ID: 3692837 • Letter: A
Question
Account Class Definition:
Class Account
Private Data Members:
int accountId
double balance
double annualInterestRate
Account ID
Current Balance of the account
Current Annual Interest Rate of the Account (ex. 4.5)
Public Member Functions:
Account()
Account(int id, double bal, double interest)
Constructors:
Default Constructor (no parameters)
Three-Parameter Constructor
Public Member Functions:
void setAccountId (int x)
void setBalance(double x)
void setInterest(double x)
Setters:
Function sets id to x
Function sets balance to x
Function sets annualInterestRate to x
Public Member Functions:
int getAccountId()
double getBalance()
double getInterest()
Getters:
Function returns accountId
Function returns balance
Function returns annualInterestRate
Public Member Functions:
double getMonthlyInterestRate()
Function calculates the monthly interest rate and returns the value
double getMonthlyInterest()
Function calculates the amount earned per month from interest and returns the value rounded to 2 decimal places. (Assume interest is not compounding)
Bool withdraw(double amount)
Function only allows withdraw if the current balance is greater than or equal to the withdraw amount. Return true if withdrawal is successful, else return false.
void deposit(double amount)
Function adds the amount passed as a parameter to the current balance.
Write a program that creates an array of 10 Account objects.
When creating each Account object use the following guidelines:
Each object’s accountId should be the index of its position within the array.
The balance of each Account object should be created from a random number generator function returning values between 10,000.00 and 20,000.00. This return value should be rounded to two decimal places.
The interest rate of each Account object should be created from a random number generator function returning values between 1.5 and 5.0. This return value should be rounded to one decimal place.
The program should then ask the user to select an account number from 0 – 9 or -1 to exit the program.
If an account number is selected, then the user should be presented with some options. The five main bullets should form the menu presented. If a user makes any of the top five selections, then the menu should be represented. The menu should only not be represented with an entry of -1.
Enter 1 to make a deposit
Ask the user for the amount they wish to deposit
Enter 2 to make a withdraw
Ask the user for the amount they wish to withdraw
Return if withdrawal was successful or unsuccessful depending on function return value
Enter 3 to check balance
Display the account’s current balance
Enter 4 to check interest rate
Display the account’s monthly and yearly interest rate
Enter 5 to display account summary
Display account id, balance, monthly interest rate, and monthly interest amount
Enter -1 to return to the main menu
This will return the user to the main menu prompting to select an account number
Sample Run:
Class Account
Private Data Members:
int accountId
double balance
double annualInterestRate
Account ID
Current Balance of the account
Current Annual Interest Rate of the Account (ex. 4.5)
Public Member Functions:
Account()
Account(int id, double bal, double interest)
Constructors:
Default Constructor (no parameters)
Three-Parameter Constructor
Public Member Functions:
void setAccountId (int x)
void setBalance(double x)
void setInterest(double x)
Setters:
Function sets id to x
Function sets balance to x
Function sets annualInterestRate to x
Public Member Functions:
int getAccountId()
double getBalance()
double getInterest()
Getters:
Function returns accountId
Function returns balance
Function returns annualInterestRate
Public Member Functions:
double getMonthlyInterestRate()
Function calculates the monthly interest rate and returns the value
double getMonthlyInterest()
Function calculates the amount earned per month from interest and returns the value rounded to 2 decimal places. (Assume interest is not compounding)
Bool withdraw(double amount)
Function only allows withdraw if the current balance is greater than or equal to the withdraw amount. Return true if withdrawal is successful, else return false.
void deposit(double amount)
Function adds the amount passed as a parameter to the current balance.
Explanation / Answer
import java.util.Date;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Account[] accounts = new Account[10];
for (int i = 0; i < accounts.length; i++) {
accounts[i] = new Account(0, 100);
}
// @SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
while (true) {
int id = -1;
while ((id >= 10) || (id < 0)) {
System.out.print(" Enter an id: ");
id = input.nextInt();
}
boolean continueThisAccount = true;
do {
int choise = -1;
while ((choise >= 5) || (choise < 1)) {
System.out.print(" Main menu 1: check balance 2: withdraw 3: deposit 4: exit Enter a choice: ");
choise = input.nextInt();
}
switch (choise) {
case 1:
System.out.println("The balance is " + accounts[id].getBalance());
break;
case 2:
System.out.print("Enter an amount to withdraw: ");
double amount = input.nextDouble();
accounts[id].withdraw(amount);
break;
case 3:
System.out.print("Enter an amount to deposit: ");
double deposit = input.nextDouble();
accounts[id].deposit(deposit);
break;
case 4:
continueThisAccount = false;
}
} while(continueThisAccount);
}
}
}
class Account {
private int id;
private double balance;
private static double annualInterestRate;
//private Date dateCreated = new Date();
Account() {
}
Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
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;
}
public double getMonthlyInterestRate() {
return (annualInterestRate / 100) / 12;
}
public double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
public void withdraw(double amount) {
if ((amount > 0) && (amount <= balance)) {
balance -= amount;
}
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
sample output
Enter an id: 1234567890
Enter an id: -1
Enter an id: 0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 100.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.