Answer to Question #298110 in Java | JSP | JSF for Oslo

Question #298110

Create a class called Account with member variable balance. Create three methods one for Depositing Money, the second for withdrawing money and the third for checking balance. In addition to the three classes, add a constructor to initialize the balance variable. Have the initial balance of the account set to 712,983.88. Have a user deposit any amount and prompt them if they wish to withdraw any amount. If not, print out the balance of the account.


1
Expert's answer
2022-02-15T15:11:06-0500
public class Account {
    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }

    public double getBalance() {
        return balance;
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Account account = new Account(712983.88);
        System.out.println("Deposit:");
        account.deposit(Double.parseDouble(in.nextLine()));
        System.out.println("Want withdraw(Y | N):");
        if (in.nextLine().equalsIgnoreCase("y")) {
            System.out.println("Amount:");
            account.withdraw(Double.parseDouble(in.nextLine()));
        } else {
            System.out.println("Balance: " + account.getBalance());
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS