Question

JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance...

JAVA PLEASE

Create a class called Account with the following instance data

  • Integer id
  • Double balance

Provides the following methods

  • Default constructor (defaults balance to 100)
  • Constructor with parameters for the ID and the starting balance
  • Accessor and mutator methods for id and balance
  • Method to perform a withdrawal
  • Method to perform a deposit

Create a class called Bank which has an array of Account objects. This class will manage the accounts. It must provide methods

  • Do withdrawal
  • Do deposits
  • Display the account balances
  • Method to set the current account ID

Main will start by asking the user for an ID. Then it display a menu with options to Check Balance, Withdraw, Deposit and Exit. For the first 3 options the ID must be passed to the methods in Bank so it know which account to perform the transaction on. All of the operations will be on the same account until the user selects exit. The Exit option does not exit the program, the program prompts the user for another ID. Once the ID is entered the menu is display again.

The bank class will search the array for the account with the matching ID. If no matching account number then the user told the ID is invalid and is prompted again for a valid ID

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey, I have attached the codes below. The codes has comments for explanation. If you have any problem, feel free to discuss it with me.

Account.java:

public class Account{
    private int id;
    private int balance;

    public Account() {
        balance = 100;
    }

    public Account(int id, int balance) {
        this.id = id;
        this.balance = balance;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBalance() {
        return this.balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public boolean deposit(int amount){
        //returns if deposit is successfeull or not
        if(amount < 0){
            //negative amounts can not be deposited
            return false;
        }
        //add money to balance
        balance += amount;
        return true;
    }

    public boolean withdraw(int amount){
        if(amount > balance){
            //balace is insufficient
            return false;
        }
        //otherwise
        balance -= amount;
        return true;
    }

}

Code Screenshot:

Bank.java:

import java.util.Scanner;

public class Bank{
    private Account[] accounts;//stores accounts
    private int currentAccount; // the selected account
    //default constructor
    public Bank(){
        //allocate the array for 10 accounts
        accounts = new Account[10];
        //set id from 0 to 9
        for(int i = 0; i < 10 ; i++){
            accounts[i]= new Account(i , 100);
        }
        currentAccount = 0; //defualt 0 id
    }
    // constructor with given number of accounts
    public Bank(int n){
        //allocate the array for n accounts
        accounts = new Account[n];
        //set id from 0 to n-1
        for(int i = 0; i < n ; i++){
            accounts[i]= new Account(i , 100);
        }
        currentAccount = 0;
    }

    public void doDeposit(int amount){
        System.out.println("Deposit of $" + amount + " successfull: " + accounts[currentAccount].deposit(amount));
    }
    public void doWithdraw(int amount){
        System.out.println("Withdraw of $" + amount + " successfull: " + accounts[currentAccount].withdraw(amount));
    }

    public boolean setCurrentAccount(int id){
        if(id < 0 || id >= accounts.length){
            //id is out of range
            System.out.println("Account with given id does not exist");
            return false;
        }
        else{
            currentAccount = id;
            return true;
        }
    }

    public void displayBalance(){
        System.out.println("Account " + currentAccount + " has balance: $" + accounts[currentAccount].getBalance());
    }

    //main method
    public static void main(String[] args){
        //get a scanner object
        Scanner scn = new Scanner(System.in);
        //get a default bank object
        Bank bank = new Bank();
        while(true){
            System.out.print("Enter bank account id (Enter -1 to exit): ");
            int id = scn.nextInt();
            if(id == -1)
                return;
            //ask until a valid id is input
            while(!bank.setCurrentAccount(id)){
                System.out.print("Enter bank account id: ");
                id = scn.nextInt();
                if(id == -1)
                    return;
            }
            // got a valid id now chow menu
            System.out.println("1) Check Balance");
            System.out.println("2) Withdraw");
            System.out.println("3) Deposit");
            System.out.println("4) Exit");
            int amount;
            boolean exit = false;
            while(!exit){
                System.out.print("Choose: ");
                int choice = scn.nextInt();
                switch(choice){
                    case 1:
                        bank.displayBalance();
                        break;
                    case 2:
                        System.out.print("Enter amount: ");
                        amount = scn.nextInt();
                        bank.doWithdraw(amount);
                        break;
                    case 3:
                        System.out.print("Enter amount: ");
                        amount = scn.nextInt();
                        bank.doDeposit(amount);
                        break;
                    case 4:
                        exit = true; //set exit flag to true
                        break;
                    default:
                        //invalid choice
                        System.out.println("Invalid choice");
                }
            }
        }
    }
}

Code Screenshots:

Sample Output:

Add a comment
Know the answer?
Add Answer to:
JAVA PLEASE Create a class called Account with the following instance data Integer id Double balance...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named UserAccounts that defines an array of 8 accounts as an instance variable....

    Create a class named UserAccounts that defines an array of 8 accounts as an instance variable. In the default constructor of this class, write a loop that creates 8 accounts with ids 1 through 7 and initial balance of $50 for each account. Store these accounts in the array. When the program runs, it asks the use to enter a specific id. When the user enters a correct id, the system displays a menu as shown in the sample run...

  • In Java: DATA TYPE CLASS Create one data type class Account_yourLastName that hold the information of...

    In Java: DATA TYPE CLASS Create one data type class Account_yourLastName that hold the information of an account such as accountNumber (String), name (String), address (String), balance (float), interestRate(float) and beside the no-argument constructor, parameterized constructor, the class has method openNewAccount, method checkCurrentBalance, method deposit, method withdrawal, method changeInterestRate REQUIREMENT - DRIVER CLASS Provide the application for the Bank Service that first displays the following menu to allow users to select one task to work on. After finishing one task,...

  • *****language : java****** ceate a class RightTriangle with three instance variables of type double called base,...

    *****language : java****** ceate a class RightTriangle with three instance variables of type double called base, height, and hypotenuse and three instance variables of type int called ID, xLoc and yLoc. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Then define an appropriate constructor that takes initial values for all instance variables except hypotenuse and sets the values of all instance variables including calculating the hypotenuse. Then define...

  • this is for java programming Please use comments Use the Account class created in homework 8_2...

    this is for java programming Please use comments Use the Account class created in homework 8_2 to simulate an ATM machine. Create ten checking accounts in an array with id 0, 1, …, 9, initial balance of $100, and annualInterestRate of 0. The system prompts the user to enter an id between 0 and 9, or 999 to exit the program. If the id is invalid (not an integer between 0 and 9), ask the user to enter a valid...

  • Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement...

    Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement a constructor that takes the balance (float). –Provide abstract methods deposit(amount) and withdraw(amount) •Design a class called SavingsAccount which extends BankAccount. –Implement a constructor that takes the balance as input. –It should store a boolean flag active. If the balance at any point falls below $25, it sets the active flag to false. It should NOT allow the balance to fall below $0. –If...

  • Java Code: 2. Define a class ACCOUNT with the following members: Private members Acno of type...

    Java Code: 2. Define a class ACCOUNT with the following members: Private members Acno of type int Name of type String Balance of type float Public members void Init) method to enter the values of data members void Show) method to display the values of data members void Deposit(int Amt) method to increase Balance by Amt void Withdraw(int Amt) method to reduce Balance by Amt (only if required balance exists in account) float RBalance() member function that returns the value...

  • Design a bank account class named Account that has the following private member variables:

    Need help please!.. C++ programDesign a bank account class named Account that has the following private member variables: accountNumber of type int ownerName of type string balance of type double transactionHistory of type pointer to Transaction structure (structure is defined below) numberTransactions of type int totalNetDeposits of type static double.The totalNetDeposits is the cumulative sum of all deposits (at account creation and at deposits) minus the cumulative sum of all withdrawals. numberAccounts of type static intand the following public member...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • C++ Design a class bankAccount that defines a bank account as an ADT and implements the...

    C++ Design a class bankAccount that defines a bank account as an ADT and implements the basic properties of a bank account. The program will be an interactive, menu-driven program. a. Each object of the class bankAccount will hold the following information about an account: account holder’s name account number balance interest rate The data members MUST be private. Create an array of the bankAccount class that can hold up to 20 class objects. b. Include the member functions to...

  • in java Account that contains: balance: double data field date: Date data field. Use Date class...

    in java Account that contains: balance: double data field date: Date data field. Use Date class from the java.util package accountNumber: long data field. You should generate this value randomly. The account number should be 9 digits long. You can you random method from java Math class. annuallnterestRate: double data field. customer: Customer data field. This is the other class that you will have to design. See description below. The accessor and mutator methods for balance, annuallnterestRate, date, and customer....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT