File tree Expand file tree Collapse file tree
inheritance/testing-inheritance/exercises/src/main/java
interfaces/exercises/ice-cream-exercises/src/main/java/org/launchcode Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public interface CatInterface {
2+
3+ }
Original file line number Diff line number Diff line change 1+ public class Catowner {
2+
3+ }
Original file line number Diff line number Diff line change 1+ package org .launchcode ;
2+ import java .util .Comparator ;
3+ public class ConeComparator implements Comparator <Cone >{
4+
5+ @ Override
6+ public int compare (Cone cone1 , Cone cone2 ) {
7+ if (cone1 .getCost () - cone2 .getCost () < 0 ) {
8+ return -1 ;
9+ } else if (cone1 .getCost () - cone2 .getCost () > 0 ) {
10+ return 1 ;
11+ } else {
12+ return 0 ;
13+ }
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package org .launchcode ;
2+
3+ import java .util .Comparator ;
4+
5+ public class FlavorComparator implements Comparator <Flavor > {
6+ @ Override
7+ public int compare (Flavor flavor1 , Flavor flavor2 ) {
8+ return flavor1 .getName ().compareTo (flavor2 .getName ());
9+ }
10+ }
Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ public void setAllergens(ArrayList<String> allergens) {
3939
4040 @ Override
4141 public String toString () {
42- return "Name: " + name + "\n " +
42+ return " \n " + "Name: " + name + "\n " +
4343 "Cost: $" + cost + "\n " +
4444 "Allergens: " + allergens + "\n " ;
4545 }
Original file line number Diff line number Diff line change 11package org .launchcode ;
22
33import java .util .ArrayList ;
4+ import java .util .Comparator ;
45
56public class Main {
67 public static void main (String [] args ) {
78 Case menu = new Case ();
89 ArrayList <Flavor > flavors = menu .getFlavors ();
910 ArrayList <Cone > cones = menu .getCones ();
11+ System .out .printf (flavors .toString ());
12+ //System.out.printf(cones.toString());
13+
1014
1115 // TODO: Use a Comparator class to sort the 'flavors' array alphabetically by the 'name' field.
16+ Comparator comparator = new FlavorComparator ();
17+ flavors .sort (comparator );
18+ //flavors.sort(new FlavorComparator());
19+ System .out .println (flavors .toString ());
1220
1321 // TODO: Use a Comparator class to sort the 'cones' array in increasing order by the 'cost' field.
14-
22+ cones .sort (new ConeComparator ());
23+ System .out .println (cones .toString ());
1524 // TODO: Print the 'flavors' and 'cones' lists (in a clear manner) to verify the sorting.
1625 }
1726}
You can’t perform that action at this time.
0 commit comments