Question
JAVA Create an Account class with the following member data:1. Account ID (a random 4-digit number that is generated when the object is created)2. Balance
JAVA
Create an Account class with the following member data:1. Account ID (a random 4-digit number that is generated when the object is created)2. Balance (starts at $0.00 until a deposit is made)Your program will read in commands from an input file and perform the correspondingtransactions on an instance of Account: w - withdraw from the account (e.g. w 50 means withdraw $50). Print a message statinghow much was withdrawn from the account. If the account will be overdrawn, cancel
the withdraw and print an error message. d - deposit into the account (e.g. d 10 means deposit $10). Print a message stating howmuch was deposited into the account. i - add interest to the account. Print a message stating how much interest was added tothe account. The interest rate is %0.5 (e.g. i means multiply the current balance by1.005). The interest rate should be stored as a final variable inside your class. p - prints the current balance of the account, formatted to 2 decimal places.
Notes Your program should utilize 2 separate files: Prog8.java (for your main class) and Account.java (for your Account class) You must make appropriate use of objects in this program All member data of class Account should be private. You should make appropriate use ofconstructors and getter/setter methods to interact with your data.
Record each transaction (up to 5 per instance of account), and print out the accounts history whenever the p command is read from the input file.
Print a message with the account ID every time a new instance of Account is created(this should happen at the very beginning of your program)
SAMPLE INPUT/OUTPUT:
I have most of it except for the last two bullet points. This is my code so far:
import java.util.Scanner;
public class Prog8 { public static void main(String[] args) { Scanner reader = new Scanner(System.in); Account myAccount = new Account(); char type; double amount;
while(reader.hasNext()) { type = reader.next().charAt(0);
if(type == 'd'){ amount = reader.nextDouble(); myAccount.deposit(amount);
System.out.printf(" $%.2f deposited into account %d ", amount, myAccount.getAccountNumber()); }
else if(type == 'w'){ amount = reader.nextDouble(); myAccount.withdraw(amount); System.out.printf(" $%.2f withdrawn from account %d ", amount, myAccount.getAccountNumber()); }
else if(type == 'i'){ double intAdded = myAccount.addInterest(); System.out.printf(" $%.2f interest added to account %d ", intAdded, myAccount.getAccountNumber()); }
else if(type == 'p'){ System.out.printf(" Current balance for account %d: $%.2f", myAccount.getAccountNumber(), myAccount.getBalance()); } } System.out.println(); } }
******************
class Account{
private int accountNumber; private double balance; public Account(){
//Generating a random 4 digit Account Number accountNumber = (int)(Math.random() * 9000) + 1000; balance = 0.0; } //Getter method for Account Number public int getAccountNumber(){ return accountNumber; } //Method that adds amount to balance public void deposit(double amt){ //Updating new balance balance = balance + amt; } //Method that withdraws amount from balance public void withdraw(double amt){ //Validating amount if((balance - amt) File in dat w o 10 p 0 cution Account 9721 created 0.00 deposited into account 9721 $5 $10.00 ithdrawn account 9721 $0.20 interest added to account 721 Transaction history for account 9721 550.00 deposited $10.00 W. ithdrawn. 50 20 earned in interest Current balance: 40.20
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started