-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaggage.java
More file actions
128 lines (113 loc) · 4.21 KB
/
Baggage.java
File metadata and controls
128 lines (113 loc) · 4.21 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
128
/**
* The Baggage class represents a piece of baggage with specific dimensions and weight.
* It includes methods to get and set the dimensions and weight of the baggage.
* It also provides functionality to check if the baggage can be used as a carry-on and to calculate any applicable surcharges.
*/
public class Baggage {
// Dimensions of the baggage in centimeters
private int length, width, height;
// Weight of the baggage in kilograms
private double weight;
// Default values for dimensions and weight
private static final int DEFAULT_LENGTH = 65;
private static final int DEFAULT_WIDTH = 25;
private static final int DEFAULT_HEIGHT = 50;
private static final double DEFAULT_WEIGHT = 10.0;
/**
* Default constructor initializes baggage with default dimensions and weight.
*/
public Baggage() {
this(DEFAULT_LENGTH, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_WEIGHT);
}
/**
* Parameterized constructor initializes baggage with specified dimensions and weight.
*
* @param length The length of the baggage in centimeters.
* @param width The width of the baggage in centimeters.
* @param height The height of the baggage in centimeters.
* @param weight The weight of the baggage in kilograms.
*/
public Baggage(int length, int width, int height, double weight) {
this.length = length;
this.width = width;
this.height = height;
this.weight = weight;
}
/**
* Gets the length of the baggage.
* @return The length of the baggage in centimeters.
*/
public int getLength() {
return length;
}
/**
* Sets the length of the baggage.
* @param length The length of the baggage in centimeters.
*/
public void setLength(int length) {
this.length = length;
}
/**
* Gets the width of the baggage.
* @return The width of the baggage in centimeters.
*/
public int getWidth() {
return width;
}
/**
* Sets the width of the baggage.
* @param width The width of the baggage in centimeters.
*/
public void setWidth(int width) {
this.width = width;
}
/**
* Gets the height of the baggage.
* @return The height of the baggage in centimeters.
*/
public int getHeight() {
return height;
}
/**
* Sets the height of the baggage.
* @param height The height of the baggage in centimeters.
*/
public void setHeight(int height) {
this.height = height;
}
/**
* Gets the weight of the baggage.
* @return The weight of the baggage in kilograms.
*/
public double getWeight() {
return weight;
}
/**
* Sets the weight of the baggage.
* @param weight The weight of the baggage in kilograms.
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* Returns a string representation of the baggage, including its dimensions and weight.
* Also indicates whether the baggage can be used as a carry-on and if there is any weight surcharge.
* @return A formatted string describing the baggage and any applicable carry-on or surcharge information.
*/
@Override
public String toString() {
String result = String.format("This bag has length %d cm, width %d cm, height %d cm, and weight %.2f kg.\n", length, width, height, weight);
// Check if the baggage can be used as a carry-on
if (BaggageChecker.isCarryOn(this)) {
result += "This bag can be used as a carry-on.\n";
}
// Check if the baggage is overweight and calculate the surcharge if applicable
if (BaggageChecker.allowedWeightIsMoreThanMax(this)) {
double surcharge = BaggageChecker.calculateWeightSurcharge(this);
double maxRegularWeight = BaggageChecker.MAX_REGULAR_WEIGHT;
result += String.format("This bag is overweight; it exceeds the allowed weight of %.2f kg. A surcharge of $%.2f applies.\n",
maxRegularWeight, surcharge);
}
return result;
}
}