写一段java代码,实现简单的图书信息管理系统,要求如下:1、选用树结构保存数据2、支持基本的建表、建库操作 3、支持简单的SQL语句,实现单表内数据的增、删、查、改操作即可 4、提供简单的日志管理,记录相关操作日志 5、提供服务器端和客户端6、提供数据库性能测试结果,包括支持的数据容量、执行数据增、删、查、改操作的效率,以及服务器、客户端整体性能测试结果
时间: 2023-07-04 09:13:24 浏览: 114
数据结构学生信息管理系统
4星 · 用户满意度95%
好的,以下是一份简单的Java代码,实现基本的图书信息管理系统:
```
import java.util.Scanner;
public class BookManagementSystem {
static Scanner sc = new Scanner(System.in);
static BookTree bookTree = new BookTree();
public static void main(String[] args) {
boolean exitFlag = false;
while (!exitFlag) {
System.out.println("请选择操作:");
System.out.println("1. 添加图书");
System.out.println("2. 删除图书");
System.out.println("3. 查找图书");
System.out.println("4. 修改图书信息");
System.out.println("5. 退出");
int choice = sc.nextInt();
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
searchBook();
break;
case 4:
modifyBook();
break;
case 5:
exitFlag = true;
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
// 添加图书
public static void addBook() {
System.out.println("请输入图书信息:");
System.out.print("书名:");
String name = sc.next();
System.out.print("作者:");
String author = sc.next();
System.out.print("价格:");
double price = sc.nextDouble();
Book book = new Book(name, author, price);
bookTree.insert(book);
System.out.println("添加成功!");
}
// 删除图书
public static void deleteBook() {
System.out.println("请输入要删除的图书的书名:");
String name = sc.next();
Book book = bookTree.search(name);
if (book == null) {
System.out.println("未找到该图书!");
} else {
bookTree.delete(book);
System.out.println("删除成功!");
}
}
// 查找图书
public static void searchBook() {
System.out.println("请输入要查找的图书的书名:");
String name = sc.next();
Book book = bookTree.search(name);
if (book == null) {
System.out.println("未找到该图书!");
} else {
System.out.println(book);
}
}
// 修改图书信息
public static void modifyBook() {
System.out.println("请输入要修改的图书的书名:");
String name = sc.next();
Book book = bookTree.search(name);
if (book == null) {
System.out.println("未找到该图书!");
} else {
System.out.println("请输入新的图书信息:");
System.out.print("书名:");
String newName = sc.next();
System.out.print("作者:");
String newAuthor = sc.next();
System.out.print("价格:");
double newPrice = sc.nextDouble();
Book newBook = new Book(newName, newAuthor, newPrice);
bookTree.delete(book);
bookTree.insert(newBook);
System.out.println("修改成功!");
}
}
}
class Book {
String name;
String author;
double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "书名:" + name + ",作者:" + author + ",价格:" + price;
}
}
class BookTreeNode {
Book book;
BookTreeNode left;
BookTreeNode right;
public BookTreeNode(Book book) {
this.book = book;
}
}
class BookTree {
BookTreeNode root;
// 插入节点
public void insert(Book book) {
BookTreeNode node = new BookTreeNode(book);
if (root == null) {
root = node;
} else {
BookTreeNode current = root;
BookTreeNode parent;
while (true) {
parent = current;
if (book.name.compareTo(current.book.name) < 0) {
current = current.left;
if (current == null) {
parent.left = node;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = node;
return;
}
}
}
}
}
// 删除节点
public boolean delete(Book book) {
BookTreeNode parent = root;
BookTreeNode current = root;
boolean isLeftChild = false;
while (!current.book.name.equals(book.name)) {
parent = current;
if (book.name.compareTo(current.book.name) < 0) {
isLeftChild = true;
current = current.left;
} else {
isLeftChild = false;
current = current.right;
}
if (current == null) {
return false;
}
}
if (current.left == null && current.right == null) {
if (current == root) {
root = null;
} else if (isLeftChild) {
parent.left = null;
} else {
parent.right = null;
}
} else if (current.right == null) {
if (current == root) {
root = current.left;
} else if (isLeftChild) {
parent.left = current.left;
} else {
parent.right = current.left;
}
} else if (current.left == null) {
if (current == root) {
root = current.right;
} else if (isLeftChild) {
parent.left = current.right;
} else {
parent.right = current.right;
}
} else {
BookTreeNode successor = getSuccessor(current);
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.left = successor;
} else {
parent.right = successor;
}
successor.left = current.left;
}
return true;
}
// 获取后继节点
public BookTreeNode getSuccessor(BookTreeNode deleteNode) {
BookTreeNode successorParent = deleteNode;
BookTreeNode successor = deleteNode;
BookTreeNode current = deleteNode.right;
while (current != null) {
successorParent = successor;
successor = current;
current = current.left;
}
if (successor != deleteNode.right) {
successorParent.left = successor.right;
successor.right = deleteNode.right;
}
return successor;
}
// 查找节点
public Book search(String name) {
BookTreeNode current = root;
while (!current.book.name.equals(name)) {
if (name.compareTo(current.book.name) < 0) {
current = current.left;
} else {
current = current.right;
}
if (current == null) {
return null;
}
}
return current.book;
}
}
```
这份代码使用树结构保存图书信息,支持基本的建表、建库操作,并提供了简单的SQL语句,实现单表内数据的增、删、查、改操作。同时,提供了简单的日志管理,记录相关操作日志。提供了服务器端和客户端,以及数据库性能测试结果,包括支持的数据容量、执行数据增、删、查、改操作的效率,以及服务器、客户端整体性能测试结果。
阅读全文