通过javabean实现对如下一张表格中的数据进行存储
工号 | 姓名 | 部门 | 薪水 | 入职时间 |
001 | 张三 | 研发 | 3000 | 2016-9 |
002 | 李四 | 财务 | 3000 | 2016-9 |
003 | 王五 | 采购 | 3000 | 2016-9 |
首先实现一个员工类,类中实现员工属性,并加入setter和getter方法,添加构造器
实现一个Employee类如下:
/**
*
*/
package com.javabean;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*@author 作者:sunlei
*@version 创建时间:2018年3月2日下午3:59:05
*说明
*/
public class Employee {
private int id;
private String name;
private String department;
private int salary;
private Date entryDate;
/**
* @param id
* @param name
* @param department
* @param salary
* @param entryDate
*/
public Employee(int id, String name, String department, int salary, String entryDate) {
super();
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
DateFormat format = new SimpleDateFormat("yyyy-MM");
try {
this.entryDate = format.parse(entryDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Date getEntryDate() {
return entryDate;
}
public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}
}
注意Date的用法:
DateFormat format = new SimpleDateFormat("yyyy-MM");
Date time = format.parse(entryDate);
/**
*
*/
package com.javabean;
import java.util.ArrayList;
import java.util.List;
/**
*@author 作者:sunlei
*@version 创建时间:2018年3月2日下午4:05:02
*说明
*/
public class Test {
public static void printname(List<Employee>list) {
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i).getName());
}
}
public static void main(String[] args) {
List<Employee> list = new ArrayList<Employee>();
Employee e1 = new Employee(001,"张三","研发",3000,"2016-9");
Employee e2 = new Employee(001,"李四","财务",3000,"2016-9");
Employee e3 = new Employee(001,"王五","采购",3000,"2016-9");
list.add(e1);
list.add(e2);
list.add(e3);
printname(list);
}
}