使用集合实现书籍管理系统

1)创建书籍实体类(书籍编号,书籍名称,书籍借阅状态,书籍借阅次数)
2)创建多本书籍存储在集合中
3)在书籍管理系统中显示菜单:
        1.查看所有书籍
        2.借阅书籍
        3.归还书籍
        4.退出系统
4)输入对应的编号进入特定功能模块,并实现各模块功能
在这里插入图片描述

public class BookManager {
	static List<Book> list = new ArrayList<Book>();

	public static void init() {
		list.add(new Book(1, "三国", 0, 10));
		list.add(new Book(2, "红楼梦", 0, 6));
		list.add(new Book(3, "西游记", 1, 13));
		list.add(new Book(4, "水浒装", 0, 12));
	}

	public static void menu() {
		System.out.println("---------欢迎光临图书管理系统---------");
		System.out.println("----------------------------------");
		System.out.println("1、查看所有图书");
		System.out.println("2、借阅图书");
		System.out.println("3、归还图书");
		System.out.println("4、退出系统");
		System.out.println("----------------------------------");
	}

	public static void main(String[] args) {
		init();
		Scanner input = new Scanner(System.in);
		while (true) {
			menu();
			System.out.println("请输入选择");
			int n = input.nextInt();
			if (n==1) {
				System.out.println("编号" + "\t\t" + "名称" + "\t\t" + "状态" + "\t\t" + "次数");
				for (int i = 0; i < list.size(); i++) {
					String state = "";
					if (list.get(i).getCount()== 0) {// 如果状态那栏是0的话输出可借
						state = "可借";
					} else {
						state = "已借";
					}
					System.out.println(list.get(i).getId() + "\t\t" + list.get(i).getBname() + "\t\t" + state + "\t\t"
							+list.get(i).getTimes());// 遍历数组中的元素
				}
				System.out.println("*******************************");
			}else if (n==2) {
				System.out.println("请输入你要借阅书籍的名称");
				String name = input.next();
				//boolean flag = false;// 用来判断书籍名称是否相等
				String str = "没有图书";
				for (int i = 0; i < list.size(); i++) {
					if (name.equals(list.get(i).getBname())) {
						//flag = true;
						 str="有图书";
						if (list.get(i).getCount() == 1) {// 表示已借
							System.out.println("图书已借出");
						} else {
							list.get(i).setCount(1);// 将状态数子改为1
							list.get(i).setTimes(list.get(i).getTimes() + 1);// 修改次数加1
						}
						System.out.println("成功借阅《" +list.get(i).getBname() + "》");
					}
				}
					if (str.equals("没有图书")) {
						System.out.println("借阅书籍有误");
					}
			}else if (n==3) {
				System.out.println("请输入你要归还书籍的名称");
				String name = input.next();
				boolean flag = false;// 用来判断书籍书否可借
				for (int i = 0; i <list.size(); i++) {
					if (name.equals(list.get(i).getBname())) {
						flag = true;
						if (list.get(i).getCount() == 1) {// 表示已借
							list.get(i).setCount(0);// 将状态数子改为0表示可借
							System.out.println("成功归还《" + list.get(i).getBname() + "》");
						}
					}

				}
				if (!flag) {
					System.out.println("你归还的书籍有误");
				}
			}else if (n==4) {//退出系统
				break;
			}
		}
	}
}

BOOk类

public class Book {
private int id ;
private String bname;
private  int count ;
private int times;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getBname() {
	return bname;
}
public void setBname(String bname) {
	this.bname = bname;
}
public int getCount() {
	return count;
}
public void setCount(int count) {
	this.count = count;
}
public int getTimes() {
	return times;
}
public void setTimes(int times) {
	this.times = times;
}
public Book() {
	super();
}
public Book(int id, String bname, int count, int times) {
	super();
	this.id = id;
	this.bname = bname;
	this.count = count;
	this.times = times;
}
@Override
public String toString() {
	return "Book [id=" + id + ", bname=" + bname + ", count=" + count + ", times=" + times + "]";
}

}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值