Compare commits

..

No commits in common. 'master' and 'main' have entirely different histories.
master ... main

@ -1,101 +0,0 @@
package com.student.view;
import com.student.model.Student;
import java.util.List;
import java.util.Scanner;
/**
*
*/
public class ConsoleStudentView implements StudentView {
private final Scanner scanner;
/**
* Scanner
*/
public ConsoleStudentView() {
this.scanner = new Scanner(System.in);
}
@Override
public void displayStudent(Student student) {
if (student != null) {
System.out.println("\n===== 学生信息 =====");
System.out.println("ID: " + student.getId());
System.out.println("姓名: " + student.getName());
System.out.println("年龄: " + student.getAge());
System.out.println("课程: " + student.getCourse());
System.out.println("==================\n");
} else {
System.out.println("未找到该学生信息");
}
}
@Override
public void displayAllStudents(List<Student> students) {
if (students.isEmpty()) {
System.out.println("当前没有学生信息");
return;
}
System.out.println("\n===== 所有学生信息 =====");
System.out.printf("%-10s %-10s %-5s %-15s\n", "ID", "姓名", "年龄", "课程");
System.out.println("----------------------------------------");
for (Student student : students) {
System.out.printf("%-10s %-10s %-5d %-15s\n",
student.getId(),
student.getName(),
student.getAge(),
student.getCourse());
}
System.out.println("========================================\n");
}
@Override
public void displayMessage(String message) {
System.out.println(message);
}
@Override
public String getStudentIdInput() {
System.out.print("请输入学生ID: ");
return scanner.nextLine();
}
@Override
public Student getStudentInput() {
System.out.println("请输入学生信息:");
System.out.print("ID: ");
String id = scanner.nextLine();
System.out.print("姓名: ");
String name = scanner.nextLine();
System.out.print("年龄: ");
int age = Integer.parseInt(scanner.nextLine());
System.out.print("课程: ");
String course = scanner.nextLine();
return new Student(id, name, age, course);
}
@Override
public void displayMenu() {
System.out.println("\n===== 学生管理系统 =====");
System.out.println("1. 添加学生");
System.out.println("2. 查看所有学生");
System.out.println("3. 查找学生");
System.out.println("4. 更新学生信息");
System.out.println("5. 删除学生");
System.out.println("0. 退出系统");
System.out.println("=======================");
}
@Override
public int getMenuChoice() {
System.out.print("请选择操作 (0-5): ");
try {
return Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
return -1; // 无效输入
}
}
}

@ -1,90 +0,0 @@
package com.student.utils;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
*
*
*/
public class ExceptionHandler {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
*
* @param e
* @return
*/
public static String handleException(Exception e) {
StringBuilder message = new StringBuilder();
message.append("[错误] ")
.append(getCurrentTime())
.append(" - ")
.append(e.getClass().getSimpleName())
.append(": ")
.append(e.getMessage())
.append("\n");
// 添加异常堆栈信息
message.append("异常堆栈:\n")
.append(getStackTraceAsString(e));
// 记录到控制台
System.err.println(message.toString());
// 这里可以扩展为写入日志文件
// logToFile(message.toString());
return e.getMessage(); // 返回简洁的错误信息给用户
}
/**
*
* @return
*/
private static String getCurrentTime() {
return LocalDateTime.now().format(formatter);
}
/**
*
* @param e
* @return
*/
private static String getStackTraceAsString(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.close();
return sw.toString();
}
/**
* ID
* @param id ID
* @return
*/
public static boolean isValidStudentId(String id) {
return id != null && !id.trim().isEmpty();
}
/**
*
* @param name
* @return
*/
public static boolean isValidStudentName(String name) {
return name != null && !name.trim().isEmpty() && name.length() <= 20;
}
/**
*
* @param age
* @return
*/
public static boolean isValidStudentAge(int age) {
return age >= 1 && age <= 150;
}
}

@ -1,35 +0,0 @@
package com.student;
import com.student.controller.StudentController;
import com.student.model.StudentRepository;
import com.student.view.ConsoleStudentView;
import com.student.view.StudentView;
/**
*
*
*/
public class Main {
/**
*
* @param args
*/
public static void main(String[] args) {
// 创建模型层组件
StudentRepository repository = StudentRepository.getInstance();
// 创建视图层组件
StudentView view = new ConsoleStudentView();
// 创建控制器,连接模型和视图
StudentController controller = new StudentController(repository, view);
// 显示欢迎信息
view.displayMessage("==================================");
view.displayMessage(" 欢迎使用学生管理系统");
view.displayMessage("==================================");
// 启动控制器,开始处理用户交互
controller.start();
}
}

@ -0,0 +1,2 @@
# 1111

@ -1,68 +0,0 @@
package com.student.model;
/**
*
*/
public class Student {
private String id; // 学生ID
private String name; // 学生姓名
private int age; // 学生年龄
private String course; // 学生课程
/**
*
* @param id ID
* @param name
* @param age
* @param course
*/
public Student(String id, String name, int age, String course) {
this.id = id;
this.name = name;
this.age = age;
this.course = course;
}
// Getter和Setter方法
public String getId() {
return id;
}
public void setId(String 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;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", course='" + course + '\'' +
'}';
}
}

@ -1,138 +0,0 @@
package com.student.controller;
import com.student.model.Student;
import com.student.model.StudentRepository;
import com.student.view.StudentView;
import java.util.List;
/**
*
* MVC
*/
public class StudentController {
private final StudentRepository repository;
private final StudentView view;
/**
*
* @param repository
* @param view
*/
public StudentController(StudentRepository repository, StudentView view) {
this.repository = repository;
this.view = view;
}
/**
*
*/
public void start() {
boolean running = true;
while (running) {
view.displayMenu();
int choice = view.getMenuChoice();
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayAllStudents();
break;
case 3:
findStudent();
break;
case 4:
updateStudent();
break;
case 5:
deleteStudent();
break;
case 0:
running = false;
view.displayMessage("感谢使用学生管理系统,再见!");
break;
default:
view.displayMessage("无效的选择,请重新输入");
}
}
}
/**
*
*/
private void addStudent() {
Student student = view.getStudentInput();
// 检查学生ID是否已存在
if (repository.getStudentById(student.getId()) != null) {
view.displayMessage("该学生ID已存在添加失败");
return;
}
repository.addStudent(student);
view.displayMessage("学生添加成功");
}
/**
*
*/
private void displayAllStudents() {
List<Student> students = repository.getAllStudents();
view.displayAllStudents(students);
}
/**
*
*/
private void findStudent() {
String id = view.getStudentIdInput();
Student student = repository.getStudentById(id);
view.displayStudent(student);
}
/**
*
*/
private void updateStudent() {
String id = view.getStudentIdInput();
Student existingStudent = repository.getStudentById(id);
if (existingStudent == null) {
view.displayMessage("未找到该学生,更新失败");
return;
}
// 显示当前学生信息
view.displayStudent(existingStudent);
view.displayMessage("请输入更新后的学生信息:");
// 获取更新后的信息
Student updatedStudent = view.getStudentInput();
// 确保ID保持一致
updatedStudent.setId(id);
if (repository.updateStudent(updatedStudent)) {
view.displayMessage("学生信息更新成功");
} else {
view.displayMessage("学生信息更新失败");
}
}
/**
*
*/
private void deleteStudent() {
String id = view.getStudentIdInput();
if (repository.getStudentById(id) == null) {
view.displayMessage("未找到该学生,删除失败");
return;
}
if (repository.deleteStudent(id)) {
view.displayMessage("学生删除成功");
} else {
view.displayMessage("学生删除失败");
}
}
}

@ -1,97 +0,0 @@
package com.student.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 使
*/
public class StudentRepository {
// 使用Map存储学生数据以ID为键
private final Map<String, Student> students;
// 单例实例
private static volatile StudentRepository instance;
/**
*
*/
private StudentRepository() {
students = new HashMap<>();
// 初始化一些示例数据
addSampleData();
}
/**
*
* @return StudentRepository
*/
public static StudentRepository getInstance() {
if (instance == null) {
synchronized (StudentRepository.class) {
if (instance == null) {
instance = new StudentRepository();
}
}
}
return instance;
}
/**
*
*/
private void addSampleData() {
addStudent(new Student("1", "张三", 20, "计算机科学"));
addStudent(new Student("2", "李四", 21, "软件工程"));
addStudent(new Student("3", "王五", 19, "数据结构"));
}
/**
*
* @param student
*/
public void addStudent(Student student) {
students.put(student.getId(), student);
}
/**
* ID
* @param id ID
* @return null
*/
public Student getStudentById(String id) {
return students.get(id);
}
/**
*
* @return
*/
public List<Student> getAllStudents() {
return new ArrayList<>(students.values());
}
/**
*
* @param student
* @return
*/
public boolean updateStudent(Student student) {
if (students.containsKey(student.getId())) {
students.put(student.getId(), student);
return true;
}
return false;
}
/**
*
* @param id ID
* @return
*/
public boolean deleteStudent(String id) {
return students.remove(id) != null;
}
}

@ -1,52 +0,0 @@
package com.student.view;
import com.student.model.Student;
import java.util.List;
/**
*
*
*/
public interface StudentView {
/**
*
* @param student
*/
void displayStudent(Student student);
/**
*
* @param students
*/
void displayAllStudents(List<Student> students);
/**
*
* @param message
*/
void displayMessage(String message);
/**
* ID
* @return ID
*/
String getStudentIdInput();
/**
*
* @return
*/
Student getStudentInput();
/**
*
*/
void displayMenu();
/**
*
* @return
*/
int getMenuChoice();
}
Loading…
Cancel
Save