Question

In: Computer Science

Task 1: BetterHealth is a membership-based fitness club in Toronto. Customers can purchase a membership that...

Task 1: BetterHealth is a membership-based fitness club in Toronto. Customers can purchase a membership that provides a fixed number (e.g. 100) of free visits to the club upon registration. When the initial number of visits runs out, they can purchase additional visits as needed. All types of fitness equipment and all fitness classes offered in the club are open for this membership. Please write a Java program (Membership.java) for membership management.

The Membership class should have the following public interface:

1. public Membership(String name, int visits): A constructor to set up the member’s name and the initial number of visits allowed. The parameter visits is supposed to be positive (if not, initialize the number of visits to 0).

2. public String getName(): An accessor to return the member’s name.

3. public int getRemainingVisits(): An accessor to return the number of remaining visits.

4. public boolean isValid(): A method to decide if one’s membership is still valid (true if the number of remaining visits is greater than 0; false otherwise).

5. public boolean topUp (int additionalVisits): A method to add additional visits (represented by additionalVisits) to the remainingVisits. Returns true if the top-up succeeds and false otherwise. The top-up succeeds only If the given additionalVisits is non-negative.

6. public boolean charge(): A method that deducts 1 from the number of remaining visits when the membership is valid. Returns true if the charge succeeds and false otherwise. The charge succeeds only if the membership is valid before charging.

7. public boolean equipmentAllowed(): A method that indicates whether the equipment at the gym is available to this kind of membership. Returns true for now; to be updated in Task 2.

8. public boolean classesAllowed(): A method that indicates whether the fitness classes at the gym are available to this kind of membership. Returns true for now; to be updated in Task 2.

Write a tester program Task1MembershipTester to test the class you have written.

Follow the examples given in class (e.g., BankAccountTester), and create an objectfrom the class Membership above. Test the public interface of your class thoroughly (each public method must be tested at least once). For accessor methods, print out the expected return value and the return value of your method (see the BankAccountTester example) to compare. This tester class is not to be submitted for marking, but it is beneficial for you to make sure your Membership class works as intended.

Task 2: The club now introduces a new type of membership called PremiumMembership. It is valid within one year from the date of purchase for unlimited visits. Such members can use fitness equipment, but cannot attend fitness classes. In addition, pool access is available for this type of membership. Since now we have two types of memberships (and imagine down the road we might have more), you should implement a hierarchy of classes to represent the different types of membership. At the top of the hierarchy, you must define an abstract class called BaseMembership defining the methods common to different types of memberships. BaseMembership has the following public interface:

1. public BaseMembership(String name, String type): A constructor to set up the member’s name and member’s type.

2. public String getName(): An accessor to return the member’s name.

3. public String getType(): An accessor to return a string that represents the type of membership.

4. public abstract boolean isValid(): An abstract method that should be overridden in the subclasses.

5. public boolean equipmentAllowed(): Set an initial return value and override in the subclasses.

6. public boolean classesAllowed(): Set an initial return value and override in the subclasses.

You must then define classes StandardMembership and PremiumMembership that inherit from BaseMembership, defining additional methods as needed. StandardMembership should be implemented to model the type of membership in Task 1. The class PremiumMembership should have the following public interface:

1. public PremiumMembership(String name, String startDateString): A constructor to set up the member’s name and his start date. To transform startDateString to a Date type, a method stringToDate is provided in the hint below.

2. public boolean isValid(): A method to decide if one’s membership is still valid (If the current time is less than one year from the start time, it is valid). Please see the hint below.

3. public boolean equipmentAllowed(): Return true.

4. public boolean classesAllowed(): Return false.

5. public boolean poolAllowed(): Return true.

Hint: Java uses the number of milliseconds since January 1, 1970, 00:00:00 GMT to represent time/date. Use ‘new java.util.Date()’ to create a Date object of the current time in milliseconds. ‘System.currentTimeMillis()’ is used to get the current time in milliseconds; ‘getTime()’ is used to return the long milliseconds of a Date object; One year in milliseconds can be presented: long MILLIS_YEAR = 360 * 24 * 60 * 60 * 1000L;

/**

* Utility method to transform a string represented date to Date object.

* E.g., "2010-01-02" will be transformed to Sat Jan 02 00:00:00 GMT 2010

*/

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public static Date stringToDate(String dateString) {

DateFormat f = new SimpleDateFormat("yyyy-MM-dd");

Date date = null;

try {

date= f.parse(dateString);

} catch (ParseException e) {

e.printStackTrace();

}

return date;

}Similarly, write a tester program Task2MembershipTester to test the classes you

have written in Task 2. Create objects from the classes StandardMembership and

PremiumMembership above. This tester class is not to be submitted.

Solutions

Expert Solution

Task 1:

public Membership(String name, int visits): A constructor to set up the member’s name and the initial number of visits allowed. The parameter visits is supposed to be positive (if not, initialize the number of visits to 0).

Membership.java

/**
* public Membership(String name, int visits):
* A constructor to set up the member’s name and the initial number of visits allowed.
* The parameter visits is supposed to be positive (if not, initialize the number of visits to 0).
*
* @author ammuarjun
* name :
* version:
*
*/
public class Membership {
private String name;
private int visits;
  
/**
* A constructor to set up the member’s name and the initial number of visits allowed.
* @param name
* @param visits
*/
public Membership(String name, int visits) {
this.name = name;
// The parameter visits is supposed to be positive (if not, initialize the number of visits to 0).
if(visits > 0) {
this.visits = visits;
}
else {
this.visits = 0;
}
}
  
/**
* An accessor to return the member’s name.
* @return member’s name.
*/
public String getName() {
return this.name;
}
  
/**
* An accessor to return the number of remaining visits.
* @return the number of remaining visits
*/
public int getRemainingVisits() {
return this.visits;
}
  


/**
* A method to decide if one’s membership is still valid
* @return true if the number of remaining visits is greater than 0; false otherwise
*/
public boolean isValid() {
return this.visits > 0;
}
  
/**
* A method to add additional visits (represented by additionalVisits) to the remainingVisits.
* @param additionalVisits
* @return true if the top-up succeeds and false otherwise
*/
public boolean topUp (int additionalVisits) {
//The top-up succeeds only If the given additionalVisits is non-negative.
if(additionalVisits > 0) {
this.visits += additionalVisits;
return true;
}
else {
return false;
}
}
  
/**
* A method that deducts 1 from the number of remaining visits when the membership is valid.
* @return true if the charge succeeds and false otherwise.
*/
public boolean charge() {
if (this.isValid()) {
this.visits -= 1;
return true;
}
else {
return false;
}
}
  
/**
* A method that indicates whether the equipment at the gym is available to this kind of membership
* @return
*/
public boolean equipmentAllowed() {
return true;
}

/**
* A method that indicates whether the fitness classes at the gym are available to this kind of membership.
* @return
*/
public boolean classesAllowed() {
return true;
}
}

MembershipTest.java

public class MembershipTest {

public static void main(String[] args) {
Membership membership = new Membership("Sara", 2);
System.out.println("Name: " + membership.getName());
System.out.println("Visits: " + membership.getRemainingVisits());
System.out.println("Is Valid: " + membership.isValid());
System.out.println("Charge 3 times!");
  
for(int index = 0; index < 3; index ++) {
if(membership.charge()) {
System.out.println("Charge is successful");
}
else {
System.out.println("Charge is NOT successful");
}
}
  
System.out.println("Is Valid: " + membership.isValid());
System.out.println("Adding addtional 3 charges...");
membership.topUp(3);
System.out.println("Visits: " + membership.getRemainingVisits());
}

}

Output 1:

Name: Sara
Visits: 2
Is Valid: true
Charge 3 times!
Charge is successful
Charge is successful
Charge is NOT successful
Is Valid: false
Adding addtional 3 charges...
Visits: 3

Task 2 :

public abstract class BaseMembership {
   private String name;
   private String type;

   /**
   * A constructor to set up the member’s name and member’s type.
   * @param name
   * @param type
   */
   public BaseMembership(String name, String type) {
       this.name = name;
       this.type = type;
   }

   /**
   * An accessor to return the member’s name.
   * @return
   */
   public String getName() {
       return this.name;
   }

   /**
   * An accessor to return a string that represents the type of membership.
   * @return
   */
   public String getType() {
       return this.type;
   }

   /**
   * An abstract method that should be overridden in the subclasses.
   * @return
   */
   public abstract boolean isValid();

   /**
   * Set an initial return value and override in the subclasses
   * @return
   */
   public boolean equipmentAllowed() {
       return false;
   }

   /**
   * Set an initial return value and override in the subclasses.
   * @return
   */
   public boolean classesAllowed() {
       return false;
   }
}

StandardMembership.java

public class StandardMembership extends BaseMembership{

private int visits;
  
public StandardMembership(String name, int visits) {
super(name, "Standard Membership");
// The parameter visits is supposed to be positive (if not, initialize the number of visits to 0).
if(visits > 0) {
this.visits = visits;
}
else {
this.visits = 0;
}
  
}
  
/**
* An accessor to return the number of remaining visits.
* @return the number of remaining visits
*/
public int getRemainingVisits() {
return this.visits;
}
  
/**
* A method to decide if one’s membership is still valid
* @return true if the number of remaining visits is greater than 0; false otherwise
*/
@Override
public boolean isValid() {
return this.visits > 0;
}
  
/**
* A method to add additional visits (represented by additionalVisits) to the remainingVisits.
* @param additionalVisits
* @return true if the top-up succeeds and false otherwise
*/
public boolean topUp (int additionalVisits) {
//The top-up succeeds only If the given additionalVisits is non-negative.
if(additionalVisits > 0) {
this.visits += additionalVisits;
return true;
}
else {
return false;
}
}
  
/**
* A method that deducts 1 from the number of remaining visits when the membership is valid.
* @return true if the charge succeeds and false otherwise.
*/
public boolean charge() {
if (this.isValid()) {
this.visits -= 1;
return true;
}
else {
return false;
}
}
  
/**
* standard membership not allowed to access equipments
* @return false
*/
@Override
public boolean equipmentAllowed() {
return false;
}

/**
* A method that indicates whether the fitness classes at the gym are available to this kind of membership.
* @return true
*/
@Override
public boolean classesAllowed() {
return true;
}

}

PremiumMembership.java

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PremiumMembership extends BaseMembership{

private Date startDate;
private static long MILLIS_YEAR = 360 * 24 * 60 * 60 * 1000L;
/**
* A constructor to set up the member’s name and his start date.
* @return
*/
public PremiumMembership(String name, String startDateString) {
super(name, "Premium Membership");
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
this.startDate = null;
try {
this.startDate= f.parse(startDateString);
} catch (ParseException e) {
e.printStackTrace();
//Default to today's date
this.startDate = new Date();
}
}
  
//To transform startDateString to a Date type, a method stringToDate is provided in the hint below
  
/**
* A method to decide if one’s membership is still valid
*/
@Override
public boolean isValid() {
//If the current time is less than one year from the start time, it is valid).
return ((this.startDate.getTime() - System.currentTimeMillis()) / MILLIS_YEAR) >= 0;
}

@Override
public boolean equipmentAllowed() {
return true;
}

public boolean classesAllowed() {
return false;
}

public boolean poolAllowed() {
return true;
}
}

MembershipTest.java

public class MembershipTest {

public static void main(String[] args) {
System.out.println("StandardMembership Test:");
StandardMembership stdMembership = new StandardMembership("Sara", 2);
System.out.println("Name: " + stdMembership.getName());
System.out.println("Type: " + stdMembership.getType());
System.out.println("Visits: " + stdMembership.getRemainingVisits());
System.out.println("Is Valid: " + stdMembership.isValid());
System.out.println("Charge 3 times!");
  
for(int index = 0; index < 3; index ++) {
if(stdMembership.charge()) {
System.out.println("Charge is successful");
}
else {
System.out.println("Charge is NOT successful");
}
}
  
System.out.println("Is Valid: " + stdMembership.isValid());
System.out.println("Adding addtional 3 charges...");
stdMembership.topUp(3);
System.out.println("Visits: " + stdMembership.getRemainingVisits());
System.out.println("Equipment Allowed: " + stdMembership.equipmentAllowed());
System.out.println("Classes Allowed: " + stdMembership.classesAllowed());
  
System.out.println("\n\nPremiumMembership Test:");
PremiumMembership premMembership = new PremiumMembership("Durgs", "2020-09-20");
System.out.println("Name: " + premMembership.getName());
System.out.println("Type: " + premMembership.getType());
System.out.println("Is Valid: " + premMembership.isValid());
  
System.out.println("Equipment Allowed: " + premMembership.equipmentAllowed());
System.out.println("Classes Allowed: " + premMembership.classesAllowed());
System.out.println("Pool Allowed: " + premMembership.poolAllowed());
  
System.out.println("\n\nExpired PremiumMembership Test:");
PremiumMembership premMembership1 = new PremiumMembership("Durgs", "2019-09-18");
System.out.println("Is Valid: " + premMembership1.isValid());
}
  

}

Output:

StandardMembership Test:
Name: Sara
Type: Standard Membership
Visits: 2
Is Valid: true
Charge 3 times!
Charge is successful
Charge is successful
Charge is NOT successful
Is Valid: false
Adding addtional 3 charges...
Visits: 3
Equipment Allowed: false
Classes Allowed: true


PremiumMembership Test:
Name: Durgs
Type: Premium Membership
Is Valid: true
Equipment Allowed: true
Classes Allowed: false
Pool Allowed: true


Expired PremiumMembership Test:
Is Valid: false


Related Solutions

The following is information pertaining to a local fitness club, Fitness for Life. Month Club Membership...
The following is information pertaining to a local fitness club, Fitness for Life. Month Club Membership (number of members) Total Operating Costs July 450 $                      8,900 August 480 $                      9,800 September 500 $                   10,100 October 550 $                   10,150 November 560 $                   10,500 December 525 $                   10,200 1. By looking at the Total Operating Costs and the Average Operating Costs Per Member, can you tell whether the club's operating costs are variable, fixed, or mixed? 2. Perform Regression analysis using Microsoft Excel. What is the monthly operating...
1] Analyze the health club industry (NOT just Planet Fitness but the industry in which Planet...
1] Analyze the health club industry (NOT just Planet Fitness but the industry in which Planet Fitness is operating using Porter's Five Forces Model 2]    How has Planet Fitness developed a sustainable competitive advantage in this industry? 3] How has Planet Fitness achieved solid financial results in a challenging environment? 4]    What are the pros and cons of Planet Fitness’s franchising strategy?                                  5]        Can Planet Fitness sustain its competitive advantage moving forward? Are the company’s growth objectives reasonable and attainable
A-310 Fall 2018 Quiz 1 The following information relates to Vida Fitness Club which was started...
A-310 Fall 2018 Quiz 1 The following information relates to Vida Fitness Club which was started by Mark Cooper and includes one fitness club that sells athletic apparel and provides personal training. On July 1, 2018, Mark decided to incorporate and open his club. At the time of incorporation he had one investor who was eager to provide capital. The company uses the accrual method of accounting and made the following transactions during the months of July and August. Transactions...
The fees for the first three years of a hunting club membership are given in Table 1. If fees continue to rise at the same rate, how much will the total cost be for the first ten years of membership?
The fees for the first three years of a hunting club membership are given in Table 1. If fees continue to rise at the same rate, how much will the total cost be for the first ten years of membership? 
A firm has identified 3 segments of customers based on their frequency of purchase. Low frequency...
A firm has identified 3 segments of customers based on their frequency of purchase. Low frequency customers spend on average $100/year on the brand, medium frequency customers spend $380/year on the brand and high frequency customers spend $500/year on the brand. A new marketing campaign wants to target converting customers to higher levels of frequency. Based on their budget they are considering 4 strategies. Which strategy should they pursue if they want to increase revenue as much as possible? a....
A company has two different devices it can purchase to perform a specific task. Device A...
A company has two different devices it can purchase to perform a specific task. Device A costs ?$110,000 ?initially, whereas device B costs ?$150,000. It has been estimated that the cost of maintenance will be ?$5,000 for device A and ?$3,000 for device B in the first year. Management expects these maintenance costs to increase 10?% per year. The company uses a? six-year study? period, and its effective income tax rate is 55?%. Both devices qualify as? five-year MACRS GDS...
1) Union membership can present itself in different ways, as not all are the same. Identify...
1) Union membership can present itself in different ways, as not all are the same. Identify and define the 5 types of union membership. 2) List and describe the 5 different types of management/union relationships. 3) During negotiations, management and unions are required to bargain in good faith. Failure to do so may result in the Labour Board becoming involved. Identify 10 actions from management that would be deemed to be in bad faith during negotiations. 4) Arbitrators have significant...
Question 1. Airports can be identified globally with a 3-digit letter code (e.g., YYZ for Toronto...
Question 1. Airports can be identified globally with a 3-digit letter code (e.g., YYZ for Toronto Pearson, and CDG for Charles de Gaulle in Paris). All possible combinations of letters A-Z can be used. 1a. What is the probability of selecting an airport at random and it being Montreal’s YUL? 1b. What is the probability of selecting one of London England’s six major airports? 1c. What is the probability of selecting an airport with two letter Y’s, (e.g., YYZ, YXY)?...
Crooked Creek Wines operates a cellar door venue in which customers can sample and purchase wines....
Crooked Creek Wines operates a cellar door venue in which customers can sample and purchase wines. The average revenue per day is $750 , variable cost per day is $250 and annual fixed costs for the cellar door operations is $95,000. Assume 365 operating days. What is the operating profit (loss) fo r cellar door operations per year? As CVP analysis allows us to play “what if” games with the information, calculate the operating profit under each of the following...
Show all work/formulas used: 1) Based on the following data: Genotype: AA Aa aa Relative fitness:...
Show all work/formulas used: 1) Based on the following data: Genotype: AA Aa aa Relative fitness:   WAA= 1 WAa= 1 Waa= 0.5 Number of young: 100 100 800 1a) Estimate the frequency of 'aa' young individuals after 5 and 10 generations 2b) Show (graph) how the frequency of the alleles 'A' and 'a' change for 20 generations 2) If the initial Frequency of a lethal recessive allele is 10%, how many generations are needed to reduce it to 5%? (assume...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT