好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java快速实现图书管理基本功能

前言

今天的内容主要是利用前面所学的知识点:类,抽象类,封装,继承,多态,接口等进行的一个简单的代码练习。

主要要求:

1、用户登录

2、管理端

查找图书 新增图书 删减图书 显示图书列表 退出系统

3、用户端

查找图书 借阅图书 归还图书 退出系统

我们可以将以上内容分为对书的创建初始化,对用户的操作,对书的操作。

主函数

Main:(对所有流程进行整合)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

import book.BookList;

import User.NormalUser;

import User.AdminUser;

import User.user;

import java.util.Scanner;

public class Main {

     public static user login(){

         System.out.print( "请输入你的姓名:" );

         Scanner scanner= new Scanner(System.in);

         String name=scanner.nextLine();

         System.out.println( "请选择你的身份:1->管理员  0->普通用户" );

         int choice=scanner.nextInt();

         if (choice== 1 ){

             return new AdminUser(name);

         } else {

             return new NormalUser(name);

         }

     }

     public static void main(String[] args) {

         BookList bookList = new BookList();

         user us = login(); //向上转型

         while ( true ) {

             int choice = us.menu(); //多态绑定

             us.doWork(choice, bookList);

         }

     }

}

书的创建

Book:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

package book;

//创建图书

public class Book {

     private String name; //书名

     private String author; //书的作者

     private String type; //书的类型

     private int price; //书的价格

     boolean isBorrowed; //书是否被借出

//在这里我们就不对isBorrowed进行构造,在最后打印的时候对他进行判断就好

     public Book(String name, String author, String type, int price) {

         this .name = name;

         this .author = author;

         this .type = type;

         this .price = price;

     }

     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 getType() {

         return type;

     }

     public void setType(String type) {

         this .type = type;

     }

     public int getPrice() {

         return price;

     }

     public void setPrice( int price) {

         this .price = price;

     }

     public boolean isBorrowed() {

         return isBorrowed;

     }

     public void setBorrowed( boolean borrowed) {

         isBorrowed = borrowed;

     }

     @Override

     public String toString() {

         return "Book{" +

                 "name='" + name + '\ '' +

                 ", author='" + author + '\ '' +

                 ", type='" + type + '\ '' +

                 ", price=" + price +

                 ((isBorrowed== true )? " 已被借出" : " 未被借出" ) +

                 '}' ;

     }

}

BookList:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

package book;

//创建书架

public class BookList {

    private Book[] books= new Book[ 10 ]; //创建一个数组当书架放书,假设一共可以放10本

     public int usedSize; //书架上有效放书数

     public BookList(){ //假设一开始书架上就放了三本书

         books[ 0 ]= new Book( "红楼梦" , "曹雪芹" , "文学" , 55 );

         books[ 1 ]= new Book( "西游记" , "吴承恩" , "文学" , 65 );

         books[ 2 ]= new Book( "水浒传" , "施耐庵" , "文学" , 23 );

         usedSize= 3 ;

     }

     public int getUsedSize(){

         return usedSize;

     }

     public void setUsedSize( int usedSize){

         this .usedSize=usedSize;

     }

     //获取一本书的位置

     public Book getpos( int pos){

         return this .books[pos];

     }

     //在下标为pos的位置上放一本书(添加)

     public void setBook( int pos,Book book){

         this .books[pos]=book;

     }

}

对用户的操作

创建用户user类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package User;

import book.BookList;

import operation.IOperation;

 

public abstract class user {

     protected String name;

     protected   IOperation[] iOperations; //创建操作类数组,通过返回的数字用于对书的操作

     public user(String name){

         this .name=name;

     }

     public abstract int menu(); //用于动态绑定

     public void doWork( int choice, BookList bookList){

         iOperations[choice].work(bookList);

     }

}

创建管理员用户类AdminUser ,是 user 的子类.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

package User;

import operation.*;

import java.util.Scanner;

public class AdminUser extends user {

     public AdminUser(String name) {

         super (name);

         this .iOperations= new IOperation[]{ //初始化数组,将菜单上的数字与对应的操作放好

                 new ExitOperation(),

                 new FindOperation(),

                 new AddOperation(),

                 new DelOperation(),

                 new DisplayOperation()

         };

     }

     public int menu(){

         System.out.println( "========管理员的菜单========" );

         System.out.println( "欢迎" + this .name+ "来到图书馆" );

         System.out.println( "1.查找图书" );

         System.out.println( "2.新增图书" );

         System.out.println( "3.删减图书" );

         System.out.println( "4.显示图书" );

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

         System.out.println( "===========================" );

         System.out.print( "请选择你要的操作: " );

         Scanner scanner= new Scanner(System.in);

         int choice=scanner.nextInt();

         return choice; //返回选择的数字

     }

}

创建普通用户类NormalUser, 是 user 的子类.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

package User;

import operation.*;

import java.util.Scanner;

public class NormalUser extends user {

     public NormalUser(String name){

         super (name);

         this .iOperations= new IOperation[]{

              new ExitOperation(),

              new FindOperation(),

              new BorrowOperation(),

              new ReturnOperation()

         };

     }

     public int menu(){

         System.out.println( "========普通用户的菜单========" );

         System.out.println( "欢迎" + this .name+ "来到图书馆" );

         System.out.println( "1.查找图书" );

         System.out.println( "2.借阅图书" );

         System.out.println( "3.归还图书" );

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

         System.out.println( "============================" );

         System.out.print( "请选择你要的操作: " );

         Scanner scanner= new Scanner(System.in);

         int choice=scanner.nextInt();

         return choice;

     }

}

对书的操作

创建接口 IOperation:

?

1

2

3

4

5

6

7

package operation;

import book.BookList;

import java.util.Scanner;

public interface IOperation {

     Scanner scanner= new Scanner(System.in);

     void work(BookList bookList);

}

创建增加图书类AddOperation,继承接口 IOperation

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

package operation;

import book.Book;

import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{

     public void work(BookList bookList){

         System.out.println( "新增图书!" );

         System.out.println( "请输入图书的名字:" );

         String name = scanner.nextLine();

         System.out.println( "请输入图书的作者:" );

         String author = scanner.nextLine();

         System.out.println( "请输入图书的类型:" );

         String type = scanner.nextLine();

         System.out.println( "请输入图书的价格:" );

         int price = scanner.nextInt();

         Book book= new Book(name,author,type,price); //新创建一本书

         int size= bookList.getUsedSize();

         bookList.setBook(size,book);

         bookList.setUsedSize(size+ 1 );

         System.out.println( "新增图书成功" );

     }

}

创建删减图书类DelOperation,继承接口 IOperation

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

package operation;

import book.Book;

import book.BookList;

public class DelOperation implements IOperation{

     public void work(BookList bookList){

         System.out.println( "删减图书" );

         System.out.println( "请输入要删减图书的名字:" );

         String name=scanner.nextLine();

         int size= bookList.getUsedSize();

         int index= 0 ;

         int i= 0 ;

         for (i = 0 ; i <size ; i++) {

             Book book = bookList.getpos(i);

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

                index=i;

                break ;

             }

         }

         if (i>=size){

             System.out.println( "没有你要删除的图书" );

         }

         //删除就是要将j+1下标的信息放进j下标里面信息去,这样子j里面的信息就被删除了

         for ( int j = index; j < size- 1 ; j++) {

          Book book=bookList.getpos(j+ 1 ); //获取j+1下标放的书的信息给j下标

          bookList.setBook(j,book);

         }

         bookList.setBook(size, null ); //删除之后,把最后一个下标信息置为null

         bookList.setUsedSize(size- 1 ); //下标减1

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

     }

}

创建查找图书类FindOperation,继承接口 IOperation

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package operation;

import book.BookList;

import book.Book;

public class FindOperation implements IOperation{

     public void work(BookList bookList){

         System.out.println( "查找图书!" );

         System.out.println( "请输入要查找书的名字" );

         String name = scanner.nextLine();

         int size = bookList.getUsedSize();

         for ( int i = 0 ; i < size; i++) {

             Book book = bookList.getpos(i); //找到这本书的信息

             if (name.equals(book.getName())) { //比较名字是否相同

                 System.out.println( "找到了这本书,信息如下:" );

                 System.out.println(book);

                 return ;

             }

         }

         System.out.println( "没有找到这本书!" );

     }

}

创建显示图书列表类DisplayOperation,继承接口 IOperation

?

1

2

3

4

5

6

7

8

9

10

11

12

package operation;

import book.Book;

import book.BookList;

public class DisplayOperation implements IOperation{

     public void work(BookList bookList){

         System.out.println( "显示图书" );

         for ( int i= 0 ;i<bookList.usedSize;i++){

             Book book=bookList.getpos(i);

             System.out.println(book);

         }

     }

}

创建借阅图书列表类BorrowOperation,继承接口 IOperation

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package operation;

import book.Book;

import book.BookList;

public class BorrowOperation implements IOperation{

     public void work(BookList bookList){

         System.out.println( "借阅图书" );

         System.out.println( "请输入你要借阅的图书的名字" );

         String name=scanner.nextLine();

         int size= bookList.getUsedSize();

         for ( int i= 0 ;i<size;i++){

             Book book = bookList.getpos(i);

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

                 book.setBorrowed( true );

                 System.out.println( "借阅成功" );

                 System.out.println(book);

                 return ;

             }

         }

         System.out.println( "没有你要借阅的书" );

     }

}

创建归还图书列表类ReturnOperation,继承接口 IOperation

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package operation;

import book.Book;

import book.BookList;

public class ReturnOperation implements IOperation{

     public void work(BookList bookList){

         System.out.println( "归还图书" );

         System.out.println( "请输入你要归还的图书的名字" );

         String name=scanner.nextLine();

         int size= bookList.getUsedSize();

         for ( int i= 0 ;i<size;i++){

             Book book = bookList.getpos(i);

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

                 book.setBorrowed( true );

                 System.out.println( "归还成功" );

                 System.out.println(book);

                 return ;

             }

         }

         System.out.println( "没有你要归还的书" );

     }

}

创建退出系统类ExitOperation,继承接口 IOperation

?

1

2

3

4

5

6

7

8

package operation;

import book.BookList;

public class ExitOperation implements IOperation{

     public void work(BookList bookList){

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

         System.exit( 0 );

     }

}

到此这篇关于Java快速实现图书管理基本功能的文章就介绍到这了,更多相关Java图书管理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/m0_65673419/article/details/124583875

查看更多关于Java快速实现图书管理基本功能的详细内容...

  阅读:16次