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 int capacity;
public Course(String courseName, String courseCode, int capacity) {
public Course(String courseName, String courseCode, int capacity)
{
this.courseName = courseName;
this.courseCode = courseCode;
this.capacity = capacity;
@@ -23,7 +24,8 @@ public class Course {
return capacity;
}
public boolean reduceCapacity() {
public boolean reduceCapacity()
{
if (capacity > 0) {
capacity--;
return true;
+2 -1
View File
@@ -1,7 +1,8 @@
package CourseRegistrationStarter;
public class Main {
public static void main(String[] args) {
public static void main(String[] args)
{
// TODO:
// 1. Create at least 3 courses
// 2. Create 1 student
@@ -1,24 +1,53 @@
package CourseRegistrationStarter;
import java.util.ArrayList;
public class RegistrationSystem {
public class RegistrationSystem
{
private ArrayList<Course> courses;
public RegistrationSystem() {
public RegistrationSystem()
{
courses = new ArrayList<>();
}
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
public void addCourseToSystem(Course c)
{
// 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) {
// TODO: Search for a course by course code
// Return the matching course if found, otherwise return null
public Course findCourseByCode(String code)
{
// 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;
}
public void showAllCourses() {
// TODO: Print all courses in the system
public void showAllCourses()
{
// 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;
import java.util.ArrayList;
public class Student {
public class Student
{
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
public Student(String name, String studentId) {
public Student(String name, String studentId)
{
this.name = name;
this.studentId = studentId;
this.registeredCourses = new ArrayList<>();
@@ -20,20 +22,55 @@ public class Student {
return studentId;
}
public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false
return false;
public boolean addCourse(Course c)
{
// Register the student in the course if capacity allows
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) {
// TODO: Remove the course from the student's list
public boolean dropCourse(Course c)
{
// Remove the course from the student's list
// Restore the course capacity if removal was successful
// Return true if the course was dropped, otherwise false
return false;
if (registeredCourses.contains(c))
{
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() {
// TODO: Print all registered courses
public void showRegisteredCourses()
{
// 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");
}
}