import java.text.*; import java.io.*; //------------------------------------------------------------------- // Program P2 Q2 // // This program defines two classes of Gym membership, and illustrates // how the classes work with a simple program. // // The yearly charge is calculated based on average weekly frequency // over the year. More complicated calculation is possible, for // example, this code can be modifed to keep track of frequency each // month, and charge the extras on a monthly basis. This requires // more complex data structures such as arrays. // // Author : Wei Tsang Ooi // Date : 11 July 1999 //------------------------------------------------------------------- //------------------------------------------------------------------- // class GymMembership // // This class represents a gym membership. // // Author : Wei Tsang Ooi // Date : 11 July 1999 //------------------------------------------------------------------- class GymMembership { protected final static double YOGURT_PRICE = 9.95; protected String description; // description of the membership type; protected String id; // membersip id. protected String name; // name of the member protected int frequency; // how many this member have frequent the gym protected int numOfYogurt; // how many yogurt this member have consumed // helper object for formatting member's id protected static DecimalFormat idFormatter = new DecimalFormat("000000"); // helper object for formatting costs protected static DecimalFormat costFormatter = new DecimalFormat("####.00"); //------------------------------------------------------------------- // GymMembership's constructor // // input : memberName - name of a new member //------------------------------------------------------------------- GymMembership (String memberName) { name = memberName; frequency = 0; numOfYogurt = 0; description = "ERROR : No account type specified"; } //------------------------------------------------------------------- // buyYogurt // // input : number of Yogurt this member has bought // // This increments the total number of yogurt consumed by this member. //------------------------------------------------------------------- public void buyYogurt(int howMany) { numOfYogurt += howMany; } //------------------------------------------------------------------- // comeToGym // // input : number of times this member has come to gym. // // This increments the total number of times this member has come to // the gym. //------------------------------------------------------------------- public void comeToGym(int howMany) { frequency += howMany; } //------------------------------------------------------------------- // toString // // input : none // Returns a string describing this member. //------------------------------------------------------------------- public String toString() { return "\nMember : " + name + "\n" + "ID : " + id + "\n" + "Type : " + description + "\n" + "Mean Frequency/Week : " + frequency/52f + "\n" + "Yogurts Comsumed : " + numOfYogurt; } };
//------------------------------------------------------------------- // class FitMembership // // This class represents a membership with a Fit account. // //------------------------------------------------------------------- class FitMembership extends GymMembership { private static int lastId = 0; //------------------------------------------------------------------- // FitMembership // // constructor for this class. //------------------------------------------------------------------- FitMembership(String memberName) { super(memberName); id = generateNewId(); description = "Fit"; } //------------------------------------------------------------------- // generateNewID // // input : none // return : a unique id for this new member. //------------------------------------------------------------------- private String generateNewId() { lastId ++; return idFormatter.format(lastId); } //------------------------------------------------------------------- // getTotalCharge // // input : none // return : how much this member needs to pay. // $250/year + $240/year charge if _average_ frequency is // less then 4/week over the year. (assuming 1 year = 52 // weeks) + total Yogurt cost. //------------------------------------------------------------------- public float getTotalCharge() { float sum = 250; // yearly fee float meanFreq = frequency/52f; if (meanFreq < 4) sum += 20*12; // extra charge sum += (numOfYogurt * 0.75 * YOGURT_PRICE); return sum; } }; //------------------------------------------------------------------- // class FabMembership // // This class represents a membership with a Fab account. // //------------------------------------------------------------------- class FabMembership extends GymMembership { private static int lastId = 500000; //------------------------------------------------------------------- // FabMembership // // constructor for this class. //------------------------------------------------------------------- FabMembership(String memberName) { super(memberName); id = generateNewId(); description = "Fab"; } //------------------------------------------------------------------- // generateNewID // // input : none // returns : a unique id for this new member. //------------------------------------------------------------------- private String generateNewId() { lastId ++; return idFormatter.format(lastId); } //------------------------------------------------------------------- // getTotalCharge // // input : none // returns : how much this member needs to pay. // $250/year + $240/year charge if _average_ frequency is // less then 4/week over the year. (assuming 1 year = 52 // weeks) + total Yogurt cost. //------------------------------------------------------------------- public float getTotalCharge() { float sum = 100; // yearly fee float meanFreq = frequency/52f; if (meanFreq > 3) sum += 25*12; // extra charge sum += (numOfYogurt * YOGURT_PRICE); // yogurt cost return sum; } }; //------------------------------------------------------------------- // class Gym // // This simple class demonstrates the classes defined above. // //------------------------------------------------------------------- class Gym { //--------------------------------------------------------------- // main // // This creates three members, records how many times they frequent // the gym and how many yogurts they ate, and finally print out // how much each member owes. // //--------------------------------------------------------------- public static void main (String args[]) { FitMembership billGates = new FitMembership ("Bill Gates"); billGates.buyYogurt(1); billGates.buyYogurt(29); billGates.comeToGym(41); billGates.buyYogurt(9); billGates.comeToGym(21); billGates.comeToGym(19); billGates.comeToGym(50); float cost = billGates.getTotalCharge(); System.out.println(billGates.toString()); System.out.println("Total cost : $" + GymMembership.costFormatter.format(cost)); FabMembership alGore = new FabMembership ("Al Gore"); alGore.buyYogurt(21); alGore.buyYogurt(23); alGore.comeToGym(21); alGore.buyYogurt(19); alGore.comeToGym(11); alGore.comeToGym(9); alGore.comeToGym(5); cost = alGore.getTotalCharge(); System.out.println(alGore.toString()); System.out.println("Total cost : $" + GymMembership.costFormatter.format(cost)); FabMembership west = new FabMembership ("James West"); west.buyYogurt(91); west.comeToGym(231); west.comeToGym(135); west.buyYogurt(84); west.comeToGym(160); west.comeToGym(70); cost = west.getTotalCharge(); System.out.println(west.toString()); System.out.println("Total cost : $" + GymMembership.costFormatter.format(cost)); } }