public class Book {
我们拥有十载网页设计和网站建设经验,从网站策划到网站制作,我们的网页设计师为您提供的解决方案。为企业提供成都做网站、成都网站设计、微信开发、小程序制作、手机网站制作、H5场景定制、等业务。无论您有什么样的网站设计或者设计方案要求,我们都将富于创造性的提供专业设计服务并满足您的需求。
private String title;
private Date pdate;
private int words;
public double price(){
日期这里用一个类处理比较简单,不知道你用没用过
SimpleDateFormat sdf = new SimpleDateFormat("MM");
month = sdf.format(pdate);
double xs = 0d;
if (month = 6){
xs = 1.2;
} else {
xs = 1.17;
}
return words / (1000 * 4.5) * xs;
}
}
这里不好空格,你自己格式化一下吧。
get/set方法没写
自己定义个main方法测一下吧
public class Book {
private String title ;
private String author;
private int sell;
public Book(){
}
public Book(String title,String author,int sell){
this.title=title;
this.author=author;
this.sell=sell;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getSell() {
return sell;
}
public void setSell(int sell) {
this.sell = sell;
}
public String toString() {
return "D [title=" + title + ", author=" + author + ", sell=" + sell
+ "]";
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class HomeWork {
public static void main(String[] args) {
CollectionBook c1 = new ArrayListBook();
CollectionBook c2 = new ArrayListBook();
//~ c1 add data ----------------------------------------------------------------
c1.add(new Book("数据结构","高等教育"));
c1.add(new Book("操作系统","电子工业出版社"));
c1.add(new Book("Java","电子工业出版社"));
//~ c2 add data ----------------------------------------------------------------
c2.add(new Book("操作系统","电子工业出版社"));
c2.add(new Book("数据库系统","清华大学出版社"));
Book searchBook = new Book("Java","机械工业出版社");
System.out.println("c1中是否包含书名为Java,出版社为机械工业出版社的图书信息:"+ (c1.contains(searchBook)?"包含":"不包含"));
c1.removeAll(c2);
System.out.println("差以后c1的元素个数:"+c1.size());
for(IteratorBook iterator = c1.iterator();iterator.hasNext();System.out.println(iterator.next()));
}
}
class Book {
private String name;//书名
private String publish;//出版社
public Book(String name, String publish) {
this.name = name;
this.publish = publish;
}
//~ Setter Getter Method ----------------------------------------------------------------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
//~ Override equals Method ----------------------------------------------------------------
@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 (name == null) {
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
if (publish == null) {
if (other.publish != null)
return false;
}
else if (!publish.equals(other.publish))
return false;
return true;
}
//~ Override toString Method ----------------------------------------------------------------
@Override
public String toString() {
return "Book [name=" + name + ", publish=" + publish + "]";
}
}
我写了一个,方法可能有点麻烦,但是要求实现了
book类:
public class Book {
private String name;
private String publish;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public Book(String name, String publish) {
super();
this.name = name;
this.publish = publish;
}
public Book() {
}
}
测试类:
public class TestBook {
public static void main(String[] args) {
Book book1=new Book("数据结构", "高等教育");
Book book2=new Book("操作系统", "电子工业出版社");
Book book3=new Book("Java", "电子工业出版社");
Book book4=new Book("数据结构", "清华大学出版社");
CollectionBook c1=new ArrayList();
CollectionBook c2=new ArrayList();
CollectionBook c3=new ArrayList();
c1.add(book1);
c1.add(book2);
c1.add(book3);
c2.add(book2);
c2.add(book4);
boolean cont=false;
for(Book book:c1){
if(book.getName().equals("Java")book.getPublish().equals("机械工业出版社")){
cont=true;
}
}
System.out.println(cont);
for(Book book02:c2){
boolean a=true;
for(Book book01:c1){
if(book01.getName().equals(book02.getName())book01.getPublish().equals(book02.getPublish())){
a=false;
}
}
if(a){
c3.add(book02);
}
}
for(Book b:c3){
c1.add(b);
}
IteratorBook iterator=c1.iterator();
while(iterator.hasNext()){
Book b=iterator.next();
System.out.println("书名:"+b.getName());
System.out.println("出版社:"+b.getPublish());
}
}
}