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....
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...
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()
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 #:        ...
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....
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT