package com.test.library.newlib; import java.util.HashSet; import java.util.Scanner; import java.util.Set; //图书馆类 public class Library { // 图书所有图书的集合 private Set<Books> books = new HashSet<Books>(); // 图书馆所有会员用户 private Set<Students> stus = new HashSet<Students>(); Library() { // 初始化图书和会员 initBooks(); initStudents(); } // 初始化所有的图书 private void initBooks() { Books b1 = new Books("b0001", "三国", "罗贯中", "北京出版社", 100); Books b2 = new Books("b0002", "西游记", "吴承恩", "北京出版社", 50); Books b3 = new Books("b0003", "水浒", "施耐庵", "北京出版社", 80); Books b4 = new Books("b0004", "红楼梦", "曹雪芹", "北京出版社", 200); // 把图书添加到集合里 books.add(b1); books.add(b2); books.add(b3); books.add(b4); } // 初始化所有会员用户 private void initStudents() { Students s1 = new Students("s0001", "张三");// 管理员 Students s2 = new Students("s0002", "李四"); Students s3 = new Students("s0003", "王五"); Students s4 = new Students("s0004", "赵六"); // 把会员添加到会员集合 stus.add(s1); stus.add(s2); stus.add(s3); stus.add(s4); } //添加用户 public void addUser(String s) { // 不重复即可添加 boolean result = checkUser(s); if (result == false) { // 分解然后添加到现有集合里边 String[] arrStr = s.split("-"); Students s1 = new Students(arrStr[0],arrStr[1]); stus.add(s1); System.out.println("添加用户成功"); } else { System.out.println("已有此会员,不能重复添加!"); } /*String[] arrStr = s.split("-"); for (Students stu2 : stus) { if (stu2.getSid().equals(arrStr[0]) || stu2.getName().equals(arrStr[1])) { System.out.println(stu2.getSid()+"-"+stu2.getName()+"用户已存在"); }else { Students s1 = new Students(arrStr[0],arrStr[1]); System.out.println(arrStr[0]+"-"+arrStr[1]+"用户添加成功"); } }*/ } //查询会员 public Students findBySId(String bid) { for (Students s : stus) { if (s.getSid().equals(bid)) { return s; } } return null; } //删除用户 public void removeUser(String bid) { Students b = findBySId(bid); stus.remove(b); System.out.println("删除会员成功"); } public void addBooks(String input) { // 不重复即可添加 boolean result = checkBook(input); if (result == false) { // 分解然后添加到现有集合里边 String[] arrStr = input.split("-"); Books tmp = new Books(arrStr[0], arrStr[1], arrStr[2], arrStr[3], Integer.parseInt(arrStr[4])); books.add(tmp); System.out.println("添加图书成功"); } else { System.out.println("书库已有此数,不能重复添加!"); } } public void removeBooks(String bid) { Books b = findById(bid); books.remove(b); System.out.println("删除图书成功"); } // 修改图书信息――需要图书id和修改后的图书信息 public void modifyBooks(String bid, String input2) { // 检查是否存在id和书名相同的图书 boolean result = checkBook(input2); // 如果没有 if (result == false) { // 则分解然后添加到现有集合里边 String[] arrStr = input2.split("-"); // 遍历书库 for (Books b : books) { if (b.getBid().equals(bid)) { b.setBid(arrStr[0]); b.setName(arrStr[1]); b.setAuthor(arrStr[2]); b.setPress(arrStr[3]); b.setName(arrStr[4]); } } System.out.println("修改图书信息成功"); } else { System.out.println("书库已有此数,不能重复添加!"); } } // 单个查询图书――通过id返回一个图书对象 public Books findById(String bid) { for (Books b : books) { if (b.getBid().equals(bid)) { return b; } } return null; } // 全查图书 public void queryAllBooks() { // 遍历输出所有的图书 for (Books b : books) { System.out.println(b); } } // 查询图书数量――馆存数 public int queryBooksNumByBook(Books b) { int num = -1; for (Books temp : books) { if (temp.getBid().equals(b.getBid())) { num = temp.getNumber(); return num; } } return num; } // 通过学号查会员――检验会员是否存在于集合中 public Students queryStudentsBySid(String sid) { // 遍历会员集合 for (Students s : stus) { if (s.getSid().equals(sid)) { return s; } } return null; } // 这个还书借书,我是参考网上的例子,经过多次修改才弄好的。 // 借书 public void lendBooks(Books b, Students s) { Scanner in = new Scanner(System.in); int num; System.out.println("请输入借书的数量:"); num = in.nextInt(); if (queryBooksNumByBook(b) != -1) { if (num > queryBooksNumByBook(b)) { System.out.println("借书数量大于馆藏数量,借书失败!"); return; } else { // 找到要借的图书 Books temp = findById(b.getBid()); // 从集合中移走 books.remove(temp); // 把数量减一 temp.setNumber(temp.getNumber() - num); // 刷新集合中的数据 books.add(temp); // 添加到放书的书包里 s.lendBooks(b, num); System.out.println("借书成功!"); return; } } } // 还书 public void returnBooks(Books b, Students s) { Scanner in = new Scanner(System.in); int num; System.out.println("请输入还书的数量:"); num = in.nextInt(); if (s.getBooksNumByBook(b) != -1) { if (num > s.getBooksNumByBook(b)) { System.out.println("还书数量大于借书数量,还书失败!"); return; } else if (num == s.getBooksNumByBook(b)) { Books temp = findById(b.getBid()); books.remove(temp); temp.setNumber(temp.getNumber() + num); books.add(temp); // 还一本 s.returnBooks(b); System.out.println("还书成功!"); } else { Books temp = findById(b.getBid()); books.remove(temp); temp.setNumber(temp.getNumber() + num); books.add(temp); // 还多本 s.returnBooks(b, num); System.out.println("还书成功!"); } } } // 校验输入图书和已有的图书是否相同 public boolean checkBook(String s) { // 比较书号和书名 boolean flag = false;// 默认为不相同――可以增加此图书 String[] arrStr = s.split("-"); for (Books tmp : books) { // 比较如果有其中一个相同 if (tmp.getBid().equals(arrStr[0]) || tmp.getName().equals(arrStr[1])) { // 设置flag为true flag = true; } } return flag; } //校验输入会员和已有的会员是否相同 public boolean checkUser(String s) { // 比较书号和书名 boolean flag = false;// 默认为不相同――可以增加此图书 String[] arrStr = s.split("-"); for (Students tmp : stus) { // 比较如果有其中一个相同 if (tmp.getSid().equals(arrStr[0]) || tmp.getName().equals(arrStr[1])) { // 设置flag为true flag = true; } } return flag; } }

啦啦啦rain LV1
2024年12月11日
xiaoyu111ewsd LV4
2024年1月7日
叼哉0909 LV1
2023年12月18日
upup996 LV6
2023年9月21日
poipoiiop LV8
2023年1月8日
52java LV1
2022年12月22日
15342201772 LV9
2022年12月4日
18871201024 LV4
2022年12月4日
GYYYYG LV1
2022年11月20日
rongyu LV1
2022年10月16日