写一下java利用集合实现模拟对数据库增删查改的代码 设置图书馆的登录页面,根据两种身份(读者管理员图书管理员)进行登录)登录功能如果没有用户信息,则需要进入注册功能进行注册。修改密码等基本功能。 用户登录成功之后,可以浏览所有读者的信息,也可以检索特定读者的信息;同时,可以对读者信息进行维护,包括增加、删除及修改。具体信息包括读者类型(读者的类型决定着他可以借到书的最大数量和最大归还天数)、读者姓名、出生日期、性别、电话、所在院系、注册日期等。(2)图书信息管理:包括图书信息添加和图书信息查询与修改功能。用户登录成功之后,可以浏览所有图书信息和检索特定图书的信息;也可以对图书信息进行维护。包括添加图书、删除图书以及修改图书信息。具体信息包括:图书ISBN、图书名称、作者、出版日期、单价、图书类别等。
时间: 2023-12-17 19:03:10 浏览: 95
以下是利用Java集合实现模拟对数据库增删查改的代码:
```
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class LibraryManagementSystem {
public static void main(String[] args) {
ArrayList<Book> books = new ArrayList<Book>();
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (true) {
System.out.println("请选择要进行的操作:");
System.out.println("1.增加图书");
System.out.println("2.删除图书");
System.out.println("3.查询图书");
System.out.println("4.修改图书信息");
System.out.println("5.退出系统");
choice = scanner.nextInt();
switch (choice) {
case 1:
addBook(books, scanner);
break;
case 2:
deleteBook(books, scanner);
break;
case 3:
searchBook(books, scanner);
break;
case 4:
modifyBook(books, scanner);
break;
case 5:
System.out.println("感谢使用图书管理系统!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 增加图书
public static void addBook(ArrayList<Book> books, Scanner scanner) {
System.out.println("请输入图书信息:");
System.out.print("ISBN:");
String isbn = scanner.next();
System.out.print("书名:");
String name = scanner.next();
System.out.print("作者:");
String author = scanner.next();
System.out.print("出版日期:");
String date = scanner.next();
System.out.print("单价:");
double price = scanner.nextDouble();
System.out.print("图书类别:");
String category = scanner.next();
Book book = new Book(isbn, name, author, date, price, category);
books.add(book);
System.out.println("图书添加成功!");
}
// 删除图书
public static void deleteBook(ArrayList<Book> books, Scanner scanner) {
System.out.print("请输入要删除的图书ISBN:");
String isbn = scanner.next();
Iterator<Book> it = books.iterator();
while (it.hasNext()) {
Book book = it.next();
if (book.getIsbn().equals(isbn)) {
it.remove();
System.out.println("图书删除成功!");
return;
}
}
System.out.println("未找到该图书!");
}
// 查询图书
public static void searchBook(ArrayList<Book> books, Scanner scanner) {
System.out.print("请输入要查询的图书ISBN:");
String isbn = scanner.next();
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
System.out.println(book);
return;
}
}
System.out.println("未找到该图书!");
}
// 修改图书信息
public static void modifyBook(ArrayList<Book> books, Scanner scanner) {
System.out.print("请输入要修改的图书ISBN:");
String isbn = scanner.next();
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
System.out.println("请输入新的图书信息(不修改请直接回车):");
System.out.print("ISBN:");
String newIsbn = scanner.nextLine();
if (!newIsbn.isEmpty()) {
book.setIsbn(newIsbn);
}
System.out.print("书名:");
String newName = scanner.nextLine();
if (!newName.isEmpty()) {
book.setName(newName);
}
System.out.print("作者:");
String newAuthor = scanner.nextLine();
if (!newAuthor.isEmpty()) {
book.setAuthor(newAuthor);
}
System.out.print("出版日期:");
String newDate = scanner.nextLine();
if (!newDate.isEmpty()) {
book.setDate(newDate);
}
System.out.print("单价:");
String newPrice = scanner.nextLine();
if (!newPrice.isEmpty()) {
book.setPrice(Double.parseDouble(newPrice));
}
System.out.print("图书类别:");
String newCategory = scanner.nextLine();
if (!newCategory.isEmpty()) {
book.setCategory(newCategory);
}
System.out.println("图书信息修改成功!");
return;
}
}
System.out.println("未找到该图书!");
}
}
class Book {
private String isbn;
private String name;
private String author;
private String date;
private double price;
private String category;
public Book(String isbn, String name, String author, String date, double price, String category) {
this.isbn = isbn;
this.name = name;
this.author = author;
this.date = date;
this.price = price;
this.category = category;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public String toString() {
return "ISBN:" + isbn + "\n书名:" + name + "\n作者:" + author +
"\n出版日期:" + date + "\n单价:" + price + "\n图书类别:" + category;
}
}
```
以下是图书馆登录页面的代码:
```
import java.util.HashMap;
import java.util.Scanner;
public class LibraryLogin {
public static void main(String[] args) {
HashMap<String, String> readers = new HashMap<String, String>();
readers.put("Tom", "123456");
readers.put("Jerry", "654321");
HashMap<String, String> managers = new HashMap<String, String>();
managers.put("Alice", "111111");
managers.put("Bob", "222222");
HashMap<String, String> bookManagers = new HashMap<String, String>();
bookManagers.put("Cindy", "333333");
bookManagers.put("David", "444444");
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (true) {
System.out.println("欢迎使用图书馆管理系统!");
System.out.println("请选择登录身份:");
System.out.println("1.读者");
System.out.println("2.管理员");
System.out.println("3.图书管理员");
System.out.println("4.退出系统");
choice = scanner.nextInt();
switch (choice) {
case 1:
readerLogin(readers, scanner);
break;
case 2:
managerLogin(managers, scanner);
break;
case 3:
bookManagerLogin(bookManagers, scanner);
break;
case 4:
System.out.println("感谢使用图书馆管理系统!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 读者登录
public static void readerLogin(HashMap<String, String> readers, Scanner scanner) {
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String password = scanner.next();
if (!readers.containsKey(username) || !readers.get(username).equals(password)) {
System.out.println("用户名或密码错误!");
return;
}
System.out.println("登录成功!欢迎" + username + "使用图书馆管理系统!");
// TODO: 读者功能
}
// 管理员登录
public static void managerLogin(HashMap<String, String> managers, Scanner scanner) {
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String password = scanner.next();
if (!managers.containsKey(username) || !managers.get(username).equals(password)) {
System.out.println("用户名或密码错误!");
return;
}
System.out.println("登录成功!欢迎" + username + "使用图书馆管理系统!");
// TODO: 管理员功能
}
// 图书管理员登录
public static void bookManagerLogin(HashMap<String, String> bookManagers, Scanner scanner) {
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String password = scanner.next();
if (!bookManagers.containsKey(username) || !bookManagers.get(username).equals(password)) {
System.out.println("用户名或密码错误!");
return;
}
System.out.println("登录成功!欢迎" + username + "使用图书馆管理系统!");
// TODO: 图书管理员功能
}
}
```
阅读全文