<< Back to Object Oriented Programming Portfolio

Pizza Route Assignment


Delivery Driver class

import java.time.LocalDate;

public class DeliveryDriver
{
	private String name;
	private LocalDate dateOfBirth;
	
	public DeliveryDriver(String name, LocalDate dateOfBirth)
	{
		if(LocalDate.now().getYear() - dateOfBirth.getYear() < 16)
		{
			System.out.println("Driver must be at least 16 years old.");
		}
		this.name = name;
		this.dateOfBirth = dateOfBirth;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public LocalDate getDateOfBirth()
	{
		return this.dateOfBirth;
	}
	
	public int getAge()
	{
		return LocalDate.now().getYear() - dateOfBirth.getYear();
	}
	
	public double getSalary()
	{
		int age = getAge();

		return (age >= 16 && age < 20) ? 4.0 : (age >= 20 && age <= 22) ? 4.75 : 5.25;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setDateOfBirth(LocalDate dateOfBirth)
	{
		this.dateOfBirth = dateOfBirth;
	}
}

Destination class

public class Destination
{
    private String name;
    private String address;
    private double distanceFromPrevious;
    private int numberOfPizzas;

    public Destination(String name, String address, double distanceFromPrevious, int numberOfPizzas)
    {
        this.name = name;
        this.address = address;
        this.distanceFromPrevious = distanceFromPrevious;
        this.numberOfPizzas = numberOfPizzas;
    }

    public double getTotalPrice()
    {
        return this.numberOfPizzas * 5.0;
    }

    public String getName()
    {
        return name;
    }

    public String getAddress()
    {
        return address;
    }

    public double getDistanceFromPrevious()
    {
        return distanceFromPrevious;
    }

    public int getNumberOfPizzas()
    {
        return numberOfPizzas;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public void setDistanceFromPrevious(double distanceFromPrevious)
    {
        this.distanceFromPrevious = distanceFromPrevious;
    }

    public void setNumberOfPizzas(int numberOfPizzas)
    {
        this.numberOfPizzas = numberOfPizzas;
    }
}

Pizza Company class

import java.util.ArrayList;

public class PizzaCompany
{
    private String name;
    private ArrayList<Route> routes;

    public PizzaCompany(String name)
    {
        this.name = name;
        this.routes = new ArrayList<>();
    }

    public void addRoute(Route route)
    {
        this.routes.add(route);
    }

    public double getTotalProfit()
    {
        double totalProfit = 0;
        for (Route route : routes)
        {
            totalProfit += route.getProfit();
        }

        return totalProfit;
    }

    public Route getShortestRoute()
    {
        Route shortestRoute = routes.get(0);
        for (Route route : routes)
        {
            if (route.getTotalDistance() < shortestRoute.getTotalDistance())
            {
                shortestRoute = route;
            }
        }

        return shortestRoute;
    }

    public Route getLongestRoute()
    {
        Route longestRoute = routes.get(0);
        for (Route route : routes)
        {
            if (route.getTotalDistance() > longestRoute.getTotalDistance())
            {
                longestRoute = route;
            }
        }

        return longestRoute;
    }

    public Route getRouteWithMostPizzas()
    {
        Route routeWithMostPizzas = routes.get(0);
        for (Route route : routes)
        {
            if (route.getAmountOfPizzas() > routeWithMostPizzas.getAmountOfPizzas())
            {
                routeWithMostPizzas = route;
            }
        }

        return routeWithMostPizzas;
    }

    public String getName()
    {
        return this.name;
    }

    public ArrayList<Route> getRoutes()
    {
        return this.routes;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}

Route class

import java.util.ArrayList;

public class Route
{
    private DeliveryDriver driver;
    private ArrayList<Destination> destinations;

    public Route(DeliveryDriver driver, ArrayList<Destination> destinations)
    {
        this.driver = driver;
        this.destinations = new ArrayList<>();
    }

    public void addDestination(Destination destination)
    {
        this.destinations.add(destination);
    }

    public double getTotalDistance()
    {
        double totalDistance = 0.0;
        for (Destination destination : destinations)
        {
            totalDistance += destination.getDistanceFromPrevious();
        }

        return totalDistance;
    }

    public double getDistancePrice()
    {
        return getTotalDistance() * 0.05;
    }

    public double getTotalPrice()
    {
        double totalPrice = 0;
        for (Destination destination : destinations)
        {
            totalPrice += destination.getTotalPrice();
        }

        return totalPrice;
    }

    public int getAmountOfPizzas()
    {
        int totalPizzas = 0;
        for (Destination destination : destinations)
        {
            totalPizzas += destination.getNumberOfPizzas();
        }

        return totalPizzas;
    }

    public double getProfit()
    {
        return getTotalPrice() - (getDistancePrice() + driver.getSalary());
    }

    public DeliveryDriver getDriver()
    {
        return this.driver;
    }

    public ArrayList<Destination> getDestinations()
    {
        return this.destinations;
    }
}