Untitled
unknown
plain_text
4 years ago
1.7 kB
9
Indexable
public class Purchase {
private SalableBook[] items;
private int itemCounter;
private String date;
public Purchase(int count, String date) {
items = new SalableBook[count];
this.date = date;
}
public void addItem(SalableBook sobj) {
if (this.itemCounter < this.items.length) {
this.items[itemCounter++] = sobj;
} else {
System.out.println("Purchase is full");
}
}
public SalableBook getBook(int index) {
if (index >= 0 && index < items.length) {
return items[index];
}
return null;
}
public double sump() {
double sum = 0;
for (SalableBook b : items) {
sum += b.computeCost();
}
return sum;
}
public String getInfo(int index) {
SalableBook book = getBook(index);
String info = "";
if (book != null) {
if (book instanceof ElectronicBook) {
ElectronicBook ebook = (ElectronicBook) book;
info += "size:" + ebook.getSize() + ", URL:" + ebook.getUrl();
} else if (book instanceof PaperBook) {
PaperBook pbook = (PaperBook) book;
info += "weight: " + pbook.getWeight() + ", quantity: " + pbook.getQuantity();
}
}
return info;
}
public String toString() {
String s = "\n Listing of item in the purchase " + this.date + "\n";
for (SalableBook p : this.items) {
s += "\n \t-" + p + "\n";
}
s += "End of list of book";
return s;
}
}
Editor is loading...