Question

In: Computer Science

Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...

Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method to close the account, that is setting the ‘active’ data member to be false which makes it impossible for the user to add (deposit) or deduct(withdraw) from their account.

Account Class code:

public class Account {
private String ownerName;
private double balance;
  
//Constructor
public Account(){
ownerName = "Unassigned";
balance = 0.0;
}
  
//Adds passed balance to current balance
public void add(double amt){
if(amt>=0 &&(balance-amt)>=0){
balance += amt;
}
  
}
  
//Deducts passed balance from current balance
public void deduct(double amt){
if(amt>=0 &&(balance-amt)>=0){
balance -= amt;
}
}
  
//Return current balance
public double getCurrentBalance(){
return balance;
}
  
//Return the name of account's owner
public String getOwnerName(){
return ownerName;
}
  
//Sets initial account balance
public void setInitialBalance(double val){
balance = val;
}
  
//Assigns name of account's owner
public void setOwnerName(String name){
ownerName = name;
}
}

Main code:
public static void main(String[] args) {
// TODO code application logic here
Bicycle bike;
Account acct;
  
String myName = "No Name";
  
bike = new Bicycle();
bike.setOwnerName(myName);
  
acct = new Account();
acct.setOwnerName(myName);
acct.setInitialBalance(250.00);
  
acct.add(25.00);
acct.deduct(175);
  
DecimalFormat df = new DecimalFormat("0.00");
  
System.out.println(bike.getOwnerName()+" owns a bicycle and");
System.out.println("has $"+df.format(acct.getCurrentBalance())+" left in the bank ");
}
  
}

Solutions

Expert Solution

public class Account {
private String ownerName;
private double balance;
boolean active=true;

       public Account(){
       ownerName = "Unassigned";
       balance = 0.0;
       active=true;
       }
      
      
   public Account(String ownerName, double balance, boolean active) {
           super();
           this.ownerName = ownerName;
           this.balance = balance;
           this.active = active;
       }


   private void   isActive()
       {
           System.out.println(active);
       }
  
  
  
   private void   isClose()
   {
       active=false;
       System.out.println(active);
   }
  
      
      
      
//Adds passed balance to current balance
       public void add(double amt){
          
          
       if(amt>=0 &&(balance-amt)>=0 && active==true){
       balance += amt;
       }
       else {
          
           isClose();
       }
      
       }
  
//Deducts passed balance from current balance
           public void deduct(double amt){
           if(amt>=0 &&(balance-amt)>=0 && active==true){
           balance -= amt;
           }
           else {
               isClose();
           }
           }
          
       //Return current balance
       public double getCurrentBalance(){
           System.out.println(balance);
       return balance;
       }
      
       //Return the name of account's owner
       public String getOwnerName(){
       return ownerName;
       }
      
       //Sets initial account balance
       public void setInitialBalance(double val){
       balance = val;
       }
  
//Assigns name of account's owner
public void setOwnerName(String name){
ownerName = name;
}
}

Main method using Driver Class:


public class Driver {
  
   public static void main(String[] args) {

      
  
      
       String myName = "No Name";
      
      
      
       Account acct = new Account();
       acct.setOwnerName(myName);
       acct.setInitialBalance(250.00);
      
       acct.getCurrentBalance();
       acct.add(25.00);
       acct.getCurrentBalance();
       acct.deduct(175);
       acct.getCurrentBalance();
       acct.deduct(100);
       acct.deduct(100);

       }
      
       }
  
  
  
Once the balance reduce to less than zero the active status set to false and account is set to close.

Thank you!


Related Solutions

JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a BankAccountconstructed from data input from the keyboard. Override ReadAccount in SavingsAccount to return an account that refers to a SavingsAccount that you construct, again initializing it with data from the keyboard. Similarly, implement ReadAccount in the CheckingAccount class. Use the following code to test your work. public static void testCode()         {             SavingsAccount savings = new SavingsAccount(100.00, 3.5);             SavingsAccount s = (SavingsAccount)savings.ReadAccount();...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. This is a good reference for how throwing exceptions works in java. I would encourage you...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in. *NOTE*- I...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new behavior as well: add(Section s) this behavior will add a section to the Person’s schedule. The Person’s display() function will also need to be modified to display() the Person’s Schedule. Schedule class has Properties: An array of Sections and a Count. Constructors: only a default constructor is needed. Behaviors: add() and display(). This is my schedule class class Schedule     {         private int...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT