---
# System prepended metadata

title: 2023/05/15 lab1

---

# 2023/05/15 lab1
# Course
```
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 Course() {
		super();
	}

	public Course(String name,int price) {
		super();
		this.name = name;
		this.price = price;
	}

}

```
# CourseCategory
```
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;
	}
	
}

```
# CourseCategoryService
```
package com.cctbcbank.demo.Lab1.Service;

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

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

public interface CourseCategoryService {
	List<CourseCategory> getAllCourseCategory();

	CourseCategory addCourseCategory(String name, String detail);

	void removeCourseCategory(UUID uuid);

	CourseCategory modifyCourseCategory(UUID id, String name, String detail);
}

```
# CourseCategoryServiceJDKImplement
```
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 = new CourseCategory();
	private List<CourseCategory> allCourseCategory = new ArrayList<>();
	

	@Override
	public List<CourseCategory> getAllCourseCategory() {
		return allCourseCategory;
	}

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

		return courseCategory;
	}

	@Override
	public void removeCourseCategory(UUID id) {
		int i = 0;
		while (i < allCourseCategory.size()) {
			if (allCourseCategory.get(i).getId() == id) {
				allCourseCategory.set(i,null);
				break;
			} else {
				i++;
				System.out.println("null");
				break;
			}

		}
	}

	@Override
	public CourseCategory modifyCourseCategory(UUID id, String name, String detail) {
		if (courseCategory.getId() == id) {
			courseCategory.setName(name);
			courseCategory.setDetail(detail);
		} else {
			System.out.println("null");
		}
		return courseCategory;
	}

}

```
# CourseService
```
package com.cctbcbank.demo.Lab1.Service;

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

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

public interface CourseService {
	List<Course> getAllCourseService();

	Course addCourseService(String name, int price);

	void removeCourseService(UUID uuid);

	Course modifyCourseService(UUID id, String name, int price);
}

```
# CourseServiceJDKImplement
```
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> allCourses = new ArrayList<>();
	private static CourseCategory courseCategory = new CourseCategory();

	@Override
	public List<Course> getAllCourseService() {

		return allCourses;
	}

	@Override
	public Course addCourseService(String name, int price) {
		course = new Course();
		course.setId(UUID.randomUUID());
		course.setName(name);
		course.setPrice(price);
		allCourses.add(course);
		courseCategory = new CourseCategory();
		courseCategory.addCourse(course);

		return course;
	}

	@Override
	public void removeCourseService(UUID id) {
		int i = 0;
		while (i < allCourses.size()) {
			if (allCourses.get(i).getId() == id) {
				allCourses.set(i, null);
				courseCategory.removeCourse(id);
				break;
			} else {
				i++;
				System.out.println("null");
				break;
			}
		}

	}

	@Override
	public Course modifyCourseService(UUID id, String name, int price) {
		if (course.getId() == id) {
			course.setName(name);
			course.setPrice(price);
		} else {
			System.out.println("null");
		}
		return course;
	}

}

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

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.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 addCourseNullTest() {
		courseCategory = new CourseCategory();
		courseCategory.addCourse(course);
//		System.out.println(courseCategory.getCourses().get(0));
		Assertions.assertNull(courseCategory.getCourses().get(0));

	}

//	判斷有無生成
	@Test
	public void removeCourseTest() {
		courseCategory = new CourseCategory();
		courseCategory.removeCourse(UUID.randomUUID());
		System.out.println(courseCategory.getCourses());

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

}

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

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 = null;

	@Test
	public void CourseNullTest() {
		course = new Course("Java",5000);
		Assertions.assertNotNull(course);
	}
	
	@Test
	public void addCourseTest() {
		category = new CourseCategory();
		course = new Course("Java",5000);
		category.addCourse(course);
		Assertions.assertNotNull(category);
	}

	@Test
	public void removeCourseTest() {
		category = new CourseCategory();
		course = new Course("abc",1000);
		category.addCourse(course);
		category.removeCourse(category.getId());
		Assertions.assertNull(category.getId());
	}
	
	
	@Test
	public void CourseGetterSetterTest() {
		course = new Course("xyz",1000);
		Assertions.assertEquals("xyz",course.getName());
	}
}

```
# CourseCategoryServiceTest
```
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.CourseCategoryServiceJDKImplement;
import com.cctbcbank.demo.Lab1.model.CourseCategory;

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

//	服務生成時是否為空
	@Test
	public void CourseCategoryServiceNullTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
//		System.out.println(service.getAllCourseCategory().size());
//		使用size()判斷是否有生成並且為空
		Assertions.assertEquals(0, service.getAllCourseCategory().size());
	}

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

//	新增一筆課程類別
	@Test
	public void addOneCourseCategoryTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		String Name = "Java";
		String Detail = "Java is good";
		System.out.println("新增課程類別 "+Name);
		courseCategory = service.addCourseCategory(Name, Detail);
		System.out.println("新增結果 "+courseCategory.getName()+"\r\n");
		Assertions.assertEquals(Name, courseCategory.getName());
	}
	
//	新增多筆課程類別
	@Test
	public void addManyCourseCategoryTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		String Name = "Java";
		String Detail = "Java is good";
		String Name2 = "Java2";
		String Detail2 = "Java is good2";
		String Name3 = "Java3";
		String Detail3 = "Java is good3";
		System.out.println("新增課程類別");
		courseCategory = service.addCourseCategory(Name, Detail);
		allCourseCategory.add(courseCategory);
		courseCategory = service.addCourseCategory(Name2, Detail2);
		allCourseCategory.add(courseCategory);
		courseCategory = service.addCourseCategory(Name3, Detail3);
		allCourseCategory.add(courseCategory);
		System.out.println("新增第一筆 "+allCourseCategory.get(0).getName());
		System.out.println("新增第二筆 "+allCourseCategory.get(1).getName());
		System.out.println("新增第三筆 "+allCourseCategory.get(2).getName()+"\r\n");
		Assertions.assertEquals(Name, allCourseCategory.get(0).getName());
		Assertions.assertEquals(Name2, allCourseCategory.get(1).getName());
		Assertions.assertEquals(Name3, allCourseCategory.get(2).getName());
	}

//	刪除課程類別
	@Test
	public void removeCourseCategoryUUIDNotExistTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		courseCategory = service.addCourseCategory("Java05", "Java is good!!!");
		courseCategory = service.addCourseCategory("java06", "Java is good!!");
		allCourseCategory = service.getAllCourseCategory();
		System.out.println("刪除課程類別 "+allCourseCategory.get(0).getName());
		service.removeCourseCategory(allCourseCategory.get(0).getId());	
		System.out.println("刪除結果 "+allCourseCategory.get(0)+"\r\n");
		
		Assertions.assertEquals(null,allCourseCategory.get(0));
	}
	
//	刪除課程類別(uuid 不存在
	@Test
	public void removeCourseCategoryUUIDExistTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();
		courseCategory = service.addCourseCategory("Java", "Java is good");
		courseCategory = service.addCourseCategory("Java2", "Java is good2");
		allCourseCategory = service.getAllCourseCategory();
		
//		刪除不存在的id
		UUID id = UUID.randomUUID();
		System.out.println("刪除不存在的id "+id);
		service.removeCourseCategory(id);	
		System.out.println();
		
		Assertions.assertEquals("Java",allCourseCategory.get(0).getName());
		Assertions.assertEquals("Java2",allCourseCategory.get(1).getName());
	}


//	修改課程類別
	@Test
	public void modifyCourseCategoryTest() {
		CourseCategoryServiceJDKImplement service = new CourseCategoryServiceJDKImplement();

		courseCategory = service.addCourseCategory("Java", "Java is good");
		System.out.println("修改前課程類別名稱 "+courseCategory.getName());
		service.modifyCourseCategory(courseCategory.getId(), "C#", "C# is good");
		System.out.println("修改後課程類別名稱(Java->C#) "+courseCategory.getName()+"\r\n");
		Assertions.assertEquals("C#", courseCategory.getName());
		
//		修改不存在的id
		UUID id = UUID.randomUUID();
		System.out.println("修改不存在的id "+id);
		service.modifyCourseCategory(id, "C#", "C# is good");
		System.out.println();
	}

}

```
# 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.Order;
import org.junit.jupiter.api.Test;

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

public class CourseServiceTest {
	private Course course = new Course();
	private List<Course> allCourses = new ArrayList<>();

//	服務生成時是否為空
	@Test
	public void CourseServiceNullTest() {
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
//		System.out.println(service.getAllCourseService().size());
//		使用size()判斷是否有生成並且為空
		Assertions.assertEquals(0, service.getAllCourseService().size());
	}

//	取得所有課程
	@Test
	public void getAllCourseServiceTest() {
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		course = service.addCourseService("Java1", 10000);
		Assertions.assertNotNull(service.getAllCourseService());
	}

//	新增一筆課程
	@Test
	public void addOneCourseServiceTest() {
		course = new Course();
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		String Name = "Java2";
		int Price = 20000;
		System.out.println("新增一筆課程 "+Name);
		course = service.addCourseService(Name, Price);
		System.out.println("新增結果 "+course.getName()+"\r\n");
		Assertions.assertEquals(Name, course.getName());
	}
	
//	新增多筆課程
	@Test
	public void addManyCourseServiceTest() {
		course = new Course();
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		String Name = "Java3";
		int Price = 30000;
		String Name2 = "Java4";
		int Price2 = 40000;
		String Name3 = "Java5";
		int Price3 = 50000;
		System.out.println("新增多筆課程");
		course = service.addCourseService(Name, Price);
		course = service.addCourseService(Name2, Price2);
		course = service.addCourseService(Name3, Price3);
		allCourses=service.getAllCourseService();
		System.out.println("新增第一筆 "+allCourses.get(0).getName());
		System.out.println("新增第二筆 "+allCourses.get(1).getName());
		System.out.println("新增第三筆 "+allCourses.get(2).getName()+"\r\n");
		
		Assertions.assertEquals(Name, allCourses.get(0).getName());
		Assertions.assertEquals(Name2, allCourses.get(1).getName());
		Assertions.assertEquals(Name3, allCourses.get(2).getName());
	}

//	刪除課程 uuid存在
	@Test
	public void removeCourseServiceUUIDExistTest() {
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		course = service.addCourseService("Java6",60000);
		course = service.addCourseService("Java7",70000);
		allCourses=service.getAllCourseService();
		System.out.println("刪除課程 "+allCourses.get(0).getName());
		service.removeCourseService(allCourses.get(0).getId());
		System.out.println("刪除結果 "+allCourses.get(0)+"\r\n");
		Assertions.assertEquals(null,allCourses.get(0));
	}
	
//	刪除課程 uuid不存在
	@Test
	public void removeCourseServiceUUIDNotExistTest() {
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		course = service.addCourseService("Java8",80000);
		course = service.addCourseService("Java9",90000);
		allCourses=service.getAllCourseService();
		UUID id = UUID.randomUUID();
		System.out.println("\r\n刪除不存在課程 "+id);
		service.removeCourseService(id);
		Assertions.assertEquals("Java8",allCourses.get(0).getName());
		Assertions.assertEquals("Java9",allCourses.get(1).getName());
	}

//	修改課程 uuid存在
	@Test
	public void modifyCourseServiceTest() {
		CourseServiceJDKImplement service = new CourseServiceJDKImplement();
		course = service.addCourseService("Java10",100000);
		System.out.println("修改前課程名稱 "+course.getName());
		service.modifyCourseService(course.getId(), "C#", 12000);
		System.out.println("修改後課程名稱(Java10->C#) "+ course.getName());
		Assertions.assertEquals("C#", course.getName());
		
//		修改不存在的uuid
		UUID id = UUID.randomUUID();
		System.out.println("\r\n修改不存在的id "+id);
		service.modifyCourseService(id, "C#", 12000);
		
	}
}
```
# AddCourseToCategoryTest
```
package com.cctbcbank.demo.Step3;

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.model.Course;
import com.cctbcbank.demo.Lab1.model.CourseCategory;

public class AddCourseToCategoryTest {
	public static Course course = null;
	public static CourseCategory courseCategory = new CourseCategory();
	private List<Course> courses = new ArrayList<>();


	
//	課程加到類別
	@Test
	public void addCourseTest() {
		Course course = new Course();
		UUID id = UUID.randomUUID();
		course.setId(id);
		course.setName("ABC1");
		course.setPrice(1000);
		
		CourseCategory courseCategory = new CourseCategory();
		courseCategory.addCourse(course);
		
		List<Course> courses = new ArrayList<>();
		courses.add(course);
		
		Assertions.assertEquals("ABC1",courseCategory.getCourses().get(0).getName());
	}
	
//	課程加到另一個類別，把課程從原本類別刪除(沒完成
	@Test
	public void removeCourseTest() {
		Course course = new Course();
		UUID id = UUID.randomUUID();
		course.setId(id);
		course.setName("ABC1");
		course.setPrice(1000);
		
		CourseCategory courseCategory = new CourseCategory();
		courseCategory.addCourse(course);
		CourseCategory courseCategory2 = new CourseCategory();
		courseCategory2.addCourse(course);
	
		List<Course> courses = new ArrayList<>();
		courses.add(course);
		
		courseCategory.removeCourse(id);
		
		
		Assertions.assertEquals(0,courseCategory.getCourses().get(0).getName());
	}
}

```
# CourseServiceFactoryTest
```
package com.cctbcbank.demo.Step4;

import com.cctbcbank.demo.Lab1.Service.CourseService;

public class CourseServiceFactoryTest {
	CourseServiceFactoryTest factory = new CourseServiceFactoryTest();

	public synchronized static CourseService getServiceTest() {

		return null;
	}
}

```