Skip to content

Commit

Permalink
add detailed solution to lld problems in java
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishps1 committed May 23, 2024
1 parent 0816474 commit 995b0fb
Show file tree
Hide file tree
Showing 200 changed files with 5,848 additions and 3,115 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Learn Low Level Design (LLD) and prepare for interviews using free resources.
- [Design Airline Management System](problems/airline-management-system.md)
- [Design a Digital Wallet System](problems/digital-wallet-system.md)
- [Design an Online Auction System](problems/online-auction-system.md)
- [Design a Vehicle Rental System:](problems/vehicle-rental-system.md)
- [Design a Concert Ticket Booking System](problems/concert-ticketing-system.md)
- [Design a Cache using LRU Eviction Policy](problems/lru-cache.md)
### Hard
Expand Down
160 changes: 21 additions & 139 deletions problems/airline-management-system.md
Original file line number Diff line number Diff line change
@@ -1,141 +1,23 @@
# Designing an Airline Management System

This article delves into the object-oriented design and implementation of an Airline Management System using Java.

This system will handle various aspects of airline operations including managing flights, passengers, crew, and aircraft.

## System Requirements

The Airline Management System should:

1. **Flight Management:** Create and schedule flights.
2. **Passenger Management:** Manage passenger bookings and check-ins.
3. **Crew Management:** Assign crew members to flights.
4. **Aircraft Management:** Track aircraft and maintenance schedules.

## Core Use Cases

1. **Scheduling and Managing Flights**
2. **Booking and Managing Passenger Seats**
3. **Assigning Crew to Flights**
4. **Managing Aircraft**

## Key Classes:
- `AirlineManagementSystem`: Manages the entire system.
- `Flight`: Represents a flight.
- `Passenger`: Represents a passenger.
- `CrewMember`: Represents a flight crew member.
- `Aircraft`: Represents an aircraft.

## Java Implementation

### Flight Class

Represents a flight.

```java
import java.util.Date;

public class Flight {
private String flightNumber;
private Aircraft aircraft;
private Date departureTime;
private String origin;
private String destination;

public Flight(String flightNumber, Aircraft aircraft, Date departureTime, String origin, String destination) {
this.flightNumber = flightNumber;
this.aircraft = aircraft;
this.departureTime = departureTime;
this.origin = origin;
this.destination = destination;
}

// Getters and setters...
}
```
### Passenger Class
Manages passenger information.
```java
public class Passenger {
private String name;
private String passportNumber;

public Passenger(String name, String passportNumber) {
this.name = name;
this.passportNumber = passportNumber;
}

// Getters and setters...
}
```
### CrewMember Class
Represents a crew member.
```java
public class CrewMember {
private String name;
private String employeeId;

public CrewMember(String name, String employeeId) {
this.name = name;
this.employeeId = employeeId;
}

// Getters and setters...
}
```
### Aircraft Class
Represents an aircraft.
```java
public class Aircraft {
private String registrationNumber;
private String model;
private int totalSeats;

public Aircraft(String registrationNumber, String model, int totalSeats) {
this.registrationNumber = registrationNumber;
this.model = model;
this.totalSeats = totalSeats;
}

// Getters and setters...
}
```
### AirlineManagementSystem Class
Manages overall airline operations.
```java
import java.util.ArrayList;
import java.util.List;

public class AirlineManagementSystem {
private List<Flight> flights;
private List<Passenger> passengers;
private List<CrewMember> crewMembers;
private List<Aircraft> aircrafts;

public AirlineManagementSystem() {
this.flights = new ArrayList<>();
this.passengers = new ArrayList<>();
this.crewMembers = new ArrayList<>();
this.aircrafts = new ArrayList<>();
}

public void addFlight(Flight flight) {
flights.add(flight);
}

public void addPassenger(Passenger passenger) {
passengers.add(passenger);
}

public void addCrewMember(CrewMember crewMember) {
crewMembers.add(crewMember);
}

public void addAircraft(Aircraft aircraft) {
aircrafts.add(aircraft);
}

// Other necessary methods...
}
```
## Requirements
1. The airline management system should allow users to search for flights based on source, destination, and date.
2. Users should be able to book flights, select seats, and make payments.
3. The system should manage flight schedules, aircraft assignments, and crew assignments.
4. The system should handle passenger information, including personal details and baggage information.
5. The system should support different types of users, such as passengers, airline staff, and administrators.
6. The system should be able to handle cancellations, refunds, and flight changes.
7. The system should ensure data consistency and handle concurrent access to shared resources.
8. The system should be scalable and extensible to accommodate future enhancements and new features.

### Java Implementation
1. The Flight class represents a flight in the airline management system, with properties such as flight number, source, destination, departure time, arrival time, and available seats.
2. The Aircraft class represents an aircraft, with properties like tail number, model, and total seats.
3. The Passenger class represents a passenger, with properties such as ID, name, email, and phone number.
4. The Booking class represents a booking made by a passenger for a specific flight and seat, with properties such as booking number, flight, passenger, seat, price, and booking status.
5. The Seat class represents a seat on a flight, with properties like seat number, seat type, and seat status.
6. The Payment class represents a payment made for a booking, with properties such as payment ID, payment method, amount, and payment status.
7. The FlightSearch class provides functionality to search for flights based on source, destination, and date.
8. The BookingManager class manages the creation and cancellation of bookings. It follows the Singleton pattern to ensure a single instance of the booking manager.
9. The PaymentProcessor class handles the processing of payments. It follows the Singleton pattern to ensure a single instance of the payment processor.
10. The AirlineManagementSystem class serves as the main entry point of the system, combining all the components and providing methods for flight management, booking, payment processing, and other operations.
184 changes: 22 additions & 162 deletions problems/amazon.md
Original file line number Diff line number Diff line change
@@ -1,164 +1,24 @@
# Designing an Online Shopping System Like Amazon

In this article, we're going to design and implement an Online Shopping System resembling Amazon, using Java.

This system will cover product listings, user accounts, shopping carts, and order processing.

## System Requirements

The Online Shopping System should:

1. **Product Management:** Manage a catalog of products.
2. **User Account Management:** Handle user registrations and logins.
3. **Shopping Cart Management:** Allow users to add and remove products from their shopping cart.
4. **Order Processing:** Process user orders and maintain order history.

## Core Use Cases

1. **Browsing Products**
2. **Managing User Accounts**
3. **Handling Shopping Carts**
4. **Processing Orders**

## Key Classes:
- `OnlineShoppingSystem`: Manages the overall system.
- `Product`: Represents a product in the catalog.
- `User`: Represents a user of the system.
- `ShoppingCart`: Manages the shopping cart.
- `Order`: Represents a user's order.

## Java Implementation

### Product Class

Represents a product in the store.

```java
public class Product {
private String productId;
private String name;
private double price;
private String description;

public Product(String productId, String name, double price, String description) {
this.productId = productId;
this.name = name;
this.price = price;
this.description = description;
}

// Getters and setters...
}
```
### User Class
Handles user account information.
```java
public class User {
private String userId;
private String name;
private String email;
private ShoppingCart cart;
private List<Order> orderHistory;

public User(String userId, String name, String email) {
this.userId = userId;
this.name = name;
this.email = email;
this.cart = new ShoppingCart(this);
this.orderHistory = new ArrayList<>();
}

public void addProductToCart(Product product, int quantity) {
cart.addProduct(product, quantity);
}

public Order checkout() {
Order order = cart.checkout();
orderHistory.add(order);
return order;
}

// Getters and setters...
}
```
### ShoppingCart Class
Manages shopping cart operations.
```java
import java.util.HashMap;
import java.util.Map;

public class ShoppingCart {
private User owner;
private Map<Product, Integer> items;

public ShoppingCart(User owner) {
this.owner = owner;
this.items = new HashMap<>();
}

public void addProduct(Product product, int quantity) {
items.put(product, items.getOrDefault(product, 0) + quantity);
}

public Order checkout() {
Order newOrder = new Order(this.owner, new HashMap<>(items));
items.clear();
return newOrder;
}

// Getters and setters...
}
```
### Order Class
Represents a user's order.
```java
import java.util.Map;

public class Order {
private User user;
private Map<Product, Integer> orderedItems;
private OrderStatus status;

public Order(User user, Map<Product, Integer> orderedItems) {
this.user = user;
this.orderedItems = orderedItems;
this.status = OrderStatus.PROCESSING;
}

public void updateStatus(OrderStatus newStatus) {
this.status = newStatus;
}

// Getters and setters...
}

enum OrderStatus {
PROCESSING, SHIPPED, DELIVERED
}
```
### OnlineShoppingSystem Class
Main class for managing the online shopping platform.
```java
import java.util.ArrayList;
import java.util.List;

public class OnlineShoppingSystem {
private List<Product> catalog;
private List<User> users;

public OnlineShoppingSystem() {
this.catalog = new ArrayList<>();
this.users = new ArrayList<>();
}

public void addProductToCatalog(Product product) {
catalog.add(product);
}

public void addUser(User user) {
users.add(user);
}

// Other necessary methods...
}
```
## Requirements
1. The online shopping service should allow users to browse products, add them to the shopping cart, and place orders.
2. The system should support multiple product categories and provide search functionality.
3. Users should be able to manage their profiles, view order history, and track order status.
4. The system should handle inventory management and update product availability accordingly.
5. The system should support multiple payment methods and ensure secure transactions.
6. The system should handle concurrent user requests and ensure data consistency.
7. The system should be scalable to handle a large number of products and users.
8. The system should provide a user-friendly interface for a seamless shopping experience.

### Java Implementation
[Full Code](../solutions/java/src/onlineshopping/)

1. The User class represents a user in the online shopping service, with properties such as ID, name, email, password, and a list of orders.
2. The Product class represents a product available for purchase, with properties like ID, name, description, price, and quantity. It provides methods to update the quantity and check product availability.
3. The Order class represents an order placed by a user, containing properties such as ID, user, order items, total amount, and order status. It calculates the total amount based on the order items.
4. The OrderItem class represents an item within an order, consisting of the product and the quantity ordered.
5. The OrderStatus enum represents the different statuses an order can have, such as pending, processing, shipped, delivered, or cancelled.
6. The ShoppingCart class represents the user's shopping cart, allowing them to add, remove, and update item quantities. It maintains a map of product IDs and order items.
7. The Payment interface defines the contract for processing payments, with a concrete implementation CreditCardPayment.
8. The OnlineShoppingService class is the central component of the online shopping service. It follows the Singleton pattern to ensure only one instance of the service exists. It provides methods to register users, add products, search products, place orders, and retrieve order information. It handles concurrent access to shared resources using synchronization.
9. The OnlineShoppingServiceDemo class demonstrates the usage of the online shopping service by registering users, adding products, searching for products, placing orders, and viewing order history.
Loading

0 comments on commit 995b0fb

Please sign in to comment.