-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigBoxLogistics.java
More file actions
51 lines (44 loc) · 1.26 KB
/
BigBoxLogistics.java
File metadata and controls
51 lines (44 loc) · 1.26 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
import java.util.ArrayList;
import java.util.List;
// Implement interface
interface PackageInterface {
void addSubPackage(Package pkg);
int getTotalWeight();
}
class Package implements PackageInterface {
private int weight;
private List<Package> subPackages;
public Package(int weight) {
this.weight = weight;
this.subPackages = new ArrayList<>();
}
// Add a subpackage
public void addSubPackage(Package pkg) {
subPackages.add(pkg);
}
// Calculate total weight
public int getTotalWeight() {
int totalWeight = this.weight; // Initial weight
for (Package p : subPackages) {
totalWeight += p.getTotalWeight(); // Add weight with each sub-package
}
return totalWeight;
}
}
// Test cases
class BigBoxLogistics {
public static void main(String[] args) {
// Test Case 1
Package p1 = new Package(1);
Package p2 = new Package(2);
p1.addSubPackage(p2);
System.out.println(p1.getTotalWeight());
// Test Case 2
p1 = new Package(1);
p2 = new Package(2);
Package p3 = new Package(3);
p1.addSubPackage(p2);
p2.addSubPackage(p3);
System.out.println(p1.getTotalWeight());
}
}