Java program I made this Account.java below. Using the attached code I need help
ID: 3786990 • Letter: J
Question
Java program
I made this Account.java below. Using the attached code I need help with 10.7 (Game: ATM machine)
Use the Account class created in Programming Exercise 9.7 to simulate an ATM machine.
Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100.
The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id.
Once an id is accepted, the main menu is displayed as shown in the sample run.
You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu.
Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.
*/
import java.util.Date;
public class Account {
/**
* @param args
*/
private int id=0;
private double balance=0;
private double annualIntrestRate=0;
private Date dateCreated;
public Account() {
super();
}
public Account(int id, double balance) {
super();
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 double getAnnualIntrestRate() {
return annualIntrestRate;
}
public void setAnnualIntrestRate(double annualIntrestRate) {
this.annualIntrestRate = annualIntrestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public double getMonthlyInterestRate()
{
return (this.getAnnualIntrestRate()/12);
}
public double getMonthlyInterest()
{
return (getBalance() *getMonthlyInterestRate()/100);
}
public double withDraw(double balance)
{
this.setBalance(this.getBalance()-balance);
return this.getBalance();
}
public double diposite(double balance)
{
this.setBalance(this.getBalance()+balance);
return this.getBalance();
}
public double totalBalance()
{
balance =balance + getMonthlyInterest();
return balance;
}
}
//AccountTest.java
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class AccountTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
Account ac=new Account(1,5000.00);
System.out.println("Enter the annual intrest rate");
double intrestRate=sc.nextDouble();
ac.setAnnualIntrestRate(intrestRate);
Date d=new Date();
Calendar currentDate = Calendar.getInstance();
ac.setDateCreated(currentDate.getTime());
System.out.println("Date id "+ac.getDateCreated());
System.out.println("Monthly intrest rate is :"+ac.getMonthlyInterestRate());
System.out.println("Monthly intrest is :"+ac.getMonthlyInterest());
System.out.println("Enter Amount for diposite ");
double dipositeAmount=sc.nextDouble();
System.out.println("The amount after diposite is :"+ac.diposite(dipositeAmount));
System.out.println("Enter Amount to withdraw :");
double withdramount= sc.nextDouble();
System.out.println("The amount remain after with draw "+ac.withDraw(withdramount));
System.out.println("The total amount is "+ac.totalBalance());
}
}
/**
Sample output :
Enter the annual intrest rate
2.5
Date id Thu Mar 07 04:55:38 IST 2013
Monthly intrest rate is :0.208
Monthly intrest is :10.42
Enter Amount for diposite
300
The amount after diposite is :5300.0
Enter Amount to withdraw :
3000
The amount remain after with draw 2300.0
The total amount is 2304.79
*/
Explanation / Answer
//Account.java
//Account represents the object of Account class
import java.util.Date;
public class Account {
/**
* @param args
*/
private int id=0;
private double balance=0;
private double annualIntrestRate=0;
private Date dateCreated;
public Account() {
super();
}
public Account(int id, double balance) {
super();
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 double getAnnualIntrestRate() {
return annualIntrestRate;
}
public void setAnnualIntrestRate(double annualIntrestRate) {
this.annualIntrestRate = annualIntrestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public double getMonthlyInterestRate()
{
return (this.getAnnualIntrestRate()/12);
}
public double getMonthlyInterest()
{
return (getBalance() *getMonthlyInterestRate()/100);
}
public double withDraw(double balance)
{
this.setBalance(this.getBalance()-balance);
return this.getBalance();
}
public double diposite(double balance)
{
this.setBalance(this.getBalance()+balance);
return this.getBalance();
}
public double totalBalance()
{
balance =balance + getMonthlyInterest();
return balance;
}
}
------------------------------------------------------------------------------------------------------------------------
/**
* The java program ATMSimulation that prompts user to enter
* an id number and re prompt if user id is not valid.
* Then prints the display menu that contains user choices.
* Then prints the correspoding output. Then repeats the
* menu.
* */
//ATMSimulation.java
import java.util.Scanner;
public class ATMSimulation {
public static void main(String[] args) {
//declare an array of type Account of size=10
final int SIZE=10;
Account[] accts=new Account[SIZE];
//set id and amount
for (int id = 0; id < accts.length; id++) {
accts[id]=new Account(id, 100.0);
}
Scanner scan=new Scanner(System.in);
int userId;
boolean repeat=true;
//Run the program
while(true)
{
do
{
//promot for user id
System.out.println("Enter your id number [0-9]: ");
userId=scan.nextInt();
if(userId<0 || userId>10)
System.out.println("Invalid id number.");
}while(userId<0 || userId>10);
//Get account into holder object
Account holder=accts[userId];
double amt=0;
repeat=true;
//repeat until user enter 4 to stop
while(repeat)
{
int choice=menu();
switch(choice)
{
case 1:
System.out.println("Balance : "+holder.totalBalance());
break;
case 2:
System.out.println("Enter amount to withdraw :");
amt=scan.nextDouble();
holder.withDraw(amt);
break;
case 3:
System.out.println("Enter amount to deposit :");
amt=scan.nextDouble();
holder.diposite(amt);
break;
case 4:
repeat=false;
break;
}
}
}
}
//Method menu display a menu of choices to users to select
private static int menu() {
Scanner scan=new Scanner(System.in);
int choice;
do
{
System.out.println("1.Viewcurrent balance");
System.out.println("2.Withdrawing money");
System.out.println("3.Depositing money");
System.out.println("4.Exit menu.");
choice=scan.nextInt();
if(choice<0 || choice>4)
System.out.println("Invalid choice");
}while(choice<0 || choice>4);
return choice;
}
}
------------------------------------------------------------------------------------------------------------------------
Output:
Enter your id number :
1
1.Viewcurrent balance
2.Withdrawing money
3.Depositing money
4.Exit menu.
1
Balance : 100.0
1.Viewcurrent balance
2.Withdrawing money
3.Depositing money
4.Exit menu.
3
Enter amount to deposit :
2500
1.Viewcurrent balance
2.Withdrawing money
3.Depositing money
4.Exit menu.
4
Enter your id number :
5
1.Viewcurrent balance
2.Withdrawing money
3.Depositing money
4.Exit menu.
1
Balance : 100.0
1.Viewcurrent balance
2.Withdrawing money
3.Depositing money
4.Exit menu.
5
Invalid choice
1.Viewcurrent balance
2.Withdrawing money
3.Depositing money
4.Exit menu.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.