implement triangle method

This commit is contained in:
2026-04-20 23:36:18 +03:30
parent 8ddd529c85
commit 67f45fe78d
4 changed files with 93 additions and 24 deletions
+4 -2
View File
@@ -5,7 +5,8 @@ public class Course {
private String courseCode; private String courseCode;
private int capacity; private int capacity;
public Course(String courseName, String courseCode, int capacity) { public Course(String courseName, String courseCode, int capacity)
{
this.courseName = courseName; this.courseName = courseName;
this.courseCode = courseCode; this.courseCode = courseCode;
this.capacity = capacity; this.capacity = capacity;
@@ -23,7 +24,8 @@ public class Course {
return capacity; return capacity;
} }
public boolean reduceCapacity() { public boolean reduceCapacity()
{
if (capacity > 0) { if (capacity > 0) {
capacity--; capacity--;
return true; return true;
+2 -1
View File
@@ -1,7 +1,8 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args)
{
// TODO: // TODO:
// 1. Create at least 3 courses // 1. Create at least 3 courses
// 2. Create 1 student // 2. Create 1 student
@@ -1,24 +1,53 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import java.util.ArrayList; import java.util.ArrayList;
public class RegistrationSystem { public class RegistrationSystem
{
private ArrayList<Course> courses; private ArrayList<Course> courses;
public RegistrationSystem() { public RegistrationSystem()
{
courses = new ArrayList<>(); courses = new ArrayList<>();
} }
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course c)
// TODO: Add the course to the system {
// Add the course to the system
courses.add(c);
System.out.println("Course " + c.getCourseName() + " added to the system."); // Feedback message
} }
public Course findCourseByCode(String code) { public Course findCourseByCode(String code)
// TODO: Search for a course by course code {
// Return the matching course if found, otherwise return null // Search for a course by course code
for (Course c : courses)
{
if (c.getCourseCode().equals(code))
{
// Return the matching course if found
return c;
}
}
// otherwise return null
System.out.println("Course with code " + code + " not found."); // Feedback message
return null; return null;
} }
public void showAllCourses() { public void showAllCourses()
// TODO: Print all courses in the system {
// Print all courses in the system
System.out.println("\n--- All Courses in the System ---");
if (courses.isEmpty())
{
System.out.println("No courses available in the system.");
}
else
{
for (Course c : courses)
{
System.out.println("Name: " + c.getCourseName() + ", Code: " + c.getCourseCode() + ", Capacity: " + c.getCapacity());
}
}
System.out.println("-------------------------------\n");
} }
} }
+49 -12
View File
@@ -1,12 +1,14 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import java.util.ArrayList; import java.util.ArrayList;
public class Student { public class Student
{
private String name; private String name;
private String studentId; private String studentId;
private ArrayList<Course> registeredCourses; private ArrayList<Course> registeredCourses;
public Student(String name, String studentId) { public Student(String name, String studentId)
{
this.name = name; this.name = name;
this.studentId = studentId; this.studentId = studentId;
this.registeredCourses = new ArrayList<>(); this.registeredCourses = new ArrayList<>();
@@ -20,20 +22,55 @@ public class Student {
return studentId; return studentId;
} }
public boolean addCourse(Course c) { public boolean addCourse(Course c)
// TODO: Register the student in the course if capacity allows {
// Return true if registration was successful, otherwise false // Register the student in the course if capacity allows
return false; if (c.reduceCapacity())
{ // Reduce capacity, returns true if successful (capacity > 0)
registeredCourses.add(c);
System.out.println(name + " successfully registered for course: " + c.getCourseName());
return true;
}
else
{
System.out.println(name + " could not register for course " + c.getCourseName() + ". Course is full.");
return false;
}
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c)
// TODO: Remove the course from the student's list {
// Remove the course from the student's list
// Restore the course capacity if removal was successful // Restore the course capacity if removal was successful
// Return true if the course was dropped, otherwise false if (registeredCourses.contains(c))
return false; {
registeredCourses.remove(c);
c.increaseCapacity(); // Restore capacity
System.out.println(name + " successfully dropped course: " + c.getCourseName());
return true;
}
else
{
System.out.println(name + " is not registered for course: " + c.getCourseName() + ", cannot drop.");
return false;
}
} }
public void showRegisteredCourses() { public void showRegisteredCourses()
// TODO: Print all registered courses {
// Print all registered courses
System.out.println("\n--- Courses Registered by " + name + " ---");
if (registeredCourses.isEmpty())
{
System.out.println(name + " is not registered for any courses.");
}
else
{
for (Course c : registeredCourses)
{
System.out.println("Name: " + c.getCourseName() + ", Code: " + c.getCourseCode());
}
}
System.out.println("------------------------------------\n");
} }
} }