<< Back to Object Oriented Programming Portfolio
Assignment 1
UML
Customer Class
package com.nhlstenden.week7;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;
public class Customer
{
private String name;
private LocalDate dateOfBirth;
private List<Booking> bookings;
public Customer(String name, LocalDate dateOfBirth)
{
this.setName(name);
this.setDateOfBirth(dateOfBirth);
this.bookings = new ArrayList<Booking>();
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
if(name == null || name.isEmpty())
{
throw new IllegalArgumentException("Name cannot be empty");
}
this.name = name;
}
public LocalDate getDateOfBirth()
{
return this.dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth)
{
if(dateOfBirth == null || dateOfBirth.isAfter(LocalDate.now()))
{
throw new IllegalArgumentException("Invalid date of birth");
}
this.dateOfBirth = dateOfBirth;
}
public List<Booking> getBookings()
{
return this.bookings;
}
public void setBookings(List<Booking> bookings)
{
this.bookings = bookings;
}
public int getAgeAt(LocalDate date)
{
return Period.between(this.dateOfBirth, date).getYears();
}
public Booking bookTrip(Trip trip, LocalDate bookingDate)
{
if(trip.isLongDistance() && getAgeAt(bookingDate) < 18)
{
throw new IllegalArgumentException("Must be 18+ to book a long-distance trip");
}
Booking booking = new Booking(trip, this, bookingDate);
bookings.add(booking);
return booking;
}
}
Booking Class
package com.nhlstenden.week7;
import java.time.LocalDate;
public class Booking
{
private Trip trip;
private Customer customer;
private LocalDate bookingDate;
private final double cancellationFeePercent = 0.10; // Assuming that the cancellation percentage is a constant
public Booking(Trip trip, Customer customer, LocalDate bookingDate)
{
this.setTrip(trip);
this.setCustomer(customer);
this.setBookingDate(bookingDate);
}
public Trip getTrip()
{
return this.trip;
}
public void setTrip(Trip trip)
{
if(trip == null)
{
throw new IllegalArgumentException("Trip cannot be null");
}
this.trip = trip;
}
public Customer getCustomer()
{
return this.customer;
}
public void setCustomer(Customer customer)
{
if(customer == null)
{
throw new IllegalArgumentException("Customer cannot be null");
}
this.customer = customer;
}
public LocalDate getBookingDate()
{
return this.bookingDate;
}
public void setBookingDate(LocalDate bookingDate)
{
if(bookingDate == null || bookingDate.isBefore(LocalDate.now()))
{
throw new IllegalArgumentException("Invalid booking date");
}
this.bookingDate = bookingDate;
}
public double getCancellationFeePercent()
{
return this.cancellationFeePercent;
}
public double cancelBooking()
{
return this.trip.getPricePerPerson() * this.cancellationFeePercent;
}
}
Trip Class
package com.nhlstenden.week7;
public class Trip
{
private String code;
private String destination;
private double pricePerPerson;
private boolean isLongDistance;
public Trip(String code, String destination, double pricePerPerson, boolean isLongDistance)
{
this.setCode(code);
this.setDestination(destination);
this.setPricePerPerson(pricePerPerson);
this.setLongDistance(isLongDistance);
}
public String getCode()
{
return this.code;
}
public void setCode(String code)
{
if(code == null || code.isEmpty())
{
throw new IllegalArgumentException("Code cannot be empty");
}
this.code = code;
}
public String getDestination()
{
return this.destination;
}
public void setDestination(String destination)
{
if(destination == null || destination.isEmpty())
{
throw new IllegalArgumentException("Destination cannot be empty");
}
this.destination = destination;
}
public double getPricePerPerson()
{
return this.pricePerPerson;
}
public void setPricePerPerson(double pricePerPerson)
{
if(pricePerPerson < 0)
{
throw new IllegalArgumentException("Price must be positive");
}
this.pricePerPerson = pricePerPerson;
}
public boolean isLongDistance()
{
return this.isLongDistance;
}
public void setLongDistance(boolean longDistance)
{
this.isLongDistance = longDistance;
}
}
PopularDestinationManager Class
package com.nhlstenden.week7;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class PopularDestinationManager
{
private Map<String, Integer> bookingStats = new HashMap<>();
public PopularDestinationManager()
{
this.bookingStats = new HashMap<>();
}
public void registerBooking(Trip trip)
{
String dest = trip.getDestination();
this.bookingStats.put(dest, this.bookingStats.getOrDefault(dest, 0) + 1);
}
public List<String> getMostPopularDestinations()
{
int max = bookingStats.values().stream().max(Integer::compareTo).orElse(0);
List<String> popular = new ArrayList<>();
for(Map.Entry<String, Integer> entry : bookingStats.entrySet())
{
if(entry.getValue() == max)
{
popular.add(entry.getKey());
}
}
return popular;
}
}