基于Collection集合完成图书管理系统

直接贴代码:

主类:定义了 管理员登陆和图书管理的一系列方法

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class ManageSystem {

	public static void main(String[] args) {
		Administrator p1 = new Administrator("张三","1213");
		Administrator p2 = new Administrator("李四","1113");

		//通过HashSet来存储管理员对象,通过重写equals和hashCode,只要用户名相同,便判定为同一对象
		HashSet<Administrator> set = new HashSet<>();
		set.add(p1);
		set.add(p2);
		//通过HashSet来存储图书对象,通过重写equals和hashCode,保证图书编号不重复
		Book b1 = new Book("英语","001","英语教科书","Lisa","2019-10-10",29);
		Book b2 = new Book("数学","002","数学教科书","Zua","2019-08-10",49);
		Book b3 = new Book("语文","003","语文教科书","Wen","2019-01-10",35);
		Book b4 = new Book("化学","004","化学教科书","Ruc","2019-02-10",25);
		HashSet<Book> list = new HashSet<>();
		list.add(b1);
		list.add(b2);
		list.add(b3);
		list.add(b4);
		
		//管理员登录,若登录不成功,则一直停留在登陆界面
		while(!UserLogin(set)) {
			
		}
		
		bookManage(list);
		
	}
	
	/**
	 * 管理员登录
	 * @param 管理员集合
	 * @return 登陆成功,返回true,否则返回false
	 *
	 * */
	public static boolean UserLogin(HashSet<Administrator> set) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String name = input.nextLine();
		
		int count=0;
		for(Administrator user:set) {
			if(user.getName().equals(name)) {
				count++;
			}
		
		    if(count==1) {  //若用户名匹配,则判断密码是否匹配
				System.out.println("请输入密码:");
				String passWord = input.nextLine();
				if(user.getPassWord().equals(passWord)) {
					System.out.println("登陆成功");
					return true;
				}else {
					System.out.println("密码错误");
					return false;
				}
		    }
		}
		if(count==0) {
			System.out.println("没有该用户");
		}
		return false;
	}
	
	/**
	 * 管理图书
	 * @param 图书的集合
	 * @return 
	 * */
	public static void bookManage(HashSet<Book> list) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您要进行的操作:\n1.图书新增\n2.图书修改\n3.图书删除\n4.查找图书\n5.查看所有图书 ");
		
		int choose = input.nextInt();
	    if(choose==1) {
	    	bookInsert(list);
	    }else if(choose==2) {
	    	bookModify(list);
	    }else if(choose==3) {
	    	bookDelete(list);
	    }else if(choose==4) {
	    	bookFind(list);
	    }else if(choose==5) {
	    	bookAll(list);
	    }else {
	    	System.out.println("您输入的数字有误");
	    }
	}
	
	/**
	 * 图书新增
	 * @param 图书的集合
	 * */
	public static void bookInsert(HashSet<Book> list) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入图书的名称:");
    	String name = input.nextLine();
    	System.out.println("请输入图书的编号");
    	String num = input.nextLine();
    	System.out.println("请输入图书的简介");
    	String introduce = input.nextLine();
    	System.out.println("请输入图书的作者");
    	String author = input.nextLine();
    	System.out.println("请输入图书的出版日期");
    	String data = input.nextLine();
    	System.out.println("请输入图书的价格");
    	double price = input.nextDouble();
    	
    	Book book = new Book(name,num,introduce,author,data,price);
    	list.add(book);
    	/*输出全部图书的信息
    	for(Book book1:list) {
			System.out.println(book1);
		}*/
	}
	
	/**
	 *图书修改
	 *@param 图书的集合
	 **/
	public static void bookModify(HashSet<Book> list) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入要修改的图书的编号");
		String num = input.nextLine();
		int count=0;    //作为查找到该图书的标记
		for(Book book:list) {    //遍历查找该图书
			if(book.getNum().equals(num)) {
				count++;
				System.out.println("1.修改名称  2.修改编号 3.修改简介 4.修改作者 5.修改出版日期 6.修改价格");
				int modify = input.nextInt();
				System.out.println("请输入您要修改的内容");
				String str = input.next();
				if(modify==1) {
					book.setName(str);
					System.out.println("修改完成");
				}else if(modify==2) {
					book.setNum(str);
					System.out.println("修改完成");
				}else if(modify==3) {
					book.setIntroduce(str);
					System.out.println("修改完成");
				}else if(modify==4) {
					book.setAuthor(str);
					System.out.println("修改完成");
				}else if(modify==5) {
					book.setDate(str);
					System.out.println("修改完成");
				}else if(modify==6) {
					double price = Double.parseDouble(str);
					book.setPrice(price);
					System.out.println("修改完成");
				}
				break;
			}
		}
		if(count==0) {
			System.out.println("没有查找到您要修改的图书");
		}
		/*输出全部图书的信息
    	for(Book book1:list) {
			System.out.println(book1);
		}*/
	}
	
	/**
	 * 图书删除
	 * @param 图书的集合
	 * */
	public static void bookDelete(HashSet<Book> list) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您要删除的图书的编号:");
		String num = input.nextLine();
		int count = 0;
		for(Book book:list) {
			if(book.getNum().equals(num)) {
				count++;
				list.remove(book);
				break;
			}
		}
		if(count==0) {
			System.out.println("没有查找到您要删除的图书");
		}
		/*输出全部图书的信息
    	for(Book book1:list) {
			System.out.println(book1);
		}*/
		
	}
	
	/**
	 * 根据名称查找图书
	 * @param 图书的集合
	 * */
	public static void bookFind(HashSet<Book> list) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您要查找的图书的名称");
		String name = input.nextLine();
		int count = 0;
		for(Book book:list) {
			if(book.getName().equals(name)) {
				count++;
				System.out.println(book);
			}
		}
		if(count==0) {
			System.out.println("没有查找到您要查找的图书");
		}
		
	}

	/**
	 *查看所有图书
	 *@param 图书的集合
	 **/
	public static void bookAll(HashSet<Book> list) {
		Scanner input = new Scanner(System.in);
		//通过遍历将HashSet里的放进ArrayList,便于排序
		ArrayList<Book> data = new ArrayList<>();
		for(Book book:list) {
			data.add(book);
		}
		System.out.println("请选择图书的排序:\n1.价格从高到低排序\n2.价格从低到高排序 \n3.新旧排序");
		int choose = input.nextInt();
		if(choose==1) {
			MyCompareDescend descend = new MyCompareDescend();
			Collections.sort(data,descend);
			for(Book book:data) {    // 遍历输出排序后的集合
				System.out.println(book);
			}
		}else if(choose==2) {
			MyCompareAscend ascend = new MyCompareAscend();
			Collections.sort(data,ascend);
			for(Book book:data) {
				System.out.println(book);
			}
		}else if(choose==3) {
			MyCompareData dataOrder = new MyCompareData();
			Collections.sort(data,dataOrder);
			for(Book book:data) {
				System.out.println(book);
			}
		}else {
			System.out.println("请输入正确的数字");
		}
		
	}
	

管理员类:属性只有账户名跟密码

重写hashcode和equals,用户名不能重复

public class Administrator {
	private String name;
	private String passWord;
	
	public Administrator(String name,String passWord) {
		this.name=name;
		this.passWord=passWord;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Administrator other = (Administrator) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}


	public String getName() {
		return name;
	}

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

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

}

图书类:图书编号不重复

public class Book {
	private String name;
	private String num;
	private String introduce;
	private String author;
	private String date;
	private double price;
	
	public Book(String name,String num,String introduce,String author,String date,double price) {
		this.name=name;
		this.num=num;
		this.introduce=introduce;
		this.author=author;
		this.date=date;
		this.price=price;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((num == null) ? 0 : num.hashCode());
		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 (num == null) {
			if (other.num != null)
				return false;
		} else if (!num.equals(other.num))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Book [名称=" + name + ", 编号=" + num + ", 简介=" + introduce + ", 作者=" + author + ", 出版日期="
				+ date + ", 价格=" + price + "]";
	}

	public String getName() {
		return name;
	}

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

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getIntroduce() {
		return introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public String getAuthor() {
		return author;
	}

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

	public String getDate() {
		return date;
	}

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

	public double getPrice() {
		return price;
	}

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

}

下面的是三个自定义的比较类用于排序,继承了 Comparator 接口,重写了 比较方法

/**
 * 自定义比较类,继承Comparator接口,用于将对象比较排序
 * 根据价格来降序排序
 * */
public class MyCompareDescend implements Comparator<Book>{

	@Override
	public int compare(Book b1, Book b2) {
		if(b1.getPrice()<b2.getPrice()) {
			return 1;
		}else if(b1.getPrice()==b2.getPrice()) {
			return 0;
		}
		return -1;
	}
}
/**
 * 根据价格来升序排序
 * */
class MyCompareAscend implements Comparator<Book>{
	@Override
	public int compare(Book b1, Book b2) {
		if(b1.getPrice()>b2.getPrice()) {
			return 1;
		}else if(b1.getPrice()==b2.getPrice()) {
			return 0;
		}
		return -1;
	}
}
/**
 * 根据日期新旧来排序,将日期转换为时间戳进行排序
 * */
class MyCompareData implements Comparator<Book>{

	@Override
	public int compare(Book b1, Book b2){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = new Date();
		try {
			date1 = format.parse(b1.getDate());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date date2 = new Date();
		try {
			date2 = format.parse(b2.getDate());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time1 = date1.getTime();  
		long time2 = date2.getTime(); 
		
		if(time1>time2) {
			return 1;
		}else if(time1==time2) {
			return 0;
		}
		return -1;
	}
	
}

 

 

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页