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

Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables   ...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables    private long accountNumber;    private String firstName;    private String lastName;    private double balance;       //constructor    public Account(long accountNumber, String firstName, String lastName, double balance) {        this.accountNumber = accountNumber;        this.firstName = firstName;        this.lastName = lastName;        this.balance = balance;    }    //all getters and setters    public long getAccountNumber() {        return...
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 Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT