In: Computer Science
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 ");
}
}
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!