1.程序设计:图书管理器,设计一个图书管理器,实现对图
书进行的存储管理操作,实现功能,
1.添加一本图书(书名、作者(姓名,年龄,性别)、售价)
2.删除一本图书(通过书名删除)
3.删除所有的指定作者的书(通过作者姓名删除)
4.将所有的图书按照图书售价降序排序。若售价相同,按
照作者年龄升序)
public class Book implements Comparable<Book> {
private String bookname;
private Author author;
private int price;
public Book(String bookname, Author author, int price) {
super();
this.bookname = bookname;
this.author = author;
this.price = price;
}
public Book() {
super();
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Book [bookname=" + bookname + ", author=" + author + ", price=" + price + "]";
}
@Override
public int compareTo(Book o) {
if(this.price>o.price) {
return -1;
}else if (this.price<o.price) {
return 1;
}else {
int num = this.author.compareTo(o.author);
return num;
}
}
}
public class Author implements Comparable<Author>{
private String name;
private int age;
private String gender;
public Author(String name, int age, String gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public Author() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Author [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
@Override
public int compareTo(Author o) {
if(this.age>o.age) {
return 1;
}else if (this.age<o.age) {
return -1;
}
return 0;
}
}
import java.util.Iterator;
import java.util.TreeSet;
public class Test3 {
public static void main(String[] args) {
TreeSet<Book> set = new TreeSet<>();
set.add(new Book("西游记", new Author("吴承恩", 40, "男"), 100));
set.add(new Book("红楼梦", new Author("曹雪芹", 30, "男"), 100));
set.add(new Book("水浒传", new Author("施耐庵", 40, "男"), 200));
set.add(new Book("三国演义", new Author("罗贯中", 50, "男"), 200));
Iterator<Book> iterator = set.iterator();
while (iterator.hasNext()) {
Book book = iterator.next();
if("西游记".equals(book.getBookname())){
iterator.remove();
}
}
for (Book book : set) {
System.out.println(book);
}
}
}