需求:学生、老师可以选择课程,学生选课分数须达到3分以上
import java.util.ArrayList;
import java.util.List;
/**
* @author wujun
* @create 2020-07-02 17:06
* 学生类:学生必须选满课程学分3分才能达到要求
*/
public class Student implements IMember {
private String stu_num; //学号
private String name; //姓名
private int age; //年龄
private String address; //地址
private String sex; //性别
private double grade = 0; //拥有学分
private List<Course> courses; //所选课程
public Student(String stu_num, String name, int age, String address, String sex) {
this.stu_num = stu_num;
this.name = name;
this.age = age;
this.address = address;
this.sex = sex;
courses = new ArrayList<>();
}
public String getStu_num() {
return stu_num;
}
public void setStu_num(String stu_num) {
this.stu_num = stu_num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
@Override
public String toString() {
return "Student{" +
"stu_num='" + stu_num + '\'' +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", sex='" + sex + '\'' +
", courses=" + courses +
'}';
}
//添加课程
public void addCourse(Course course){
if (course !=null){
this.courses.add(course);
System.out.println(this.name+"新增课程:"+course.getName());
//将课程设置为已被选中
course.setSelect(true);
this.grade = this.grade+course.grade;
System.out.println("学生"+this.name+"学分为:"+this.grade);
if (this.grade<3){
System.out.println("学生"+this.name+"选课的学分未达到标准!!");
}else {
System.out.println("学生"+this.name+"选课学分已够");
}
}
}
//移除课程
public void removeCourse(String name){
if (name != null){
try {
for (Course cours : courses) {
if (cours.getName().equals(name)){
courses.remove(cours);
this.grade=this.grade-cours.grade;
System.out.println("学生"+this.name+"移除了"+cours.getName());
System.out.println("学生"+this.name+"学分为:"+this.grade);
if (this.grade<3){
System.out.println("学生"+this.name+"选课的学分未达到标准!!");
}else {
System.out.println("学生"+this.name+"选课学分已够");
}
}
}
}catch (Exception e){
System.out.print("");
}
}
}
}
/**
* @author wujun
* @create 2020-07-02 17:53
*
*/
public class Course {
public static final String GAOSHU="高数";
public static final String Computer="计算机基础";
public static final String DataBase="数据库原理";
public static final String Engilsh="英语";
public static final String JAVA="java语言";
public static final String Internet="网络原理";
private String cour_num; //课程号
private String name; //课程名
private int time;//课时
protected double grade; //学分
private boolean isSelect; //是否被选
public Course(String number,String name){
this.isSelect=false; //默认课程没有选中
this.name = name;
this.cour_num = number;
if (name.equals("高数")){
this.grade=1.0;
this.time=48;
}if (name.equals("计算机基础")){
this.grade=0.5;
this.time=16;
}if (name.equals("数据库原理")){
this.grade=1.5;
this.time=18;
}if (name.equals("英语")){
this.grade=1.0;
this.time=20;
}if (name.equals("java语言")){
this.grade=1.0;
this.time=52;
}if (name.equals("网络原理")){
this.grade=1.0;
this.time=32;
}
}
public String getCour_num() {
return cour_num;
}
public void setCour_num(String cour_num) {
this.cour_num = cour_num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Course{" +
"cour_num='" + cour_num + '\'' +
", name='" + name + '\'' +
", time=" + time +
", grade=" + grade +
", isSelect=" + isSelect +
'}';
}
}
import com.wujun.dao.IMember;
import java.util.List;
/**
* @author wujun
* @create 2020-07-02 19:08
*/
public class Teacher implements IMember {
private String tea_num; //教师号
private String name; //姓名
private int age; //年龄
private String duties; //职务
private List<Course> courses; //所教课程
public Teacher(String tea_num, String name, int age, String duties) {
this.tea_num = tea_num;
this.name = name;
this.age = age;
this.duties = duties;
}
public String getTea_num() {
return tea_num;
}
public void setTea_num(String tea_num) {
this.tea_num = tea_num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDuties() {
return duties;
}
public void setDuties(String duties) {
this.duties = duties;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
@Override
public String toString() {
return "Teacher{" +
"tea_num='" + tea_num + '\'' +
", name='" + name + '\'' +
", age=" + age +
", duties='" + duties + '\'' +
", courses=" + courses +
'}';
}
//添加课程
public void addCourse(Course course){
if (course !=null){
this.courses.add(course);
System.out.println("老师"+this.name+"新增课程:"+course.getName());
}
}
//移除课程
public void removeCourse(String name){
if (name != null){
for (Course cours : courses) {
if (cours.getName().equals(name)){
courses.remove(cours);
System.out.println("老师"+this.name+"移除了"+cours.getName());
}
}
}
}
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* @author wujun
* @create 2020-07-02 20:38
* 课程功能类
*/
public class CourseDao {
private static Map<Integer, Course> courses = null;
static {
courses = new HashMap<Integer,Course>();
courses.put(1001,new Course("1001",Course.Computer));
courses.put(1002,new Course("1002",Course.DataBase));
courses.put(1003,new Course("1003",Course.Engilsh));
courses.put(1004,new Course("1004",Course.GAOSHU));
courses.put(1005,new Course("1005",Course.Internet));
courses.put(1006,new Course("1006",Course.JAVA));
}
//列出全部课程
public Collection<Course> getCourses(){
return courses.values();
}
//查找课程
public Course getCourse(Integer id){
return courses.get(id);
}
// public static void main(String[] args) {
// System.out.println(courses.values());
// System.out.println(courses.get(1001));
// }
}
/**
* @author wujun
* @create 2020-07-03 10:37
* 老师和学生公共接口
*/
public interface IMember {
public void addCourse(Course course);
public void removeCourse(String name);
}
测试:
import com.wujun.dao.CourseDao;
import com.wujun.ibz.Course;
import com.wujun.ibz.Student;
/**
* @author wujun
* @create 2020-07-03 10:39
*/
public class Test {
public static void main(String[] args) {
CourseDao courseDao = new CourseDao();
System.out.println("查询全部课程信息");
System.out.println(courseDao.getCourses());
System.out.println("根据课程号查询课程信息");
System.out.println(courseDao.getCourse(1002));
Course course1 = new Course("1001", "计算机基础");
Course course2 = new Course("1002", "数据库原理");
Course course3 = new Course("1003","英语");
Student student1 = new Student("0434", "张三", 18, "四川遂宁", "男");
student1.addCourse(course1);
student1.addCourse(course2);
student1.addCourse(course3);
student1.removeCourse("计算机基础");
//Teacher teacher1 = new Teacher("2131","小丽",35,"教授");
}
}