---
# System prepended metadata

title: lab1初

---

# lab1初
# Course.java
```
package com.cctbcbank.demo.Lab1.model;

import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Course {
	private UUID id;

	private String name;

	private int price;

	private CourseCategory category;

	public void addCourse(Course c) {
		category.addCourse(c);
	}

	public void removeCourse(UUID uuid) {
		category.removeCourse(uuid);
	}

	public void modifyCourse(Course c) {
		category.setName(name);
	}

	public Course() {
		super();
	}

	public Course(CourseCategory category) {
		super();
		this.category = category;
	}


}

```
# CourseTest
```
package com.cctbcbank.demo.Step1;

import java.util.UUID;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.cctbcbank.demo.Lab1.model.Course;
import com.cctbcbank.demo.Lab1.model.CourseCategory;

public class CourseTest {
	private CourseCategory category;
	//private List<Course> courses = new ArrayList<>();
	private Course course;

	@Test
	public void CourseNullTest() {
		course = new Course();
		Assertions.assertNotNull(course);
	}
	
	@Test
	public void addCousreTest() {
		category = new CourseCategory();
		course = new Course();
		category.addCourse(course);
		Assertions.assertNotNull(category);
	}

	@Test
	public void removeCourseTest() {
		category = new CourseCategory();
		course = new Course();
		UUID id = UUID.randomUUID();
		course.setId(id);
		course.setName("abc");
		course.setPrice(1000);
//		System.out.println("1="+course);
		category.addCourse(course);
		
//		System.out.println("2="+category.getCourses().get(0));
		category.removeCourse(id);
//		System.out.println("3="+category.getId());
		Assertions.assertNull(category.getId());
	}
	
	
	@Test
	public void CourseGetterSetterTest() {
		course = new Course();
		course.setName("xyz");
		Assertions.assertEquals("xyz",course.getName());
	}
}

```
# CourseServiceJDKImplement.java
```
package com.cctbcbank.demo.Lab1.Service;

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

import com.cctbcbank.demo.Lab1.model.Course;
import com.cctbcbank.demo.Lab1.model.CourseCategory;

public class CourseServiceJDKImplement implements CourseService {
	public static Course course = null;
	private List<Course> courses = new ArrayList<>();
	@Override
	public List<Course> getAllCourseService() {
		courses = new ArrayList<>();
		courses.add(course);
		return courses;
	}

	@Override
	public Course addCourseService(String name, int price) {
		course = new Course();
		courses = new ArrayList<>();
		course.setId(UUID.randomUUID());
		course.setName(name);
		course.setPrice(price);
		courses.add(course);

		return course;
	}

	@Override
	public void removeCourseService(UUID id) {
		courses = new ArrayList<>();
		course = new Course();
		if(course.getId()!=null) {
			courses.remove(id);			
		}
		else {
			course.setId(null);
			courses.add(course);
		}
	}

	@Override
	public Course modifyCourseService(UUID id, String name, int price) {
		course = new Course();
		if(id!=null) {
			course.setId(id);
			course.setName(name);
			course.setPrice(price);			
		}
		else {
			course.setId(null);
		}

		return course;
	}

}

```
# CourseServiceTest
```
package com.cctbcbank.demo.Step2;

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

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.cctbcbank.demo.Lab1.Service.CourseServiceJDKImplement;
import com.cctbcbank.demo.Lab1.model.Course;
import com.cctbcbank.demo.Lab1.model.CourseCategory;

public class CourseServiceTest {
	private Course course = null;
//	private Course allCourseService = null;
//	private Course category = new Course();
//	private Course category = null;
	private List<Course> courses = new ArrayList<>();

//	取得所有課程
	@Test
	public void getAllCourseServiceTest() {
		course = new Course();
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		course = service.addCourseService("Java", 10000);
//		System.out.println(course.getName());
		Assertions.assertEquals("Java", course.getName());
	}

//	新增課程測試
	@Test
	public void addCourseServiceTest() {
		course = new Course();
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		String Name = "Java";
		int Price = 10000;
		course = service.addCourseService(Name, Price);
		Assertions.assertEquals(Name, course.getName());
	}

//	刪除課程測試(未完成)
	@Test
	public void removeCourseServiceTest() {
		course = new Course();
//		實作
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		String Name = "Java";
		int Price = 10000;
		course = service.addCourseService(Name, Price);
		courses.add(course);
//		System.out.println(service.getAllCourseService().get(0).getId());
		service.removeCourseService(service.getAllCourseService().get(0).getId());
//		System.out.println(service.getAllCourseService().get(0).getId());
		Assertions.assertNull(service.getAllCourseService().get(0).getId());
	}

//	修改課程
	@Test
	public void modifyCourseServiceTest() {
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		UUID uuid = UUID.randomUUID();
		String Name = "Java";
		int Price = 10000;
		service.addCourseService("C#", 12000);
//		System.out.println(service.getAllCourseService().get(0).getName());
		service.modifyCourseService(uuid, Name, Price);
//		System.out.println(service.getAllCourseService().get(0).getName());
		Assertions.assertEquals(Name, service.getAllCourseService().get(0).getName());
	}
}

```
# CourseCategory.java

```
package com.cctbcbank.demo.Lab1.model;

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

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CourseCategory {
	private UUID id;

	private String name;

	private String detail;

	private List<Course> courses = new ArrayList<>();

	public void addCourse(Course c) {
		courses.add(c);
	}
	public void removeCourse(UUID uuid) {
		courses.remove(uuid);
	}
	
	public CourseCategory() {
		super();
	}
	public CourseCategory(List<Course> courses) {
		super();
		this.courses = courses;
	}
	
}

```
# CourseCategoryTest
```
package com.cctbcbank.demo.Step1;

import java.util.UUID;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.cctbcbank.demo.Lab1.model.Course;
import com.cctbcbank.demo.Lab1.model.CourseCategory;

public class CourseCategoryTest {
	public static Course course = null;
	public static CourseCategory courseCategory = null;

	@Test
	public void CourseCategoryNullTest() {
		CourseCategory c1 = new CourseCategory();
		Assertions.assertNotNull(c1);
	}

	@Test
	public void addCourseTest() {
		course = new Course();
		courseCategory = new CourseCategory();

		course.setId(UUID.randomUUID());
		course.setName("ABC");
		course.setPrice(1000);

//		list有內容
//		System.out.println(courses.get(0).getName());
//		去叫courseCategory中的addCourse()
		courseCategory.addCourse(course);

//		System.out.println(courseCategory.getCourses().get(0).getName());
		Assertions.assertNotNull(courseCategory.getCourses());

	}

	@Test
	public void removeCourseTest() {
		course = new Course();
		courseCategory = new CourseCategory();
		UUID id = UUID.randomUUID();

		course.setId(id);
		course.setName("ABC");
		course.setPrice(1000);

//		System.out.println(course.getName());
		courseCategory.removeCourse(id);

//		System.out.println(courseCategory.getName());

		Assertions.assertNull(courseCategory.getName());

	}
	@Test
	public void CourseCategoryGetterSetterTest() {
		courseCategory = new CourseCategory();
		courseCategory.setName("xyz");
		Assertions.assertEquals("xyz", courseCategory.getName());
	}

}

```
# CourseCategoryServiceJDKImplement.java
```
package com.cctbcbank.demo.Lab1.Service;

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

import com.cctbcbank.demo.Lab1.model.CourseCategory;

public class CourseCategoryServiceJDKImplement implements CourseCategoryService {
	private static CourseCategory courseCategory = null;
	private List<CourseCategory> allCourseCategory = new ArrayList<>();

	@Override
	public List<CourseCategory> getAllCourseCategory() {
		allCourseCategory = new ArrayList<>();	
		allCourseCategory.add(courseCategory);
		return allCourseCategory;
	}

	@Override
	public CourseCategory addCourseCategory(String name, String detail) {
		courseCategory = new CourseCategory();
		allCourseCategory = new ArrayList<>();
		courseCategory.setId(UUID.randomUUID());
		courseCategory.setName(name);
		courseCategory.setDetail(detail);
		allCourseCategory.add(courseCategory);
		
		return courseCategory;
	}

	@Override
	public void removeCourseCategory(UUID id) {
		courseCategory = new CourseCategory();
		allCourseCategory = new ArrayList<>();
		if(courseCategory.getId()!=null) {
			allCourseCategory.remove(id);
			courseCategory.removeCourse(id);	
		}
		else if(courseCategory.getId()== null) {
			courseCategory.setId(null);
		}
	}

	@Override
	public CourseCategory modifyCourseCategory(UUID id, String name, String detail) {
		courseCategory = new CourseCategory();
		if(id!=null) {
			courseCategory.setId(id);
			courseCategory.setName(name);
			courseCategory.setDetail(detail);	
		}
		else {
			courseCategory.setId(null);
		}
		
		return courseCategory;
	}

}

```
# CourseCategoryServiceTest
```
package com.cctbcbank.demo.Step2;

import java.util.UUID;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.cctbcbank.demo.Lab1.Service.CourseCategoryServiceJDKImplement;
import com.cctbcbank.demo.Lab1.model.CourseCategory;

public class CourseCategoryServiceTest {
	private CourseCategory courseCategory;
//	private List<CourseCategory> allcourseCategory = new ArrayList<>();

//	取得所有課程類別
	@Test
	public void getAllCourseCategoryTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		service.addCourseCategory("Java","Java is good");
//		System.out.println(service.getAllCourseCategory().get(0).getName());
		Assertions.assertNotNull(service.getAllCourseCategory());
	}

//	新增測試課程名稱、資訊
	@Test
	public void addCourseCategoryTest() {
		String Name = "Java";
		String Detail = "Java is good";
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		courseCategory = service.addCourseCategory(Name, Detail);
//		System.out.println(courseCategory.getName());
		Assertions.assertEquals(Name, courseCategory.getName());

	}

//	刪除測試課程名稱、資訊
	@Test
	public void removeCourseCategoryTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		String Name = "Java";
		String Detail = "Java is good";
		service.addCourseCategory(Name, Detail);	
//		System.out.println(service.getAllCourseCategory().get(0).getId());
		service.removeCourseCategory(service.getAllCourseCategory().get(0).getId());
//		System.out.println(service.getAllCourseCategory().get(0).getId());
		Assertions.assertEquals(null,service.getAllCourseCategory().get(0).getId());
	}

//	修改測試課程名稱、資訊
	@Test
	public void modifyCourseCategoryTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		
		UUID id = UUID.randomUUID();
		String Name = "Java";
		String Detail = "Java is good";
		service.addCourseCategory(Name, Detail);
//		System.out.println(service.getAllCourseCategory().get(0).getName());
		service.modifyCourseCategory(id, "C#","C# is good");
//		System.out.println(service.getAllCourseCategory().get(0).getName());
		Assertions.assertEquals("C#", service.getAllCourseCategory().get(0).getName());
	}
}

```
