Java System Design and Implementation Exercises

Experiment 2: Movie Ticket Booking System

Design and implement an Object-Oriented Movie Ticket Booking System using appropriate class relationships (association, aggregation, composition) to manage movies, shows, seats, and bookings.

import java.util.*;

class Seat {
    int seatNumber;
    boolean isAvailable;
    Seat(int num) {
        seatNumber = num;
        isAvailable = true;
    }
}

class Show {
    int showId;
    ArrayList<Seat> seats = new ArrayList<>();
    Show(int id, int totalSeats) {
        showId = id;
        for(int i = 1; i <= totalSeats; i++) {
            seats.add(new Seat(i));
        }
    }
    void bookSeat(int num) {
        Seat s = seats.get(num - 1);
        if(s.isAvailable) {
            s.isAvailable = false;
            System.out.println("Seat " + num + " booked successfully");
        } else {
            System.out.println("Seat already booked");
        }
    }
}

class Movie {
    String title;
    Show show;
    Movie(String t, int id, int seats) {
        title = t;
        show = new Show(id, seats);
    }
}

public class Main {
    public static void main(String[] args) {
        Movie m = new Movie("Avengers", 101, 5);
        System.out.println("Booking Seat 2...");
        m.show.bookSeat(2);
        System.out.println("Booking Seat 2 again...");
        m.show.bookSeat(2);
    }
}

Experiment 3: Online Movie Booking System

Design and implement a low-level structure of an Online Movie Ticket Booking System by applying SOLID principles and implementing appropriate design patterns (Factory Method, Observer, and Strategy) for extensibility and maintainability.

package com.movieticket;
import java.time.LocalDateTime;
import java.util.List;
import com.movieticket.model.*;
import com.movieticket.service.*;

public class Main {
    public static void main(String[] args) {
        MovieService movieService = new MovieService();
        TheaterService theaterService = new TheaterService();
        UserService userService = new UserService();
        BookingService bookingService = new BookingService();
        PaymentService paymentService = new PaymentService();
        bookingService.setServices(userService, theaterService, movieService);
        
        User user = new User("U1", "Poornima", "poornima@mail.com", "9999999999");
        userService.registerUser(user);
        
        Movie movie = new Movie("M1", "Avengers", "Action", 150, 9.0, "2024");
        movieService.addMovie(movie);
        
        Theater theater = new Theater("T1", "PVR", "City", 100);
        theaterService.addTheater(theater);
        Showtime show = new Showtime("S1", movie.getId(), theater.getId(), LocalDateTime.now().plusDays(1), "10:00", 250);
        theaterService.addShowtime(theater.getId(), show);
        
        List<String> seats = List.of("A1", "A2");
        Booking booking = bookingService.createBooking(user.getId(), show.getId(), seats);
        Payment payment = paymentService.processPayment(booking.getId(), booking.getTotalAmount(), "CREDIT_CARD");
        bookingService.confirmBooking(booking.getId());
        Ticket ticket = bookingService.generateTicket(booking.getId());
        
        System.out.println("Booking Done!");
        System.out.println("Movie: " + ticket.getMovieTitle());
        System.out.println("Seats: " + ticket.getSeats());
        System.out.println("Amount: Rs. " + ticket.getTotalCost());
    }
}

Experiment 4: Music Streaming Service LLD

Design and implement a Low-Level Design (LLD) for a Music Streaming Service using UML modeling, OOP concepts, SOLID principles, and design patterns (Singleton, Factory Method, Observer, Strategy) to support core features.

package com.example.musicstreaming;
import java.util.List;
import com.example.musicstreaming.model.*;
import com.example.musicstreaming.repo.InMemoryTrackRepository;
import com.example.musicstreaming.service.StreamService;

public class Main {
    public static void main(String[] args) {
        InMemoryTrackRepository repo = new InMemoryTrackRepository();
        StreamService service = new StreamService(repo);
        
        Track t1 = new Track("Song1", "Artist1", 200, "url1");
        Track t2 = new Track("Song2", "Artist2", 210, "url2");
        service.addTrack(t1);
        service.addTrack(t2);
        
        User user = new User("Poornima");
        Playlist playlist = service.createPlaylistForUser(user, "Favorites");
        service.addTrackToPlaylist(playlist, t1);
        service.addTrackToPlaylist(playlist, t2);
        
        service.playTrack(t1.getId());
        service.playTrack(t2.getId());
        
        List<Track> top = service.topTracks(1);
        System.out.println("Top Track: " + top.get(0).getTitle());
    }
}

Experiment 5: URL Request Flow Simulation

Simulate the complete flow of a URL request from browser to website, including DNS lookup, HTTP/HTTPS request handling, and reverse proxy behavior.

import components.*;
import models.*;

public class Main {
    public static void main(String[] args) {
        String urlInput = "https://www.example.com/about";
        URLModel url = new URLModel(urlInput);
        DNSResolver dns = new DNSResolver();
        String ip = dns.resolve(url.getHostname());
        
        TCPSimulator tcp = new TCPSimulator();
        tcp.connect(ip, url.getPort(), url.getProtocol().equals("https"));
        
        HttpRequest request = new HttpRequest();
        request.setMethod("GET");
        request.setPath(url.getPath());
        request.setSecure(url.getProtocol().equals("https"));
        
        ReverseProxy proxy = new ReverseProxy();
        request = proxy.forward(request);
        
        WebServer server = new WebServer();
        HttpResponse response = server.handle(request);
        
        BrowserSimulator browser = new BrowserSimulator();
        browser.render(response);
        tcp.close();
        System.out.println("Status: " + response.getStatusCode());
    }
}

Experiment 6: Load Balancing Simulation

Design, implement, and analyze a load-balancing simulation demonstrating horizontal scaling using stateless servers and Java multi-threading.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Server> servers = Arrays.asList(
                new Server("S1"),
                new Server("S2"),
                new Server("S3")
        );
        LoadBalancer lb = new LoadBalancer(servers);
        for (int i = 1; i <= 10; i++) {
            int requestId = i;
            new Thread(() -> {
                lb.handleRequest("Request-" + requestId);
            }).start();
        }
    }
}