码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习-第二个代码模型

时间:2015-07-19 23:26:24      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

第二个模型: 数据表与简单java类映射

题目要求,要求使用emp表(empnoenamejobsalcommmgrdeptno)和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学习-第二个代码模型

标签:

原文地址:http://www.cnblogs.com/kvikon/p/4659926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!