java实验五optional操作实验

一、题目描述

在com.experiment05.entity下,创建Student/Teacher/College类,直接从github复制使用实验代码

在com.experiment05.resource下,创建DatabaseUtils类,直接从github复制使用实验代码

基于Optional,按需求实现测试类Test中的方法,模拟正确/错误数据信息验证代码的有效性
https://github.com/bwhyman/java-course/tree/master/java-experiments

二、代码实现

college:
 package com.experiment05.entity;

public class College {
    private String name;

    public College(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Student:
package com.experiment05.entity;

public class Student {
    private int number;
    private String name;
    private int year;
    private Teacher teacher;

    public Student(int number, String name, int year) {
        this.number = number;
        this.name = name;
        this.year = year;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    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;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

}


Teacher:
package com.experiment05.entity;

public class Teacher {
    private int number;
    private String name;
    private College college;

    public Teacher(int number, String name) {
        this.number = number;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public College getCollege() {
        return college;
    }

    public void setCollege(College college) {
        this.college = college;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}


DatabaseUtils:
package com.experiment05.resource;


import com.experiment05.entity.College;
import com.experiment05.entity.Student;
import com.experiment05.entity.Teacher;

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

public class DatabaseUtils {
    private static final List<Student> STUDENTS = create();

    private static List<Student> create() {
        College c1 = new College("信息学院");
        College c2 = new College("经济管理学院");

        Teacher t1 = new Teacher(1001, "吕惠玲");
        t1.setCollege(c1);
        Teacher t2 = new Teacher(1002, "曾志优");
        t2.setCollege(c2);

        Student s1 = new Student(201001,"赵阳阳", 2010);
        s1.setTeacher(t1);
        Student s2 = new Student(201002,"邵鹏", 2010);
        s2.setTeacher(t1);
        Student s3 = new Student(201103,"高学斌", 2011);
        s3.setTeacher(t2);
        Student s4 = new Student(201104,"张扬", 2011);
        s4.setTeacher(t2);

        List<Student> students = new ArrayList<>();
        students.add(s1); students.add(s2);
        students.add(s3); students.add(s4);
        return students;
    }

    public static List<Student> getStudents() {
        return STUDENTS;
    }
}


package com.experiment05;

import com.experiment05.entity.College;
import com.experiment05.entity.Student;
import com.experiment05.entity.Teacher;
import com.experiment05.resource.DatabaseUtils;

import java.util.List;
import java.util.Optional;

public class Test {
    public static void main(String[] args) {
        System.out.println(getCollegeName(DatabaseUtils.getStudents(), 888888));
        printCollegeName(DatabaseUtils.getStudents(), 201001, 8888888);
    }

    /**
     * 基于给定学生集合,学生编号,实现获取学生的导师的所在学院名称。
     * 任何一项不存在或失败,返回 未知学院
     * 结合collection stream findFirst()方法实现
     * 可直接通过一条方法链实现
     *
     * @param students
     * @param sNumber
     * @return
     */

    private static String getCollegeName(List<Student> students, int sNumber) {
        return students.stream()
                .filter(s -> s.getNumber() == sNumber)
                .findFirst()
                .map(Student::getTeacher)
                .map(Teacher::getCollege)
                .map(College::getName)
                .orElse("未知学院");
    }

    /**
     * 实现在给定学生集合中查询指定编号学生,如果学生导师编号为指定编号,打印显式导师所在学院名称。
     * 任何一项不存在或失败,打印显式 未知学院
     * 结合collection stream findFirst()方法实现
     * 可直接通过一条方法链实现
     *
     * @param students
     * @param sNumber,学生编号
     * @param tNumber,教师编号
     */
    private static void printCollegeName(List<Student> students, int sNumber, int tNumber) {
        System.out.println(students.stream()
                .filter(s -> s.getNumber() == sNumber)
                .findFirst()
                .map(Student::getTeacher)
                .filter(t -> t.getNumber() == tNumber)
                .map(Teacher::getCollege)
                .map(College::getName)
                .orElse("未知学院"));
    }

}
上一篇:java中orElse()和orElseGet()的区别


下一篇:Optional类的介绍和使用举例