In: Computer Science
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.
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