-
-
Save EnioRich/6686d6e282954c0baeb159e04ff46a1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Lesson_7_8.Lesson_7.Task_1; | |
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
public class Max { | |
static <T> T max(List<T> list, Comparator<T> comparator) { | |
list.sort(comparator); | |
return list.get(0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Lesson_7_8.Lesson_7.Task_1; | |
import java.util.Comparator; | |
public class PriceComparator implements Comparator<Vegetables> { | |
public int compare(Vegetables v1, Vegetables v2) { | |
if (v1.price > v2.price) { | |
return -1; | |
} else if (v1.price < v2.price) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Lesson_7_8.Lesson_7.Task_1; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Runner { | |
public static void main(String[] args) { | |
List <Vegetables> vegetables = new ArrayList<>(); | |
Vegetables v1 = new Vegetables("Cucumbers", 15); | |
Vegetables v2 = new Vegetables("Tomato", 20); | |
Vegetables v3 = new Vegetables("Potato", 10); | |
Vegetables v4 = new Vegetables("Cibulium", 5); | |
Vegetables v5 = new Vegetables("Pepper", 45); | |
vegetables.add(v1); | |
vegetables.add(v2); | |
vegetables.add(v3); | |
vegetables.add(v4); | |
vegetables.add(v5); | |
Vegetables h1 = Max.max(vegetables, new PriceComparator()); | |
System.out.println(h1); | |
// for (Vegetables vbl : vegetables){ | |
// System.out.println(vbl); | |
// } | |
} | |
} | |
//public static void main(String[] args) { | |
// List<String> names = Arrays.asList("Bob", "Alexander", "Sophia", "Zoe"); | |
// String longestName = Max.max(names, new LengthComparator()); | |
// System.out.println(longestName); // Alexander |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Lesson_7_8.Lesson_7.Task_1; | |
import java.util.Comparator; | |
public class Vegetables { | |
private String name; | |
int price; | |
public Vegetables(String name, int price) { | |
this.name = name; | |
this.price = price; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getPrice() { | |
return price; | |
} | |
public String toString() { | |
StringBuilder vbl = new StringBuilder() | |
.append("Vegetable: ") | |
.append(name) | |
.append(" worth is: ") | |
.append(price); | |
return vbl.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment