preface : The code is written separately , When you use it, just write it in a bag

Topic content :

stay com.experiment04.entity lower , establish Student class , The code is as follows ;

stay com.experiment04.resource lower , establish DatabaseUtils class , The code is as follows ;

stay com.experiment04.service lower , establish StudentService Interface , The code is as follows ;

stay com.experiment04.service.impl lower , establish StudentService Implementation class of interface StudentServiceImpl,
Set based stream Other operations , Note the requirements and implement according to the interface method

stay com.experiment04 lower , establish Test class ,

Create static test methods for each interface method , stay main() Main function call

Interface oriented programming , test StudentService Implementation of methods in interface

com/experiment04/entity/Student.java
package com.experiment04.entity; public class Student { public enum Sex {
MALE, FEMALE } private int id; private Sex sex; private String name; // for example 2017 level
private int year; public Student(int id, Sex sex, String name, int year) {
this.id = id; this.sex = sex; this.name = name; this.year = year; } public int
getId() { return id; } public void setId(int id) { this.id = id; } public Sex
getSex() { return sex; } public void setSex(Sex sex) { this.sex = sex; } public
String getName() { return name; } public void setName(String name) { this.name
= name; } public int getYear() { return year; } public void setYear(int year) {
this.year = year; } }
 com/experiment04/resource/DatabaseUtils.java
package com.experiment04.resource; import com.experiment04.entity.Student;
import java.util.ArrayList; import java.util.List; public class DatabaseUtils {
private static final List<Student> STUDENTS = create(); private static
List<Student> create() { List<Student> students = new ArrayList<>();
students.add(new Student(201001, Student.Sex.FEMALE, " Zhao Yangyang ", 2010));
students.add(new Student(201002, Student.Sex.MALE, " Shao Peng ", 2010));
students.add(new Student(201103, Student.Sex.MALE, " Gao Xuebin ", 2011));
students.add(new Student(201104, Student.Sex.MALE, " Publicize ", 2011));
students.add(new Student(201205, Student.Sex.FEMALE, " LV Huiling ", 2012));
students.add(new Student(201206, Student.Sex.MALE, " Zeng Zhiyou ", 2012)); return
students; } public static List<Student> getStudents() { return STUDENTS; } }
 com/experiment04/service/impl/StudentServiceImpl.java
package com.experiment04.service.impl; import com.experiment04.entity.Student;
import com.experiment04.resource.DatabaseUtils; import
com.experiment04.service.StudentService; import java.util.Collections; import
java.util.List; import java.util.Map; import java.util.stream.Collectors;
public class StudentServiceImpl implements StudentService { public
List<Student> addStudent(Student student) { List<Student>
students=DatabaseUtils.getStudents(); students.add(student); return students; }
public List<Student> listStudentsByYear(int year) { List<Student>
students=DatabaseUtils.getStudents(); return students.stream() .filter(student
-> student.getYear()==year) .collect(Collectors.toList()); } public
List<String> listStudentsNames(int year, Student.Sex sex) { List<Student>
students=DatabaseUtils.getStudents(); return students.stream() .filter(student
-> student.getYear()==year&&student.getSex()==sex) .map(student ->
student.getName()) .collect(Collectors.toList()); } public Map<Student.Sex,
List<Student>> mapStudentsBySex() { List<Student>
students=DatabaseUtils.getStudents(); return students.stream()
.collect(Collectors.groupingBy(student -> student.getSex())); } public boolean
removeStudent(int id) { List<Student> students=DatabaseUtils.getStudents();
return students.removeIf(student -> student.getId()==id); } }
 com/experiment04/service/StudentService.java
package com.experiment04.service; import com.experiment04.entity.Student;
import java.util.List; import java.util.Map; public interface StudentService {
/** * Add a student to the collection , Return all current students * @param student * @return */ List<Student>
addStudent(Student student); /** * Returns all students of the specified session * @param year * @return */
List<Student> listStudentsByYear(int year); /** * Return to the specified session , Names of all students of the specified gender * @param
year * @param sex * @return */ List<String> listStudentsNames(int year,
Student.Sex sex); /** * All students , Grouped by gender * @return */ Map<Student.Sex,
List<Student>> mapStudentsBySex(); /** * Delete the student with the specified student number , Returns whether the deletion was successful * @param id *
@return */ boolean removeStudent(int id); }
 com/experiment04/Test.java
package com.experiment04; import com.experiment04.entity.Student; import
com.experiment04.service.StudentService; import
com.experiment04.service.impl.StudentServiceImpl; public class Test { private
static StudentService stuSer=new StudentServiceImpl(); public static void
addStuTest() { Student s=new Student(1001, Student.Sex.FEMALE,"zhangsan",2016);
System.out.println("-------addStuTest---------"); stuSer.addStudent(s)
.forEach(a->{ System.out.println(a.getId()+" "+a.getName()+" "+a.getSex()+"
"+a.getYear()); }); } public static void listStudentsByYear(){ int year=2016;
System.out.println("-------listStudentsByYear---------");
stuSer.listStudentsByYear(year) .forEach(a->{ System.out.println(a.getId()+"
"+a.getName()+" "+a.getSex()+" "+a.getYear()); }); } public static void
listStudentsNames(){ int year=2012; Student.Sex sex= Student.Sex.FEMALE;
System.out.println("-------listStudentsNames---------");
System.out.println(stuSer.listStudentsNames(year,sex)); } public static void
removeStudent(){ int id=1001;
System.out.println("-------removeStudent---------");
System.out.println(stuSer.removeStudent(id)); } public static void
main(String[] args) { addStuTest(); listStudentsByYear(); listStudentsNames();
removeStudent(); } }

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test