This assignment must be done in Java. Main Task o Use a factory pattern to suppo
ID: 3726489 • Letter: T
Question
This assignment must be done in Java.
Main Task
o Use a factory pattern to support a rental car program
o Create RentalCar class, with following attributes
o Name
o # of people can fit
o 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
o Create 4 types of rental cars
o Toyota Corolla, 4
o Dodge Minivan, 7
o Ford Escape Hybrid, 5
o Yamaha Motorcycle, 1
o Create a RentalCarFactory class that takes number of people as a parameter and returns the smallest car that will fit everyone
o Write a Test class that does the following in its main()
Extra Credit
o Use the observer pattern to support a mythical broadcast system
o Create a Broadcaster class that supports:
o void register (Receiver r) – objects of type Receiver can register
o void broadcast (String message) – sneds message to all registered Receivers
o The Receiver class should support:
o Receiver(String name) – constructor that sets the name
o void receive (String message) – accepts messages sent to it and prints them out
o Write a Test class that does the following in its main()
This should result in the output below
for iExplanation / Answer
As per your requirement the below one is solution please follow it
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);
}
}
}