Need help with my \'Account\' class program. Basically, I need to make a java fi
ID: 671158 • Letter: N
Question
Need help with my 'Account' class program.
Basically, I need to make a java file that mimicks an ATM Machine (subclass of my Account class).
I need to follow these:
* Make 10 account objects with a random balance between 0-100. (done)
* Each account has a unique id (0-9) (done)
* Ask user for id number to access account (done)
* Display this menu (done)
1. Check balance
2. Withdraw
3. Deposit
4. Exit
* &Call to different methods for these options.
What I really need help with is making the menu appear again after entering an option (so that it returns back to the user). I was trying to use a do while loop, but just got confused.
And somehow making the account object interchangeable. (I'll explain).
This is what I have so far:
Account class
public class Account
{
//data fields for balance, id
//methods for deposit, withdraw
double balance;
int id;
public Account()
{
}
public Account(double balance, int id)
{
}
public int getId()
{
return id;
}
public double getBalance()
{
return balance;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public void setId(int id)
{
this.id = id;
}
public void Withdraw(double amount)
{
if (balance >= amount)
balance -= amount;//subtract amount if enough funds
else
System.out.println("Insufficient Funds.");
}
public void Deposit(double amount)
{
balance += amount; //add amount
}
}
ATM
import java.util.*;
import java.text.DecimalFormat;
import java.text.Format;
public class ATMMachine extends Account
{
public ATMMachine()
{
}
public static void main(String[] args)
{
//Creates Accounts
Account a1 = new Account();
Account a2 = new Account();
Account a3 = new Account();
Account a4 = new Account();
Account a5 = new Account();
Account a6 = new Account();
Account a7 = new Account();
Account a8 = new Account();
Account a9 = new Account();
Account a10 = new Account();
//Sets random balances
a1.setBalance(Math.random() * 100);
a2.setBalance(Math.random() * 100);
a3.setBalance(Math.random() * 100);
a4.setBalance(Math.random() * 100);
a5.setBalance(Math.random() * 100);
a6.setBalance(Math.random() * 100);
a7.setBalance(Math.random() * 100);
a8.setBalance(Math.random() * 100);
a9.setBalance(Math.random() * 100);
a10.setBalance(Math.random()* 100);
//Sets Ids
a1.setId(0);
a2.setId(1);
a3.setId(2);
a4.setId(3);
a5.setId(4);
a6.setId(5);
a7.setId(6);
a8.setId(7);
a9.setId(8);
a10.setId(9);
System.out.println(a9.getBalance());
//Ask the user to enter an account ID or q to terminate.
Scanner input = new Scanner(System.in);
System.out.println("Enter an account ID: ");
int id = input.nextInt();
//Main Menu
for (int i = 0; i <= 9; i++)
{
if (id == i)
{
System.out.println("Main Menu for Account {ID " + id + "} "
+ " 1. Check balance "
+ " 2. Withdraw "
+ " 3. Deposit "
+ " 4. Exit");
int choice = input.nextInt();
if (choice == 1)
{
ATMMachine.checkBalance(id, a9); ---- This is what Is what I mean by making account interchangable. I can't seem to get my code to work without specifying an exact account object. I need it to be what the user chooses. So if their ID number is 0, the account accessed would be a1.
}
if (choice == 2)
{
DecimalFormat f = new DecimalFormat("##.00");
System.out.println("Enter amount to withdraw: ");
double amount = input.nextDouble();
a9.Withdraw(amount);
System.out.println("The balance is now: $" + f.format(a9.getBalance()));
}
if (choice == 3)
{
DecimalFormat f = new DecimalFormat("##.00");
System.out.println("Enter amount to deposit: ");
double amount = input.nextDouble();
a9.Deposit(amount);
System.out.println("The balance is now: $" + f.format(a9.getBalance()));
}
/*Error Message*/
if (id != i)
System.out.println("Account cannot be found, please enter another ID.");
}
}
}
public static void checkBalance(int id, Account a)
{
DecimalFormat f = new DecimalFormat("##.00");
System.out.println("The balance is: $" + f.format(a.getBalance()) + " for ID: " + id);
}
public void exit()
{
}
Thank you!
Explanation / Answer
import java.util.Scanner;
public class BankTeller {
static Scanner input = new Scanner(System.in);
private int id, balance;
private String name;
BankTeller(String name, int id, int balance) {
this.name = name;
this.id = id;
this.balance = balance;
}
void display() {
System.out.println("Name:" + name);
System.out.println("Account id:" + id);
System.out.println("Balance:" + balance);
}
public static void main(String args[]){
System.out.println("Enter your name: ");
String name = input.nextLine();
System.out.println("Please enter your account id: ");
int id = input.nextInt();
System.out.println("Enter initial balance: ");
int balance = input.nextInt();
BankTeller a1 = new BankTeller(name, id, balance);
int menu;
System.out.println(" 1. Print balance");
System.out.println(" 2. Deposit");
System.out.println(" 3. Withdraw");
System.out.println(" 4. Exit");
boolean quit = false;
do {
System.out.print("Please enter your choice: ");
menu = input.nextInt();
switch(menu) {
case 1:
Account.getBalance();
break;
case 2:
Account.depositAmount();
break;
case 3:
Account.withdrawAmount();
break;
case 4:
quit = true;
break;
}
}
while(!quit);
}
}
customer.java
public class Customer {
private int id;
private String name;
private Account account;
//declare integers
public Customer(int customerId, String customerName)
{
id = customerId;
name = customerName;
}
//method to set id
public void setId(int id)
{
this.id = id;
}
//method to get id
public int getId()
{
return id;
}
//method to set name
public void setName(String name)
{
this.name = name;
}
//method to get name
public String getName()
{
return name;
}
//account types
}
Account.java
import java.util.Scanner;
import java.util.Scanner;
public class Account {
private static int id;
private static int balance = 0;
static //scanner
Scanner input = new Scanner(System.in);
//declare stuff
public Account(int customerId, int accountBalance) {
id = customerId;
balance = accountBalance;
}
//method to set id
public void setId(int id) {
this.id = id;
}
//method to get id
public static int getId() {
return id;
}
//method to get balance
public static int getBalance() {
return balance;
}
//method to deposit
public static void deposit(int depositAmount) {
System.out.println("Enter an amount to deposit:");
depositAmount = input.nextInt();
if (depositAmount < 0) {
System.out.print("Invalid Amount!");
}
balance = balance + depositAmount;
}
//method to withdraw
public int withdraw(int withdrawAmount) {
System.out.println("Your Balance is: " + balance);
System.out.println("Enter amount to withdraw: ");
withdrawAmount = input.nextInt();
if (withdrawAmount > balance) {
System.out.println("You do not have a sufficient balance to withdraw this amount!");
return 1;
}
if (withdrawAmount < 0) {
System.out.println("Invalid Amount!");
return 1;
}
balance = balance - withdrawAmount;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.