Java Learning-a second code model

Source: Internet
Author: User

Second model: Data tables and simple Java class mappings

Required to use the EMP table (empno,ename,job,sal, Comm,Mgr,deptno) and dept (Deptno, dname, loc) tables are required to perform the following functions:

· Function One: The complete information of the department can be exported, and the information of all employees and employees in the department will be exported.

· Function Two: can be based on an employee to find the employee's leadership information and his department information.

There are two related relationships in this program:

· Relationship one: The relation between the employee and the department depends on the Deptno field;

· Relationship two: The link between employees and leaders, relying on the Mgr field.

Step One: Write a simple Java class that temporarily contains only the basic fields in the table.

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 "Employee Number:" + This.empno + ", Name:" + This.ename + ", Position:" + this.job + ", Salary:" + this.sal + ", Commission:" + 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 "department Number:" + This.deptno + ", Name:" + this.dname + ", Location:" + this.loc;

}

}

Step Two: Set the Relationship field

· One-to-many relationships: departments and employees;

· One-to-one relationships: employees and leaders.

Class Emp {

private int empno;

Private String ename;

Private String job;

Private double Sal;

Private double comm;

Private Dept Dept; An employee belongs to a department

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 "Employee Number:" + This.empno + ", Name:" + This.ename + ", Position:" + this.job + ", Salary:" + this.sal + ", Commission:" + This.comm;

}

}

Class Dept {

private int deptno;

Private String dname;

Private String Loc;

Private EMP Emps []; Multiple employees

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 "department Number:" + This.deptno + ", Name:" + this.dname + ", Location:" + this.loc;

}

}

Step Three: set the data according to the table structure, and set the data after the table structure to remove

public class Testdemo {

public static void Main (String args[]) {

First level: Setting up relationships

1. Set each object entity separately, no connection between each other

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 ("ACCOUNTING", "New York");

2. Setting up employees and leadership relationships

Ea.setmgr (EB);

Eb.setmgr (EC); EC does not lead

3. Set up employee and departmental relationships

Ea.setdept (dept); Employees and Departments

Eb.setdept (dept); Employees and Departments

Ec.setdept (dept); Employees and Departments

EMP [] temp = new emp[] {EA,EB,EC};

Dept.setemps (temp);

Second level: Take data based on relationship

1. Remove Department information

System.out.println (Dept.getinfo ());

2. Remove all employee information from the department

EMP [] allemps = Dept.getemps (); Receive first

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

Emp e = allemps[x]; Take out an employee

System.out.println ("\ T" + e.getinfo ());

if (e.getmgr () = null) {//has a leader

System.out.println ("\t\t|-" + e.getmgr (). GetInfo ());

}

}

3, according to employees to take information

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 ());

}

}

}

After today's study, you have to have this simple Java class and data table conversion capabilities.

Java Learning-a second code model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.