Answer to Question #297645 in Java | JSP | JSF for Bruhh

Question #297645

2. Account

by CodeChum Admin

Create a new file called Account.java and construct a class called Account, which models a simple bank account. It contains the following members: 


More details here, important!

https://pastebin.com/j79JBVgp


1
Expert's answer
2022-02-14T19:21:25-0500
public class Account {
    private int accountNumber;
    private double balance;

    public Account(int accountNumber) {
        this.accountNumber = accountNumber;
        balance = 3000;
        System.out.println("First Overloaded Constructor");
    }

    public Account(int accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
        System.out.println("Second Overloaded Constructor");
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        return balance;
    }

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

    public void debit(double amount) {
        if (balance < amount) {
            System.out.println("Insufficient Funds");
        } else {
            balance -= amount;
        }
    }

    @Override
    public String toString() {
        return String.format("Account Number: %d, Balance: Php %.2f", accountNumber, balance);
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Account account;
        Scanner in = new Scanner(System.in);
        System.out.print("1 / 2: ");
        if (in.nextLine().equals("1")) {
            account = new Account(1001);
        } else {
            System.out.print("Account number: ");
            int accountNumber = Integer.parseInt(in.nextLine());
            System.out.print("Balance: ");
            account = new Account(accountNumber, Double.parseDouble(in.nextLine()));
        }
        while (true) {
            System.out.println("\n1 - credit \n" +
                    "2 - debit\n" +
                    "3 - getAccountNumber\n" +
                    "4 - getBalance\n" +
                    "5 - toString\n" +
                    "0 - exit");
            switch (in.nextLine()) {
                case "1":
                    account.credit(Double.parseDouble(in.nextLine()));
                    break;
                case "2":
                    account.debit(Double.parseDouble(in.nextLine()));
                    break;
                case "3":
                    System.out.println("Account·Number: " + account.getAccountNumber());
                    break;
                case "4":
                    System.out.println("Balance: " + account.getBalance());
                    break;
                case "5":
                    System.out.println(account);
                    break;
                case "0":
                    System.exit(0);
            }
        }
    }
}

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