Skip to content

Commit a934dcc

Browse files
committed
interfacesExercise - Complete
1 parent 5b83c7f commit a934dcc

6 files changed

Lines changed: 95 additions & 3 deletions

File tree

interfaces/exercises/ice-cream-exercises/src/main/java/org/launchcode/Case.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class Case {
77
private ArrayList<Flavor> flavors = new ArrayList<>();
88
private ArrayList<Cone> cones = new ArrayList<>();
9+
private ArrayList<Topping> toppings = new ArrayList<>(); // Add a field for toppings
910

1011
public Case(){
1112
Cone cone1 = new Cone("Waffle", 1.25, new ArrayList<>(Arrays.asList( "gluten")));
@@ -37,11 +38,19 @@ public ArrayList<Cone> getCones() {
3738
return cones;
3839
}
3940

41+
public ArrayList<Topping> getToppings() { // Getter method for toppings
42+
return toppings;
43+
}
44+
4045
public void setFlavors(ArrayList<Flavor> flavors) {
4146
this.flavors = flavors;
4247
}
4348

4449
public void setCones(ArrayList<Cone> cones) {
4550
this.cones = cones;
4651
}
47-
}
52+
53+
public void addTopping(Topping topping) {
54+
toppings.add(topping);
55+
}
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.launchcode;
2+
3+
import java.util.Comparator;
4+
5+
public class ConeComparator implements Comparator<Cone> {
6+
@Override
7+
public int compare(Cone cone1, Cone cone2) {
8+
if (cone1.getCost() < cone2.getCost()) {
9+
return -1; // Return a negative integer if cone1 is cheaper.
10+
} else if (cone1.getCost() > cone2.getCost()) {
11+
return 1; // Return a positive integer if cone1 is more expensive.
12+
} else {
13+
return 0; // Return 0 if both cones have the same cost.
14+
}
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.launchcode;
2+
import java.util.Comparator;
3+
4+
public class FlavorComparator implements Comparator<Flavor> {
5+
@Override
6+
public int compare(Flavor flavor1, Flavor flavor2) {
7+
int allergenCount1 = flavor1.getAllergens().size();
8+
int allergenCount2 = flavor2.getAllergens().size();
9+
10+
// Sort in descending order (highest to lowest allergen count)
11+
return Integer.compare(allergenCount2, allergenCount1);
12+
}
13+
}
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
11
package org.launchcode;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
46

57
public class Main {
68
public static void main(String[] args) {
79
Case menu = new Case();
810
ArrayList<Flavor> flavors = menu.getFlavors();
911
ArrayList<Cone> cones = menu.getCones();
1012

11-
// TODO: Use a Comparator class to sort the 'flavors' array alphabetically by the 'name' field.
13+
// Add some toppings to the Case
14+
Topping topping1 = new Topping("Chocolate Chips", 0.75, new ArrayList<>());
15+
Topping topping2 = new Topping("Sprinkles", 0.50, new ArrayList<>());
16+
Topping topping3 = new Topping("Caramel Drizzle", 1.00, new ArrayList<>());
17+
menu.addTopping(topping1);
18+
menu.addTopping(topping2);
19+
menu.addTopping(topping3);
1220

21+
// TODO: Use a Comparator class to sort the 'flavors' array alphabetically by the 'name' field.
22+
flavors.sort(new FlavorComparator());
1323
// TODO: Use a Comparator class to sort the 'cones' array in increasing order by the 'cost' field.
14-
24+
cones.sort(new ConeComparator());
1525
// TODO: Print the 'flavors' and 'cones' lists (in a clear manner) to verify the sorting.
26+
System.out.println("\nFlavors:");
27+
for (Flavor flavor : flavors) {
28+
System.out.println(flavor.getName());
29+
}
30+
31+
// Print the sorted 'cones' list
32+
System.out.println("\nCones:");
33+
for (Cone cone : cones) {
34+
String formattedCost = String.format("%.2f", cone.getCost());
35+
System.out.println(cone.getName() + ": $" + formattedCost);
36+
}
37+
38+
// Sort the toppings by the number of allergens (ascending order)
39+
ArrayList<Topping> toppings = menu.getToppings();
40+
toppings.sort(new ToppingComparator());
41+
42+
// Print the sorted toppings list
43+
System.out.println("\nToppings:");
44+
for (Topping topping : toppings) {
45+
String formattedCost = String.format("%.2f", topping.getCost());
46+
System.out.println(topping.getName() + ": $" + formattedCost + " (Allergen Count: " + topping.getAllergens().size() + ")");
47+
}
1648
}
1749
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.launchcode;
2+
3+
import java.util.ArrayList;
4+
5+
public class Topping extends Ingredient {
6+
public Topping(String aName, double aCost, ArrayList<String> someAllergens) {
7+
super(aName, aCost, someAllergens);
8+
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.launchcode;
2+
import java.util.Comparator;
3+
4+
public class ToppingComparator implements Comparator<Topping> {
5+
@Override
6+
public int compare(Topping topping1, Topping topping2) {
7+
int allergenCount1 = topping1.getAllergens().size();
8+
int allergenCount2 = topping2.getAllergens().size();
9+
10+
// Sort in ascending order (from lowest to highest allergen count)
11+
return Integer.compare(allergenCount1, allergenCount2);
12+
}
13+
}

0 commit comments

Comments
 (0)