当前位置:   article > 正文

图书管理系统java简易实现—超简单,超详细_图书管理系统怎么弄java

图书管理系统怎么弄java

所需要的知识:类,对象,抽象类,接口,封装,继承,多态,顺序表

源码链接:

图书管理系统java

对于书的信息,进行编写

创建 关于书的 包

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Zmc5Zmc5Zmc5Zmc6bKB5YWI55Sf,size_20,color_FFFFFF,t_70,g_se,x_16

有书名,作者,价格,类型,以及是否借出等信息,这里用private进行封装,可有效保护代码隐私。

  1. class Books{
  2. private String name;//书名
  3. private String author;//作者
  4. private String price;//价格
  5. private String type;//类型
  6. private boolean isBorrowed;//是否借出
  7. }

写出构造方法

注意:对于isBorrowed的Boolean类型,无需写进构造方法之中。

  1. public class Book {
  2. private String name;//书名
  3. private String author;//作者
  4. private String price;//价格
  5. private String type;//类型
  6. private boolean isBorrowed;//是否借出
  7. public Book(String name, String author, String price, String type) {
  8. this.name = name;
  9. this.author = author;
  10. this.price = price;
  11. this.type = type;
  12. }
  13. }

为了访问被private封装的属性,必须提供getter与setter方法。

并且再提供一个toString方法

  1. public class Book {
  2. private String name;//书名
  3. private String author;//作者
  4. private String price;//价格
  5. private String type;//类型
  6. private boolean isBorrowed;//是否借出
  7. public Book(String name, String author, String price, String type) {
  8. this.name = name;
  9. this.author = author;
  10. this.price = price;
  11. this.type = type;
  12. }
  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 String getPrice() {
  27. return price;
  28. }
  29. public void setPrice(String 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=" + isBorrowed +
  52. '}';
  53. }
  54. }

创建链表

  1. package books;
  2. public class BookList {
  3. private Book[] books = new Book[10];
  4. private int usedSize;
  5. public BookList(){
  6. books[0] = new Book("三国演义","罗贯中",30,"小说");
  7. books[1] = new Book("水浒传","施耐庵",40,"小说");
  8. books[2] = new Book("红楼梦","曹雪芹",50,"小说");
  9. books[3] = new Book("西游记","吴承恩",60,"小说");
  10. this.usedSize = 4;
  11. }
  12. public int getUsedSize() {
  13. return usedSize;
  14. }
  15. public void setUsedSize(int usedSize) {
  16. this.usedSize = usedSize;
  17. }
  18. }

获取pos位置的书,以及在pos位置设置为一本书(添加一本书)

  1. //获取pos位置的书
  2. public Book getPos(int pos){
  3. return this.books[pos];
  4. }
  5. //设置pos下标为一本书
  6. public void setBook(int pos,Book book){
  7. this.books[pos] = book;
  8. }

使用者的编写

对于使用者来说,既有管理员,也有普通用户。管理员与普通用户的目的是不一样的,但是他们有一些相同的特点。

8c466323d81a4f05ad4b9d2952c86dba.png

创建User,

他们相同特点,比如都有名字

  1. package user;
  2. import java.security.PrivateKey;
  3. public class User {
  4. protected String name;
  5. public User(String name){
  6. this.name = name;
  7. }
  8. }

由于在主程序中需要对于User类中填写menu(菜单)方法,且并不实现menu,所以将User改为抽象类

protected IOperation[] iOperations;

填写如下代码,创建数组

protected IOperation[] iOperations;

然后创建doWork方法,来实现操作

public void doWork(int choice, BookList bookList){
    iOperations[choice].work(bookList);
}

 

对于管理员

创建AdminUser类,继承User类

  1. package user;
  2. public class AdminUser extends User{
  3. public AdminUser(String name){
  4. super(name);
  5. }
  6. }

管理员菜单:

  1. package user;
  2. import java.util.Scanner;
  3. public class AdminUser extends User{
  4. public AdminUser(String name){
  5. super(name);
  6. }
  7. public int menu(){
  8. System.out.println("==========管理员菜单==========");
  9. System.out.println("hello "+this.name+"欢迎来到这里!:>");
  10. System.out.println("1.查找图书:>");
  11. System.out.println("2.新增图书:>");
  12. System.out.println("3.删除图书:>");
  13. System.out.println("4.展示图书:>");
  14. System.out.println("0.退出系统:>");
  15. System.out.println("============================");
  16. Scanner scanner = new Scanner(System.in);
  17. int choice = scanner.nextInt();
  18. return choice;
  19. }
  20. }

创建数组,为后续完成1-4的各项操作

  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.iOperations = new IOperation[]{
  8. new ExitOperation(),
  9. new FindOperation(),
  10. new AddOperation(),
  11. new DelOperation(),
  12. new DisplayOperation()
  13. };
  14. }
  15. public int menu(){
  16. System.out.println("==========管理员菜单==========");
  17. System.out.println("hello "+this.name+"欢迎来到这里!:>");
  18. System.out.println("1.查找图书:>");
  19. System.out.println("2.新增图书:>");
  20. System.out.println("3.删除图书:>");
  21. System.out.println("4.展示图书:>");
  22. System.out.println("0.退出系统:>");
  23. System.out.println("============================");
  24. Scanner scanner = new Scanner(System.in);
  25. int choice = scanner.nextInt();
  26. return choice;
  27. }
  28. }

 对于普通用户

创建NormalUser类,继承User类

  1. package user;
  2. public class NormalUser extends User{
  3. public NormalUser(String name){
  4. super(name);
  5. }
  6. }

普通用户菜单:

  1. package user;
  2. import java.util.Scanner;
  3. public class NormalUser extends User{
  4. public NormalUser(String name){
  5. super(name);
  6. }
  7. public int menu(){
  8. System.out.println("==========普通用户菜单==========");
  9. System.out.println("hello "+this.name+"欢迎来到这里!:>");
  10. System.out.println("1.查找图书:>");
  11. System.out.println("2.借阅图书:>");
  12. System.out.println("3.归还图书:>");
  13. System.out.println("0.退出系统:>");
  14. System.out.println("==============================");
  15. Scanner scanner = new Scanner(System.in);
  16. int choice = scanner.nextInt();
  17. return choice;
  18. }
  19. }

创建数组,为后续完成1-3的各项操作

  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.iOperations = 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("hello "+this.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. }

 

 

添加Operation包

并且写一些类,去一一实现

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Zmc5Zmc5Zmc5Zmc6bKB5YWI55Sf,size_9,color_FFFFFF,t_70,g_se,x_16

在实现每一个类之前,可以写一个接口,来使后面的代码便于书写

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

1. AddOperation

源码

  1. package operation;
  2. import books.Book;
  3. import books.BookList;
  4. public class AddOperation implements IOperation {
  5. public void work(BookList bookList){
  6. System.out.println("新增图书:>");
  7. System.out.println("请输入图书的名字:>");
  8. String name = scanner.nextLine();
  9. System.out.println("请输入图书的作者:>");
  10. String author = scanner.nextLine();
  11. System.out.println("请输入图书的类型:>");
  12. String type = scanner.nextLine();
  13. System.out.println("请输入图书的价格:>");
  14. double price = scanner.nextDouble();
  15. Book book = new Book(name,author,price,type);
  16. int size = bookList.getUsedSize();
  17. bookList.setBook(size,book);
  18. bookList.setUsedSize(size+1);
  19. System.out.println("图书新增成功!:>");
  20. }
  21. }

2.BorrowOperation

源码

  1. package operation;
  2. import books.Book;
  3. import books.BookList;
  4. public class BorrowOperation implements IOperation{
  5. public void work(BookList bookList){
  6. System.out.println("借阅图书:>");
  7. System.out.println("你想借阅什么书? :>");
  8. String name = scanner.nextLine();
  9. int size = bookList.getUsedSize();
  10. for(int i = 0; i < size; i++){
  11. Book book = bookList.getPos(i);
  12. if(name.equals(book.getName())){
  13. book.setBorrowed(true);
  14. System.out.println("借阅成功! :>");
  15. System.out.println(book);
  16. return;
  17. }
  18. }
  19. System.out.println("没有你要借阅的这本书:>");
  20. }
  21. }

3.DelOperation

源码

  1. package operation;
  2. import books.Book;
  3. import books.BookList;
  4. public class DelOperation implements IOperation{
  5. public void work(BookList bookList){
  6. System.out.println("删除图书:>");
  7. System.out.println("请输入你要删除的书名:>");
  8. String name = scanner.nextLine();
  9. int currentSize = bookList.getUsedSize();
  10. int i;
  11. int index = 0;
  12. for (i = 0; i < currentSize; i++) {
  13. Book book = bookList.getPos(i);
  14. if(book.getName().equals(name)){
  15. index = i;
  16. break;
  17. }
  18. }
  19. if(i >= currentSize){
  20. System.out.println("没有你要删除的书");
  21. return;
  22. }
  23. for (int j = index; j < currentSize-1; j++) {
  24. Book book = bookList.getPos(j+1);
  25. bookList.setBook(j,book);
  26. }
  27. bookList.setBook(currentSize,null);
  28. bookList.setUsedSize(currentSize-1);
  29. System.out.println("删除成功!:>");
  30. }
  31. }

4.DisplayOperation

源码

  1. package operation;
  2. import books.Book;
  3. import books.BookList;
  4. public class DisplayOperation implements IOperation{
  5. public void work(BookList bookList){
  6. System.out.println("展示图书:>");
  7. int size = bookList.getUsedSize();
  8. for(int i = 0;i < size;i++){
  9. Book book = bookList.getPos(i);
  10. System.out.println(book);
  11. }
  12. }
  13. }

5.ExitOperation

源码

  1. package operation;
  2. import books.BookList;
  3. public class ExitOperation implements IOperation{
  4. public void work(BookList bookList){
  5. System.out.println("退出系统:>");
  6. System.exit(0);
  7. }
  8. }

 

6.FindOperation

  1. package operation;
  2. import books.Book;
  3. import books.BookList;
  4. public class FindOperation implements IOperation{
  5. public void work(BookList bookList){
  6. System.out.println("查找图书:>");
  7. String name = scanner.nextLine();
  8. int size = bookList.getUsedSize();
  9. for(int i = 0; i < size; i++){
  10. Book book = bookList.getPos(i);
  11. if(name.equals(book.getName())){
  12. System.out.println("找到了这本书,信息如下:>");
  13. System.out.println(book);
  14. return;
  15. }
  16. }
  17. System.out.println("没有找到这本书:>");
  18. }
  19. }

7.ReturnOperation

  1. package operation;
  2. import books.Book;
  3. import books.BookList;
  4. public class ReturnOperation implements IOperation{
  5. public void work(BookList bookList){
  6. System.out.println("归还图书:>");
  7. System.out.println("你想归还什么书? :>");
  8. String name = scanner.nextLine();
  9. int size = bookList.getUsedSize();
  10. for(int i = 0; i < size; i++){
  11. Book book = bookList.getPos(i);
  12. if(name.equals(book.getName())){
  13. book.setBorrowed(false);
  14. System.out.println("归还成功! :>");
  15. System.out.println(book);
  16. return;
  17. }
  18. }
  19. System.out.println("没有你要归还的这本书:>");
  20. }
  21. }

 

创建程序入口Main

创建登录方法,并且在主函数中用user接收

 

  1. import user.AdminUser;
  2. import user.NormalUser;
  3. import user.User;
  4. import java.util.Scanner;
  5. public class Main {
  6. public static User login(){
  7. System.out.println("请输入你的姓名:>");
  8. Scanner scanner = new Scanner(System.in);
  9. String name = scanner.nextLine();
  10. System.out.println("请输入你的身份:>");
  11. System.out.println("1.管理员 0.普通用户");
  12. int choice = scanner.nextInt();
  13. if(choice == 1){
  14. return new AdminUser(name);
  15. }else{
  16. return new NormalUser(name);
  17. }
  18. }
  19. public static void main(String[] args) {
  20. User user = login();//向上转型
  21. int choice = user.menu();//动态绑定
  22. }
  23. }

此时,对于user。无法直接获取菜单。应在父类(User)中,写出菜单方法

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Zmc5Zmc5Zmc5Zmc6bKB5YWI55Sf,size_20,color_FFFFFF,t_70,g_se,x_16

 然后在,主程序中使用user调用menu。

最终的主函数代码:

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

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/556042?site
推荐阅读
相关标签
  

闽ICP备14008679号