dgPadBootcamps / Java-Bootcamp-2024

1 stars 0 forks source link

Task 3 : Student management system with courses #161

Open mohammad-fahs opened 3 weeks ago

mohammad-fahs commented 3 weeks ago

Objective

The goal of this task is to build a simple Student Management System using Spring Boot, where each student has a list of courses. Students will implement APIs to manage students and their courses without using a database, storing data in-memory using ArrayList.

Requirements

  1. Student Class:
    • Each student should have the following attributes:
      • id: A unique identifier for the student (e.g., Long).
      • name: The student's name (e.g., String).
      • email: The student's email address (e.g., String).
      • courses: A list of courses the student is enrolled in (e.g., List<String>).
  2. Course Class
    1. Each class should have the following attributes:
      • id: A unique identifier for the student (e.g., Long).
      • name: The student's name (e.g., String).
      • teacher: the course instructor (e.g., String).
      • credits: the course credits (e.g., Integer)
  3. Course Management:
    • Implement functionality to add and remove courses for a student.
  4. Service Class:
    • Create a StudentService class that contains business logic for managing students and their courses using an ArrayList to store the students.
  5. Controller Class:
    • Create a StudentController class to handle HTTP requests and interact with the StudentService.
  6. APIs: Implement the following APIs and test them using Postman:
    • Create a Student:
      • Method: POST
      • Endpoint: /students
      • Request Body: JSON object with name and email.
      • Response: Created student with id, name, email, and an empty list of courses.
    • Get All Students:
      • Method: GET
      • Endpoint: /students
      • Response: A list of all students.
    • Get a Student by ID:
      • Method: GET
      • Endpoint: /students/{id}
      • Response: The student with the specified id, including their list of courses.
    • Update a Student's Details:
      • Method: PUT
      • Endpoint: /students/{id}
      • Request Body: JSON object with name and email.
      • Response: The updated student information.
    • Delete a Student:
      • Method: DELETE
      • Endpoint: /students/{id}
      • Response: A confirmation that the student was deleted.
    • Add a Course to a Student: (student can has a maximum of 17 credit)
      • Method: POST
      • Endpoint: /students/{id}/courses
      • Request Body: JSON object with courseName.
      • Response: The updated list of courses for the student.
    • Remove a Course from a Student:
      • Method: DELETE
      • Endpoint: /students/{id}/courses
      • Request Body: JSON object with courseName.
      • Response: The updated list of courses for the student.
    • Create a Course:
      • Method: POST
      • Endpoint: /courses
    • Get All Courses:
      • Method: GET
      • Endpoint: /courses
      • Response: A list of all courses.
    • Get a Course by ID:
      • Method: GET
      • Endpoint: /courses/{courseId}
      • Response: The course with the specified courseId.
    • Update a Course's Details:
      • Method: PUT
      • Endpoint: /courses/{courseId}
    • Delete a Course:
      • Method: DELETE
      • Endpoint: /courses/{courseId}
      • Response: A confirmation that the course was deleted.
  7. In-Memory Storage:
    • Use an ArrayList in the StudentService class to store the list of students. No database is required for this task.

Instructions

  1. Project Setup:
    • Use Spring Initializr or your IDE to create a Spring Boot project with the Web dependency.
  2. Implement the Student Class:
    • Create a Student class with the required attributes and methods (e.g., constructors, getters, and setters).
  3. Implement the StudentService Class:
    • Create a StudentService class that handles the business logic for managing students and their courses.
  4. Implement the StudentController Class:
    • Create a StudentController class to map the APIs to their respective service methods.
  5. Test the APIs:
    • Use Postman to test all the implemented APIs, ensuring they work as expected.

Example Scenario

create a simple scenario to add list of courses, list of students with set of courses, in case the student had more than 17 course let the operation fail,

update courses and remove some courses and list them

Submission

Submit the completed project files along with a Postman screenshots containing the API requests and response . Include screenshots of successful API responses in Postman as evidence of testing.

ZahraaSaleh13 commented 2 weeks ago

Student Class

package dto;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Getter
@Setter
public class Student {
    private String id;
    private String Name;
    private String email;
    private List<Course> courses= new ArrayList<Course>();

    public Student( String name, String email, List<Course> courses) {
        this.id = UUID.randomUUID().toString();
        Name = name;
        this.email = email;
        this.courses = courses;
    }
    public void addCourse(String Name, String Teacher, int cridets){
        if(cridets <= 17)
             courses.add(new Course(Name,Teacher,cridets));
    }

    public void RemoveCourse(String courseName){
        for(Course c: courses){
            if(c.getName().equals(courseName))
                courses.remove(c);
        }
    }

}

Course Class

package dto;

import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Getter
@Setter
public class Course {
    private String id;
    private String Name;
    private String Teacher;
    private int credits;

    public Course( String name, String teacher, int credits) {
        this.id = UUID.randomUUID().toString();
        Name = name;
        Teacher = teacher;
        this.credits = credits;
    }
}

Student Service Class

package Service;

import dto.Course;
import dto.Student;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentService {
   public List<Student> students=new ArrayList<>();
   public Student addStudent(String name, String email, List<Course> courses){
       Student s=new Student(name,email,courses);
      students.add(s);
       return s;
   }

   public List<Student> getListStudents(){
       return students;
   }

   public Student getStudentById(String id){
       for(Student s :students){
           if(s.getId().equals(id))
               return s;
       }
       return new Student(null,null,null);
   }

   //update details
    public Student updateStudentDetails(String id, Student updatedStudent){
        for(Student s :students){
            if(s.getId().equals(id)) {
                s.setName(updatedStudent.getName());
                s.setEmail(updatedStudent.getEmail());
                return s;
            }
        }
        return null;
    }

    //delete student
    public void deleteStudent(String id){
       Student s= getStudentById(id);
        students.remove(s);
    }

    //add course to student
    public Course addCourse(Student student, String courseName, String Teacher, int cridets){
       Course c= new Course(courseName,Teacher,cridets);
       student.addCourse(c.getName(),c.getTeacher(),c.getCredits());
       return c;
    }

    public void deleteCourse(String  studentId, String courseName){
       Student s= getStudentById(studentId);
       s.RemoveCourse(courseName);
    }
    public Course getCourseById(String studentId,String courseId){
            Student s= getStudentById(studentId);
            for(Course c : s.getCourses()){
                if(c.getId().equals(courseId))
                    return c;
            }
            return null;
    }
//update course details for a student
    public Course updateCourseDetails(String studentId, Course updatedCourse){
       Student s= getStudentById(studentId);
        for(Course c : s.getCourses()){
            if(c.getId().equals(updatedCourse.getId())) {
                c.setName(updatedCourse.getName());
                c.setTeacher(updatedCourse.getTeacher());
                c.setCredits(updatedCourse.getCredits());
                return c;
            }
        }
        return null;
    }

}

Student Controller


package Controller;

import Service.StudentService;
import dto.Course;
import dto.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("api/v1/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //add student
    @PostMapping
    public ResponseEntity<Student> createStudent(@RequestBody Student student){
        Student newstudent= studentService.addStudent(student.getName(), student.getEmail(),null);
        return new ResponseEntity<>(newstudent, HttpStatus.OK);
    }

    //get all students
    @GetMapping
    public ResponseEntity<List<Student>> getStudents(){
        List<Student> students =studentService.getListStudents();
        return new ResponseEntity<>(students, HttpStatus.OK);
    }

    //get student by id
    @GetMapping("/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable String id){
        Student student= studentService.getStudentById(id);
        return new ResponseEntity<>(student, HttpStatus.OK);
    }

    //update student info
    @PutMapping("/{id}")
    public ResponseEntity<Student> updateStudent(@PathVariable String id, @RequestBody Student student){
        Student updatedStudent= studentService.updateStudentDetails(id,student);
        if(updatedStudent!=null)
              return new ResponseEntity<>(updatedStudent, HttpStatus.OK);
        else
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteStudent(@PathVariable String id){
        studentService.deleteStudent(id);
        return new ResponseEntity<>("student deleted successfully", HttpStatus.OK);
    }

    //add course to students
    @PostMapping("/{id}/courses")
    public ResponseEntity<String> createStudent(@RequestBody Course course, @PathVariable String id){
        Student s= studentService.getStudentById(id);
        Course addedCourse=studentService.addCourse(s,course.getName(),course.getTeacher(),course.getCredits());
        return new ResponseEntity<>(addedCourse.getName(), HttpStatus.OK);
    }

    //delete course for student
    @DeleteMapping("/{id}/courses")
    public ResponseEntity<List<Course>> deleteCourse(@PathVariable String studentId, @PathVariable String courseName){
        Student s= studentService.getStudentById(studentId);
        s.RemoveCourse(courseName);
        return new ResponseEntity<>(s.getCourses(), HttpStatus.OK);
    }

    //add course to student
    @PostMapping("/{id}/courses")
    public ResponseEntity<String> createCourse(@PathVariable String studentId, @RequestBody Course course){
        Student s= studentService.getStudentById(studentId);
        s.addCourse(course.getName(),course.getTeacher(),course.getCredits());
        return new ResponseEntity<>("course created successfully", HttpStatus.OK);
    }

    //get all courses for a student
    @GetMapping("/{id}/courses")
    public ResponseEntity<List<Course>> getCoursesForStudent(@PathVariable String studentId){
        Student s= studentService.getStudentById(studentId);
        List<Course> courses= s.getCourses();
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }

    //get course by id
    @GetMapping("/{id}/courses/{courseId}")
    public ResponseEntity<Course> getCourseById(@PathVariable String studentId,@PathVariable String courseId){
        Course c= studentService.getCourseById(studentId,courseId);
        return new ResponseEntity<>(c ,HttpStatus.OK);
    }

    //update course info
    @PutMapping("/{id}/courses/{courseId}")
    public ResponseEntity<Course> updateCourse(@PathVariable String studentId, @RequestBody Course course){
        Course updatedCourse= studentService.updateCourseDetails(studentId,course);
            return new ResponseEntity<>(updatedCourse, HttpStatus.OK);

    }

}