java救命 ><" 希望大大能帮忙写写. 感激不尽

2025-01-05 07:13:49
推荐回答(2个)
回答1:

/**
* 书本类
* @author zp
* @version 0.0.1 下午01:35:21 2010-6-13
*/
public class Book {

private String title;

private String author;

private String publisher;

private float price;

public Book(String title, String author, String publisher, float price) {
super();
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}

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 String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public float getPrice() {
return price;
}

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

}

import java.util.LinkedList;
import java.util.List;

/**
* 图书收藏管理类
* @author zp
* @version 0.0.1 下午01:37:26 2010-6-13
*/
public class BookManager {

private List bks = new LinkedList();

public int getSize() {
return bks.size();
}

public float getTotalPrice() {
float tp = 0f;
for (Book book : bks) {
tp += book.getPrice();
}
return tp;
}

public float getAveragePrice() {
if (getSize() == 0)
return 0;
return getTotalPrice() / getSize();
}

public Book getHighestPriceBook() {
Book tp = null;
for (Book book : bks) {
if (null == tp)
tp = book;
else
tp = tp.getPrice() > book.getPrice() ? tp : book;
}
return tp;
}

public Book getLowestPriceBook() {
Book tp = null;
for(Book book : bks) {
if (null == tp)
tp = book;
else
tp = tp.getPrice() > book.getPrice() ? book : tp;
}
return tp;
}

public void addBook(Book book) {
bks.add(book);
}

public void removeBook(Book book) {
bks.remove(book.getTitle());
}

public void show() {
System.out.println("书籍数量:" + getSize());
System.out.println("总价:" + getTotalPrice());
System.out.println("平均价:" + getAveragePrice());
System.out.println("书单:");
System.out.println(" 书名 作者 出版社 价格(元)");
for(Book book : bks){
System.out.println(book.getTitle()+" "+book.getAuthor()+" "+book.getPublisher()+" "+book.getPrice());
}
System.out.println("最便宜:");
Book book = getLowestPriceBook();
if(null!=book)
System.out.println(book.getTitle()+" "+book.getAuthor()+" "+book.getPublisher()+" "+book.getPrice());
System.out.println("最昂贵:");
book = getHighestPriceBook();
if(null!=book)
System.out.println(book.getTitle()+" "+book.getAuthor()+" "+book.getPublisher()+" "+book.getPrice());

}

public static void main(String[] args) {
BookManager manage = new BookManager();
manage.addBook(new Book("第一本书","无名","百度",65.00f));
manage.addBook(new Book("第二本书","有名","百度",95.00f));
manage.addBook(new Book("第三本书","花名","百度",48.00f));
manage.addBook(new Book("第四本书","小命","百度",57.00f));
manage.show();
}
}

回答2:

你hi我吧。。题目不太清楚。