In: Computer Science
You have been assigned to a development team that is building software that will be used to control the operations of a bicycle rental store. A rental store has a limited number of vehicles that can be managed. A bicycle rental store must maintain information about how many vehicles are available for rental. The bicycle rental store must provide publicly available methods to allow vehicles to be added and removed from it. The rental store should also provide publicly available methods that reports its capacity. An attempt to add or remove the vehicle other than it’s capacity should print the message letting user know that he/she can’t add or delete the vehicle (Hint: use “if” condition to check the number of vehicles. They shouldn’t be more that 5/5 each to add and less than 1 to delete). At the moment there are two distinct types of vehicles: bicycle and quadricycle (four-wheel bicycle). Every vehicle has a company code, a fun name, number of wheels and a rental price. The bicycle has two wheels whereas quadricycle has four. Define the Java classes that are required to implement the functionality that is described above. Be sure to use object-oriented principles in your Java code. Hints • Vehicle class, Bicycle class, Quadricycle class, RentalStore class. • Bicycle class and Quadricycle class inherits extends from Vehicle class • RentalStore class will have methods to show the total number of vehicles, add/delete Bicycle and add/delete Quadricycle • Rental class should have ArrayList • In general every class should have attributes, constructor and it’s methods. • Besides RentalStore class, all other classes should have toString() method . • Create TestClass that have Main() method. Bicycles: company code 0001, a fun name ( your choice), number of wheels : 2 and a rental price 150 company code 0002, a fun name ( your choice), number of wheels : 2 and a rental price 110 company code 0003, a fun name ( your choice), number of wheels : 2 and a rental price 50 company code 0004, a fun name ( your choice), number of wheels : 2 and a rental price 250 company code 0005, a fun name ( your choice), number of wheels : 2 and a rental price 90 quadricycle : company code 0011, a fun name ( your choice), number of wheels : 4 and a rental price 250 company code 0012, a fun name ( your choice), number of wheels : 4 and a rental price 110 company code 0013, a fun name ( your choice), number of wheels : 4 and a rental price 210 company code 0014, a fun name ( your choice), number of wheels : 4 and a rental price 210 company code 0015, a fun name ( your choice), number of wheels : 4 and a rental price 190 For this scenario, we will have 5 bicycles and 5 quadricycles (total of 10 vehicles). Add all those vehicles to an ArrayList. Find bicycles with price less than $100 and delete all of them. Find quadricycles with price less than $200 and delete all of them. At last, show to total number of remaining vehicles with their details.
Vehicle.java
public class Vehicle {
public int companyCode;
public String funName;
public int numberOfWheels;
public int rentalPrice;
public Vehicle(int companyCode, String funName, int
numberOfWheels, int rentalPrice) {
super();
this.companyCode =
companyCode;
this.funName = funName;
this.numberOfWheels =
numberOfWheels;
this.rentalPrice =
rentalPrice;
}
public int getRentalPrice() {
return rentalPrice;
}
public void setRentalPrice(int rentalPrice) {
this.rentalPrice =
rentalPrice;
}
public int getCompanyCode() {
return companyCode;
}
public void setCompanyCode(int companyCode) {
this.companyCode =
companyCode;
}
public String getFunName() {
return funName;
}
public void setFunName(String funName) {
this.funName = funName;
}
public int getNumberOfWheels() {
return numberOfWheels;
}
public void setNumberOfWheels(int numberOfWheels)
{
this.numberOfWheels =
numberOfWheels;
}
@Override
public String toString() {
return "Vehicle [companyCode=" +
companyCode + ", funName=" + funName + ", numberOfWheels=" +
numberOfWheels
+ ", rentalPrice=" + rentalPrice + "]";
}
}
Bicycle.java
public class Bicycle extends Vehicle{
public Bicycle(int companyCode, String funName, int
numberOfWheels, int rentalPrice) {
super(companyCode, funName,
numberOfWheels, rentalPrice);
// TODO Auto-generated constructor
stub
}
@Override
public int getCompanyCode() {
// TODO Auto-generated method
stub
return
super.getCompanyCode();
}
@Override
public void setCompanyCode(int companyCode) {
// TODO Auto-generated method
stub
super.setCompanyCode(companyCode);
}
@Override
public String getFunName() {
// TODO Auto-generated method
stub
return super.getFunName();
}
@Override
public void setFunName(String funName) {
// TODO Auto-generated method
stub
super.setFunName(funName);
}
@Override
public int getNumberOfWheels() {
// TODO Auto-generated method
stub
return
super.getNumberOfWheels();
}
@Override
public void setNumberOfWheels(int numberOfWheels)
{
// TODO Auto-generated method
stub
super.setNumberOfWheels(numberOfWheels);
}
@Override
public String toString() {
// TODO Auto-generated method
stub
return super.toString();
}
}
Quadricycle.java
public class Quadricycle extends Vehicle{
public Quadricycle(int companyCode, String funName,
int numberOfWheels, int rentalPrice) {
super(companyCode, funName,
numberOfWheels, rentalPrice);
// TODO Auto-generated constructor
stub
}
@Override
public int getRentalPrice() {
// TODO Auto-generated method
stub
return
super.getRentalPrice();
}
@Override
public void setRentalPrice(int rentalPrice) {
// TODO Auto-generated method
stub
super.setRentalPrice(rentalPrice);
}
@Override
public int getCompanyCode() {
// TODO Auto-generated method
stub
return
super.getCompanyCode();
}
@Override
public void setCompanyCode(int companyCode) {
// TODO Auto-generated method
stub
super.setCompanyCode(companyCode);
}
@Override
public String getFunName() {
// TODO Auto-generated method
stub
return super.getFunName();
}
@Override
public void setFunName(String funName) {
// TODO Auto-generated method
stub
super.setFunName(funName);
}
@Override
public int getNumberOfWheels() {
// TODO Auto-generated method
stub
return
super.getNumberOfWheels();
}
@Override
public void setNumberOfWheels(int numberOfWheels)
{
// TODO Auto-generated method
stub
super.setNumberOfWheels(numberOfWheels);
}
@Override
public String toString() {
// TODO Auto-generated method
stub
return super.toString();
}
}
RentalStore.java
public class RentalStore {
ArrayList<Vehicle> vehicles;
int bicycles=0;
int quadricycles=0;
public RentalStore() {
vehicles= new
ArrayList<>();
}
public ArrayList<Vehicle> getVehicles()
{
return vehicles;
}
public void setVehicles(ArrayList<Vehicle>
vehicles) {
this.vehicles = vehicles;
}
public int getBicycles() {
return bicycles;
}
public void setBicycles(int bicycles) {
this.bicycles = bicycles;
}
public int getQuadricycles() {
return quadricycles;
}
public void setQuadricycles(int quadricycles)
{
this.quadricycles =
quadricycles;
}
public void addVehicle(Vehicle vehicle) {
if(vehicle.getNumberOfWheels()==2)
{
if(bicycles<5) {
vehicles.add(vehicle);
System.out.println("Bicycle is added
Successfully");
}
else
System.out.println("More than 5 Bicycles are not
allowed");
}
else {
if(quadricycles<5) {
vehicles.add(vehicle);
System.out.println("Quadricycle is added
successfully");
}
else
System.out.println("More than 5 Quadricycles are
not allowed");
}
}
public void removeVehicle(Vehicle vehicle) {
int index =
vehicles.indexOf(vehicle);
vehicles.remove(index);
}
}
TestClass.java
import java.util.ArrayList;
public class TestClass {
public static void main(String[] args) {
// TODO Auto-generated method
stub
RentalStore store = new
RentalStore();
store.addVehicle(new
Bicycle(0001,"ammy",2,150));
store.addVehicle(new
Bicycle(0002,"bammy",2,110));
store.addVehicle(new
Bicycle(0003,"cammy",2,50));
store.addVehicle(new
Bicycle(0004,"dammy",2,250));
store.addVehicle(new
Bicycle(0005,"eammy",2,90));
store.addVehicle(new
Quadricycle(0011,"fammy",4,250));
store.addVehicle(new
Quadricycle(0012,"gammy",4,110));
store.addVehicle(new
Quadricycle(0013,"hammy",4,210));
store.addVehicle(new
Quadricycle(0014,"jammy",4,210));
store.addVehicle(new
Quadricycle(0011,"kammy",4,190));
ArrayList<Vehicle> vehicles =
store.getVehicles();
for(int
i=0;i<vehicles.size();i++) {
if(vehicles.get(i).numberOfWheels==2 &&
vehicles.get(i).getRentalPrice()<100)
store.removeVehicle(vehicles.get(i));
if(vehicles.get(i).numberOfWheels==4 &&
vehicles.get(i).getRentalPrice()<200)
store.removeVehicle(vehicles.get(i));
}
vehicles=
store.getVehicles();
for(Vehicle vehicle:vehicles)
System.out.println(vehicle);
}
}
Output
Bicycle is added Successfully
Bicycle is added Successfully
Bicycle is added Successfully
Bicycle is added Successfully
Bicycle is added Successfully
Quadricycle is added successfully
Quadricycle is added successfully
Quadricycle is added successfully
Quadricycle is added successfully
Quadricycle is added successfully
Vehicle [companyCode=1, funName=ammy, numberOfWheels=2,
rentalPrice=150]
Vehicle [companyCode=2, funName=bammy, numberOfWheels=2,
rentalPrice=110]
Vehicle [companyCode=4, funName=dammy, numberOfWheels=2,
rentalPrice=250]
Vehicle [companyCode=9, funName=fammy, numberOfWheels=4,
rentalPrice=250]
Vehicle [companyCode=11, funName=hammy, numberOfWheels=4,
rentalPrice=210]
Vehicle [companyCode=12, funName=jammy, numberOfWheels=4,
rentalPrice=210]
Screenshot
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it
helpful