Question

In: Computer Science

Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...

Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true) • Credit function that takes an amount from the user and add it to the balance. Write a test program called TestAccount to test the constructor and public methods in the class Account. Ask the user to enter the information for four accounts and instantiate them, then allow the user to choose the operation that he wants on those accounts. Operations are Show balance or Debit or Credit, if he chose debit or credit ask him to specify the amount, in case of debit if the amount is not sufficient tell him “insufficient amount”.

Sample Output: Enter Name: Ali Moussa Enter Account Number: 11223344 Enter Balance: 1000

Enter Name: Mohammed Omran Enter Account Number: 22334455 Enter Balance: 1500

Enter Name: Hassan Youssef Enter Account Number: 33445566 Enter Balance: 200

Enter Name: Yasser Mustafa Enter Account Number: 44556677 Enter Balance: 2000 Which account you wish to process? 3 Choose your operation: 1. Show Balance 2. Debit 3. Credit 2 Enter Amount: 50 Your new balance is 150 Which account you wish to process? 3 Choose your operation: 1. Show Balance 2. Debit 3. Credit 2 Enter Amount: 170 Insufficient Balance Which account you wish to process? 3 Choose your operation: 1. Show Balance 2. Debit 3. Credit 1 Your balance is 150

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


// Account.java

public class Account {
   private String name;
   private String account;
   private double balance;

   public Account(String name, String account, double balance) {
       this.name = name;
       this.account = account;
       this.balance = balance;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getAccount() {
       return account;
   }

   public void setAccount(String account) {
       this.account = account;
   }

   public double getBalance() {
       return balance;
   }

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

   public boolean debit(double amt) {
       if ((balance - amt) >= 0)
       {
           this.balance-=amt;
           return true;
       }
       else
           return false;
   }

   public void credit(double amt) {
       this.balance += amt;
   }

  
}
___________________________

// TestAccount.java

import java.util.Scanner;

public class TestAccount {

   public static void main(String[] args) {
       String name, accno;
       double bal;

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       Account accounts[] = new Account[4];

       for (int i = 0; i < accounts.length; i++) {
           System.out.print("Enter Name:");
           name = sc.nextLine();
           System.out.print("Enter Account Number:");
           accno = sc.nextLine();
           System.out.print("Enter Balance::");
           bal = sc.nextDouble();
sc.nextLine();
           Account acc = new Account(name, accno, bal);
           accounts[i] = acc;

       }

       for(int i=0;i<3;)
       {
           System.out.print("\nWhich account you wish to process?");
           int num=sc.nextInt();
           if(num<1 || num>accounts.length)
           {
               System.out.println("** Invalid Number **");
           }
           System.out.print("Choose your operation: 1. Show Balance 2. Debit 3. Credit:");
           int choice=sc.nextInt();
       switch(choice)
       {
       case 1:{
           System.out.println("Your balance is "+accounts[num-1].getBalance());
           i++;
           break;
       }
       case 2:{
           System.out.print("Enter Amount:");
           double amt=sc.nextDouble();
           if(!accounts[num-1].debit(amt))
           {
               System.out.println("insufficient amount");
           }
           else
           {

               System.out.println("Your new balance is "+accounts[num-1].getBalance());
           }
           i++;
           break;
       }
       case 3:{
           System.out.print("Enter Amount:");
           double amt=sc.nextDouble();
           accounts[num-1].credit(amt);
           System.out.println("Your new balance is "+accounts[num-1].getBalance());
           i++;
           break;
       }
       default:{
           System.out.println("** invalid choice **");
           break;
       }
      
       }
       }
      
   }

}
_________________________

Output:

Enter Name:Ali Moussa
Enter Account Number:11223344
Enter Balance::1000
Enter Name:Mohammed Omran
Enter Account Number:22334455
Enter Balance::1500
Enter Name:Hassan Youssef
Enter Account Number:33445566
Enter Balance::200
Enter Name:Yasser Mustafa
Enter Account Number:44556677
Enter Balance::2000

Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:2
Enter Amount:50
Your new balance is 150.0

Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:170
** invalid choice **

Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:2
Enter Amount:170
insufficient amount

Which account you wish to process?3
Choose your operation: 1. Show Balance 2. Debit 3. Credit:1
Your balance is 150.0


Related Solutions

with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account Number and Account Balance. There are also other variables called MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like constants, but for now, they are variables of type double Here is the UML for the class:                                                         BankAccount -string accountName // First and Last name of Account holder -int accountNumber // integer -double accountBalance // current balance amount + BankAccount()                     //default constructor that sets name to “”,...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
Create a UEmployee class that contains member variables for the university employee name and salary. The...
Create a UEmployee class that contains member variables for the university employee name and salary. The UEmployee class should contain member methods for returning the employee name and salary. Create Faculty and Staff classes that inherit the UEmployee class. The Faculty class should include members for storing and returning the department name. The Staff class should include members for storing and returning the job title. Write a runner program that creates one instance of each class and prints all of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT