• java图书管理系统


    分三个包,分别存放了使用者的信息,书籍信息,以及进行的操作方法 

    一、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 isLend;//是否被借出
    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 isLend() {
    39. return isLend;
    40. }
    41. public void setLend(boolean lend) {
    42. isLend = lend;
    43. }
    44. @Override
    45. public String toString() {
    46. return "book{" +
    47. "name='" + name + '\'' +
    48. ", author='" + author + '\'' +
    49. ", price=" + price +
    50. ", type='" + type + '\'' +
    51. ", isLend=" + isLend +
    52. '}';
    53. }
    54. }

    再来创建一个书架,存放书籍和记录书的个数

    1. package book;
    2. public class BookList {
    3. private Book[] books = new Book[10];
    4. private int useSize;//书架存放书的个数
    5. public BookList(){
    6. books[0]=new Book("三国演义","罗贯中",10,"小说");
    7. books[1]=new Book("水浒传","施耐庵",10,"小说");
    8. books[2]=new Book("红楼梦","曹雪芹",10,"小说");
    9. this.useSize = 3;
    10. }
    11. public int getUseSize() {
    12. return useSize;
    13. }
    14. public void setUseSize(int useSize) {
    15. this.useSize = useSize;
    16. }
    17. public Book getBook(int pos){
    18. return books[pos];
    19. }//得到某本书
    20. public boolean isFull(){
    21. if(this.useSize == books.length){
    22. return true;
    23. }
    24. return false;
    25. }//判断是否满了
    26. public void setBook(int pos,Book book){
    27. books[pos] = book;
    28. }
    29. }

    二、user 

     之后再来实现使用者User类,存放了使用者的姓名和操作选择

    1. package user;
    2. import book.BookList;
    3. import operation.IOperation;
    4. public abstract class User {
    5. protected String name;
    6. protected IOperation[] operations;
    7. public User(String name){
    8. this.name = name;
    9. }
    10. public abstract int menu();//抽象类,在子类中实现对应的选择菜单
    11. public void doIoperation(int choice,BookList bookList){
    12. this.operations[choice].work(bookList);
    13. }//对书架进行相应的操作
    14. }

    使用者分两类分别是管理员还有普通用户,两者都是使用者,所以需要继承User类

    管理员

    1. package user;
    2. import operation.*;
    3. import java.util.Scanner;
    4. public class AdminUser extends User{
    5. public AdminUser(String name) {
    6. super(name);
    7. this.operations = new IOperation[]{new ExitOperation(),new FindOperation(),
    8. new AddOperation(),new DelOperation(),new ShowOperation()};
    9. }
    10. public int menu(){
    11. System.out.println("**********管理员菜单*************");
    12. System.out.println("1、查找图书");
    13. System.out.println("2、新增图书");
    14. System.out.println("3、删除图书");
    15. System.out.println("4、显示图书");
    16. System.out.println("0、退出系统");
    17. System.out.println("*********************************");
    18. System.out.println("请输入你的操作:");
    19. Scanner scanner = new Scanner(System.in);
    20. int choice = scanner.nextInt();
    21. return choice;
    22. }
    23. }

     普通用户

    1. package user;
    2. import operation.*;
    3. import java.util.Scanner;
    4. public class NormalUser extends User{
    5. public NormalUser(String name) {
    6. super(name);
    7. this.operations = new IOperation[]{new ExitOperation(),new FindOperation(),new BorrowedOperation(),
    8. new ReturnOperation()};
    9. }
    10. public int menu(){
    11. System.out.println("**********普通用户菜单*************");
    12. System.out.println("1、查找图书");
    13. System.out.println("2、借阅图书");
    14. System.out.println("3、归还图书");
    15. System.out.println("0、退出系统");
    16. System.out.println("*********************************");
    17. System.out.println("请输入你的操作:");
    18. Scanner scanner = new Scanner(System.in);
    19. int choice = scanner.nextInt();
    20. return choice;//返回操作选择
    21. }
    22. }

     三、operation

    先来实现接口IOperation

    1. package operation;
    2. import book.BookList;
    3. public interface IOperation {
    4. void work(BookList bookList);
    5. }

     查找图书

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

    新增图书

    1. package operation;
    2. import book.BookList;
    3. import java.util.Scanner;
    4. import book.Book;
    5. public class AddOperation implements IOperation{
    6. public void work (BookList bookList){
    7. System.out.println("新增图书......");
    8. if(bookList.isFull()){
    9. System.out.println("书架满了 不能新增了");
    10. }
    11. Scanner scanner = new Scanner(System.in);
    12. System.out.println("请输入你要新增的图书的书名:");
    13. String bookName = scanner.nextLine();
    14. System.out.println("请输入你要新增的图书的作者:");
    15. String author = scanner.nextLine();
    16. System.out.println("请输入你要新增的图书的价格:");
    17. int price = scanner.nextInt();
    18. System.out.println("请输入你要新增的图书的类型:");
    19. String blank = scanner.nextLine();//拿取因输入price的'\n'来进行下一步的输入类型操作
    20. String type = scanner.nextLine();
    21. Book book = new Book(bookName,author,price,type);
    22. int currentSize =bookList.getUseSize();
    23. bookList.setBook(currentSize,book);
    24. bookList.setUseSize(currentSize+1);
    25. System.out.println("新增图书成功");
    26. }
    27. }

    删除图书

    1. package operation;
    2. import book.BookList;
    3. import java.util.Scanner;
    4. import book.Book;
    5. public class DelOperation implements IOperation{
    6. public void work(BookList bookList) {
    7. System.out.println("删除图书..........");
    8. System.out.println("请输入你要删除的图书的书名:");
    9. Scanner scanner = new Scanner(System.in);
    10. String bookName = scanner.nextLine();
    11. int currentSize =bookList.getUseSize();
    12. int pos = -1;
    13. int i =0;
    14. for(i=0;i
    15. Book book = bookList.getBook(i);
    16. if(book.getName().equals(bookName)){
    17. //找到记录下标
    18. pos = i;
    19. break;
    20. }
    21. }
    22. if(i>=currentSize){
    23. System.out.println("没有找到你要删除的书");
    24. return;
    25. }
    26. //开始删除
    27. int j = 0;
    28. for(j=pos;j1 ; j++){
    29. Book book = bookList.getBook(j+1);
    30. bookList.setBook(j,book);
    31. }
    32. bookList.setUseSize(currentSize-1);
    33. bookList.setBook(currentSize-1,null);
    34. System.out.println("删除成功");
    35. }
    36. }

    显示图书

    1. package operation;
    2. import book.BookList;
    3. import book.Book;
    4. public class ShowOperation implements IOperation{
    5. public void work(BookList bookList){
    6. System.out.println("显示图书......");
    7. int currentSize = bookList.getUseSize();
    8. for(int i = 0;i
    9. Book book = bookList.getBook(i);
    10. System.out.println(book);
    11. }
    12. }
    13. }

    借阅图书

    1. package operation;
    2. import book.BookList;
    3. import book.Book;
    4. import java.util.Scanner;
    5. public class BorrowedOperation implements IOperation {
    6. public void work(BookList bookList) {
    7. System.out.println("借阅图书.......");
    8. System.out.println("请输入你要借阅的图书名:");
    9. Scanner scanner = new Scanner(System.in);
    10. String bookName = scanner.nextLine();
    11. int currentSize = bookList.getUseSize();
    12. int i = 0;
    13. for (i = 0; i < currentSize; i++) {
    14. Book book = bookList.getBook(i);
    15. if (book.getName().equals(bookName)) {
    16. if (book.isLend()) {
    17. System.out.println("图书已经被借出 借阅失败");
    18. return;
    19. }
    20. book.setLend(true);
    21. System.out.println("借阅成功");
    22. return;
    23. }
    24. }
    25. System.out.println("没有找到你要借阅的书");
    26. }
    27. }

    归还图书

    1. package operation;
    2. import book.BookList;
    3. import book.Book;
    4. import java.util.Scanner;
    5. public class ReturnOperation implements IOperation{
    6. public void work(BookList bookList){
    7. System.out.println("归还图书.........");
    8. System.out.println("请输入你要归还的图书名:");
    9. Scanner scanner = new Scanner(System.in);
    10. String bookName = scanner.nextLine();
    11. int currentSize = bookList.getUseSize();
    12. for(int i=0;i
    13. Book book = bookList.getBook(i);
    14. if(book.getName().equals(bookName)){
    15. book.setLend(false);
    16. System.out.println("归还成功");
    17. return;
    18. }
    19. }
    20. System.out.println("归还失败");
    21. }
    22. }

    退出系统

    1. package operation;
    2. import book.BookList;
    3. public class ExitOperation implements IOperation{
    4. public void work(BookList bookList){
    5. System.out.println("退出系统..........");
    6. int currentSize =bookList.getUseSize();
    7. for(int i=0;i
    8. bookList.setBook(i,null);
    9. }//将书架置为空
    10. System.exit(0);
    11. }
    12. }

     四、Library

    登录

    1. import book.BookList;
    2. import user.AdminUser;
    3. import user.NormalUser;
    4. import user.User;
    5. import java.util.Scanner;
    6. public class Library {
    7. //AdminUser和NormalUser都继承User,所以返回值是User
    8. public static User Login(){
    9. Scanner scanner = new Scanner(System.in);
    10. System.out.println("请输入你的姓名:");
    11. String name = scanner.nextLine();
    12. System.out.println("请输入你的身份:1、管理员 2、普通用户");
    13. int choice = scanner.nextInt();
    14. if(choice == 1){
    15. AdminUser adminUser = new AdminUser(name);
    16. return adminUser;
    17. }else{
    18. NormalUser normalUser = new NormalUser(name);
    19. return normalUser;
    20. }
    21. }
    22. public static void main(String[] args) {
    23. BookList bookList = new BookList();//创建书架
    24. User user = Login();//根据用户选择看是管理员还是普通用户
    25. while(true) {
    26. int choice = user.menu();//可能是管理员或普通用户的功能,需要区分组织
    27. user.doIoperation(choice, bookList);
    28. }
    29. }
    30. }

  • 相关阅读:
    编译执行JAVA含中文字符串文件,终端输出乱码问题处理
    CAS部署使用以及登录成功跳转地址
    Rabbitmq参数优化
    [模型]拉格朗日插值法
    CopyOnWriteArrayList源码分析
    上海亚商投顾:沪指探底回升 华为汽车概念股集体大涨
    服务器优化
    GitHub的使用
    QTday5
    FreeRTOS 任务的创建与删除
  • 原文地址:https://blog.csdn.net/2301_79783967/article/details/136765936