数据表与简单java类(多对多)

定义一个学生选课的操作表:三张表
学生表:编号,姓名,年龄
课程表:课程编号,课程名称,学分
学生-课程关联表:学生编号,课程编号,成绩。


要求可以输出如下信息:
(1)可以找到一门课程参加此课程的所有信息以及成绩
(2)根据一个学生,找到所参加的所有课程和每门课程的成绩




第一步:定义出基本类,暂不考虑所有的关系。
public class Student {
private int stuid;
private String  name;
private int age;
//构造函数
public Student()
{}
public Student(int stuid,String name,int age)
{
this.stuid=stuid;
this.name=name;
this.age=age;

}
public String getInfo()
{
String result="学生编号为"+this.stuid+",姓名为"+this.name+",年龄是"+this.age;
return result;
}


}










public class Course {
 
private int cid;
private String name;
private int credit;
public Course()
{}
public Course(int cid,String name,int credit)
{
this.cid=cid;
this.name=name;
this.credit=credit;
}
public String getInfo()
{
String result="课程编号为"+this.cid+",名称为"+this.name+",学分是"+this.credit;
return result;
}


}






第二步:一个学生有多门课,一门课有多个学生,所以应该保存有各自对象的数组,
即在Student中添加private Course[] courses;
在Course中添加private Student[] students;


第三步:学生和每门课程之间都有一个成绩。现在发现关联表中不仅有关联字段,而且有普通字段成绩,那么应该再建立一个类StudentCourse,其中在studentcourse中有了对于course的定义,所以可删除。(因为这是在student表中进行选课的操作,所以删除student中 的course,而且在student中加入StudentCourse有student来进行操作)


第四步:进行要实现的操作.
最终的代码如下:
public class Test {


public static void main(String[] args) {
/**
* 第一步:根据结构进行关系的设置
*/
         //1创建各自的对象
Student stu1=new Student(1,"张飒",12);
Student stu2=new Student(2,"奥斯卡大姐夫",122);
Student stu3=new Student(3,"阿斯蒂芬",123);
Course ca=new Course(1001,"物理",3);
Course cb=new Course(1002,"英语",4);
//2需要设置学生和课程的关系,这里面需要出成绩。
stu1.setStudentCourses(new StudentCourse[] {
new StudentCourse(stu1,ca,78),
new StudentCourse(stu1,cb,90)
});

stu2.setStudentCourses(new StudentCourse[] {
new StudentCourse(stu2,ca,38),
//new StudentCourse(stu1,cb,90)
});

stu3.setStudentCourses(new StudentCourse[] {
new StudentCourse(stu3,cb,90),
new StudentCourse(stu3,ca,90)
});
//3设置课程和学生的关系
ca.setStudentCourses(new StudentCourse[] {
new StudentCourse(stu1,ca,78),
new StudentCourse(stu3,ca,90)

});

cb.setStudentCourses(new StudentCourse[] {
new StudentCourse(stu1,cb,90),
new StudentCourse(stu3,cb,90)
});

//根据结构取出数据
//1 可以找到一门课程,以及参加此课程的所有学生信息,和他的成绩
        System.out.println(ca.getInfo());
        for(int x=0;x<ca.getStudentCourses().length;x++)
        {//下面之所以有ca.getStudentCourses()是因为在上述代码中通过setStudentCourse来添加了一些值
        System.out.println(ca.getStudentCourses()[x].getStudent().getInfo()+",成绩"+ca.getStudentCourses()[x].getScore());
        //System.out.println();
        System.out.println();
        }
        
        //2 根据学生找到与其相关的课程信息
        System.out.println(stu1.getInfo());
        for(int x=0;x<stu1.getStudentCourses().length;x++)
        {
        System.out.println(stu1.getStudentCourses()[x].getCourse().getInfo()+",成绩是"+stu1.getStudentCourses()[x].getScore());
       
        }
        
        
        
}


}








public class Course {
 
private int cid;
private String name;
private int credit;
private Student[] students;
private StudentCourse studentCourses[];//因为学生对学生选课表进行操作,所以在这里定义一个。
public StudentCourse[] getStudentCourses() {
return studentCourses;
}
public void setStudentCourses(StudentCourse[] studentCourses) {
this.studentCourses = studentCourses;
}


public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public Course()
{}
public Course(int cid,String name,int credit)
{
this.cid=cid;
this.name=name;
this.credit=credit;
}
public String getInfo()
{
String result="课程编号为"+this.cid+",名称为"+this.name+",学分是"+this.credit;
return result;
}


}








public class Student {
private int stuid;
private String  name;
private int age;
//private Course[] courses;虽然这是多对多的,但是这是学生在studentcourse中进行的操作,studentcourse中有了对course的操作,所以可删除
private StudentCourse studentCourses[];//因为学生对学生选课表进行操作,所以在这里定义一个。这种表示定义一个studentCourses对象的集合


public StudentCourse[] getStudentCourses() {
return studentCourses;
}
public void setStudentCourses(StudentCourse[] studentCourses) {
this.studentCourses = studentCourses;
}
/**
* public Course[] getCourses() {
return courses;
}
public void setCourses(Course[] courses) {
this.courses = courses;
}
* @return
*/

//构造函数
public Student()
{}
public Student(int stuid,String name,int age)
{
this.stuid=stuid;
this.name=name;
this.age=age;

}
public String getInfo()
{
String result="学生编号为"+this.stuid+",姓名为"+this.name+",年龄是"+this.age;
return result;
}


}












public class StudentCourse {//学生选课
 private Student student;
private Course course;
 private double score;
 
 
 public StudentCourse()
 {}
 public StudentCourse(Student student,Course course,double score)
 {
this.student=student;
this.course=course;
this.score=score;
 
 }
 
    public Student getStudent() {
return this.student;
}
public Course getCourse() {
return this.course;
}
public double getScore() {
return this.score;
}

}

结果是:

课程编号为1001,名称为物理,学分是3
学生编号为1,姓名为张飒,年龄是12,成绩78.0


学生编号为3,姓名为阿斯蒂芬,年龄是123,成绩90.0


学生编号为1,姓名为张飒,年龄是12
课程编号为1001,名称为物理,学分是3,成绩是78.0
课程编号为1002,名称为英语,学分是4,成绩是90.0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值