Java Reference
In-Depth Information
Discounted?:
Whether the price includes a discount
In stock?:
Whether the item is in stock
We collect this information in a class
ItemQuote.java
. For convenience in viewing the informa-
tion in our program examples, we include a
toString()
method. Throughout this chapter, the
variable
item
refers to an instance of
ItemQuote
.
ItemQuote.java
0
public class ItemQuote {
1
2
public long itemNumber;
// Item identification number
3
public String itemDescription; // String description of item
4
public int quantity;
// Number of items in quote (always >= 1)
5
public int unitPrice;
// Price (in cents) per item
6
public boolean discounted;
// Price reflect a discount?
7
public boolean inStock;
// Item(s) ready to ship?
8
9
public ItemQuote(long itemNumber, String itemDescription,
10
int quantity, int unitPrice, boolean discounted, boolean inStock) {
11
this.itemNumber = itemNumber;
12
this.itemDescription = itemDescription;
13
this.quantity = quantity;
14
this.unitPrice = unitPrice;
15
this.discounted = discounted;
16
this.inStock = inStock;
17
}
18
19
public String toString() {
20
final String EOLN = java.lang.System.getProperty("line.separator");
21
String value = "Item#=" + itemNumber + EOLN +
22
"Description=" + itemDescription + EOLN +
23
"Quantity=" + quantity + EOLN +
24
"Price(each)=" + unitPrice + EOLN +
25
"Total=" + (quantity * unitPrice);
26
if (discounted)
27
value += " (discounted)";
28
if (inStock)
29
value += EOLN + "In Stock" + EOLN;
30
else
31
value += EOLN + "Out of Stock" + EOLN;
32
return value;
33
}
34
}
ItemQuote.java
Search WWH ::
Custom Search