Java Reference
In-Depth Information
Display 8.1
The Base Class
Sale
(part 2 of 3)
41
/**
42
Precondition: newPrice is nonnegative.
43
*/
44
public void
setPrice(
double
newPrice)
45 {
46
if
(newPrice >= 0)
47 price = newPrice;
48
else
49 {
50 System.out.println("Error: Negative price.");
51 System.exit(0);
52 }
53 }
54
public
String getName()
55 {
56
return
name;
57 }
58
/**
59
Precondition: newName is a nonempty string.
60
*/
61
public void
setName(String newName)
62 {
63
if
(newName !=
null
&& newName != "")
64 name = newName;
65
else
66 {
67 System.out.println("Error: Improper name value.");
68 System.exit(0);
69 }
70 }
71
public
String toString()
72 {
73
return
(name + " Price and total cost = $" + price);
74 }
75
public double
bill()
76 {
77
return
price;
78 }