Java项目—图书馆[通俗易懂]

Java项目—图书馆[通俗易懂]//Library.book包//book包下Book类packageLibrary.book;publicclassBook{privateStringname;privateStringauthor;privateStringid;privateintprice;privateStringtype;…

大家好,又见面了,我是你们的朋友全栈君。

//Library.book包
//book包下 Book类
package Library.book;

public class Book { 
   
    private String name;
    private String author;
    private String id ;
    private int price;
    private String type;
    private boolean isBorrowed;

    public Book(String name, String author, String id, int price, String type, boolean isBorrowed) { 
   
        this.name = name;
        this.author = author;
        this.id = id;
        this.price = price;
        this.type = type;
        this.isBorrowed = isBorrowed;
    }

    public String getId() { 
   
        return id;
    }

    public String getName(){ 
   
        return name;
    }

    @Override
    public String toString() { 
   
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", id='" + id + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }

    public boolean isBorrowed() { 
   
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) { 
   
        isBorrowed = borrowed;
    }
}

//book包下BookList类
package Library.book;
public class BookList { 
   
    private Book[] books = new Book[100];
    private int size;
    public BookList() { 
   
        books[0] = new Book("三国演义","罗贯中","001",100,"古典小说",false);
        books[1] = new Book("水浒传","施耐庵","002",100,"古典小说",false);
        books[2] = new Book("西游记", "吴承恩", "003", 100, "古典小说", false);
        size = 3;
    }

    public int getSize(){ 
   
        return size;
    }

    public Book getBooks(int index) { 
   
        return books[index];
    }

    public void setBooks(int index,Book book) { 
   
        books[index] = book;
    }

    public void setSize(int size) { 
   
        this.size = size;
    }
}


//Library包下Opeartion包
//Opeartion包下IOperation接口
package Library.Operation;

import Library.book.BookList;

public interface IOperation { 
   
    void work(BookList bookList);
}

//Opeartion包下AddBook类
package Library.book;

public class BookList { 
   
    private Book[] books = new Book[100];
    private int size;
    public BookList() { 
   
        books[0] = new Book("三国演义","罗贯中","001",100,"古典小说",false);
        books[1] = new Book("水浒传","施耐庵","002",100,"古典小说",false);
        books[2] = new Book("西游记", "吴承恩", "003", 100, "古典小说", false);
        size = 3;
    }

    public int getSize(){ 
   
        return size;
    }

    public Book getBooks(int index) { 
   
        return books[index];
    }

    public void setBooks(int index,Book book) { 
   
        books[index] = book;
    }

    public void setSize(int size) { 
   
        this.size = size;
    }
}

//Opeartion包下BorrowBook类
package Library.Operation;

import Library.book.Book;
import Library.book.BookList;

import java.util.Scanner;

public class BorrowBook implements IOperation { 
   
    @Override
    public void work(BookList bookList){ 
   
        System.out.println("借阅书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入借阅数据id号码: ");
        String id = scanner.next();
        for(int i = 0; i < bookList.getSize(); ++i){ 
   
            Book book = bookList.getBooks(i);
            if(!book.getId().equals(id)){ 
   
                continue;
            }
            if(book.isBorrowed()){ 
   
                System.out.println("这本书已经被借走");
                break;
            }
            System.out.println("借阅成功");
            book.setBorrowed(true);
        }
    }
}

//Opeartion包下DelBook类
package Library.Operation;

import Library.book.Book;
import Library.book.BookList;

import java.util.Scanner;

public class DelBook implements IOperation { 
   
    @Override
    public void work(BookList bookList){ 
   
        System.out.println("删除书籍信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除书籍的id号码:");
        String id = scanner.next();
        int  i = 0;
        for(; i < bookList.getSize(); ++i){ 
   
            Book book = bookList.getBooks(i);
            if(book.getId().equals(id)){ 
   
               bookList.setBooks(i,bookList.getBooks(bookList.getSize() - 1));
               bookList.setSize(bookList.getSize() - 1);
                System.out.println("删除成功");
                break;
            }
        }
        if(i >= bookList.getSize()){ 
   
            System.out.println("没有找到该书籍");
        }
    }
}

//Opeartion包下ExitOperation类
package Library.Operation;

import Library.book.BookList;

public class ExitOperation implements IOperation { 
   
    @Override
    public void work(BookList bookList){ 
   
        System.out.println("Good Bye!");
        System.exit(0);
    }
}

//Opeartion包下FindBook类
package Library.Operation;

import Library.book.Book;
import Library.book.BookList;

import java.util.Scanner;

public class FindBook implements IOperation { 
   
    @Override
    public void work(BookList bookList){ 
   
        System.out.println("查找书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要查阅书籍名称:");
        String name = scanner.next();
        int count = 0;
        for(int i = 0; i < bookList.getSize(); ++i){ 
   
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){ 
   
                System.out.println(book);
                count++;
            }
        }
        if(count == 0){ 
   
            System.out.println("未找到该书");
        }else { 
   
            System.out.println("找到 " + count + "本书" );
        }
    }

}

//Opeartion包下PrintAllBook类
package Library.Operation;

import Library.book.BookList;

public class PrintAllBook implements IOperation{ 
   
    @Override
    public void work(BookList bookList){ 
   
        System.out.println("打印所有书籍信息");
        for(int i = 0; i < bookList.getSize(); i++){ 
   
            System.out.println(bookList.getBooks(i));
        }
        System.out.println("共计" + bookList.getSize() + "本书籍");
    }
}

//Opeartion包下ReturnBook类
package Library.Operation;

import Library.book.Book;
import Library.book.BookList;

import java.util.Scanner;

public class ReturnBook implements IOperation { 
   
    @Override
    public void work(BookList bookList){ 
   
        System.out.println("归还书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入归还书籍的id号码:");
        String id  = scanner.next();
        for(int i = 0; i < bookList.getSize(); ++i){ 
   
            Book book = bookList.getBooks(i);
            if(!book.getId().equals(id)){ 
   
             continue;
            }
            if(!book.isBorrowed()){ 
   
                System.out.println("这本书已经被归还");
                break;
            }
            book.setBorrowed(false);
        }
    }
}


//Library包下user包
//user包下User类
package Library.user;

import Library.Operation.IOperation;

import Library.book.BookList;

abstract public class User { 
   
    protected String name;

    public User(String name){ 
   
        this.name = name;
    }

    protected IOperation[] operations;

    public abstract int menu();

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

//user包下Teacher类
package Library.user;

import Library.Operation.*;

import java.util.Scanner;

public class Teacher extends User { 
   
    public Teacher(String name) { 
   
        super(name);
        operations = new IOperation[]{ 
   
                new ExitOperation(),
                new FindBook(),
                new Addbook(),
                new DelBook(),
                new PrintAllBook()
        };

    }
    @Override
    public int menu() { 
   
        System.out.println("===========");
        System.out.println("Hello " + name);
        System.out.println("1. 查阅书籍");
        System.out.println("2. 增加书籍");
        System.out.println("3. 删除书籍");
        System.out.println("4. 打印所有书籍");
        System.out.println("0. 退出登陆");
        System.out.println("===========");
        System.out.println("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

//user包下Students类
package Library.user;

import Library.Operation.*;

import java.util.Scanner;

public class Student extends User { 
   
    public Student(String name) { 
   
        super(name);
        operations = new IOperation[]{ 
   
                new ExitOperation(),
                new FindBook(),
                new BorrowBook(),
                new ReturnBook()
        };
    }




    @Override
    public int menu(){ 
   
        System.out.println("===========");
        System.out.println("Hello " + name);
        System.out.println("1. 查找图书");
        System.out.println("2. 借阅图书");
        System.out.println("3. 归还图书");
        System.out.println("0. 退出登陆");
        System.out.println("===========");
        System.out.println("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/156880.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 用java web实现聊天室_java web实现简单聊天室「建议收藏」

    用java web实现聊天室_java web实现简单聊天室「建议收藏」目标servlet、jsp实现简单聊天室,用户通过浏览器登录后进入聊天室,可发送消息进行群聊,点击聊天信息框中的用户名可实现拍一拍功能。基础知识数据的存取setAttribute/getAttributerequest请求对象:有效时间短ServletContext上下文对象:一直存在于服务器,存储公有、共享数据Session会话对象:独立网站默认页面一般是index.jsp实现思路1….

  • vim 退出命令(保存、放弃保存)_linux保存并退出vim

    vim 退出命令(保存、放弃保存)_linux保存并退出vim测试环境VMware12Pro下CentOS7自带vim7.4.1099今天下午第一次接触这个vim文本编辑器,拿到一个陌生的工具,我们想的当然是最短的时间掌握它的基本操作,体会到成就感。如果你跟我一样,那么这篇教程或许对你有所帮助。Linux下一切皆文件,所以需要强大的文本编辑器,而vim就是Linux上一个功能强大且使用广泛的文本编辑器。它可以建立、编辑、现实文件,并且它只有命令没…

  • c语言cstdio头文件,cstdio(cstdio头文件有什么用)[通俗易懂]

    c语言cstdio头文件,cstdio(cstdio头文件有什么用)[通俗易懂]cstdio是将stdio.h的内容用c++头文件的形式表示出来。stdio.h是c标准函数库中的头文件,即:standardbufferedinput&output。提供基本的文字的输入输出流操作(包括屏.是C语言中的一个头文件,首先,stdlib.h的解释*Purpose:*Thisincludefilecontainsthefunctiondeclaration…

    2022年10月27日
  • datagrip2022.01.12最新激活码【最新永久激活】「建议收藏」

    (datagrip2022.01.12最新激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.cn/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~41MD9IQHZL-eyJsaWNlbnNlSWQiOi…

  • springmvc和mybatis面试题(含答案)

    springmvc和mybatis面试题(含答案)SpringMVCFramework有这样一些特点:1。它是基于组件技术的.全部的应用对象,无论控制器和视图,还是业务对象之类的都是java组件.并且和Spring提供的其他基础结构紧密集成.2。不依赖于ServletAPI(目标虽是如此,但是在实现的时候确实是依赖于Servlet的)3。可以任意使用各种视图技术,而不仅仅局限于JSP4。支持各种请求资源的映射策略5。它应是易于扩展的2

  • Ubuntu 18 安装rabbitVCS

    Ubuntu 18 安装rabbitVCSsudoadd-apt-repositoryppa:ondrej/phpsudoapt-getinstallphp5.6-opcachesudoapt-getinstallphp5.6-jsonsudoapt-getinstallphp5.6-commonsudoapt-getinstallphp5.6-devsudoapt-getinstallpk…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号