Skip to content

Commit

Permalink
Add java solution to more problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishps1 committed May 23, 2024
1 parent 995b0fb commit c5cf100
Show file tree
Hide file tree
Showing 88 changed files with 2,783 additions and 1,402 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Learn Low Level Design (LLD) and prepare for interviews using free resources.
- [Design an ATM](problems/atm.md)
- [Design Hotel Management System](problems/hotel-management-system.md)
- [Design LinkedIn](problems/linkedin.md)
- [Design a Social Network like Facebook](problems/facebook.md)
- [Design a Social Network like Facebook](problems/social-networking-service.md)
- [Design an Elevator System](problems/elevator-system.md)
- [Design a Library Management System](problems/library-management-system.md)
- [Design Restaurant Management System](problems/restaurant-management-system.md)
Expand All @@ -56,7 +56,7 @@ Learn Low Level Design (LLD) and prepare for interviews using free resources.
- [Design Movie Ticket Booking System](https://www.youtube.com/watch?v=CC7DwkQOsS0&list=PLAC2AM9O1C5KioUMeH9qIjbAV_RMmX8rd&index=8)
- [Design Splitwise](https://workat.tech/machine-coding/editorial/how-to-design-splitwise-machine-coding-ayvnfo1tfst6)
- [Design a Snake and Ladder game](https://workat.tech/machine-coding/editorial/how-to-design-snake-and-ladder-machine-coding-ehskk9c40x2w)
- [Design Online Shopping System like Amazon](problems/amazon.md)
- [Design Online Shopping System like Amazon](problems/online-shopping-service.md)
- [Design Online Stock Brokerage System](problems/online-stock-brokerage-system.md)
- [Design CricInfo](https://www.youtube.com/watch?v=VDqwCo6lhkY&list=PLAC2AM9O1C5KioUMeH9qIjbAV_RMmX8rd&index=6)
- [Design Chess Game](problems/chess-game.md)
Expand Down
2 changes: 2 additions & 0 deletions problems/airline-management-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
8. The system should be scalable and extensible to accommodate future enhancements and new features.

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

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.
Expand Down
24 changes: 0 additions & 24 deletions problems/amazon.md

This file was deleted.

1 change: 1 addition & 0 deletions problems/atm.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Java Implementation
[Full code](../solutions/java/src/atm/)

1. The Card class represents an ATM card with a card number and PIN.
2. The Account class represents a bank account with an account number and balance. It provides methods to debit and credit the account balance.
3. The Transaction class is an abstract base class for different types of transactions, such as withdrawal and deposit. It is extended by WithdrawalTransaction and DepositTransaction classes.
Expand Down
2 changes: 2 additions & 0 deletions problems/car-rental-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
8. The system should be able to handle concurrent reservations and ensure data consistency.

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

1. The Car class represents a car in the rental system, with properties such as make, model, year, license plate number, rental price per day, and availability status.
2. The Customer class represents a customer, with properties like name, contact information, and driver's license number.
3. The Reservation class represents a reservation made by a customer for a specific car and date range. It includes properties such as reservation ID, customer, car, start date, end date, and total price.
Expand Down
2 changes: 2 additions & 0 deletions problems/chess-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
8. The game should provide a user interface for players to interact with the game.

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

1. The Piece class is an abstract base class representing a chess piece. It contains common attributes such as color, row, and column, and declares an abstract method canMove to be implemented by each specific piece class.
2. The King, Queen, Rook, Bishop, Knight, and Pawn classes extend the Piece class and implement their respective movement logic in the canMove method.
3. The Board class represents the chess board and manages the placement of pieces. It provides methods to get and set pieces on the board, check the validity of moves, and determine checkmate and stalemate conditions.
Expand Down
1 change: 1 addition & 0 deletions problems/coffee-vending-machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

1. The Coffee class represents a coffee type with its name, price, and recipe (ingredients and their quantities).
2. The Ingredient class represents an ingredient used in making coffee, with its name and quantity. It provides a synchronized method to update the quantity.
3. The Payment class represents a payment made by a user, with the amount paid.
Expand Down
182 changes: 21 additions & 161 deletions problems/course-registration-system.md
Original file line number Diff line number Diff line change
@@ -1,163 +1,23 @@
# Designing a University Course Registration System

In this article, we explore the object-oriented design and implementation of a University Course Registration System using Java.

The system facilitates course registration and management for students and universities.

## System Requirements

The University Course Registration System should:

1. **Student Management**: Handle student profiles and academic records.
2. **Course Management**: Manage course details, schedules, and capacities.
3. **Registration Process**: Enable students to register for courses.
4. **Prerequisite Checking**: Ensure students meet course prerequisites.
5. **Enrollment Verification**: Confirm student enrollments in courses.

## Core Use Cases

1. **Registering and Managing Student Profiles**
2. **Adding and Updating Courses**
3. **Enrolling in Courses**
4. **Checking Prerequisites**
5. **Verifying Course Enrollment**

## UML/Class Diagrams

Key Classes:

- `CourseRegistrationSystem`: Manages the system.
- `Student`: Represents a student.
- `Course`: Represents a university course.
- `Enrollment`: Manages student enrollments.

## Java Implementation

### Student Class

Manages student information and enrollment records.

```java
import java.util.HashSet;
import java.util.Set;

public class Student {
private String studentId;
private String name;
private Set<String> completedCourses;
private Set<Enrollment> enrollments;

public Student(String studentId, String name) {
this.studentId = studentId;
this.name = name;
this.completedCourses = new HashSet<>();
this.enrollments = new HashSet<>();
}

public void enrollInCourse(Course course) {
if (course.checkPrerequisites(completedCourses)) {
Enrollment newEnrollment = new Enrollment(this, course);
enrollments.add(newEnrollment);
course.addStudent(this);
return true;
}
return false;
}

public void addCompletedCourse(String courseId) {
completedCourses.add(courseId);
}

// Getters and setters...
}
```
### Course Class
Represents a university course.
```java
import java.util.HashSet;
import java.util.Set;

public class Course {
private String courseId;
private String title;
private int capacity;
private Set<String> prerequisites;
private Set<Student> studentsEnrolled;

public Course(String courseId, String title, int capacity) {
this.courseId = courseId;
this.title = title;
this.capacity = capacity;
this.prerequisites = new HashSet<>();
this.studentsEnrolled = new HashSet<>();
}

public boolean addStudent(Student student) {
if (studentsEnrolled.size() < capacity) {
studentsEnrolled.add(student);
return true;
}
return false;
}

public void addPrerequisite(String prerequisiteCourseId) {
prerequisites.add(prerequisiteCourseId);
}

public boolean checkPrerequisites(Set<String> completedCourses) {
return completedCourses.containsAll(prerequisites);
}
// Getters and setters...
}
```
### Enrollment Class
Manages a student's enrollment in a course.
```java
public class Enrollment {
private Student student;
private Course course;

public Enrollment(Student student, Course course) {
this.student = student;
this.course = course;
}

// Getters and setters...
}
```
### CourseRegistrationSystem Class
Manages the course registration system operations.
```java
import java.util.ArrayList;
import java.util.List;

public class CourseRegistrationSystem {
private List<Student> students;
private List<Course> courses;

public CourseRegistrationSystem() {
this.students = new ArrayList<>();
this.courses = new ArrayList<>();
}

public void addStudent(Student student) {
students.add(student);
}

public void addCourse(Course course) {
courses.add(course);
}

public boolean registerStudentForCourse(String studentId, String courseId) {
Student student = findStudentById(studentId);
Course course = findCourseById(courseId);

if (student != null && course != null) {
return student.enrollInCourse(course);
}
return false;
}

// Other necessary methods...
}
```
## Requirements
1. The course registration system should allow students to register for courses and view their registered courses.
2. Each course should have a course code, name, instructor, and maximum enrollment capacity.
3. Students should be able to search for courses based on course code or name.
4. The system should prevent students from registering for courses that have reached their maximum enrollment capacity.
5. The system should handle concurrent registration requests from multiple students.
6. The system should ensure data consistency and prevent race conditions.
7. The system should be extensible to accommodate future enhancements and new features.

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

1. The Student class represents a student in the course registration system, with properties such as ID, name, email, and a list of registered courses.
2. The Course class represents a course offered in the system, with properties such as code, name, instructor, maximum capacity, and the number of enrolled students.
3. The Registration class represents a registration record, associating a student with a course and capturing the registration timestamp.
4. The CourseRegistrationSystem class is the main class that manages the course registration system. It follows the Singleton pattern to ensure only one instance of the system exists.
5. The CourseRegistrationSystem class provides methods for adding courses and students, searching for courses, registering students for courses, and retrieving registered courses for a student.
6. Multi-threading is implemented using concurrent data structures (ConcurrentHashMap and CopyOnWriteArrayList) to handle concurrent access to shared data, such as courses and registrations.
7. The registerCourse method is synchronized to ensure thread safety when multiple students are registering for courses simultaneously.
8. The notifyObservers method is a placeholder for notifying observers (e.g., UI components) about updates to course enrollment.
9. The CourseRegistrationDemo class demonstrates the usage of the course registration system by creating courses and students, searching for courses, registering students for courses, and retrieving registered courses for a student.
Loading

0 comments on commit c5cf100

Please sign in to comment.