本文使用IDEA,利用JAVA对数组的操作搭建后台管理系统,实现简单的增、删、改、查功能。步骤详细,讲解丰富,适合新手入门。
*IntelliJ IDEA Ultimate 2017.2.2
*JDK 1.8.0_121
*import java.util.Scanner;
在工程中新建dormitory模块,在里面src下新建“com.Jackson”包,再在com包下新建五个包,分别命名为:model,service,dao,util,UI。(如果出现下图中所示的包名重叠的情况,可以点击图中所示的图标,将“Hide empty middle package取消掉”)
上面新建的四个包:model,service,dao,util,UI,其所存放的分别是:
model: 存放自定义的java实体类。如:Person类,Room类等,每个类的属性设为private,并提供public属性的getter/setter方法让外界访问
service:定义接口,包含展现给用户封装好的功能。(之后还会在service包下再新建impl包)。
dao:定义接口,包含系统所提供的功能。(之后还会在service包下再新建impl包)。
util:定义工具类,PersonUtil,调用通用方法。
UI:展现给用户的UI界面的定义。
在modle下建立Person类和Room类
package Jacskon.model;
public class Person {
private int pid;
private String name;
private String sex;
public Person() {
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public Person(int pid, String name, String sex) {
this.pid = pid;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "人员编号:" + pid +
" 人员姓名:" + name + '\'' +
"人员性别:" + sex;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package Jacskon.model;
public class Room {
private int rid; //房间编号
private Person[] people;//住宿的人
private String type;//房间类型
private int nums;//房间人数
@Override
public String toString() {
return "房间编号:"+rid+",当前人数:"+nums+",房间类型:"+type;
}
public Room() {
}
public Room(int rid, Person[] people, String type, int nums) {
this.rid = rid;
this.people = people;
this.type = type;
this.nums = nums;
}
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public Person[] getPeople() {
return people;
}
public void setPeople(Person[] people) {
this.people = people;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
public interface PersonDao {
//先解决初始化的问题
void doInitPerson();
//查询所有的人
void doFindAll();
//根据id查询住宿人员的信息
Person dofindidPerson(int id);
//新增住宿人员
boolean doAddPerson(Person person);
//更新住宿人员信息
boolean doupPerson(Person person);
//删除住宿人员的信息
boolean dodelPerson(int id);
}
public interface RoomDao {
//1.准备好宿舍
Room[] getRomm();
}
在dao层下建立实现类PersonDaoImpl.class和RoomDaoImpl.class
package Jacskon.dao.impl;
import Jacskon.dao.PersonDao;
import Jacskon.model.Person;
import Jacskon.model.Room;
public class PersonDaoImpl implements PersonDao {
RoomDaoImpl rdi=new RoomDaoImpl();
Room[] rooms=rdi.getRomm();
@Override
public void doInitPerson() {
Person p1=new Person(1001,"刘德华","男");
Person p2=new Person(1002,"刘洋","男");
Person p3=new Person(1003,"刘媛媛","女");
rooms[0].getPeople()[0]=p1;
rooms[0].getPeople()[1]=p2;
rooms[0].setNums(2);
rooms[6].getPeople()[0]=p3;
rooms[6].setNums(1);
}
@Override
public void doFindAll() {
for (int i=0;i
package Jacskon.dao.impl;
import Jacskon.dao.RoomDao;
import Jacskon.model.Person;
import Jacskon.model.Room;
public class RoomDaoImpl implements RoomDao {
Room[] rooms=new Room[10];
@Override
public Room[] getRomm() {
//定义6个男士寝室
for (int i=0;i<=5;i++){
rooms[i]=new Room((i+100),new Person[6],"男",0);
}
//定义4个女生寝室
for (int i=6;i
package Jacskon.service.impl;
import Jacskon.dao.PersonDao;
import Jacskon.dao.impl.PersonDaoImpl;
import Jacskon.model.Person;
import Jacskon.service.PersonService;
public class PersonServiceImpl implements PersonService {
PersonDao pdi=new PersonDaoImpl();
@Override
public void doInitPerson() {
pdi.doInitPerson();
}
@Override
public void doFindAll() {
pdi.doFindAll();
}
@Override
public Person dofindidPerson(int id) {
Person person= pdi.dofindidPerson(id);
if (person!=null){
System.out.println("id为"+id+"的住宿人员信息为"+person.toString());
return person;
}else{
System.out.println("查询失败,请重新查询");
return null;
}
}
@Override
public boolean doAddPerson(Person person) {
Boolean b= pdi.doAddPerson(person);
if (b){
System.out.println("新增成功");
return true;
}else{
System.out.println("新增失败");
return false;
}
}
@Override
public boolean doupPerson(Person person) {
Boolean b= pdi.doupPerson(person);
if (b){
System.out.println("更新人员信息成功");
return true;
}else{
System.out.println("更新人员信息失败");
return false;
}
}
@Override
public boolean dodelPerson(int id) {
Boolean b= pdi.dodelPerson(id);
if (b){
System.out.println("删除人员信息成功");
return true;
}else{
System.out.println("删除人员信息失败");
return false;
}
}
}
public class PersonUtil {
Scanner in=new Scanner(System.in);
public Person getPerson(String str){
System.out.println("请您输入需要"+str+"的人员编号");
int id=in.nextInt();
System.out.println("请您输入需要"+str+"的人员名称");
String name=in.next();
System.out.println("请您输入需要"+str+"的人员性别");
String sex=in.next();
Person person=new Person(id,name,sex);
return person;
}
public int getPersonid(String str){
System.out.println("请您输入需要"+str+"的人员编号");
int id=in.nextInt();
return id;
}
}