用JAVA写一个教材管理系统
时间: 2023-11-11 14:03:26 浏览: 61
好的,一个教材管理系统应该包括以下功能:
1. 登录:管理员和教师应该可以登录系统。
2. 教材管理:管理员可以添加、编辑和删除教材,也可以查看所有的教材。
3. 教师管理:管理员可以添加、编辑和删除教师,也可以查看所有的教师。
4. 分配教材:管理员可以分配教材给教师。
5. 借还教材:教师可以借阅教材并归还给管理员。
6. 教材查询:教师和管理员可以查询教材信息。
基于以上功能,下面是一个简单的JAVA代码,可以实现教材管理系统:
```java
import java.util.*;
// 教材类
class Textbook {
private String name;
private String author;
private int quantity;
public Textbook(String name, String author, int quantity) {
this.name = name;
this.author = author;
this.quantity = quantity;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
// 教师类
class Teacher {
private String name;
private String id;
public Teacher(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
}
// 教材管理系统类
public class TextbookManagementSystem {
private static Map<String, Textbook> textbooks = new HashMap<String, Textbook>();
private static Map<String, Teacher> teachers = new HashMap<String, Teacher>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入操作:1.登录 2.退出");
int operation = scanner.nextInt();
if (operation == 1) {
System.out.println("请输入用户名和密码:");
String username = scanner.next();
String password = scanner.next();
if (username.equals("admin") && password.equals("123456")) {
System.out.println("管理员登录成功!");
adminOperation(scanner);
} else {
System.out.println("教师登录成功!");
teacherOperation(scanner);
}
} else {
break;
}
}
scanner.close();
}
// 管理员操作
public static void adminOperation(Scanner scanner) {
while (true) {
System.out.println("请输入操作:1.添加教材 2.编辑教材 3.删除教材 4.查看教材 5.添加教师 6.编辑教师 7.删除教师 8.查看教师 9.分配教材 10.退出");
int operation = scanner.nextInt();
switch (operation) {
case 1:
addTextbook(scanner);
break;
case 2:
editTextbook(scanner);
break;
case 3:
deleteTextbook(scanner);
break;
case 4:
viewTextbooks();
break;
case 5:
addTeacher(scanner);
break;
case 6:
editTeacher(scanner);
break;
case 7:
deleteTeacher(scanner);
break;
case 8:
viewTeachers();
break;
case 9:
assignTextbook(scanner);
break;
case 10:
return;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 教师操作
public static void teacherOperation(Scanner scanner) {
while (true) {
System.out.println("请输入操作:1.借阅教材 2.归还教材 3.查询教材 4.退出");
int operation = scanner.nextInt();
switch (operation) {
case 1:
borrowTextbook(scanner);
break;
case 2:
returnTextbook(scanner);
break;
case 3:
queryTextbook(scanner);
break;
case 4:
return;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 添加教材
public static void addTextbook(Scanner scanner) {
System.out.println("请输入教材名称、作者和数量:");
String name = scanner.next();
String author = scanner.next();
int quantity = scanner.nextInt();
if (textbooks.containsKey(name)) {
System.out.println("该教材已存在!");
} else {
textbooks.put(name, new Textbook(name, author, quantity));
System.out.println("添加成功!");
}
}
// 编辑教材
public static void editTextbook(Scanner scanner) {
System.out.println("请输入教材名称:");
String name = scanner.next();
if (!textbooks.containsKey(name)) {
System.out.println("该教材不存在!");
} else {
Textbook textbook = textbooks.get(name);
System.out.println("请输入新的教材名称、作者和数量:");
String newName = scanner.next();
String author = scanner.next();
int quantity = scanner.nextInt();
textbooks.remove(name);
textbooks.put(newName, new Textbook(newName, author, quantity));
System.out.println("编辑成功!");
}
}
// 删除教材
public static void deleteTextbook(Scanner scanner) {
System.out.println("请输入教材名称:");
String name = scanner.next();
if (!textbooks.containsKey(name)) {
System.out.println("该教材不存在!");
} else {
textbooks.remove(name);
System.out.println("删除成功!");
}
}
// 查看教材
public static void viewTextbooks() {
for (Textbook textbook : textbooks.values()) {
System.out.println("教材名称:" + textbook.getName() + ",作者:" + textbook.getAuthor() + ",数量:" + textbook.getQuantity());
}
}
// 添加教师
public static void addTeacher(Scanner scanner) {
System.out.println("请输入教师姓名和工号:");
String name = scanner.next();
String id = scanner.next();
if (teachers.containsKey(id)) {
System.out.println("该教师已存在!");
} else {
teachers.put(id, new Teacher(name, id));
System.out.println("添加成功!");
}
}
// 编辑教师
public static void editTeacher(Scanner scanner) {
System.out.println("请输入教师工号:");
String id = scanner.next();
if (!teachers.containsKey(id)) {
System.out.println("该教师不存在!");
} else {
Teacher teacher = teachers.get(id);
System.out.println("请输入新的教师姓名:");
String name = scanner.next();
teacher = new Teacher(name, id);
teachers.put(id, teacher);
System.out.println("编辑成功!");
}
}
// 删除教师
public static void deleteTeacher(Scanner scanner) {
System.out.println("请输入教师工号:");
String id = scanner.next();
if (!teachers.containsKey(id)) {
System.out.println("该教师不存在!");
} else {
teachers.remove(id);
System.out.println("删除成功!");
}
}
// 查看教师
public static void viewTeachers() {
for (Teacher teacher : teachers.values()) {
System.out.println("教师姓名:" + teacher.getName() + ",工号:" + teacher.getId());
}
}
// 分配教材
public static void assignTextbook(Scanner scanner) {
System.out.println("请输入教师工号和教材名称:");
String id = scanner.next();
String name = scanner.next();
if (!teachers.containsKey(id)) {
System.out.println("该教师不存在!");
} else if (!textbooks.containsKey(name)) {
System.out.println("该教材不存在!");
} else {
Teacher teacher = teachers.get(id);
Textbook textbook = textbooks.get(name);
System.out.println("请输入分配数量:");
int quantity = scanner.nextInt();
if (textbook.getQuantity() < quantity) {
System.out.println("库存不足!");
} else {
textbook.setQuantity(textbook.getQuantity() - quantity);
System.out.println("分配成功!");
}
}
}
// 借阅教材
public static void borrowTextbook(Scanner scanner) {
System.out.println("请输入教材名称:");
String name = scanner.next();
if (!textbooks.containsKey(name)) {
System.out.println("该教材不存在!");
} else {
Textbook textbook = textbooks.get(name);
System.out.println("请输入借阅数量:");
int quantity = scanner.nextInt();
if (textbook.getQuantity() < quantity) {
System.out.println("库存不足!");
} else {
textbook.setQuantity(textbook.getQuantity() - quantity);
System.out.println("借阅成功!");
}
}
}
// 归还教材
public static void returnTextbook(Scanner scanner) {
System.out.println("请输入教材名称:");
String name = scanner.next();
if (!textbooks.containsKey(name)) {
System.out.println("该教材不存在!");
} else {
Textbook textbook = textbooks.get(name);
System.out.println("请输入归还数量:");
int quantity = scanner.nextInt();
textbook.setQuantity(textbook.getQuantity() + quantity);
System.out.println("归还成功!");
}
}
// 查询教材
public static void queryTextbook(Scanner scanner) {
System.out.println("请输入教材名称:");
String name = scanner.next();
if (!textbooks.containsKey(name)) {
System.out.println("该教材不存在!");
} else {
Textbook textbook = textbooks.get(name);
System.out.println("教材名称:" + textbook.getName() + ",作者:" + textbook.getAuthor() + ",数量:" + textbook.getQuantity());
}
}
}
```
以上是一个基本的教材管理系统,可以根据具体需求进行修改和扩展。
阅读全文