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)


相关推荐

  • 电信光纤友华PT921G,烽火HG220光猫激活成功教程关闭自带路由改桥接拨号教程[通俗易懂]

    电信光纤友华PT921G,烽火HG220光猫激活成功教程关闭自带路由改桥接拨号教程[通俗易懂]电信光纤友华PT921G光猫激活成功教程关闭自带路由改桥接拨号教程电信光猫质量烂就算了,最受不了它自带的路由还做了手脚,导致VPN用不了。不让看AV就算了,打个外服游戏总可以吧?不知道为啥,网上关于光猫改桥接的教程基本没有,搜出来的也说得很不清楚,是和谐了还是什么原因不得而知。本人也是自己自己试出来的,其实修改难度并不大,只不过那个界面搞的特奇葩特不友好罢了。废话不多说,步骤如下:

  • set是什么_js把参数放入request

    set是什么_js把参数放入request1.Accept:image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/vnd.ms-powerpoint,application/vnd.ms-excel,application/msword,application/x-shockwave-flash,/2.Accept-Language:zh-cn3.Accept-Encoding:gzip,deflate4.User-Agent:Mozilla/

  • 屏幕适配 autosize

    屏幕适配 autosizeAndroidManifest<meta-dataandroid:name=”design_width_in_dp”android:value=”400″/><meta-dataandroid:name=”design_height_in_dp”android:value=”800″/>build(APP)implementa

  • 【python表白神器】手把手教你用代码浪漫追求对象!(附完整源码+讲解)

    【python表白神器】手把手教你用代码浪漫追求对象!(附完整源码+讲解)文章目录前言演示网站制作部署网站二维码制作总结前言跟着我做,不要跳着看,否则你会失败。第一步是制作二维码;第二步是制作网站。演示具体成果地址:https://yanghanwen.xyz/ai/网站制作首先你需要下载我的这个完整项目:链接:https://pan.baidu.com/s/1EmRehx_gRnT5hLjJvKuAIg提取码:pz1y–来自百度网盘超级会员V2的分享下载好后文件目录如下:然后你需要注意的是我把img里面的图片删了,涉及隐私,大家自己替换自己追

    2022年10月15日
  • rabbitmq集群安装_java实现消息队列

    rabbitmq集群安装_java实现消息队列rabbitmq集群搭建失败解决随记1现象:2原因:3解决方法:1现象:1、各节点已改hosts,各节点ping节点名正常。2、md5sum.erlang.cookie各节点值一样。按以下步骤加入失败:$rabbitmqctlstop_app #停止rabbitmq服务$rabbitmqctlreset #清空节点状态$rabbitmqctljoin_clusterrabbit@rabbitmq3$rabbitmqctlstart_app

  • C#图片按比例缩放建议收藏

    C#图片按比例缩放:1//按比例缩放图片2publicImageZoomPicture(ImageSourceImage,intTargetWidth,intTargetHei

    2021年12月20日

发表回复

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

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