-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineItem.java
More file actions
127 lines (100 loc) · 2.73 KB
/
LineItem.java
File metadata and controls
127 lines (100 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.io.Serializable;
/*
* This class creates a LineItem object with the attributes:
* Pizza
* numPizza (Number of pizzas)
*
* Provides accessors for both the pizza and the numPizza attributes. Allows numPizza to be changed. The pizzas'
* cost is also calculated with the discount implemented. A String containing all the information about the
* LineItem can be generated. Furthermore, two LineItems can be compared based on their costs.
*
* @Author Aditi Srinivasan
* @NetID 18as11
*/
public class LineItem implements Comparable<LineItem>, Serializable
{
// Stores the number of pizzas ordered
private int numPizza = 0;
// Stores the pizza ordered
private Pizza pizza;
/*
* Constructs a LineItem with the attributes pizza and numPizza (number of pizzas). If the Pizza provided is null,
* IllegalPizza is thrown.
*/
public LineItem(int n, Pizza p) throws IllegalPizza
{
// Set attributes
setNumber(n);
pizza = p;
// Search for errors. Throw IllegalPizza if errors are found
if(p==null)
throw new IllegalPizza("That's not a pizza!");
} // End constructor
/*
* Constructs a LineItem with the attribute pizza. The number of pizzas is assumed to be 1. If the Pizza
* provided is null, IllegalPizza is thrown.
*/
public LineItem(Pizza p) throws IllegalPizza
{
// Set attributes
setNumber(1);
pizza = p;
// Search for errors. Throw IllegalPizza if errors are found
if(p==null)
throw new IllegalPizza("That's not a pizza!");
} // End constructor
/*
* accessor for the pizza attribute
*/
public Pizza getPizza()
{
return pizza;
} // End getPizza
/*
* Set the number of pizzas ordered to an integer between 1-100
*/
public void setNumber(int i) throws IllegalPizza
{
if(i<1 || i>100)
throw new IllegalPizza ("Illegal number of pizzas!!!");
numPizza=i;
} // End numPizza
/*
* Accessor for numPizza (number of pizzas)
*/
public int getNumber()
{
return numPizza;
} // End getNumber
/*
* Finds the cost of the LineItem with the discount for bulk ordering if applicable
*/
public double getCost()
{
double cost;
if(numPizza>=10 && numPizza<=20)
cost = 0.9*numPizza*pizza.getCost();
else if(numPizza>20)
cost = 0.85*numPizza*pizza.getCost();
else
cost = numPizza*pizza.getCost();
return cost;
} // End getCost
/*
* Generates a String of all the attributes
*/
public String toString() {
if(numPizza<10)
return " "+numPizza+" "+pizza;
else
return numPizza+" "+pizza;
} // end toString
/*
* Compare the price of 2 LineItems. Return the difference between the prices.
*/
public int compareTo(LineItem lt)
{
int compare = (int)(lt.getCost() - getCost());
return compare;
} // End compareTo
}