实体类
package comm.quy.daomain;
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
首先是 入口
package comm.quy;
import comm.quy.view.Userview;
public class TextMain {
public static void main(String[] args) {
Userview userview=new Userview();
userview.main();
}
}
页面层
package comm.quy.view;
import comm.quy.daomain.User;
import comm.quy.service.UserServise;
import java.util.List;
import java.util.Scanner;
public class Userview {
private UserServise userServise=new UserServise();
Scanner scanner = new Scanner(System.in);
public void main() {
while (true){
menu();
System.out.println("请选择您1-5");
int i=scanner.nextInt();
switch (i){
case 1:
list();
break;
case 2:
add();
break;
case 3:
update();
break;
case 4:
del();
break;
case 5:
break;
default:
System.out.println("重新输入");
}
}
}
private void update() {
System.out.println("修改用户");
System.out.print("请输入id");
int id=scanner.nextInt();
System.out.print("请输入用户姓名");
String name=scanner.next();
System.out.print("请输入用户年龄");
int age=scanner.nextInt();
//创建user
User user = new User(id,name, age);
userServise.update(user);
}
private void del() {
System.out.println("请输入删除的id");
int id=scanner.nextInt();
userServise.del(id);
}
private void add() {
System.out.println("添加用户");
System.out.print("请输入用户姓名");
String name=scanner.next();
System.out.print("请输入用户年龄");
int age=scanner.nextInt();
//创建user
User user = new User(name, age);
userServise.add(user);
}
private void list() {
System.out.println("用户列表");
//d调用用户列表数据库
List<User> list=userServise.list();
System.out.println("序号 姓名 年龄");
for (User user : list) {
System.out.println(user.getId()+" "+user.getName()+" "+user.getAge());
}
}
private void menu(){
System.out.println("==================欢迎使用用户管理===============");
System.out.println("1用户列表");
System.out.println("2添加用户");
System.out.println("3修改用户");
System.out.println("4删除用户");
System.out.println("5退出系统");
}
}
输出处理层Service
package comm.quy.service;
import comm.quy.dao.UserDao;
import comm.quy.daomain.User;
import javax.jws.soap.SOAPBinding;
import java.util.List;
public class UserServise {
private UserDao userDao=new UserDao();
public void add(User user) {
userDao.add(user);
}
public List<User> list() {
return userDao.list();
}
public void update(User user) {
userDao.update(user);
}
public void del(int id) {
userDao.del(id);
}
}
数据存储层dao
package comm.quy.dao;
import comm.quy.daomain.User;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
private static int num=1;;
private static List<User> users=new ArrayList<>();
public void add(User user) {
user.setId(num++);
users.add(user);
}
//显示
public List<User> list() {
return users;
}
//修改
public void update(User user) {
for (int i=0;i<users.size();i++){
if (users.get(i).getId()==user.getId()){
users.set(1,user);
break;
}
}
}
public void del(int id) {
for (User user : users) {
if (user.getId()==id){
users.remove(user);
break;
}
}
}
}