Answer to Question #281643 in Java | JSP | JSF for Jad

Question #281643
  • Create an Account class with the following attribute: Owner (String), id (int, should be auto-incremented), balance (double).
  • To create a new account, you only provide the name of the owner, the balance is then set to 0 and the account is assigned an ID (that is automatically incremented).
  • Create a method to do a deposit of money in the account object.
  • Create a method toString() that returns the details of your account in the following format: “Account N° 2 , Owner: Charlie, Balance: 200.0”.
  • Improve your program in order to prompt the user to enter name of the owner and the initial balance at each creation of the account.
1
Expert's answer
2021-12-21T06:17:38-0500
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;


public class Main {
    public static class Account {
        private static int nextId = 1;


        private String owner;
        private int id;
        private float balance;


        public Account(String owner) {
            this.owner = owner;


            id = nextId;
            nextId++;


            balance = 0;
        }


        public String getOwner() {
            return owner;
        }


        public void setOwner(String owner) {
            this.owner = owner;
        }


        public int getId() {
            return id;
        }


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


        public float getBalance() {
            return balance;
        }


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


        public void deposit(float deposit) {
            balance += deposit;
        }


        public String toString() {
            return String.format("Account №%d, Owner: %s, Balance: %.1f",
                id,
                owner,
                balance);
        }
    }


    public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
        List<Account> accounts = new ArrayList<>();


        while (true) {
            try {
                System.out.print("Do you want to create an account? (no - 0, yes - any other number): ");
                int yes = input.nextInt();
                if (yes == 0) break;


                input.nextLine();


                System.out.print("Owner: ");
                String owner = input.nextLine();


                System.out.print("Starting balance: ");
                float balance = input.nextFloat();


                System.out.print("Make a deposit: ");
                float deposit = input.nextFloat();


                Account account = new Account(owner);
                account.setBalance(balance);
                account.deposit(deposit);
                accounts.add(account);


                System.out.println("Created: " + account.toString());
            } catch (InputMismatchException e) {
                System.out.println("Something went wrong!");
                input.nextLine();
                continue;
            }
        }


        accounts.clear();


        System.out.println("Goodbye!");
	}
}

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