• JavaSE期末经典项目—>>图书管理系统


    前言:

    图书管理是许多高校的期末经典项目,在学完了类,对象,抽象类,接口,继承,封装和多态及组合等知识后,要及时的练手,以此来巩固这部分的内容,Java中基本都是在和对象这些的打交道,所以利用这些知识来写一个图书管理系统。

    JavaSE期末经典项目—>>图书管理系统

    项目简介:

    该项目为图书管理系统,实现用户的区分,包括管理员和普通用户,管理员的行为包括:添加图书,删除图书,查找图书,显示图书和退出程序等,普通用户的用法包括:查找图书,借阅图书,归还图书和退出程序。

    思路简介:

    利用类,对象,抽象类,接口,继承,封装和多态及组合等知识,将对象提取出来,对象----用户和书,用户可以分为管理员和普通用户,所以设置一个抽象类User,管理员和普通用户继承该抽象类,由于书还要包括书架,所以书和书架分两个类,为了实现对应的操作,并且是操作的方法一致,所以要构造一个操作的接口,其他的操作用类来实现该接口,就完成了对应的操作。具体实现方式见详细代码!

    详细代码: 

    book包:

    Book类:

    1. package book;
    2. public class Book {
    3. private String name;//书名
    4. private String author;//作者
    5. private int price;//价格
    6. private String type;//类型
    7. private boolean isBorrowed;//是否被借出
    8. public Book(String name, String author, int price, String type) {
    9. this.name = name;
    10. this.author = author;
    11. this.price = price;
    12. this.type = type;
    13. }
    14. public String getName() {
    15. return name;
    16. }
    17. public void setName(String name) {
    18. this.name = name;
    19. }
    20. public String getAuthor() {
    21. return author;
    22. }
    23. public void setAuthor(String author) {
    24. this.author = author;
    25. }
    26. public int getPrice() {
    27. return price;
    28. }
    29. public void setPrice(int price) {
    30. this.price = price;
    31. }
    32. public String getType() {
    33. return type;
    34. }
    35. public void setType(String type) {
    36. this.type = type;
    37. }
    38. public boolean isBorrowed() {
    39. return isBorrowed;
    40. }
    41. public void setBorrowed(boolean borrowed) {
    42. isBorrowed = borrowed;
    43. }
    44. @Override
    45. public String toString() {
    46. return "Book{" +
    47. "name='" + name + '\'' +
    48. ", author='" + author + '\'' +
    49. ", price=" + price +
    50. ", type='" + type + '\'' +
    51. ((isBorrowed==true)?"该书已借出":"该书未借出")+
    52. '}';
    53. }
    54. }

    BookList类:

    1. package book;
    2. public class BookList {
    3. public Book[] books=new Book[10];
    4. public int usedSize;//书架上书的数量
    5. /**
    6. *
    7. * 事先通过构造方法放入3本书
    8. */
    9. public BookList(){
    10. books[0]=new Book("三国演义","罗贯中",89,"小说");
    11. books[1]=new Book("西游记","吴承恩",79,"小说");
    12. books[2]=new Book("红楼梦","曹雪芹",83,"小说");
    13. this.usedSize=3;
    14. }
    15. public int getUsedSize() {
    16. return usedSize;
    17. }
    18. public void setUsedSize(int usedSize) {
    19. this.usedSize = usedSize;
    20. }
    21. //下标
    22. public Book getPos(int pos){
    23. return books[pos];
    24. }
    25. /**
    26. *
    27. * 存储一本书到指定位置
    28. * @param book
    29. * @param pos 在这里指的是当前最后可以存储书的位置
    30. */
    31. public void setBooks(Book book,int pos){
    32. books[pos]=book;
    33. }
    34. }

    operations包:

    IOperation接口:

    1. package operations;
    2. //接口
    3. import book.BookList;
    4. public interface IOperation {
    5. void work(BookList bookList);
    6. }

    AddOperation类:

    1. package operations;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class AddOperation implements IOperation {
    6. @Override
    7. public void work(BookList bookList) {
    8. System.out.println("添加图书!");
    9. System.out.println("请输入你要添加的图书的名字:");
    10. Scanner scanner=new Scanner(System.in);
    11. String name=scanner.nextLine();
    12. System.out.println("请输入你要添加的图书的作者:");
    13. String author=scanner.nextLine();
    14. System.out.println("请输入你要添加的图书的价格:");
    15. int price=scanner.nextInt();
    16. scanner.nextLine();//读取回车
    17. System.out.println("请输入你要添加的图书的类型:");
    18. String type=scanner.nextLine();
    19. Book book=new Book(name,author,price,type);
    20. //获取当前可以存放书的位置
    21. int currentSize=bookList.getUsedSize();
    22. //书放到指定位置
    23. bookList.setBooks(book,currentSize);
    24. //书的数量加1
    25. bookList.setUsedSize(currentSize+1);
    26. System.out.println("添加成功!");
    27. }
    28. }

    BorrowedOperation类:

    1. package operations;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class BorrowedOperation implements IOperation {
    6. @Override
    7. public void work(BookList bookList) {
    8. System.out.println("借阅图书!");
    9. System.out.println("请输入你要借阅的图书的名字:");
    10. Scanner scanner=new Scanner(System.in);
    11. String name=scanner.nextLine();
    12. int currentSize=bookList.getUsedSize();
    13. for (int i = 0; i < currentSize; i++) {
    14. Book book=bookList.getPos(i);
    15. if(name.equals(book.getName())){
    16. book.setBorrowed(true);
    17. System.out.println("借阅成功!");
    18. return;
    19. }
    20. }
    21. System.out.println("没有你要借阅的书!");
    22. }
    23. }

    DelOperation类:

    1. package operations;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class DelOperation implements IOperation{
    6. @Override
    7. public void work(BookList bookList) {
    8. System.out.println("删除图书!");
    9. System.out.println("请输入你要删除的图书的名字:");
    10. Scanner scanner=new Scanner(System.in);
    11. String name=scanner.nextLine();
    12. //遍历数组,看是否有你要删除的图书的下标
    13. int index=-1;
    14. int currentSize=bookList.getUsedSize();
    15. for (int i = 0; i < currentSize; i++) {
    16. Book book=bookList.getPos(i);
    17. if(name.equals(book.getName())){
    18. index=i;
    19. break;
    20. }
    21. }
    22. if(index==-1){
    23. System.out.println("没有你要删除的这本书!");
    24. return;
    25. }
    26. for (int i = index; i 1 ; i++) {
    27. Book book=bookList.getPos(i+1);
    28. bookList.setBooks(book,i);
    29. }
    30. bookList.setBooks(null,currentSize-1);
    31. bookList.setUsedSize(currentSize-1);
    32. System.out.println("删除成功!");
    33. }
    34. }

     DisplayOperation类:

    1. package operations;
    2. import book.Book;
    3. import book.BookList;
    4. public class DisplayOperation implements IOperation {
    5. @Override
    6. public void work(BookList bookList) {
    7. System.out.println("显示所有图书!");
    8. int currentSize=bookList.getUsedSize();
    9. for (int i = 0; i < currentSize; i++) {
    10. Book book=bookList.getPos(i);
    11. System.out.println(book);
    12. }
    13. }
    14. }

    ExitOperation类:

    1. package operations;
    2. import book.BookList;
    3. public class ExitOperation implements IOperation{
    4. @Override
    5. public void work(BookList bookList) {
    6. System.out.println("退出程序!");
    7. System.exit(0);//退出
    8. }
    9. }

    ReturnOperation类:

    1. package operations;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class ReturnOperation implements IOperation{
    6. @Override
    7. public void work(BookList bookList) {
    8. System.out.println("归还图书!");
    9. System.out.println("请输入你要归还的图书的名字:");
    10. Scanner scanner=new Scanner(System.in);
    11. String name=scanner.nextLine();
    12. int currentSize=bookList.getUsedSize();
    13. for (int i = 0; i < currentSize; i++) {
    14. Book book=bookList.getPos(i);
    15. if(name.equals(book.getName())){
    16. book.setBorrowed(false);
    17. System.out.println("归还图书成功!");
    18. return;
    19. }
    20. }
    21. System.out.println("没有你要归还的书!");
    22. }
    23. }

    SearchOperation类:

    1. package operations;
    2. import book.Book;
    3. import book.BookList;
    4. import java.util.Scanner;
    5. public class SearchOperation implements IOperation{
    6. @Override
    7. public void work(BookList bookList) {
    8. System.out.println("查找图书!");
    9. System.out.println("请输入你要查找的图书的名字:");
    10. Scanner scanner=new Scanner(System.in);
    11. String name=scanner.nextLine();
    12. int currentSize=bookList.getUsedSize();
    13. for (int i = 0; i < currentSize; i++) {
    14. Book book=bookList.getPos(i);
    15. if(name.equals(book.getName())){
    16. System.out.println("找到啦这本书!");
    17. System.out.println(book);
    18. return;
    19. }
    20. }
    21. System.out.println("没有这本书!");
    22. }
    23. }

    user包: 

    User类:

    1. package user;
    2. import book.BookList;
    3. import operations.IOperation;
    4. //抽象类
    5. public abstract class User {
    6. protected String name;
    7. protected IOperation[] iOperations;//接口数组
    8. public User(String name){
    9. this.name=name;
    10. }
    11. public abstract int menu();
    12. public void doOperation(int choice, BookList bookList){
    13. iOperations[choice].work(bookList);
    14. }
    15. }

    AdminUser类:

    1. package user;
    2. import operations.*;
    3. import java.util.Scanner;
    4. public class AdminUser extends User {
    5. public AdminUser(String name) {
    6. super(name);
    7. //接口数组
    8. this.iOperations=new IOperation[]{
    9. new ExitOperation(),
    10. new SearchOperation(),
    11. new AddOperation(),
    12. new DelOperation(),
    13. new DisplayOperation()
    14. };
    15. }
    16. public int menu(){
    17. System.out.println("*********************************");
    18. System.out.println( "hello ! "+name+"欢迎登入图书管理系统!");
    19. System.out.println("1.查找图书");
    20. System.out.println("2.新增图书");
    21. System.out.println("3.删除图书");
    22. System.out.println("4.显示图书");
    23. System.out.println("0.退出系统");
    24. System.out.println("*********************************");
    25. System.out.println("请输入你的操作:");
    26. Scanner scanner=new Scanner(System.in);
    27. int choice=scanner.nextInt();
    28. return choice;
    29. }
    30. }

    NormalUser类:

    1. package user;
    2. import operations.*;
    3. import java.util.Scanner;
    4. public class NormalUser extends User {
    5. public NormalUser(String name) {
    6. super(name);
    7. this.iOperations=new IOperation[]{
    8. new ExitOperation(),
    9. new SearchOperation(),
    10. new BorrowedOperation(),
    11. new ReturnOperation(),
    12. };
    13. }
    14. public int menu(){
    15. System.out.println("*********************************");
    16. System.out.println( "hello ! "+name+"欢迎登入图书管理系统!");
    17. System.out.println("1.查找图书");
    18. System.out.println("2.借阅图书");
    19. System.out.println("3.归还图书");
    20. System.out.println("0.退出系统");
    21. System.out.println("*********************************");
    22. Scanner scanner=new Scanner(System.in);
    23. int choice=scanner.nextInt();
    24. return choice;
    25. }
    26. }

    Main--管理器:

    1. import book.BookList;
    2. import user.AdminUser;
    3. import user.NormalUser;
    4. import user.User;
    5. import java.util.Scanner;
    6. public class Main {
    7. public static User login(){
    8. System.out.println("请输入你的姓名:");
    9. Scanner scanner=new Scanner(System.in);
    10. String userName=scanner.nextLine();
    11. System.out.println("请输入你的身份:1-》管理员,0-》普通用户");
    12. int choice= scanner.nextInt();
    13. if(choice==1){
    14. return new AdminUser(userName);//向上转型
    15. }else{
    16. return new NormalUser(userName);//向上转型
    17. }
    18. }
    19. public static void main(String[] args) {
    20. //1、准备数据
    21. BookList bookList=new BookList();
    22. //2、登录,
    23. User user= login();
    24. while(true){
    25. int choice= user.menu();//多态,动态绑定
    26. user.doOperation(choice,bookList);
    27. }
    28. }
    29. }

    效果演示:

    管理员:

    普通用户:

    码云上传地址:图书管理系统 

    谢谢观看!!! 

  • 相关阅读:
    Redis常用命令补充和持久化
    从零开始:使用ChatGPT快速创作引人入胜的博客内容
    雷军的开源情怀
    【python】算法与数据结构作业记录分析
    区块链学姐:6月27日 以太坊久居1200大关下,多头量能已开始受阻?
    【web前端特效源码】使用HTML5+CSS3制作一个会动的loading加载动画效果~~适合初学者~超简单~ |前端开发|IT软件
    2020华数杯全国大学生数学建模竞赛B题-基于混合模拟退火算法的三维零件的切割模型与计算(附MATLAB代码)
    「技术分享」强烈推荐小白必看的Java反射
    深入解析MySQL死锁:原因、检测与解决方案
    ThreadPoolExecutor线程池原理
  • 原文地址:https://blog.csdn.net/m0_67995737/article/details/126278515