
一、工程目录结构 二、UI层集合、面向对象
调用方法
package com.first.ui;
import java.util.Scanner;
import com.first.service.StudentService;
import com.first.util.Util;
public class UI {
public static void main(String[] args) {
Scanner s = Util.s;
StudentService service = new StudentService();
System.out.println("======欢迎来到学生管理系统======");
for(;;) {
System.out.println("============================"
+ "n| 1.录入学籍tt|"
+ "n| 2.开除学籍tt|"
+ "n| 3.修改学生信息tt|"
+ "n| 4.查询所有学生信息t|"
+ "n| 5.学号查询tt|"
+ "n| 6.退出tt|"
+ "n==========请选择 *** 作>>>>>>>>>>");
int op = s.nextInt();
switch(op) {
case 1:
service.insert();
break;
case 2:
service.delete();
break;
case 3:
service.update();
break;
case 4:
service.selectAll();
break;
case 5:
service.selectById();
break;
case 6:
System.out.println("程序结束!");
System.exit(0);
}
}
}
}
三、service层
前期准备工作 接收输入
录入学籍开除学籍修改学生信息查询所有学生信息学号查询退出
package com.first.service;
import java.util.Scanner;
import com.first.dao.StudentDao;
import com.first.pojo.Student;
import com.first.util.Util;
public class StudentService {
// - 1 录入学籍
// 输入参数 存到ArrayList
public void insert() {
Scanner s =Util.s;//调用工具类里封装好的Scanner
System.out.println("请输入学号:");
int id = s.nextInt();
System.out.println("请输入姓名:");
String name = s.next();
System.out.println("请输入手机号:");
String phone = s.next();
System.out.println("请输入地址:");
String address = s.next();
Student student = new Student(id,name,phone,address);//将数据传入到Student的属性
StudentDao dao = new StudentDao();//实例化StudentDao,调用dao的方法,用来传入student数据
dao.insert(student);
}
// - 2 开除学籍
public void delete() {
Scanner s = Util.s;
System.out.println("请输入删除的学号:");
int id = s.nextInt();
StudentDao dao = new StudentDao();
dao.delete(id);
}
// - 3 修改学生信息
public void update() {
Scanner s =Util.s;
System.out.println("请输入学号:");
int id = s.nextInt();
System.out.println("请输入姓名:");
String name = s.nextLine();
System.out.println("请输入手机号:");
String phone = s.nextLine();
System.out.println("请输入地址:");
String address = s.nextLine();
Student student = new Student(id,name,phone,address);
StudentDao dao = new StudentDao();
dao.update(student);
}
// - 4 查询所有学生信息
public void selectAll() {
StudentDao dao = new StudentDao();
dao.selectAll();
}
// - 5 根据学号查询学生信息
public void selectById() {
Scanner s = Util.s;
System.out.println("请输入要查询的学号:");
int id = s.nextInt();
StudentDao dao = new StudentDao();
dao.selectById(id);
}
// - 6 退出
public void exit() {
System.exit(0);
}
}
四、Dao层
增删改查
录入学籍开除学籍修改学生信息查询所有学生信息学号查询
package com.first.dao;
import java.util.List;
import com.first.pojo.Student;
import com.first.util.Util;
public class StudentDao {
// - 1 录入学籍
public void insert(Student student) {
List studentList = Util.StudentList;
studentList.add(student);
System.out.println("添加成功");
}
// - 2 开除学籍
public void delete(int id) {
//寻找id对应的student
List studentList = Util.StudentList;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);//获取对应Student的索引
if(student.getId() == id) {//判断是否与输入的id相同
studentList.remove(i);//移除id所对应的索引的Student对象
System.out.println("删除成功");
}
}
}
// - 3 修改学生信息
public void update(Student student) {
//找id对应的student
List studentList = Util.StudentList;
for (int i = 0; i < studentList.size(); i++) {
Student student2 = studentList.get(i);//获取对应Student的索引
if(student.getId() == student2.getId()) {//判断是否与输入的id相同
studentList.set(i,student);//通过索引号替换之前的Student
System.out.println("修改成功!");
break;
}
}
}
// - 4 查询所有学生信息
public void selectAll() {
List studentList = Util.StudentList;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
System.out.println("序号:"+student.getNum()+"t学号:"+student.getId()+"t姓名:"+student.getName()+
"t地址:"+student.getAddress()+"t手机号"+student.getPhone());
}
}
// - 5 根据学号查询学生信息
public void selectById(int id) {
List studentList = Util.StudentList;
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if(student.getId() == id) {
System.out.println("序号>> "+student.getNum()+"n学号>> "+student.getId()+"n姓名>> "+student.getName()+
"n地址>> "+student.getAddress()+"n手机号>> "+student.getPhone());
break;
}
}
}
}
五、pojo 实体类
Student
封装属性,通过构造方法实例化。
package com.first.pojo;
public class Student {
private static int count =0;//计数器
private int num;//序号
private int id;//学号
private String name;//姓名
private String phone;//手机号
private String address;//地址
//无参构造方法
public Student() {
num = ++count;//生成序列号,自动计数
}
public Student(int id, String name, String phone, String address) {
this();//调用无参构造方法
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
}
//get、set方法
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [num=" + num + ", id=" + id + ", name=" + name + ", phone=" + phone + ", address=" + address
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + num;
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (num != other.num)
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
}
六、util 工具类
Util,封装常用工具
Arraylist 存放数据 只有一份 唯一 所以使用static
package com.first.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.first.pojo.Student;
public class Util {
//封装常用工具
public static Scanner s = new Scanner(System.in);//属性
//泛型 规定集合内只放某种类型
//Arraylist 存放数据 只有一份 唯一 所以使用static
public static List StudentList = new ArrayList();
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)