JAVA學習-第二個代碼模型

來源:互聯網
上載者:User

標籤:

第二個模型: 資料表與簡單java類映射

題目要求,要求使用emp表(empno、ename、job、sal、comm、mgr、deptno)和dept(deptno、dname、loc)表進行操作,要求可以實現如下的功能:

                   · 功能一:可以輸出部門的完整資訊,同時輸出部門之中所有僱員以及僱員直接領導的資訊;

                   · 功能二:可以根據一個僱員找到僱員的領導資訊和他所在的部門資訊。

         在本程式之中有兩個關聯的對應關係:

                   · 關係一:僱員和部門之間依靠的deptno欄位的聯絡;

                   · 關係二:僱員和領導之間的聯絡,依靠mgr欄位。

步驟一:編寫簡單Java類,暫時只包含表之中的基本欄位。

class Emp {

         private int empno ;

         private String ename ;

         private String job ;

         private double sal ;

         private double comm ;

         public Emp() {}

         public Emp(int empno,String ename,String job,double sal,double comm) {

                   this.empno = empno ;

                   this.ename = ename ;

                   this.job = job ;

                   this.sal = sal ;

                   this.comm = comm ;

         }

         public String getInfo() {

                   return "僱員編號:" + this.empno + ",姓名:" + this.ename + ",職位:" + this.job + ",工資:" + this.sal + ",傭金:" + this.comm ;

         }

}

class Dept {

         private int deptno ;

         private String dname ;

         private String loc ;

         public Dept() {}

         public Dept(int deptno,String dname,String loc) {

                   this.deptno = deptno ;

                   this.dname = dname ;

                   this.loc = loc ;

         }

         public String getInfo() {

                   return "部門編號:" + this.deptno + ",名稱:" + this.dname + ",位置:" + this.loc ;

         }

}

步驟二:設定關係欄位

         · 一對多關聯性:部門和僱員;

         · 一對一關聯性:僱員和領導。

class Emp {

         private int empno ;

         private String ename ;

         private String job ;

         private double sal ;

         private double comm ;

         private Dept dept ;        // 一個僱員屬於一個部門

         private Emp mgr ;

         public Emp() {}

         public Emp(int empno,String ename,String job,double sal,double comm) {

                   this.empno = empno ;

                   this.ename = ename ;

                  this.job = job ;

                   this.sal = sal ;

                   this.comm = comm ;

         }

         public void setMgr(Emp mgr) {

                   this.mgr = mgr ;

         }

         public Emp getMgr() {

                   return this.mgr ;

         }

         public void setDept(Dept dept) {

                   this.dept = dept ;

         }

         public Dept getDept() {

                   return this.dept ;

         }

         public String getInfo() {

                   return "僱員編號:" + this.empno + ",姓名:" + this.ename + ",職位:" + this.job + ",工資:" + this.sal + ",傭金:" + this.comm ;

         }

}

class Dept {

         private int deptno ;

         private String dname ;

         private String loc ;

         private Emp emps [] ;   // 多個僱員

         public Dept() {}

         public Dept(int deptno,String dname,String loc) {

                   this.deptno = deptno ;

                   this.dname = dname ;

                   this.loc = loc ;

         }

         public void setEmps(Emp emps[]) {

                   this.emps = emps ;

         }

         public Emp [] getEmps() {

                   return this.emps ;

         }

         public String getInfo() {

                   return "部門編號:" + this.deptno + ",名稱:" + this.dname + ",位置:" + this.loc ;

         }

}

步驟三:按照表結構設定資料,同時設定完資料後按照表結構取出

public class TestDemo {

         public static void main(String args[]) {

                   // 第一層次:設定關係

                   // 1、分別設定個個對象實體,彼此之間沒有聯絡

                   Emp ea = new Emp(7369,"SMITH","CLERK",800.0,0.0) ;

                   Emp eb = new Emp(7902,"FORD","MANAGER",2450.0,0.0) ;

                   Emp ec = new Emp(7839,"KING","PRESIDENT",5000.0,0.0) ;

                   Dept dept = new Dept(10,"ACCOUNTING","New York") ;

                   // 2、設定僱員和領導關係

                   ea.setMgr(eb) ;

                   eb.setMgr(ec) ;    // ec沒有領導

                   // 3、設定僱員和部門關係

                   ea.setDept(dept) ;          // 僱員和部門

                   eb.setDept(dept) ;         // 僱員和部門

                   ec.setDept(dept) ;          // 僱員和部門

                   Emp [] temp = new Emp[] {ea,eb,ec} ;

                   dept.setEmps(temp) ;

                   // 第二層次:根據關係取資料

                   // 1、取出部門資訊

                   System.out.println(dept.getInfo()) ;

                   // 2、取出部門之中的所有僱員資訊

                   Emp [] allEmps = dept.getEmps() ; // 先接收

                   for (int x = 0 ; x < allEmps.length ; x ++) {

                            Emp e = allEmps[x] ;    // 取出一個僱員

                            System.out.println("\t" + e.getInfo()) ;

                            if (e.getMgr() != null) {         // 有領導

                                     System.out.println("\t\t|- " + e.getMgr().getInfo()) ;

                            }

                   }

                   // 3、根據僱員取資訊

                   System.out.println(ea.getInfo()) ;

                   if (ea.getMgr() != null) {

                            System.out.println(ea.getMgr().getInfo()) ;

                   }

                   if (ea.getDept() != null) {

                            System.out.println(ea.getDept().getInfo()) ;

                   }

         }

}

         在今天學習完之後,必須具備這種簡單java類與資料錶轉換的能力。

JAVA學習-第二個代碼模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.