前言:
图书管理是许多高校的期末经典项目,在学完了类,对象,抽象类,接口,继承,封装和多态及组合等知识后,要及时的练手,以此来巩固这部分的内容,Java中基本都是在和对象这些的打交道,所以利用这些知识来写一个图书管理系统。
JavaSE期末经典项目—>>图书管理系统
项目简介:
该项目为图书管理系统,实现用户的区分,包括管理员和普通用户,管理员的行为包括:添加图书,删除图书,查找图书,显示图书和退出程序等,普通用户的用法包括:查找图书,借阅图书,归还图书和退出程序。
思路简介:
利用类,对象,抽象类,接口,继承,封装和多态及组合等知识,将对象提取出来,对象----用户和书,用户可以分为管理员和普通用户,所以设置一个抽象类User,管理员和普通用户继承该抽象类,由于书还要包括书架,所以书和书架分两个类,为了实现对应的操作,并且是操作的方法一致,所以要构造一个操作的接口,其他的操作用类来实现该接口,就完成了对应的操作。具体实现方式见详细代码!
详细代码:
book包:
Book类:
1. package book; 2. 3. public class Book { 4. private String name;//书名 5. private String author;//作者 6. private int price;//价格 7. private String type;//类型 8. private boolean isBorrowed;//是否被借出 9. 10. public Book(String name, String author, int price, String type) { 11. this.name = name; 12. this.author = author; 13. this.price = price; 14. this.type = type; 15. } 16. 17. public String getName() { 18. return name; 19. } 20. 21. public void setName(String name) { 22. this.name = name; 23. } 24. 25. public String getAuthor() { 26. return author; 27. } 28. 29. public void setAuthor(String author) { 30. this.author = author; 31. } 32. 33. public int getPrice() { 34. return price; 35. } 36. 37. public void setPrice(int price) { 38. this.price = price; 39. } 40. 41. public String getType() { 42. return type; 43. } 44. 45. public void setType(String type) { 46. this.type = type; 47. } 48. 49. public boolean isBorrowed() { 50. return isBorrowed; 51. } 52. 53. public void setBorrowed(boolean borrowed) { 54. isBorrowed = borrowed; 55. } 56. 57. @Override 58. public String toString() { 59. return "Book{" + 60. "name='" + name + '\'' + 61. ", author='" + author + '\'' + 62. ", price=" + price + 63. ", type='" + type + '\'' + 64. ((isBorrowed==true)?"该书已借出":"该书未借出")+ 65. '}'; 66. } 67. }
BookList类:
1. package book; 2. 3. public class BookList { 4. 5. public Book[] books=new Book[10]; 6. public int usedSize;//书架上书的数量 7. 8. /** 9. * 10. * 事先通过构造方法放入3本书 11. */ 12. public BookList(){ 13. books[0]=new Book("三国演义","罗贯中",89,"小说"); 14. books[1]=new Book("西游记","吴承恩",79,"小说"); 15. books[2]=new Book("红楼梦","曹雪芹",83,"小说"); 16. 17. this.usedSize=3; 18. } 19. 20. public int getUsedSize() { 21. return usedSize; 22. } 23. 24. public void setUsedSize(int usedSize) { 25. this.usedSize = usedSize; 26. } 27. 28. //下标 29. public Book getPos(int pos){ 30. 31. return books[pos]; 32. } 33. 34. /** 35. * 36. * 存储一本书到指定位置 37. * @param book 38. * @param pos 在这里指的是当前最后可以存储书的位置 39. */ 40. public void setBooks(Book book,int pos){ 41. books[pos]=book; 42. } 43. }
operations包:
IOperation接口:
1. package operations; 2. 3. //接口 4. import book.BookList; 5. 6. public interface IOperation { 7. 8. 9. void work(BookList bookList); 10. }
AddOperation类:
1. package operations; 2. 3. import book.Book; 4. import book.BookList; 5. 6. import java.util.Scanner; 7. 8. public class AddOperation implements IOperation { 9. @Override 10. public void work(BookList bookList) { 11. 12. System.out.println("添加图书!"); 13. 14. System.out.println("请输入你要添加的图书的名字:"); 15. 16. Scanner scanner=new Scanner(System.in); 17. 18. String name=scanner.nextLine(); 19. 20. System.out.println("请输入你要添加的图书的作者:"); 21. 22. String author=scanner.nextLine(); 23. System.out.println("请输入你要添加的图书的价格:"); 24. 25. int price=scanner.nextInt(); 26. 27. scanner.nextLine();//读取回车 28. System.out.println("请输入你要添加的图书的类型:"); 29. 30. String type=scanner.nextLine(); 31. Book book=new Book(name,author,price,type); 32. //获取当前可以存放书的位置 33. 34. int currentSize=bookList.getUsedSize(); 35. //书放到指定位置 36. bookList.setBooks(book,currentSize); 37. //书的数量加1 38. bookList.setUsedSize(currentSize+1); 39. System.out.println("添加成功!"); 40. 41. } 42. 43. 44. 45. }
BorrowedOperation类:
1. package operations; 2. 3. import book.Book; 4. import book.BookList; 5. 6. import java.util.Scanner; 7. 8. public class BorrowedOperation implements IOperation { 9. @Override 10. public void work(BookList bookList) { 11. System.out.println("借阅图书!"); 12. System.out.println("请输入你要借阅的图书的名字:"); 13. 14. Scanner scanner=new Scanner(System.in); 15. 16. String name=scanner.nextLine(); 17. 18. int currentSize=bookList.getUsedSize(); 19. for (int i = 0; i < currentSize; i++) { 20. Book book=bookList.getPos(i); 21. if(name.equals(book.getName())){ 22. book.setBorrowed(true); 23. System.out.println("借阅成功!"); 24. return; 25. 26. } 27. } 28. System.out.println("没有你要借阅的书!"); 29. } 30. }
DelOperation类:
1. package operations; 2. 3. import book.Book; 4. import book.BookList; 5. 6. import java.util.Scanner; 7. 8. public class DelOperation implements IOperation{ 9. @Override 10. public void work(BookList bookList) { 11. System.out.println("删除图书!"); 12. System.out.println("请输入你要删除的图书的名字:"); 13. Scanner scanner=new Scanner(System.in); 14. String name=scanner.nextLine(); 15. //遍历数组,看是否有你要删除的图书的下标 16. int index=-1; 17. int currentSize=bookList.getUsedSize(); 18. for (int i = 0; i < currentSize; i++) { 19. Book book=bookList.getPos(i); 20. if(name.equals(book.getName())){ 21. index=i; 22. break; 23. 24. } 25. } 26. if(index==-1){ 27. System.out.println("没有你要删除的这本书!"); 28. return; 29. } 30. for (int i = index; i <currentSize-1 ; i++) { 31. Book book=bookList.getPos(i+1); 32. bookList.setBooks(book,i); 33. } 34. 35. bookList.setBooks(null,currentSize-1); 36. 37. bookList.setUsedSize(currentSize-1); 38. 39. System.out.println("删除成功!"); 40. } 41. }
DisplayOperation类:
1. package operations; 2. 3. import book.Book; 4. import book.BookList; 5. 6. public class DisplayOperation implements IOperation { 7. 8. @Override 9. public void work(BookList bookList) { 10. System.out.println("显示所有图书!"); 11. int currentSize=bookList.getUsedSize(); 12. for (int i = 0; i < currentSize; i++) { 13. Book book=bookList.getPos(i); 14. System.out.println(book); 15. } 16. 17. } 18. }
ExitOperation类:
1. package operations; 2. 3. import book.BookList; 4. 5. public class ExitOperation implements IOperation{ 6. 7. @Override 8. public void work(BookList bookList) { 9. System.out.println("退出程序!"); 10. System.exit(0);//退出 11. } 12. }
ReturnOperation类:
1. package operations; 2. 3. import book.Book; 4. import book.BookList; 5. 6. import java.util.Scanner; 7. 8. public class ReturnOperation implements IOperation{ 9. @Override 10. public void work(BookList bookList) { 11. System.out.println("归还图书!"); 12. 13. System.out.println("请输入你要归还的图书的名字:"); 14. Scanner scanner=new Scanner(System.in); 15. String name=scanner.nextLine(); 16. int currentSize=bookList.getUsedSize(); 17. for (int i = 0; i < currentSize; i++) { 18. Book book=bookList.getPos(i); 19. if(name.equals(book.getName())){ 20. book.setBorrowed(false); 21. System.out.println("归还图书成功!"); 22. return; 23. 24. } 25. } 26. System.out.println("没有你要归还的书!"); 27. } 28. }
SearchOperation类:
1. package operations; 2. 3. import book.Book; 4. import book.BookList; 5. 6. import java.util.Scanner; 7. 8. public class SearchOperation implements IOperation{ 9. 10. @Override 11. public void work(BookList bookList) { 12. System.out.println("查找图书!"); 13. System.out.println("请输入你要查找的图书的名字:"); 14. Scanner scanner=new Scanner(System.in); 15. String name=scanner.nextLine(); 16. int currentSize=bookList.getUsedSize(); 17. for (int i = 0; i < currentSize; i++) { 18. Book book=bookList.getPos(i); 19. if(name.equals(book.getName())){ 20. System.out.println("找到啦这本书!"); 21. System.out.println(book); 22. return; 23. } 24. } 25. System.out.println("没有这本书!"); 26. } 27. }
user包:
User类:
1. package user; 2. 3. import book.BookList; 4. import operations.IOperation; 5. 6. //抽象类 7. public abstract class User { 8. 9. protected String name; 10. 11. protected IOperation[] iOperations;//接口数组 12. 13. public User(String name){ 14. this.name=name; 15. 16. } 17. 18. public abstract int menu(); 19. 20. public void doOperation(int choice, BookList bookList){ 21. iOperations[choice].work(bookList); 22. } 23. 24. }
AdminUser类:
1. package user; 2. 3. import operations.*; 4. 5. import java.util.Scanner; 6. 7. public class AdminUser extends User { 8. 9. public AdminUser(String name) { 10. super(name); 11. //接口数组 12. this.iOperations=new IOperation[]{ 13. new ExitOperation(), 14. new SearchOperation(), 15. new AddOperation(), 16. new DelOperation(), 17. new DisplayOperation() 18. }; 19. } 20. public int menu(){ 21. System.out.println("*********************************"); 22. System.out.println( "hello ! "+name+"欢迎登入图书管理系统!"); 23. System.out.println("1.查找图书"); 24. System.out.println("2.新增图书"); 25. System.out.println("3.删除图书"); 26. System.out.println("4.显示图书"); 27. System.out.println("0.退出系统"); 28. System.out.println("*********************************"); 29. System.out.println("请输入你的操作:"); 30. Scanner scanner=new Scanner(System.in); 31. int choice=scanner.nextInt(); 32. return choice; 33. 34. } 35. }
NormalUser类:
1. package user; 2. 3. import operations.*; 4. 5. import java.util.Scanner; 6. 7. public class NormalUser extends User { 8. 9. public NormalUser(String name) { 10. super(name); 11. this.iOperations=new IOperation[]{ 12. new ExitOperation(), 13. new SearchOperation(), 14. new BorrowedOperation(), 15. new ReturnOperation(), 16. 17. }; 18. } 19. public int menu(){ 20. System.out.println("*********************************"); 21. System.out.println( "hello ! "+name+"欢迎登入图书管理系统!"); 22. System.out.println("1.查找图书"); 23. System.out.println("2.借阅图书"); 24. System.out.println("3.归还图书"); 25. System.out.println("0.退出系统"); 26. System.out.println("*********************************"); 27. 28. Scanner scanner=new Scanner(System.in); 29. int choice=scanner.nextInt(); 30. return choice; 31. } 32. }
Main--管理器:
1. import book.BookList; 2. import user.AdminUser; 3. import user.NormalUser; 4. import user.User; 5. 6. import java.util.Scanner; 7. 8. public class Main { 9. public static User login(){ 10. System.out.println("请输入你的姓名:"); 11. Scanner scanner=new Scanner(System.in); 12. String userName=scanner.nextLine(); 13. System.out.println("请输入你的身份:1-》管理员,0-》普通用户"); 14. int choice= scanner.nextInt(); 15. if(choice==1){ 16. return new AdminUser(userName);//向上转型 17. }else{ 18. return new NormalUser(userName);//向上转型 19. } 20. } 21. public static void main(String[] args) { 22. 23. //1、准备数据 24. BookList bookList=new BookList(); 25. //2、登录, 26. User user= login(); 27. while(true){ 28. int choice= user.menu();//多态,动态绑定 29. user.doOperation(choice,bookList); 30. } 31. 32. 33. } 34. }
效果演示:
管理员:
普通用户:
码云上传地址:图书管理系统