The question is: Use the Account class created in exercise 8.7 (I will add my co
ID: 3634558 • Letter: T
Question
The question is:Use the Account class created in exercise 8.7 (I will add my code for that problem at the bottom) to simulate an ATM machine. Create ten accounts in an arrray 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 menu. Once you exit the system will prompt for an id again. So once the system starts it will not stop.
Books example upon completion:
Enter an id: 4 (enter)
main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1 (enter)
the balance is 100.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 2 (enter)
Enter an amount to withdraw: 3 (enter)......
Heres my account class:
public class account {
public static void main(String[] args) {
int id = 1122;
double balance = 20000;
double annualinterestrate = 0.045;
hereaccount account = new hereaccount(id, balance, annualinterestrate);
account.withdraw(2500);
account.deposit(3000);
account.dateCreated();
account.monthlyinterestrate(annualinterestrate);
}
}
import java.util.*;
public class hereaccount {
private int id;
private double balance;
private double annualinterestrate;
private Date date;
public hereaccount() {
id = 0;
balance = 0;
annualinterestrate = 0;
}
public hereaccount(int id, double balance, double annualinterestrate) {
this.id = id;
this.balance = balance;
this.annualinterestrate = annualinterestrate;
date = new Date();
}
public double getbalance() {
return balance;
}
public Date dateCreated(){
System.out.println("Date created: " + date);
return date;
}
public int id() {
return id;
}
public double annualinterestrate() {
return annualinterestrate;
}
public void deposit(double amount) {
balance = balance + amount;
System.out.println("Your current balance is now: " + balance);
}
public void withdraw(double amount) {
balance = balance - amount;
System.out.println("Your current balance is now: " + balance);
}
public void setID(int newID) {
id = newID;
}
public double monthlyinterestrate(double interest){
interest = annualinterestrate/12;
System.out.println("The monthly interest rate is: " + interest);
return interest;
}
}
Explanation / Answer
Please Rate:Thanks
This code will help you
/*Create a class Account.java*/
//needed util.*
import java.util.*;
//create class account
class Account
{
// declare variable balance
private double balance;
//create constructor account
Account(double money)
{
// let balance=money
balance = money;
}
// create empty constructor
Account()
{
balance = 0.0;
}
// create method setBalance
public void setBalance(double Num)
{
//assign num to balance
balance = Num;
}
//create method getBalance
public double getBalance()
{
// return balance
return balance;
}
// create method Withdraw
public double Withdraw(double Num)
{
// checks the balnce is available or not
if(Num <=0 || Num > balance)
return -1;
// ask for withdraw amount
System.out.println("Withdrawing of amount " + Num);
// change balance
balance= balance - Num;
// return num
return Num;
}
// create method getWithdraw
public double getWithdraw()
{
// return balance
return balance;
}
// Create method Deposit for deposit amount in account
public double Deposit(double Num)
{
if(Num <=0)
return -1;
// Ask for deposit amount
System.out.println("Depositing of amount " + Num);
// modify the balance
balance = balance + (double)Num;
//return num
return Num;
}
// create method GetDeposit
public double getDeposit()
{
// return balance
return balance;
}
//create method zeroBalnace
public void zerobalance(double Num)
{
//if balance is 0 closse the account
System.out.println("Closing the Account");
balance = 0.0;
} // end method
} //end class
------------------------------------------------------------------------------
// create class ATM
// needed util. Scanner
import java.util.Scanner;
class ATM
{
// Main Method
public static void main(String args[])
{
// assign id numbers from 0 to 9
int id[]={0,1,2,3,4,5,6,7,8,9};
// set balance as $100 in each account
float ibal[]={100,100,100,100,100,100,100,100,100,100};
// define variables
char ch;
String str;
float amt;
boolean c=false;
// Declare Scanner
Scanner input = new Scanner(System.in);
int sid;
// while loop begins
while(true)
{
// Ask the user to enter any account id from 0 to 9
System.out.println("Enter an id from 0 to 9 ");
// get input into sid
sid=input.nextInt();
// check the account id is in b/w 0 to 9
if(sid<10)
{
// if account id is correct assign the balance in account1
Account account1 = new Account(ibal[sid]);
while(true)
{
// Display menu
if(!c)
System.out.println(" 1. (check balance) 2. (Withdrawal) 3. (Deposit) 4. (Quit) Enter Choice");
//read the option from 1 to 4 into str
str=input.nextLine();
if(str.length()==1)
ch=str.charAt(0);
else
{ c=true;
continue;
}
c=false;
try{
// if the option is 1 display the account balance
if(ch == '1')
{
System.out.println(" The balance is " +account1.getBalance());
}
// if the option is 2 then withdraw amount
else
if(ch == '2')
{
// ask the user to enter amount for withdraw
System.out.println("Enter amount :");
// get amount into amt
amt = input.nextFloat();
if(account1.Withdraw(amt)!=-1)
//display message withdraw success if account has enough balance
System.out.println("Withdrawal success");
Else
// if there is no sufficient balance in account display the below message
System.out.println("Can't withdraw this amount "+ amt);
}
Else
// if your choice is 3 deposit amount into your account
if(ch =='3')
{
// enter amount to deposit
System.out.println("Enter amount :");
// read the amount into amt
amt = input.nextFloat();
if(account1.Deposit(amt)!=-1)
// display message deposit success
System.out.println("Deposit Success");
else
System.out.println("Deposit Failure");
}
Else
// if choice is 4 exit from the loop
if(ch =='4')
break;
}catch(Exception e) {
System.out.println("Invalid intput ");
continue;
}
}
}
else
{
// if you enter id >10 the loop will break
break;
}
}
}// end main method
}// end class
-----------------------------------------------------------------------------------
Output:
Enter an id from 0 to 9
4
1. (check balance)
2. (Withdrawal)
3. (Deposit)
4. (Quit)
Enter Choice
1
The balance is 100.0
1. (check balance)
2. (Withdrawal)
3. (Deposit)
4. (Quit)
Enter Choice
2
Enter amount :
3
Withdrawing of amount 3.0
Withdrawal success
1. (check balance)
2. (Withdrawal)
3. (Deposit)
4. (Quit)
Enter Choice
1
The balance is 97.0
1. (check balance)
2. (Withdrawal)
3. (Deposit)
4. (Quit)
Enter Choice
3
Enter amount :
10
Depositing of amount 10.0
Deposit Success
1. (check balance)
2. (Withdrawal)
3. (Deposit)
4. (Quit)
Enter Choice
1
The balance is 107.0
1. (check balance)
2. (Withdrawal)
3. (Deposit)
4. (Quit)
Enter Choice
4
Enter an id from 0 to 9
10
BUILD SUCCESSFUL (total time: 57 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.