Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

# in Java Use a factory pattern to support a rental car program • Create RentalC

ID: 3725343 • Letter: #

Question

#in Java

Use a factory pattern to support a rental car program

• Create RentalCar class, with following attributes

o Name

o # of people can fit

• Write a toString() method for the RentalCar class so that it prints out the name of the car and the # of people that can fit

• Create 4 types of rental cars:(CREATE SEPARATE CLASSES OF DIFFERENT TYPE OF CARS)

o Toyota Corolla, 4

o Dodge Minivan, 7

o Ford Escape Hybrid, 5

o Yamaha Motorcycle, 1

• Create a RentalCarFactory class that takes number of people as a parameter and returns the smallest car that will fit everyone

• Write a Test class that does the following in its main()

for (int i=1; i<7; i++) {

RentalCar car = RentalCarFactory.rent(i);

System.out.println( "Car that holds + i + " people: "+car);

}

Explanation / Answer

class RentalCar
{
String name;
int noPeople;

RentalCar(String nm,int noP)
{
name=nm;
noPeople=noP;
}

public String toString()
{
return "Name of the car : "+name+" "+"The number of people that can fit : "+noPeople;
}
}

class ToyotaCorolla extends RentalCar
{
ToyotaCorolla(int noP)
{
super("ToyotaCorolla",noP);
}
}

class DodgeMinivan extends RentalCar
{
DodgeMinivan(int noP)
{
super("DodgeMinivan", noP);
}
}

class FordEscapeHybrid extends RentalCar
{
FordEscapeHybrid(int noP)
{
super("FordEscapeHybrid", noP);
}
}

class YamahaMotorcycle extends RentalCar
{
YamahaMotorcycle(int noP)
{
super("YamahaMotorcycle", noP);
}
}

class RentalCarFactory
{

public static RentalCar rent(int noP)
{
if(noP==4)
{
return new ToyotaCorolla(noP);
}
else if(noP==7)
{
return new DodgeMinivan(noP);
}
else if(noP==5)
{
return new FordEscapeHybrid(5);
}
else if(noP==1)
{
return new YamahaMotorcycle(1);
}
else
{
return null;
}
}
}
class TestClass
{
public static void main(String args[])
{
for(int i=1;i<=7;i++)
{
RentalCar car=RentalCarFactory.rent(i);
System.out.println("Car that holds "+i+" People : "+car);
}
}
}