<< Back to Object Oriented Programming Portfolio

Assignment 6


UML

UML Diagram

Customer Class

package com.nhlstenden.week4;

public class Customer
{
    private String name;
    private int age;

    public Customer(String name, int age)
    {
        setName(name);
        setAge(age);
    }

    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 int getAge()
    {
        return this.age;
    }

    public void setAge(int age)
    {
        if(age < 0 || age > 120)
        {
            throw new IllegalArgumentException("Age must be between 0 and 120.");
        }
        this.age = age;
    }

    public boolean isEligibleForDiscount()
    {
        return age < 18 || age > 65;
    }
}

Reservation Class

package com.nhlstenden.week4;

public class Reservation
{
    private Customer customer;
    private Screening screening;
    private double price;

    public Reservation(Customer customer, Screening screening, double price)
    {
        setCustomer(customer);
        setScreening(screening);
        setPrice(price);
    }

    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 Screening getScreening()
    {
        return this.screening;
    }

    public void setScreening(Screening screening)
    {
        if(screening == null)
        {
            throw new IllegalArgumentException("Screening cannot be null.");
        }

        this.screening = screening;
    }

    public double getPrice()
    {
        return this.price;
    }

    public void setPrice(double price)
    {
        if(price < 0)
        {
            throw new IllegalArgumentException("Price cannot be negative.");
        }
        
        this.price = price;
    }
}

Screening Class

package com.nhlstenden.week4;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

public class Screening
{
    private LocalDate date;
    private LocalTime time;
    private int capacity;
    private int reservedSeats = 0;
    private double ticketPrice;
    private Film film;
    private List<Reservation> reservations;

    public Screening(LocalDate date, LocalTime time, int capacity, double ticketPrice, Film film)
    {
        setDate(date);
        setTime(time);
        setCapacity(capacity);
        setTicketPrice(ticketPrice);
        setFilm(film);
        this.reservations = new ArrayList<>();
    }

    public LocalDate getDate()
    {
        return this.date;
    }

    public void setDate(LocalDate date)
    {
        if(date == null || date.isBefore(LocalDate.now()))
        {
            throw new IllegalArgumentException("Date cannot be in the past.");
        }

        this.date = date;
    }

    public LocalTime getTime()
    {
        return this.time;
    }

    public void setTime(LocalTime time)
    {
        if(time == null)
        {
            throw new IllegalArgumentException("Time cannot be null.");
        }

        this.time = time;
    }

    public int getCapacity()
    {
        return this.capacity;
    }

    public void setCapacity(int capacity)
    {
        if(capacity <= 0)
        {
            throw new IllegalArgumentException("Capacity must be greater than zero.");
        }

        this.capacity = capacity;
    }

    public int getReservedSeats()
    {
        return this.reservedSeats;
    }

    public void setReservedSeats(int reservedSeats)
    {
        this.reservedSeats = reservedSeats;
    }

    public double getTicketPrice()
    {
        return this.ticketPrice;
    }

    public void setTicketPrice(double ticketPrice)
    {
        if(ticketPrice < 0)
        {
            throw new IllegalArgumentException("Ticket price cannot be negative.");
        }
        this.ticketPrice = ticketPrice;
    }

    public Film getFilm()
    {
        return this.film;
    }

    public void setFilm(Film film)
    {
        if(film == null)
        {
            throw new IllegalArgumentException("Film cannot be null.");
        }
        this.film = film;
    }

    public List<Reservation> getReservations()
    {
        return this.reservations;
    }

    public void setReservations(List<Reservation> reservations)
    {
        this.reservations = reservations;
    }

    public Reservation reserveSeat(Customer customer)
    {
        if(reservedSeats >= capacity)
        {
            throw new IllegalStateException("Screening is full. No more seats available.");
        }
        double finalPrice = customer.isEligibleForDiscount() ? ticketPrice * 0.8 : ticketPrice;
        Reservation reservation = new Reservation(customer, this, finalPrice);
        reservations.add(reservation);
        reservedSeats++;
        return reservation;
    }

    public boolean cancelReservation(Reservation reservation)
    {
        if(reservations.remove(reservation))
        {
            reservedSeats--;
            return true;
        }

        throw new IllegalArgumentException("Reservation not found.");
    }

    public double getOccupancy()
    {
        return (double) reservedSeats / capacity;
    }

    public double getIncome()
    {
        return reservations.stream().mapToDouble(Reservation::getPrice).sum();
    }
}

Film Class

package com.nhlstenden.week4;

import java.util.ArrayList;
import java.util.List;

public class Film
{
    private String title;
    private String genre;
    private int runningTime;
    private int ageRating;
    private List<Screening> screenings;

    public Film(String title, String genre, int runningTime, int ageRating)
    {
        setTitle(title);
        setGenre(genre);
        setRunningTime(runningTime);
        setAgeRating(ageRating);
        this.screenings = new ArrayList<>();
    }

    public String getTitle()
    {
        return this.title;
    }

    public void setTitle(String title)
    {
        if(title == null || title.isEmpty())
        {
            throw new IllegalArgumentException("Title cannot be empty.");
        }
        this.title = title;
    }

    public String getGenre()
    {
        return this.genre;
    }

    public void setGenre(String genre)
    {
        if(genre == null || genre.isEmpty())
        {
            throw new IllegalArgumentException("Genre cannot be empty.");
        }
        this.genre = genre;
    }

    public int getRunningTime()
    {
        return this.runningTime;
    }

    public void setRunningTime(int runningTime)
    {
        if(runningTime <= 0)
        {
            throw new IllegalArgumentException("Running time must be positive.");
        }
        this.runningTime = runningTime;
    }

    public int getAgeRating()
    {
        return this.ageRating;
    }

    public void setAgeRating(int ageRating)
    {
        if(ageRating < 0 || ageRating > 18)
        {
            throw new IllegalArgumentException("Invalid age rating.");
        }
        this.ageRating = ageRating;
    }

    public List<Screening> getScreenings()
    {
        return this.screenings;
    }

    public void setScreenings(List<Screening> screenings)
    {
        this.screenings = screenings;
    }

    public void addScreening(Screening screening)
    {
        this.screenings.add(screening);
    }

    public double getTotalIncome()
    {
        return screenings.stream().mapToDouble(Screening::getIncome).sum();
    }
}