当前位置:   article > 正文

图书管理系统

图书管理系统

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. private Book[] books = new Book[10];
  4. public int usedSize;
  5. public Booklist(){
  6. this.books[0] = new Book("三国演义","罗贯中",10,"小说");
  7. this.books[1] = new Book("西游记","吴承恩",13,"小说");
  8. this.books[2] = new Book("红楼梦","曹雪芹",11,"小说");
  9. this.usedSize = 3;
  10. }
  11. public Book getBook(int pos) {
  12. return books[pos];
  13. }
  14. public void setBook(int pos,Book book) {
  15. this.books[pos] = book;
  16. }
  17. public int getUsedSize() {
  18. return usedSize;
  19. }
  20. public void setUsedSize(int usedSize) {
  21. this.usedSize = usedSize;
  22. }
  23. public Book[] getBooks(){
  24. return books;
  25. }
  26. }

ioperations包

IOPeration接口

  1. package ioperations;
  2. import book.Booklist;
  3. public interface IOPeration {
  4. void work(Booklist booklist);
  5. }

AddOperation类

  1. package ioperations;
  2. import book.Book;
  3. import book.Booklist;
  4. import java.util.Scanner;
  5. public class AddOperation implements IOPeration{
  6. public void work(Booklist booklist){
  7. System.out.println("新增图书");
  8. //判断书架是否已经满了
  9. int currentSize = booklist.getUsedSize();
  10. if( currentSize == booklist.getBooks().length){
  11. System.out.println("书架满了,不能放了.....");
  12. return;
  13. }
  14. //构建图书
  15. Scanner scanner = new Scanner(System.in);
  16. System.out.println("输入你的书名: ");
  17. String name = scanner.nextLine();
  18. System.out.println("请输入你的作者: ");
  19. String author = scanner.nextLine();
  20. System.out.println("请输入类型: ");
  21. String type = scanner.nextLine();
  22. System.out.println("请输入价格: ");
  23. int price = scanner.nextInt();
  24. Book newBook = new Book(name,author,price,type);
  25. //判断是否已经存在
  26. for (int i = 0; i < currentSize ; i++) {
  27. Book book = booklist.getBook(i);
  28. if(book.getName().equals(name)){
  29. System.out.println("这本书已有不能插入");
  30. return;
  31. }
  32. }
  33. //新增图书
  34. booklist.setBook(currentSize,newBook);
  35. booklist.setUsedSize(currentSize+1);
  36. System.out.println("新增图书成功!!!");
  37. }
  38. }

BorrowOperation类 

  1. package ioperations;
  2. import book.Book;
  3. import book.Booklist;
  4. import java.util.Scanner;
  5. public class BorrowOperation implements IOPeration{
  6. public void work(Booklist booklist){
  7. System.out.println("借阅图书");
  8. Scanner scanner = new Scanner(System.in);
  9. System.out.println("输入你要借阅的书");
  10. String name = scanner.nextLine();
  11. int currentSize = booklist.getUsedSize();
  12. for (int i = 0; i < currentSize; i++) {
  13. Book book = booklist.getBook(i);
  14. if(book.getName().equals(name)){
  15. book.setBorrowed(true);
  16. System.out.println("借阅成功");
  17. return;
  18. }
  19. }
  20. System.out.println("没有你要找的书");
  21. }
  22. }

 ExitOperation

  1. package ioperations;
  2. import book.Booklist;
  3. public class ExitOperation implements IOPeration{
  4. public void work(Booklist booklist){
  5. System.out.println("退出系统");
  6. System.exit(0);
  7. }
  8. }

 DelOperation类

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

 FindOperation 类 

  1. package ioperations;
  2. import book.Book;
  3. import book.Booklist;
  4. import java.util.Scanner;
  5. public class DelOperation implements IOPeration{
  6. public void work(Booklist booklist) {
  7. System.out.println("删除图书");
  8. Scanner scanner = new Scanner(System.in);
  9. System.out.println("输入你要删除的书");
  10. String name = scanner.nextLine();
  11. int currentSize = booklist.getUsedSize();
  12. int pos = -1;
  13. //找到这本书
  14. int i = 0;
  15. for ( ; i < currentSize; i++) {
  16. Book book = booklist.getBook(i);
  17. if(book.getName().equals(name)){
  18. pos = i;
  19. break;
  20. }
  21. }
  22. if(i == currentSize) {
  23. System.out.println("没有你要删除的图书!");
  24. return;
  25. }
  26. //删除
  27. for (int j = pos; j < currentSize -1; j++) {
  28. Book book = booklist.getBook(j+1);
  29. booklist.setBook(j,book);
  30. }
  31. booklist.setBook(currentSize-1,null);
  32. booklist.setUsedSize(currentSize-1);
  33. }
  34. }

 ReturnOperation类

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

 ShowOperation 类 

  1. package ioperations;
  2. import book.Book;
  3. import book.Booklist;
  4. public class ShowOperation implements IOPeration{
  5. public void work(Booklist booklist){
  6. System.out.println("显示图书");
  7. int currentSize = booklist.getUsedSize();
  8. for (int i = 0; i < currentSize; i++) {
  9. Book book = booklist.getBook(i);
  10. System.out.println(book);
  11. }
  12. }
  13. }

  user包

 User类

  1. package user;
  2. import book.Booklist;
  3. import ioperations.IOPeration;
  4. public abstract class User {
  5. protected String name;
  6. protected IOPeration[] ioperation;
  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.ioperation[choice].work(booklist);
  13. }
  14. }

AdminUser类

  1. package user;
  2. import ioperations.*;
  3. import java.util.Scanner;
  4. import static java.util.Scanner.*;
  5. public class AdminUser extends User {
  6. public AdminUser(String name) {
  7. super(name);
  8. this.ioperation = new IOPeration[]{
  9. new ExitOperation(),
  10. new FindOperation(),
  11. new AddOperation(),
  12. new DelOperation(),
  13. new ShowOperation(),
  14. };
  15. }
  16. public int menu() {
  17. System.out.println("************管理员菜单****************");
  18. System.out.println("欢迎来"+this.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. Scanner scanner = new Scanner(System.in);
  25. System.out.println("请输入你的操作");
  26. int choice = scanner.nextInt();
  27. return choice;
  28. }
  29. }

NormalUser类 

  1. package user;
  2. import ioperations.*;
  3. import java.util.Scanner;
  4. public class NormalUser extends User {
  5. public NormalUser(String name) {
  6. super(name);
  7. this.ioperation = new IOPeration[]{
  8. new ExitOperation(),
  9. new FindOperation(),
  10. new BorrowOperation(),
  11. new ReturnOperation(),
  12. };
  13. }
  14. public int menu() {
  15. System.out.println("************管理员菜单****************");
  16. System.out.println("欢迎来"+this.name+"到图书管理系统");
  17. System.out.println("1.查找图书");
  18. System.out.println("2.借阅图书");
  19. System.out.println("3.归还图书");
  20. System.out.println("0.退出图书");
  21. Scanner scanner = new Scanner(System.in);
  22. System.out.println("请输入你的操作");
  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. Scanner scanner = new Scanner(System.in);
  9. System.out.println("请输入你姓名:");
  10. String name = scanner.nextLine();
  11. System.out.println("请输入你的身份,1:管理员 2:普通用户");
  12. int choice = scanner.nextInt();
  13. if(choice == 1){
  14. return new AdminUser(name);
  15. }if(choice == 2){
  16. return new NormalUser(name);
  17. }else{
  18. return null;
  19. }
  20. }
  21. public static void main(String[] args) {
  22. Booklist booklist = new Booklist();
  23. User user = Login();
  24. while(true) user.doIoperation(user.menu(), booklist);
  25. }
  26. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号