java综合实验-图书管理系统

2023-12-14 16:47:32

二、实验项目内容(实验题目)

1. 使用Java编程语言进行实验。

2. 采用面向对象的思想进行系统设计。

3. 实现基本的图书管理功能,包括添加图书、删除图书、查询图书信息等。

4. 要求有良好的用户交互界面。

步骤参考:

步骤一:需求分析

1. 确定系统的基本功能和界面设计。

2. 定义图书类(Book)及其属性和方法。

3. 定义图书管理系统类(LibrarySystem)及其属性和方法。

步骤二:系统设计与实现

1. 创建一个图书类(Book),包含图书名称、作者、出版社等属性,并实现相关的get和set方法。

2. 创建一个图书管理系统类(LibrarySystem),包含添加图书、删除图书、查询图书信息等方法。

3. 在LibrarySystem类中,使用集合(如ArrayList)来存储图书信息。

4. 实现用户交互界面,可以使用命令行或者图形界面。

步骤三:测试与优化

1. 编写测试代码,验证图书管理系统的功能是否正常。

2. 运行程序,进行测试,并根据测试结果进行优化和修改。

参考效果如下:

四、源程序调试过程和(或)实验分析

完整的代码

Book

package Two;

public class Book {

?? ?private String name;

?? ??? private String author;

?? ??? private String publisher;

?? ??

?? ??? private boolean borrowed;

?? ??? public Book(String name, String author, String publisher, double price) {

?? ??? this.name = name;

?? ??? this.author = author;

?? ??? this.publisher = publisher;

?? ???

?? ??? this.borrowed = false;

?? }

?? public String getName() {

?? ??? return name;

?? }

?? public String getAuthor() {

?? ??? return author;

?? }

?? public String getPublisher() {

?? ??? return publisher;

?? }

??

?? public boolean isBorrowed() {

?? ??? return borrowed;

?? }

?? public void setBorrowed(boolean borrowed) {

?? ??? this.borrowed = borrowed;

?? }

?? @Override

?? public String toString() {

?? ??? return "名称:" + name + ",作者:" + author + ",出版社:" + publisher + ",状态:" + (borrowed ? "借出" : "未借出");

?? }

?? }

LibrarySystem

package Two;

import java.util.ArrayList;

import java.util.Scanner;

public class LibrarySystem {

??? private static ArrayList<Book> books = new ArrayList<Book>();

??? private static Scanner input = new Scanner(System.in);

?? private static Object name;

?? private static boolean found;

??? public static void main(String[] args) {

??????? boolean running = true;

??????? while (running) {

??????????? System.out.println("\n欢迎使用图书管理系统");

??????????

??????????? System.out.print("请选择操作:");

??????????? System.out.println("1. 添加书籍");

??????????? System.out.println("2. 删除书籍");

??????????? System.out.println("3. 查询书籍");

??????????? System.out.println("4. 查看所有书籍");

???????????

??????????? System.out.println("0. 退出系统");

??????????? int choice = input.nextInt();

??????????? input.nextLine();

??????????? switch (choice) {

??????????????? case 1:

??????????????????? addBook();

??????????????????? break;

??????????????? case 2:

??????????????????? removeBook();

??????????????????? break;

??????????????? case 3:

??????????????????? searchBook();

??????????????????? break;

??????????????? case 4:

??????????????????? borrowBook();

??????????????????? break;

??????????????? case 0:

??????????????????? running = false;

??????????????????? System.out.println("感谢使用图书管理系统!");

??????????????????? break;

??????????????? default:

??????????????????? System.out.println("无效的选择,请重新选择!");

??????????????????? break;

??????????? }

??????? }

??? }

??? private static void addBook() {

??????? System.out.print("请输入书名:");

??????? String name = input.nextLine();

??????? System.out.print("请输入作者:");

??????? String author = input.nextLine();

??????? System.out.print("请输入出版社:");

??????? String publisher = input.nextLine();

???????

??????? Book book = new Book(name, author, publisher, 0);

??????? books.add(book);

??????? System.out.println("添加成功!");

??? }

??? private static void removeBook() {

??????? System.out.print("请输入要删除的书名:");

??????? String name = input.nextLine();

??????? boolean found = false;

??????? for (Book book : books) {

??????????? if (book.getName().equals(name)) {

??????????????? books.remove(book);

??????????????? found = true;

??????????????? System.out.println("删除成功!");

??????????????? break;

??????????? }

??????? }

??????? if (!found) {

??????????? System.out.println("未找到该图书!");

??????? }

??? }

??? private static void searchBook() {

??????? System.out.print("请输入要查找的书名:");

??????? String name = input.nextLine();

??????? boolean found = false;

??????? for (Book book : books) {

??????????? if (book.getName().equals(name)) {

??????????????? System.out.println("信息如下:");

??????????????? System.out.println(book);

??????????????? found = true;

??????????????? break;

??????????? }

??????? }

??? ?

??????? if (!found) {

??????????? System.out.println("无信息!");

??????? }

??? }

//所有

?? private static void borrowBook() {

??? ? ?System.out.print("书名\t"+"作者\t"+"出版社\t");

??? ? ?for (Book book : books) {

???????????? if (book.getName().equals(name)) {

???????????????? System.out.println("信息如下:");

???????????????? System.out.println(book);

???????????????? found = true;

???????????????? break;

???????????? }

???????? }

?

if (!found) {

????? System.out.println("无信息!");

?? }

???????

??????

??? ????

?

}

}

???

?

效果

文章来源:https://blog.csdn.net/m0_64798714/article/details/134996270
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。