1. 程式人生 > >菜雞的Java筆記 雙向一對多對映

菜雞的Java筆記 雙向一對多對映

    雙向一對多對映
    two-way

    開發要求:
        根據資料表的結構進行簡單java類的轉換:
        要求實現如下的輸出資訊:
            可以根據課程取得全部參與此課程使用者的資訊
                輸出課程資訊:
                輸出參與此課程使用者的資訊以及考試成績
            使用者可以取得自己所參加的課程資訊
                輸出某一個使用者的資訊
                輸出該使用者所參加的所有課程資訊以及對應的考試成績
        關係上來講:一個使用者可以參加多門課程,一門課程可以有多個使用者參加,每個使用者在每個課程內都會有一個成績
        此時最麻煩的問題在於使用者-課程關係表中除了關聯欄位之外,還包含有其他欄位,這樣的表一定要作為一個實體類出現
        所以現在需要定義有三個類
        第一步:先完成基本欄位

class User{
    private String userid:
    private String name:
    public User(String userid,String name){
        this.userid = userid:
        this.name = name:
    }
    public String getlnfo(){
        return     "使用者編號:"+this.userid
                +",姓名"+this.name:
    }
}
class Course{
    
private int cid: private String title: private int num: private String note: public Course(int cid,String title,int num,String note){ this.cid = cid: this.title = title: this.num = num: this.note = note: } public String getlnfo(){ return
"課程編號:"+this.cid +",名稱:"+this.title +",課時:"+this.num +",簡介:"+this.note: } } public class TwoWay{ public static void main(String args[]){ } }

       
        第二步:進行欄位關聯的時候都是以外來鍵為主
            為了可以進行關聯,需要引入一個新的類:要儲存使用者,課程等資訊的聯絡

class User{
    private String userid;
    private String name;
    public User(String userid,String name){
        this.userid = userid;
        this.name = name;
    }
    public String getlnfo(){
        return     "使用者編號:"+this.userid
                +",姓名"+this.name;
    }
}
class Course{
    private int cid;
    private String title;
    private int num;
    private String note;
    public Course(int cid,String title,int num,String note){
        this.cid = cid;
        this.title = title;
        this.num = num;
        this.note = note;
    }
    public String getlnfo(){
        return     "課程編號:"+this.cid
                +",名稱:"+this.title
                +",課時:"+this.num
                +",簡介:"+this.note;
    }
    public Course getCourse(){
        return this.course;
    }
    public User getUser(){
        return this.user;
    }
}
class UserCourse{
    private User user;
    private Course course;
    private String note;
    private double score;
    public UserCourse(User user,Course course,String note,double score){
        this.user = user;
        this.course = course;
        this.note = note;
        this.score = score;
    }
}
public class TwoWay{
    public static void main(String args[]){
        
    }
}


        第三步:程式測試

class User{
    private String userid;
    private String name;
    private UserCourse ucs[];
    public User(String userid,String name){
        this.userid = userid;
        this.name = name;
    }
    public void setUcs(UserCourse ucs[]){
        this.ucs = ucs;
    }
    public UserCourse[] getUcs(){
        return this.ucs;
    }
    public String getlnfo(){
        return     "使用者編號:"+this.userid
                +",姓名"+this.name;
    }
}
class Course{
    private int cid;
    private String title;
    private int num;
    private String note;
    private UserCourse ucs[];
    public Course(int cid,String title,int num,String note){
        this.cid = cid;
        this.title = title;
        this.num = num;
        this.note = note;
    }
    public void setUcs(UserCourse ucs[]){
        this.ucs = ucs;
    }
    public UserCourse[] getUcs(){
        return this.ucs;
    }
    public String getlnfo(){
        return     "課程編號:"+this.cid
                +",名稱:"+this.title
                +",課時:"+this.num
                +",簡介:"+this.note;
    }
    
}
class UserCourse{
    private User user;
    private Course course;
    private String note;
    private double score;
    public UserCourse(User user,Course course,String note,double score){
        this.user = user;
        this.course = course;
        this.note = note;
        this.score = score;
    }
    public double getScore(){
        return this.score;
    } 
    public Course getCourse(){
        return this.course;
    }
    public User getUser(){
        return this.user;
    }
}
public class TwoWay{
    public static void main(String args[]){
        //第一步:設定類與類之間的關係 
        //1.定義單獨的類物件
        User ua = new User("zhangsan","張三");
        User ub = new User("lisi","李四");
        User uc = new User("wangwu","王五");
        Course c1 = new Course(1,"Oracle",50,"-");
        Course c2 = new Course(2,"java",300,"-");
        //2.設定彼此的關係
        UserCourse uca = new UserCourse(ua,c1,"暫無評價",90.0);
        UserCourse ucb = new UserCourse(ua,c2,"暫無評價",91.0);
        UserCourse ucc = new UserCourse(ub,c1,"暫無評價",92.0);
        UserCourse ucd = new UserCourse(uc,c1,"暫無評價",93.0);
        UserCourse uce = new UserCourse(uc,c2,"暫無評價",94.0);
        //
        ua.setUcs(new UserCourse[]{uca,ucb});
        ub.setUcs(new UserCourse[]{ucc});
        uc.setUcs(new UserCourse[]{ucd,uce});
        c1.setUcs(new UserCourse[]{uca,ucc,ucd});
        c2.setUcs(new UserCourse[]{ucb,uce});
        // 第二步:取得資料
        System.out.println(c1.getlnfo()); // 輸出一個課程資訊
        for(int x = 0;x<c1.getUcs().length;x++){ // 該門課程的使用者資訊
            System.out.println("\t|-【參與使用者】 "+c1.getUcs()[x].getUser().getlnfo()+",考試成績"+c1.getUcs()[x].getScore());
        }
        System.out.println("*******************************************");
        System.out.println(ua.getlnfo());
        for(int x = 0;x<ua.getUcs().length;x++){// 都是UserCourse物件
            System.out.println("\t|-【參與使用者】 "+ua.getUcs()[x].getCourse().getlnfo()+",考試成績"+ua.getUcs()[x].getScore());
        }
    }
}

           
            本程式與之前的程式碼相比,唯一麻煩的地方在於中間的關係表上的其他欄位
            
            程式碼鏈是本次講解的重點所在
            
            不暈的方法(笨方法容易理解的方法)

    System.out.println(ua.getlnfo());
     UserCourse uct[] = ua.getUcs();
        for(int x = 0;x<uct.length;x++){// 都是UserCourse物件
        Course c = uct[x].getCourse();
            System.out.println("\t|-【參與使用者】 "+c.getlnfo()+",考試成績"+uct[x].getScore());
        }