1、view 包下 –> New –> Class
2、Name -> UserView -> Finish
3、UserView 类 内容
package com.yg.view;
import java.util.List;
import java.util.Scanner;
import com.yg.entity.User;
import com.yg.service.UserService;
import com.yg.serviceimpl.UserServiceImpl;
public class UserView {
User user = null;
UserService userService = new UserServiceImpl();
public User login(Scanner sc) {
System.out.println("请输入用户名");
String userNo = sc.next();
System.out.println("请输入密码:");
String pwd = sc.next();
user = userService.login(userNo, pwd);
if (user != null) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败!!!");
}
return user;
}
public void add_User(Scanner sc) {
System.out.println("输入学号:");
String userNo = sc.next();
System.out.println("输入用户名:");
String userName = sc.next();
System.out.println("输入密码:");
String pwd = sc.next();
User user = new User();
user.setUserno(userNo);
user.setUsername(userName);
user.setPwd(pwd);
if (userService.add_User(user)) {
System.out.println("添加用户成功");
} else {
System.out.println("添加用户失败");
}
}
public void show_User_List() {
System.out.println("所有用户信息如下:");
List<User> list = userService.get_All_Users();
System.out.println("用户ID"
+ "\t\t用户登录账号"
+ "\t\t用户姓名"
+ "\t\t密码"
+ "\t\t性别"
+ "\t\t年龄"
+ "\t\t职位");
for (User user : list) {
System.out.println(user.getId()
+ "\t\t" + user.getUserno()
+ "\t\t" + user.getUsername()
+ "\t\t" + user.getPwd()
+ "\t\t" + user.getSex()
+ "\t\t" + user.getAge()
+ "\t\t" + user.getPosition());
}
}
public void delete_User(Scanner sc) {
System.out.println("请输入要删除的用户id:");
String id = sc.next();
if (userService.delete_User(id)) {
System.out.println("删除成功。");
} else {
System.out.println("删除失败!!!");
}
}
public void edit_User(Scanner sc) {
User user = new User();
System.out.println("请输入要修改的用户id:");
int id = sc.nextInt();
System.out.println("请输入要修改的用户username:");
String username = sc.next();
System.out.println("请输入要修改的用户userno:");
String userno = sc.next();
System.out.println("请输入要修改的用户pwd:");
String pwd = sc.next();
user.setId(id);
user.setUsername(username);
user.setUserno(userno);
user.setPwd(pwd);
if (userService.edit_User(user)) {
System.out.println("修改成功。");
} else {
System.out.println("修改失败!!!");
}
}
}