Question

Please make a program that in Bank Account class keeps a users name and balance. Will...

Please make a program that in Bank Account class keeps a users name and balance. Will throw an exception if the balance becomes negative.

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

class BankAccount {
   private int accNo;
   private String userName;
   private double balance;

   //constructor
   public BankAccount(int accNo, String userName,double balance) throws Exception {
       if(balance < 0) {
           throw new Exception("Account balance cannot be negative");
       }
       this.balance = balance;
       this.accNo = accNo;
       this.userName = userName;
   }

   public void withdraw(double amount) throws Exception {
       if (amount > balance) {
           throw new Exception(
               "Current balance %d is less than the requested withdrawal amount %d",
               balance, amount);   
       }
       balance -= amount;
   }

   public void deposit(double amount) {
       if (amount <= 0) {
throw new IllegalArgumentException("Invalid deposit amount %s", amount);
}
       balance += amount;
   }

   public void displayAccountDetails() {
       System.out.println("Account no.: ", accNo);
       System.out.println("User Name: " + userName);
       System.out.println("Account Balance: ", balance);
   }

}

Add a comment
Know the answer?
Add Answer to:
Please make a program that in Bank Account class keeps a users name and balance. Will...
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
  • You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account i...

    You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account in a bank. The class must have the following instance variables: a String called accountID                       a String called accountName a two-dimensional integer array called deposits (each row represents deposits made in a week). At this point it should not be given any initial value. Each...

  • c++ please    Define the class bankAccount to implement the basic properties of a bank account....

    c++ please    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member func- tions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 compo- nents of...

  • Please construct a program that runs as a counter: the program keeps asking users to enter...

    Please construct a program that runs as a counter: the program keeps asking users to enter a command. If the command is '+', it increases the counter by one; if the command is '-', it decreases the counter by one; and if the command is 'q', it stops asking input and prints out the total count. The following is an example of how the program would run: Total count is 2 The program is initialized as below count = 0...

  • in java (Bank Account) Write a program that inputs in the full name, street address, city, state, zip code, and social...

    in java (Bank Account) Write a program that inputs in the full name, street address, city, state, zip code, and social security for new account. In addition, it inputs in the initial account balance and allows the owner of the new account to make one withdrawal followed by one deposit. Your program should calculate the ending balancing after the withdrawal and deposit made by the account holder. It should also find the count of each one of these bills $78,...

  • Design a class named BankAccount containing the following data field and methods. • One double data...

    Design a class named BankAccount containing the following data field and methods. • One double data field named balance with default values 0.0 to denote the balance of the account. • A no-arg constructor that creates a default bank account. • A constructor that creates a bank account with the specified balance.  throw an IllegalArgumentException when constructing an account with a negative balance • The accessor method for the data field. • A method named deposit(double amount) that deposits...

  • 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...

  • Define the class bankAccount to implement the basic properties of a bank account. An object of...

    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also, declare an array of 10 components of type bankAccount to process up...

  • 2. Write a class called ATMTransaction. This class has a variable balance and account number. It...

    2. Write a class called ATMTransaction. This class has a variable balance and account number. It has three methods, checkBalance, deposit and withdraw along with constructors getters and setters and a toString. Write a program that creates an object of this class. I should be able to deposit and withdraw from my account number based on my balance , but i shouldnt be able to change my account number. Look for loopholes in your program, test it correctly, your balance...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Object Oriented Programming using class c++ Program where the users (customers) can add Product name, Product...

    Object Oriented Programming using class c++ Program where the users (customers) can add Product name, Product Price, Product Quantity. Program needs a menu: 1. Add item 2. Get total 3. Display all items

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
ADVERTISEMENT