基于Collection集合图书管理系统的设计与实现

本文档介绍了使用Java设计并实现的基于Collection集合的图书管理系统,包括系统功能模块、管理员和用户权限管理,以及图书属性和操作的方法。通过这些模块,实现了图书的高效管理和用户操作的权限控制。
摘要由CSDN通过智能技术生成

需求:

在这里插入图片描述

测试类:

package pers.sheng.demo;

import java.util.Scanner;

public class Test {
	static Admin admin = new Admin();
	static User user = new User();
	public static void main(String[] args) throws InterruptedException {	
		welcomeInterface();
	}
//	加载欢迎界面
	public static void welcomeInterface() throws InterruptedException {
		System.out.println("┌────────────────────┐");
		System.out.print("|");
		for(int i=0;i<14;i++)
		{
			System.out.print("██");
			if(i<5) {
				Thread.sleep(300);
				i++; 
			}
			if(i>10) {
				Thread.sleep(200);
				i++; 
			}
			else{
				Thread.sleep(100);
			}
		}
		System.out.println("|");
		System.out.println("└────────────────────┘");
		System.out.println("             欢迎进入图书管理系统");
		loginInterface();
	}
//	用户模块
	public static void userInterface() throws InterruptedException {
		while(true) {
			System.out.println("┏━━━━━━用户功能选择━━━━━┓");
			System.out.println("┃    1. 图书查询                    ┃");
			System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
			System.out.println("┃    2. 返回                            ┃");
			System.out.println("┗━━━━━━━━━━━━━━━━━━━┛");
			select();
			Scanner input = new Scanner(System.in);
			switch(input.nextInt()) {
			case 1:
				userFindInterface();
				break;
			case 2:
				loginInterface();
				break;
			default:
				break;
			}
		}
	}
//	管理员模块
	public static void adminInterface() throws InterruptedException  {
		while(true) {
			System.out.println("┏━━━━━━管理功能选择━━━━━┓");
			System.out.println("┃    1. 图书新增                    ┃");
			System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
			System.out.println("┃    2. 图书修改                    ┃");
			System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
			System.out.println("┃    3. 图书删除                    ┃");
			System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
			System.out.println("┃    4. 图书查询                    ┃");
			System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
			System.out.println("┃    5. 返回                            ┃");
			System.out.println("┗━━━━━━━━━━━━━━━━━━━┛");
			select();
			Scanner input = new Scanner(System.in);
			switch(input.nextInt()) {
			case 1://增
				admin.adminAdd();
				break;
			case 2://改
				admin.adminMod();
				break;
			case 3: //删
				admin.adminDel();
				break;
			case 4://adminFindInterface();
				break;
			case 5://返回上一界面
				loginInterface();
				break;
			}
		}
	}
//	选择
	public static void select() throws InterruptedException {
		System.out.print("请");
		Thread.sleep(100);
		System.out.print("选");
		Thread.sleep(100);
		System.out.print("择");
		Thread.sleep(100);
		System.out.print(":");
	}
//	登录身份选择
	public static void loginInterface() throws InterruptedException {
		while(true) {
			System.out.println("┏━━━━━━登录身份选择━━━━━┓");
			System.out.println("┃    1. 普通用户                    ┃");
			System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
			System.out.println("┃    2. 管理员                        ┃");
			System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
			System.out.println("┃    3. 退出系统                    ┃");
			System.out.println("┗━━━━━━━━━━━━━━━━━━━┛");
			select();
			
			Scanner input = new Scanner(System.in);
				switch(input.nextInt()) {
				case 1:
//					登录模块
					user.userLogin();
//					调用用户功能模块
					userInterface();
					break;
				case 2:
//					登录模块
					admin.adminLogin();
//					跳到管理员模块
					adminInterface();
					break;
				case 3:
					System.out.println("-------------------------------【系统已退出】如需再次使用,请再次启动程序!");
//					0代表正常终止java虚拟机,即结束程序
					System.exit(0);
			}	
		}
	}
//	用户查询功能选择
	public static void userFindInterface() throws InterruptedException {
		System.out.println("┏━━━━━━查询功能选择━━━━━┓");
		System.out.println("┃    1. 查询全部图书            ┃");
		System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
		System.out.println("┃    2. 查询单本图书            ┃");
		System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
		System.out.println("┃    3. 返回                            ┃");
		System.out.println("┗━━━━━━━━━━━━━━━━━━━┛");
		select();
		Scanner input = new Scanner(System.in);
		switch(input.nextInt()) {
			case 1:
				user.userFindAll();
				break;
			case 2:
				user.userFind();
				break;
			case 3:
				userInterface();
				break;
		}
	}
//	管理员查询功能选择
	public static void adminFindInterface() throws InterruptedException {
		System.out.println("┏━━━━━━查询功能选择━━━━━┓");
		System.out.println("┃    1. 查询全部图书            ┃");
		System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
		System.out.println("┃    2. 查询单本图书            ┃");
		System.out.println("┃━━━━━━━━━━━━━━━━━━━┃");
		System.out.println("┃    3. 返回                            ┃");
		System.out.println("┗━━━━━━━━━━━━━━━━━━━┛");
		select();
		Scanner input = new Scanner(System.in);
		switch(input.nextInt()) {
			case 1:
				admin.adminFindAll();
				break;
			case 2:
				admin.adminFind();
				break;
			case 3:
				adminInterface();
				break;
		}
	}
}

系统功能模块类:

package pers.sheng.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

public interface BookSystem {
	public static HashMap<String,String> account = new HashMap<>();
	public static Book book = new Book();
	Comparator findP1 = new HightToLow();
	public static HashSet set = new HashSet();
	public static List generateId = new ArrayList();
	Random r = new Random();
	
//	自动生成不重复的图书编号
	public static long bookId() {
		long id = Math.abs(r.nextLong());
		while(generateId.contains(id)) {
			id = Math.abs(r.nextLong());
		}
		return id;
	}
//	注册功能模块
	boolean reg(String account,String password);
 //	登录功能模块
	boolean log(String account,String password);
//	添加图书功能模块
	public static void add(String name,String info,String author,String date,double price) {
		Object findBook;
		boolean ifNull;
		boolean ifContain = false;
		for(Iterator ite = set.iterator();ifNull = ite.hasNext();) {
			findBook = ite.next();
			if(name.equals(((Book) findBook).getName())) {
				ifContain = true;
			}
		}
		if(ifNull || !ifContain) {
			set.add(new Book(name,bookId(),info,author,date,price));
		}
	}
//	修改图书功能模块
	public static void mod(String oldName,Integer oldId,String newName,String newInfo,String newAuthor,String newDate,double newPrice) {
		Book findBook;
		set.hashCode();
		for(Iterator ite = set.iterator();ite.hasNext();) {
			findBook = (Book)ite.next();
			if(oldName.equals(findBook.getName()) && oldId==findBook.getId()) {
				del(oldName, oldId);
				add(newName,newInfo,newAuthor,newDate,newPrice);
			}
		}
		
	}
//	删除图书功能模块
	public static void del(String name,Integer id) {
		Object findBook;
		for(Iterator ite = set.iterator();ite.hasNext();) {
			findBook = ite.next();
			if(name.equals(((Book) findBook).getName()) && id==((Book) findBook).getId()) {
				set.remove(findBook);
			}
		}
	}
//	查找所有图书功能模块
	public static void findAll(Comparator o) {
		TreeSet ts = new TreeSet(o);
		for(Iterator ite = set.iterator();ite.hasNext();) {
			 ts.add(ite.next());
		}
		for(Iterator ite = ts.iterator();ite.hasNext();) {
			System.out.println(ite.next().toString());
		}
	}
//	根据图书名称查找图书功能模块
	public static Object find(String name) {
		Book findBook = null;
		boolean isFind = false;
		for(Iterator ite = set.iterator();ite.hasNext();) {
			findBook = (Book)ite.next();
			if(name.equals(findBook.getName())) {
				isFind = true;
			}
		}
		if(isFind==false) {
			return null;
		}else {
			return findBook;
		}
	}
}
//从高到低排序
class HightToLow implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		Book b1 = (Book)o1;
		Book b2 = (Book)o2;
		return (int) (b1.getPrice() - b2.getPrice());
	}
}
//从低到高排序
class LowToHight implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		Book b1 = (Book)o1;
		Book b2 = (Book)o2;
		return (int) Math.abs((b1.getPrice() - b2.getPrice()));
	}	
}
//从新的日期到旧的日期排序
class NewToOld implements Comparator{

	@Override
	public int compare(Object o1, Object o2) {
		Book b1 = (Book)o1;
		Book b2 = (Book)o2;
		Date date1 = null;
		Date date2 = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date1 = format.parse(b1.getDate());
			date2 = format.parse(b2.getDate());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (int)(-(date1.getTime()-date2.getTime()));
	}
}

管理员权限:

package pers.sheng.demo;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Admin implements BookSystem{
	
	public void adminLogin() throws InterruptedException {
		Scanner input = new Scanner(System.in);
		reg("admin","admin");
		for(String account="0",password="0";!log(account,password);) {
			System.out.print("请输入登录账号:");
			account = input.next();
			System.out.print("请输入登录密码:");
			password = input.next();
			System.out.print("登录中");
			for(int i=0;i<10;i++) {
				Thread.sleep(100);
				System.out.print(".");
			}
			System.out.println();
		}
		System.out.println("              【登录成功】");
	}
	
	public void adminAdd() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入书籍名称:");
		String name = input.next();
		System.out.print("请输入书籍简介:");
		String info = input.next();
		System.out.print("请输入书籍作者:");
		String author = input.next();
		System.out.print("请输入出版日期【日期输入按照xxxx-xx-xx格式】:");
		String date = input.next();
		System.out.print("请输入书籍价格:¥");
		double price = input.nextDouble();
		BookSystem.add(name, info, author,date, price);
	}
	
	public void adminFindAll() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入查询方式(1-价格从高到低排序|2-价格从低到高排序|3-出版日期新旧排序):");
		switch(input.nextInt()) {
			case 1:
				BookSystem.findAll(new HightToLow());
				break;
			case 2:
				BookSystem.findAll(new LowToHight());
				break;
			case 3:
				BookSystem.findAll(new NewToOld());
				break;
			default:
				break;
		}
	}
	
	public void adminDel() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入需要删除的书籍名称:");
		String oldName = input.next();
		System.out.print("请输入需要删除的书籍编号:");
		int oldId = input.nextInt();
		BookSystem.del(oldName, oldId);
	}

	public void adminMod() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入需要修改的书籍名称:");
		String oldName = input.next();
		System.out.print("请输入需要修改的书籍编号:");
		int oldId = input.nextInt();
		System.out.print("请输入新的书籍名称:");
		String name = input.next();
		System.out.print("请输入新的书籍简介:");
		String info = input.next();
		System.out.print("请输入新的书籍作者:");
		String author = input.next();
		System.out.print("请输入新的出版日期【日期输入按照xxxx-xx-xx格式】:");
		String date = input.next();
		System.out.print("请输入新的书籍价格:¥");
		double price = input.nextDouble();
		BookSystem.mod(oldName, oldId,name, info, author, date, price);
	}


	public void adminFind() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入需要查询的书籍名称:");
		String name = input.next();
		System.out.println(BookSystem.find(name));
	}

	@Override
	public boolean log(String account, String password) {
		if(BookSystem.account.containsKey(account) && BookSystem.account.get(account).equals(password)){
			return true;
		}
		return false;
	}

	@Override
	public boolean reg(String account, String password) {
		BookSystem.account.put(account, password);
			return true;
	}
}

用户权限:

package pers.sheng.demo;

import java.util.Scanner;

public class User implements BookSystem{
	
	public void userLogin() throws InterruptedException {
		Scanner input = new Scanner(System.in);
		for(String account="0",password="0";!log(account,password);) {
			System.out.print("请输入登录账号(如未注册,请输入1):");
			account = input.next();
			if(account.equals("1")) {
				userRegister();
				continue;
			}
			System.out.print("请输入登录密码:");
			password = input.next();
			System.out.print("登录中");
			for(int i=0;i<10;i++) {
				Thread.sleep(100);
				System.out.print(".");
			}
			System.out.println();
		}
		System.out.println("              【登录成功】");
	}
	
	public void userRegister() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入设置账号:");
		String rAccount = input.next();
		System.out.print("请输入设置密码:");
		String rPassword = input.next();
		reg(rAccount, rPassword);
	}
	
	public void userFindAll() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入查询方式(1-价格从高到低排序|2-价格从低到高排序|3-出版日期新旧排序):");
		switch(input.nextInt()) {
			case 1:
				BookSystem.findAll(new HightToLow());
				break;
			case 2:
				BookSystem.findAll(new LowToHight());
				break;
			case 3:
				BookSystem.findAll(new NewToOld());
				break;
			default:
				break;
		}
	}
	
	public void userFind() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入需要查询的书籍名称:");
		String name = input.next();
		System.out.println(BookSystem.find(name));
	}

	@Override
	public boolean log(String account, String password) {
		if(BookSystem.account.containsKey(account) && BookSystem.account.get(account).equals(password)){
			return true;
		}
		return false;
	}
	
	@Override
	public boolean reg(String account, String password) {
		if(BookSystem.account.put(account, password)==null) {
			return true;
		}else {
			return false;
		}
	}
}

图书属性方法类:

package pers.sheng.demo;

import java.util.Random;

public class Book {
	private String name;
	private long id;
	private String info;
	private String author;
	private String date;
	private double price;
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Book(String name, long id, String info, String author, String date, double price) {
		super();
		this.name = name;
		this.id = id;
		this.info = info;
		this.author = author;
		this.date = date;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDate() {
		return date;
	}

	public void setData(String date) {
		this.date = date;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() { 
		return "\n图书信息\n -书籍名称:" + name +"\n"+ " -书籍编号:"+id  +"\n"+ " -书籍简介:"+info  +"\n"+ " -书籍作者:"+author  +"\n"+ " -出版日期:"+date  +"\n"+ " -书籍价格:"+price +"\n\n" ;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((author == null) ? 0 : author.hashCode());
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((info == null) ? 0 : info.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		long temp;
		temp = Double.doubleToLongBits(price);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (author == null) {
			if (other.author != null)
				return false;
		} else if (!author.equals(other.author))
			return false;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (id != other.id)
			return false;
		if (info == null) {
			if (other.info != null)
				return false;
		} else if (!info.equals(other.info))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
			return false;
		return true;
	}
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值