分析
JavaBean
Student
package com.czxy.domain;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
2)User
package com.czxy.domain;
public class User {
private String username;
private String password;
private Integer age;
public User() {
}
public User(String username, String password, Integer age) {
this.username = username;
this.password = password;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
2.3 StudentApplication
1)搭建基本结构
2)*Manager
3)入门程序
package com.czxy;
import com.czxy.domain.Student;
import com.czxy.service.StudentManager;
import com.czxy.service.UserManager;
import java.util.Scanner;
public class StudentApplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManager studentManager = new StudentManager();
UserManager userManager = new UserManager();
while(true) {
System.out.println("欢迎进入xxx学生管理系统");
System.out.println("您可以进行如下操作:");
System.out.println("1.登录");
System.out.println("2.添加学生");
System.out.println("3.查询");
System.out.println("4.退出");
System.out.print("请输入您的操作命令:");
String command = scanner.nextLine();
switch (command) {
case "1":
userLogin(scanner, userManager );
break;
case "2":
addStudent(scanner, studentManager );
break;
case "3":
printStudent(scanner, studentManager);
break;
case "4":
System.exit(0);
break;
default:
System.out.println("操作命令有误");
}
}
}
private static void userLogin(Scanner scanner, UserManager userManager) {
}
private static void addStudent(Scanner scanner, StudentManager studentManager) {
System.out.print("请输入编号:");
Integer id = Integer.parseInt(scanner.nextLine());
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入年龄:");
Integer age = Integer.parseInt(scanner.nextLine());
Student student = new Student(id,name,age);
}
private static void printStudent(Scanner scanner, StudentManager studentManager) {
}
}
2.4 登录
1)登录逻辑
package com.czxy.service;
import com.czxy.domain.User;
import java.util.HashMap;
import java.util.Map;
public class UserManager {
private Map<String, User> userData = new HashMap<>();
private User currentLoginUser;
public UserManager() {
userData.put("jack", new User("jack","1234", 18));
userData.put("rose", new User("rose","1234", 21));
}
public void login(String username , String password) {
User loginUser = userData.get(username);
if(loginUser == null) {
System.out.println("账号不存在!!!");
return;
}
if(! loginUser.getPassword().equals(password)) {
System.out.println("密码错误");
return;
}
System.out.println("登录成功");
this.currentLoginUser = loginUser;
}
public User getCurrentLoginUser() {
return this.currentLoginUser;
}
}
2)入口
private static void userLogin(Scanner scanner, UserManager userManager) {
System.out.print("请输入用户名:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
userManager.login(username, password);
}
2.5 添加学生
1)逻辑
package com.czxy.service;
import com.czxy.domain.Student;
import com.czxy.domain.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StudentManager {
private Map<String, List<Student>> studentData = new HashMap<>();
public void add(User loginUser, Student student) {
if(loginUser == null) {
System.out.println("请先登录!!!");
return;
}
List<Student> list = studentData.get(loginUser.getUsername());
if(list == null) {
list = new ArrayList<>();
studentData.put(loginUser.getUsername(), list);
}
list.add(student);
}
}
2)入口
private static void addStudent(Scanner scanner, UserManager userManager, StudentManager studentManager) {
System.out.print("请输入编号:");
Integer id = Integer.parseInt(scanner.nextLine());
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入年龄:");
Integer age = Integer.parseInt(scanner.nextLine());
Student student = new Student(id,name,age);
User loginUser = userManager.getCurrentLoginUser();
studentManager.add(loginUser, student);
}
2.6 查询
1)逻辑
public void print(User loginUser) {
if(loginUser == null) {
System.out.println("请先登录!!!");
return;
}
List<Student> list = studentData.get(loginUser.getUsername());
if(list == null) {
System.out.println("没有数据");
return;
}
for(Student student : list) {
System.out.println(student);
}
}
2)入口
private static void printStudent(Scanner scanner, UserManager userManager, StudentManager studentManager) {
User loginUser = userManager.getCurrentLoginUser();
studentManager.print(loginUser);
}