Open mohammad-fahs opened 3 weeks ago
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);
}
}
}
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;
}
}
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;
}
}
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);
}
}
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
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>
).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
)StudentService
class that contains business logic for managing students and their courses using anArrayList
to store the students.StudentController
class to handle HTTP requests and interact with theStudentService
.POST
/students
name
andemail
.id
,name
,email
, and an empty list of courses.GET
/students
GET
/students/{id}
id
, including their list of courses.PUT
/students/{id}
name
andemail
.DELETE
/students/{id}
POST
/students/{id}/courses
courseName
.DELETE
/students/{id}/courses
courseName
.POST
/courses
GET
/courses
GET
/courses/{courseId}
courseId
.PUT
/courses/{courseId}
DELETE
/courses/{courseId}
ArrayList
in theStudentService
class to store the list of students. No database is required for this task.Instructions
Student
class with the required attributes and methods (e.g., constructors, getters, and setters).StudentService
class that handles the business logic for managing students and their courses.StudentController
class to map the APIs to their respective service methods.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.