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

This assignment must be done in Java. Main Task o Use a factory pattern to suppo

ID: 3725061 • 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 i

Explanation / Answer

Hi.. I have written java class for the above.

RentalCar.java

public class RentalCar {

private String name;

private int numOfPeopleFit;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getNumOfPeopleFit() {

return numOfPeopleFit;

}

public void setNumOfPeopleFit(int numOfPeopleFit) {

this.numOfPeopleFit = numOfPeopleFit;

}

public RentalCar(String name, int numOfPeopleFit) {

super();

this.name = name;

this.numOfPeopleFit = numOfPeopleFit;

}

@Override

public String toString() {

return "Name=" + name + ", Number of People can fit=" + numOfPeopleFit + "";

}

}

RentalCarFactory.java

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class RentalCarFactory {

static List<RentalCar> rs = new ArrayList<RentalCar>();

public static void main(String args[]) {

RentalCar rc = new RentalCar("Toyota Corolla", 4);

RentalCar rc1 = new RentalCar("Dodge Minivan", 7);

RentalCar rc2 = new RentalCar("Ford Escape Hybrid", 5);

RentalCar rc3 = new RentalCar("Yamaha Motorcycle", 1);

rs.add(rc);

rs.add(rc1);

rs.add(rc2);

rs.add(rc3);

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

RentalCar car = RentalCarFactory.rent(i);

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

}

}

private static RentalCar rent(int i) {

// TODO Auto-generated method stub

int minimal=i;

int index = 0;

Collections.sort(rs, new Comparator<RentalCar>() {

@Override

public int compare(RentalCar o1, RentalCar o2) {

return o1.getNumOfPeopleFit()-o2.getNumOfPeopleFit();

}

});

for(int k=0;k<rs.size();k++) {

RentalCar car = rs.get(k);

if(minimal==car.getNumOfPeopleFit()) {

minimal = car.getNumOfPeopleFit();

index = k;

break;

}else {

if(minimal<car.getNumOfPeopleFit() && minimal<=i) {

minimal = car.getNumOfPeopleFit();

index = k;

}

}

}

return rs.get(index);

}

}

Output:

Car that holds 1 People: Yamaha Motorcycle
Car that holds 2 People: Toyota Corolla
Car that holds 3 People: Toyota Corolla
Car that holds 4 People: Toyota Corolla
Car that holds 5 People: Ford Escape Hybrid
Car that holds 6 People: Dodge Minivan

Please test the code and let me know any issues. Thank you. All the best.