Problem Description Design a class named Account that contains: A private int da
ID: 3760668 • Letter: P
Question
Problem Description
Design a class named Account that contains:
A private int data filed named accountID for the account
A private double data field named balance for the account.
A private double data field named annualInterestRate that stores the current interest rate. Assume all accounts have the same interest rate.
A no-arg constructor that creates a default account.
A constructor that creates an account with the specified account ID and balance
The accessor and mutator methods for balance, accountID, and annualInterestRate.
A method named getMonthlyInterest() that returns the monthly interest.
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 the return a string containing account information (accountID, balance and annualInterestRate)
Specifications
Write a test program that will ask the users to enter their account ID, balance in their account and annual interest rate. Then it creates the account object and print the account ID, balance, and the annual interest rate.
Prompt the user whether they have a transaction. If they have, ask for the following info:
Amount and transaction type (0 for withdrawal and 1 for deposit)
Number of days passed since the previous transaction.
Your program should calculate the current balance and display that along with the accountID.
During a withdrawal, if the balance would become negative, that transactions should be cancelled. The user should be reported about the cancelled transaction.
Steps 2 - 4 repeat as long as the user has a transaction.
Please note: The interest entered at the beginning is the annual interest. You will need to convert it to an appropriate amount for daily interest. Assume 30 day month.
Explanation / Answer
//Account.java
public class Account
{
//account variables
private int accountID;
private double balance;
private double annualInterestRate;
//Constructor sets default values
public Account()
{
accountID=0;
balance=0;
annualInterestRate=0;
}
//Parameterized constructor
public Account(int accountID, double balance,
double annualInterestRate)
{
this.accountID=accountID;
this.balance=balance;
this.annualInterestRate=annualInterestRate;
}
//Set account id
public void setID(int accountID)
{
this.accountID=accountID;
}
//Set balance
public void setBalance(double balance)
{
this.balance=balance;
}
//Set annual interest rate
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate=annualInterestRate;
}
//Returns account id
public int getID()
{
return accountID;
}
//Returns balance
public double getBalance()
{
return balance;
}
//Returns annual interest rate
public double getAnnualInterestRate()
{
return annualInterestRate;
}
//Returns monthly interest
public double getMonthlyInterest()
{
return balance*((annualInterestRate/100)/12.0);
}
//Withdraw amount from account balance
public void withdraw(double amount)
{
balance=balance-amount;
}
//deposit amount to account balance
public void deposit(double amount)
{
balance=balance+amount;
}
//returns id,balance and annual interest rate
@Override
public String toString()
{
return String.format("ID : %-10d Balance :%-10.2f Ann.Intereset%-10.2f ", accountID,balance,annualInterestRate);
}
}//end of the Account class
--------------------------------------------------
/**
* The java class AccountTestProgram
* that tests the account class.
* */
//AccountTestProgram.java
import java.util.Scanner;
public class AccountTestProgram
{
public static void main(String[] args)
{
//create an instance of Scanner class
Scanner keyboard=new Scanner(System.in);
int accountID;
double balance;
double annualInterestRate;
System.out.println("Enter account id");
//read account id
accountID=keyboard.nextInt();
System.out.println("Enter balance");
//read balance
balance=keyboard.nextDouble();
System.out.println("Enter annual interest rate");
//read annual interest rate
annualInterestRate=keyboard.nextDouble();
//Create an Account object
Account account=new Account(accountID, balance, annualInterestRate);
System.out.println(account.toString());
System.out.println("Will you do transaction [y for yes][n for n]?");
char choice;
//read choice
choice=keyboard.next().charAt(0);
//prompt until user enters n to stop
while(choice=='y'||choice=='Y')
{
double amount;
int transactionType;
int days;
System.out.println("Enter amount");
amount=keyboard.nextDouble();
System.out.println("Enter transaction type [0 for withdraw,1 for deposit]");
transactionType=keyboard.nextInt();
System.out.println("How many days has been passed from last transaction ?");
days=keyboard.nextInt();
keyboard.nextLine();
if(transactionType==1)
{
double intPerDay=account.getMonthlyInterest()/30;
double currentBalance=account.getBalance()
+days*intPerDay;
//set currentBalance to the account
account.setBalance(currentBalance);
//print account number and current balance
System.out.printf("Account ID : %-5d Current Balance : %-10.2f ",
account.getID(),account.getBalance());
}
//check if transaction type is zero
else if(transactionType==0)
{
//check if amount is less than zero
if(account.getBalance()-amount<0)
System.out.println("Traction of account id "+account.getID()
+" is cancelled.");
else
account.withdraw(amount);
}
//prompt for another transaction
System.out.println("Will you do transaction [y for yes][n for n]?");
choice=keyboard.next().charAt(0);
}
}
}//end of class
------------------------------------------------------------------------
Sample Output:
Enter account id
1234
Enter balance
1000
Enter annual interest rate
10
ID : 1234 Balance :1000.00 Ann.Intereset10.00
Will you do transaction [y for yes][n for n]?
y
Enter amount
100
Enter transaction type [0 for withdraw,1 for deposit]
1
How many days has been passed from last transaction ?
100
Account ID : 1234 Current Balance : 1027.78
Will you do transaction [y for yes][n for n]?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.