/**
* 定义图书类Book,具有属性账号id,书名name、作者author 和价格price,
在创建图书对象时要求通过构造器进行创建,一次性将四个属性全部赋值,
要求账户属性是int型,名称是String型,作者是String型,价格是double,
请合理进行封装。
2)在Book类,添加toString方法,要求返回 图书信息字符串,使用\t隔开各信息
3)要求定义一个图书馆Library类,在图书馆类中添加一个HashSet集合用于保存多本图书
4)在图书馆类中要求能够新增图书
5)在图书馆类中要求可以查看所有添加过的图书
6)不允许添加重复的图书(如果账号id和书名name相同,则认为两本书是相同的)
效果:
请输入图书编号:
1
请输入图书名称:
致青春
请输入图书作者:
王小五
请输入图书价格:
120
是否继续输入:y
请输入图书编号:
2
请输入图书名称:
西游记
请输入图书作者:
吴承恩
请输入图书价格:
230
是否继续输入:n
查看图书
1,致青春,王小五,120
2,西游记,吴承恩,230
* @author King_long
*
*/
package CollectionDemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
public class Library{
public static void main(String[] args) {
// TODO Auto-generated method stub
addTS();
}
//添加
public static void addTS() {
HashSet<Book> hashSet=new HashSet<>();
Scanner scanner=new Scanner(System.in);
do {
System.out.println("请输入账号id:");
int id=scanner.nextInt();
System.out.println("请输入图书名称:");
String bookName=scanner.next();
System.out.println("请输入作者:");
String author=scanner.next();
System.out.println("请输入图书价格:");
double price=scanner.nextDouble();
hashSet.add(new Book(id, bookName, author, price));
System.out.println("是否继续,请输入(y/n):");
String string=scanner.next();
if ("y".equals(string)) {
System.out.println(hashSet);
}else {
System.out.println("查看图书");
break;
}
}while(true);
ArrayList<Book> arrayList=new ArrayList<>(hashSet);
Collections.sort(arrayList);
Iterator<Book> iterator = arrayList.iterator();
while (iterator.hasNext()) {
Book book = (Book) iterator.next();
System.out.println(book);
}
}
}
package CollectionDemo;
public class Book implements Comparable<Book>{
private int id;
private String bookName;
private String author;
private double price;
public Book() {
super();
}
public Book(int id, String bookName, String author, double price) {
super();
this.id = id;
this.bookName = bookName;
this.author = author;
this.price = price;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((bookName == null) ? 0 : bookName.hashCode());
result = prime * result + id;
return result;
}
public int getId() {
return id;
}
@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 (bookName == null) {
if (other.bookName != null)
return false;
} else if (!bookName.equals(other.bookName))
return false;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "Book [id=" + id +"\t" + bookName + "\t" + author +"\t" + price + "]";
}
@Override
public int compareTo(Book o) {
// TODO Auto-generated method stub
return this.getId()-o.getId();
}
}