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...
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 {
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...
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...
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...
Write a Python class to represent a Salik account. The account has three attributes, a name,...
Write a Python class to represent a Salik account. The account has three attributes, a name, id, and the balance. The balance is a private attribute. The class has extra functions to add to the balance and reduce the balance. Both, these functions should return the current balance and if the balance is below AED 50.0 print the message “Note: Balance Below 50”. Your class must work for the code given below. #Test myCar = SalikAccount() myCar.setName("John") myCar.setID("190300300333") myCar.setBal(20.0) yourCar...
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 “”,...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) 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. 2) Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating...
1) Create a class called Employee that includes three instance variables — a first name (type...
1) 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. 2)Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating five...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT