In: Computer Science
Bicycles – The World’s Greatest Cost Effective Healthy Green
Fashionable Transportation Solution Downtown Dallas, Texas has seen
an explosion in a demand for bicycle rentals. You have decided to
go into the bicycle rental business to satisfy the world’s desire
for cost effective healthy green transportation. Market studies
have shown that hundreds of billions of people are interested in
renting bicycles which will result in a huge number of rentals from
your bicycle rental stores.
You need to develop a program that calculates the rental charge for
a bicycle rental based on the number of days the bicycle is rented
and the bicycle type.
You must use the following package name for developing the java
program: package bicyclerentalstorepackage;
There must be at least 2 java source code files that have the
following names: • BicycleRentalStoreMainClass.java Contains the
main BicycleStoreMainClass. • BicycleRentalClass.java Contains the
BicycleRentalClass base class and the extended classes based on
bicycle type.
The program must create and use at least an amount of bicycle
objects for each type of bicycle.
If you do not use this assignment’s stated file names, class names,
method names, variable names and package name, the assignment is an
invalid submission and will be graded a 0%.
This program introduces inheritance by creating subclasses of a
BicycleRentalClass based on bicycle type. Your program design
solution will override the equals() and calculateRentalFees()
methods in the extended classes.
//////////////////////////////////////////////////////////////BicycleRentalClass.jaav//////////////////////////////////////////////////////////////////////////////
package bicyclerentalstorepackage;
import bicyclerentalstorepackage.BicycleRentalStoreMainClass.BICYCLE_TYPE_ENUM;
public abstract class BicycleRentalClass {
private int bicycleId;
private BICYCLE_TYPE_ENUM bicyleType;
private double bicyleRentalFee;
public double getBicyleRentalFee() {
return bicyleRentalFee;
}
// bicycleRentalFee can not be negative. In case of -ve exception is thrown
public void setBicyleRentalFee(double bicyleRentalFee) {
if (bicyleRentalFee < 0) {
throw new IllegalArgumentException();
} else {
this.bicyleRentalFee = bicyleRentalFee;
}
}
// constructor with bicyleId and bicyleType
public BicycleRentalClass(int bicycleId, BICYCLE_TYPE_ENUM bicyleType) {
super();
this.bicycleId = bicycleId;
this.bicyleType = bicyleType;
}
public int getBicycleId() {
return bicycleId;
}
public void setBicycleId(int bicycleId) {
this.bicycleId = bicycleId;
}
public BICYCLE_TYPE_ENUM getBicyleType() {
return bicyleType;
}
public void setBicyleType(BICYCLE_TYPE_ENUM bicyleType) {
this.bicyleType = bicyleType;
}
/**
*
* @param daysToRent
* indicates number of days the bicycle is to be rented
* @return rental charges
*/
public abstract double calculateRentalFee(int daysToRent);
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + bicycleId;
return result;
}
/**
* compares two BicycleRentalClasses by their ids
*/
public boolean equals(BicycleRentalClass otherBicycleRentalClassObj) {
if (this == otherBicycleRentalClassObj)
return true;
if (otherBicycleRentalClassObj == null)
return false;
if (bicycleId != otherBicycleRentalClassObj.bicycleId)
return false;
return true;
}
}
////////////////////////////////////////////////////////////End BicycleRentalClass.java//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////BicycleRentalStoreMainClass.java////////////////////////////////////////////////////////////////
package bicyclerentalstorepackage;
public class BicycleRentalStoreMainClass {
public enum BICYCLE_TYPE_ENUM {
STANDARD, RACER, MOUNTAIN, BMX;
}
private class DAILY_BICYCLE_REANTAL_RATE_CLASS {
public static final double RENTAL_DAILY_FEE_STANDARD = 1.00;
public static final double RENTAL_DAILY_FEE_RACER = 1.50;
public static final double RENTAL_DAILY_FEE_MOUNTAIN = 1.79;
public static final double RENTAL_DAILY_FEE_BMX = 1.89;
}
public static void main(String args[]) {
testStandardCycle();
BicycleRentalRacerClass racerBicycle = testRacerCycle();
testMountainCycle();
BicycleRentalBmxClass bmxBicycle = testBmxCycle();
testEquals(racerBicycle, bmxBicycle);
// Test Negative Value for number of days
testNegativeNumOfDays();
testNegativeCycleRent();
}
private static void testEquals(BicycleRentalRacerClass racerBicycle, BicycleRentalBmxClass bmxBicycle) {
// Test for equals(): The two bicycles are not equal as their Ids are different
boolean result = bmxBicycle.equals(racerBicycle);
System.out.println("\nIs BmxCycle (id:" + bmxBicycle.getBicycleId() + ") equals to RacerCycle(id:"
+ racerBicycle.getBicycleId() + ") ? " + result);
// Test for equals(): The two bicycles are Equal as their Ids are same
bmxBicycle.setBicycleId(2);
result = bmxBicycle.equals(racerBicycle);
System.out.println("Is BmxCycle (id:" + bmxBicycle.getBicycleId() + ") equals to RacerCycle(id:"
+ racerBicycle.getBicycleId() + ") ? " + result + "\n");
}
private static void testStandardCycle() {
BicycleRentalStandardClass stdBicycle = new BicycleRentalStandardClass(1, BICYCLE_TYPE_ENUM.STANDARD);
stdBicycle.setBicyleRentalFee(DAILY_BICYCLE_REANTAL_RATE_CLASS.RENTAL_DAILY_FEE_STANDARD);
// bicycle id is set in constructor. check it by calling getBicycleId(). It
// should be 1
System.out.println("stdBicycle Id=" + stdBicycle.getBicycleId());
// bicycle Type is set in constructor. check it by calling getBicyleType(). It
// should be STANDARD
System.out.println("stdBicycle Type=" + stdBicycle.getBicyleType());
// Going to Rent the standard Bicycle for 3 days so the outcome should be 3
double rent = stdBicycle.calculateRentalFee(3);
System.out.println("Rent for Standard Bicycle for 3 days is =" + rent);
}
private static BicycleRentalRacerClass testRacerCycle() {
double rent;
BicycleRentalRacerClass racerBicycle = new BicycleRentalRacerClass(2, BICYCLE_TYPE_ENUM.RACER);
racerBicycle.setBicyleRentalFee(DAILY_BICYCLE_REANTAL_RATE_CLASS.RENTAL_DAILY_FEE_RACER);
// bicycle id is set in constructor. check it by calling getBicycleId(). It
// should be 2
System.out.println("\nracerBicycle Id=" + racerBicycle.getBicycleId());
// bicycle Type is set in constructor. check it by calling getBicyleType(). It
// should be RACER
System.out.println("racerBicycle Type=" + racerBicycle.getBicyleType());
// Going to Rent the Racer Bicycle for 14 days so the outcome should be 21
rent = racerBicycle.calculateRentalFee(14);
System.out.println("Rent for Standard Bicycle for 14 days is =" + rent);
return racerBicycle;
}
private static void testMountainCycle() {
double rent;
BicycleRentalMountainClass mountainBicycle = new BicycleRentalMountainClass(3, BICYCLE_TYPE_ENUM.MOUNTAIN);
mountainBicycle.setBicyleRentalFee(DAILY_BICYCLE_REANTAL_RATE_CLASS.RENTAL_DAILY_FEE_MOUNTAIN);
// bicycle id is set in constructor. check it by calling getBicycleId(). It
// should be 3
System.out.println("\nmountainBicycle Id=" + mountainBicycle.getBicycleId());
// bicycle Type is set in constructor. check it by calling getBicyleType(). It
// should be MOUNTAIN
System.out.println("mountainBicycle Type=" + mountainBicycle.getBicyleType());
// Going to Rent the Mountain Bicycle for 7 days so the outcome should be 12.53
rent = mountainBicycle.calculateRentalFee(7);
System.out.println("Rent for Mountain Bicycle for 7 days is =" + rent);
}
private static BicycleRentalBmxClass testBmxCycle() {
double rent;
BicycleRentalBmxClass bmxBicycle = new BicycleRentalBmxClass(4, BICYCLE_TYPE_ENUM.BMX);
bmxBicycle.setBicyleRentalFee(DAILY_BICYCLE_REANTAL_RATE_CLASS.RENTAL_DAILY_FEE_BMX);
// bicycle id is set in constructor. check it by calling getBicycleId(). It
// should be 4
System.out.println("\nBmxCycle Id=" + bmxBicycle.getBicycleId());
// bicycle Type is set in constructor. check it by calling getBicyleType(). It
// should be BMX
System.out.println("BmxCycle Type=" + bmxBicycle.getBicyleType());
// Going to Rent the Bmx Bicycle for 8 days so the outcome should be 15.12
rent = bmxBicycle.calculateRentalFee(8);
System.out.println("Rent for Bmx Bicycle for 8 days is =" + rent);
return bmxBicycle;
}
// Going to Rent the Bmx Bicycle for negative number of days so the outcome
// should be an exception
private static void testNegativeNumOfDays() {
double rent;
BicycleRentalBmxClass bmxBicycle = new BicycleRentalBmxClass(4, BICYCLE_TYPE_ENUM.BMX);
bmxBicycle.setBicyleRentalFee(DAILY_BICYCLE_REANTAL_RATE_CLASS.RENTAL_DAILY_FEE_BMX);
try {
rent = bmxBicycle.calculateRentalFee(-2);
System.out.println("Rent for Bmx Bicycle for -2 days is =" + rent);
} catch (IllegalArgumentException e) {
System.out.println("Number of days must be positive number " + e);
}
}
// Going to Rent the Bmx Bicycle for 2 days but the rent is negative so there
// shud be an exception
private static void testNegativeCycleRent() {
double rent;
BicycleRentalBmxClass bmxBicycle = new BicycleRentalBmxClass(4, BICYCLE_TYPE_ENUM.BMX);
try {
bmxBicycle.setBicyleRentalFee(-20);
rent = bmxBicycle.calculateRentalFee(2);
System.out.println("Rent for Bmx Bicycle for 2 days is =" + rent);
} catch (IllegalArgumentException e) {
System.out.println("Rent must be positive number " + e);
}
}
}
//////////////////////////////////////////////////////End BicycleRentalStoreMainClass.java////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////BicycleRentalBmxClass.java///////////////////////////////////////////////////////////////////////////////////
package bicyclerentalstorepackage;
import bicyclerentalstorepackage.BicycleRentalStoreMainClass.BICYCLE_TYPE_ENUM;
public class BicycleRentalBmxClass extends BicycleRentalClass {
// Constructor with bicycleId and bicycleType which calls the constructor of the
// BicycleRentalClass
public BicycleRentalBmxClass(int bicycleId, BICYCLE_TYPE_ENUM bicyleType) {
super(bicycleId, bicyleType);
}
@Override
public double calculateRentalFee(int daysToRent) {
double rent = 0;
if (daysToRent >= 0) {
rent = daysToRent * getBicyleRentalFee();
} else {
System.out.println("Please enter number of days 0 or greater than 0");
throw new IllegalArgumentException();
}
return rent;
}
}
/////////////////////////////////////////////End BicycleRentalBmxClass.java///////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////BicycleRentalMountainClass.java//////////////////////////////////////////////////////////////////
package bicyclerentalstorepackage;
import bicyclerentalstorepackage.BicycleRentalStoreMainClass.BICYCLE_TYPE_ENUM;
public class BicycleRentalMountainClass extends BicycleRentalClass {
// Constructor with bicycleId and bicycleType which calls the constructor of the
// BicycleRentalClass
public BicycleRentalMountainClass(int bicycleId, BICYCLE_TYPE_ENUM bicyleType) {
super(bicycleId, bicyleType);
}
@Override
public double calculateRentalFee(int daysToRent) {
double rent = 0;
if (daysToRent >= 0) {
rent = daysToRent * getBicyleRentalFee();
} else {
System.out.println("Please enter number of days 0 or greater than 0");
throw new IllegalArgumentException();
}
return rent;
}
///////////////////////////////////////////////////////End BicycleRentalMountainClass.java//////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////BicycleRentalRacerClass.java///////////////////////////////////////////////////////////////////////
package bicyclerentalstorepackage;
import bicyclerentalstorepackage.BicycleRentalStoreMainClass.BICYCLE_TYPE_ENUM;
public class BicycleRentalRacerClass extends BicycleRentalClass {
// Constructor with bicycleId and bicycleType which calls the constructor of the
// BicycleRentalClass
public BicycleRentalRacerClass(int bicycleId, BICYCLE_TYPE_ENUM bicyleType) {
super(bicycleId,bicyleType);
}
@Override
public double calculateRentalFee(int daysToRent) {
double rent = 0;
if (daysToRent >= 0) {
rent = daysToRent * getBicyleRentalFee();
} else {
System.out.println("Please enter number of days 0 or greater than 0");
throw new IllegalArgumentException();
}
return rent;
}
}
///////////////////////////////////////////////End BicycleRentalRacerClass.java////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////BicycleRentalStandardClass.java////////////////////////////////////////////////////////////////////////////
package bicyclerentalstorepackage;
import bicyclerentalstorepackage.BicycleRentalStoreMainClass.BICYCLE_TYPE_ENUM;
public class BicycleRentalStandardClass extends BicycleRentalClass {
// Constructor with bicycleId and bicycleType which calls the constructor of the
// BicycleRentalClass
public BicycleRentalStandardClass(int bicycleId, BICYCLE_TYPE_ENUM bicyleType) {
super(bicycleId, bicyleType);
}
@Override
public double calculateRentalFee(int daysToRent) {
double rent = 0;
if (daysToRent >= 0) {
rent = daysToRent * getBicyleRentalFee();
} else {
System.out.println("Please enter number of days 0 or greater than 0");
throw new IllegalArgumentException();
}
return rent;
}
}
///////////////////////////////////////////////End BicycleRentalStandardClass.java////////////////////////////////////////////////////////////////////////////