I have a problem with filtering.
I want the user to enter the minimum price and the maximum, and after this operation, toys in the price range by the user are displayed on the console. Before that, we create toys ourselves, specifying the size, price and age.
This is a class Toy
package Models;
public class Toy implements Comparable<Toy> {
private String size;
private int cost;
private int age;
protected String name;
public Toy(String size, int cost, int age) {
this.size = size;
this.cost = cost;
this.age = age;
}
public Toy() {
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return this.name + " -" + " Size: " + getSize() + ", Price: " + getCost() + "$, " + "Age: " + getAge();
}
@Override
public int compareTo(Toy target) {
return Integer.compare(this.getCost(), target.getCost());
}
}
This is the Main class
import Models.*;
import java.util.*;
import java.util.Scanner;
public class Main {
public static Scanner scanner = new Scanner(System.in);
private static ArrayList<Toy> toys = new ArrayList<Toy>();
public static void main(String[] args) {
MainMenu();
}
//Menu:
// 1. Create toy
// 2. Remove toys
// 3. Sort toys
// 4. Search toys
public static void MainMenu() {
System.out.println("Меню:\n" +
"1. Create toy\n" +
"2. Remove toys\n" +
"3. Sort toys\n" +
"4. Search toys\n" +
"0. Exit the application");
int selectMenu = scanner.nextInt();
switch (selecMenu) {
case 1:
AddToyMenu();
break;
case 2:
RemoveToys();
break;
case 3:
SortMenu();
break;
case 4:
SearchMenu();
break;
case 0:
System.exit(0);
break;
}
}
public static void AddToyMenu() {
System.out.println("1. Car\n" +
"2. Boll\n" +
"3. Doll\n" +
"4. Block\n" +
"0. Back");
Toy toy = null;
int selectedToys = scanner.nextInt();
switch (selectedToys) {
case 1:
toy = new Car();
break;
case 2:
toy = new Ball();
break;
case 3:
toy = new Doll();
break;
case 4:
toy = new Block();
break;
case 0:
MainMenu();
break;
}
toys.add(toy);
SelectSize(toy);
}
public static void SelectSize(Toy toy) {
System.out.println("Выберите размер: \n" +
"1. Large\n" +
"2. Medium\n" +
"3. Small");
int selectedSize = scanner.nextInt();
switch (selectedSize) {
case 1:
toy.setSize("Large");
break;
case 2:
toy.setSize("Medium");
break;
case 3:
toy.setSize("Small");
break;
}
SetCost(toy);
}
public static void SetCost(Toy toy) {
System.out.println("Keep the price: ");
toy.setCost(scanner.nextInt());
SetAge(toy);
}
public static void SetAge(Toy toy) {
System.out.println("Keep the age: ");
toy.setAge(scanner.nextInt());
MainMenu();
}
public static void RemoveToys() {
System.out.println("Choose a toy to remove: ");
for (int i = 0; i < toys.size(); i++) {
System.out.println(i + " " + toys.get(i));
}
System.out.println("0. Назад");
int removeToy = scanner.nextInt();
switch (removeToy) {
case 0:
MainMenu();
break;
}
if (removeToy <= toys.size()) {
toys.remove(removeToy - 1);
}
}
public static void SortMenu() {
System.out.println("1. Descending order\n" +
"2. Ascending\n" +
"0. Back");
ArrayList<Toy> copy = new ArrayList<>(toys);
int selectSort = scanner.nextInt();
switch (selectSort) {
case 1:
Collections.sort(copy);
System.out.println(copy);
break;
case 2:
Collections.sort(copy);
Collections.reverse(copy);
System.out.println(copy);
break;
case 0:
MainMenu();
break;
}
}
private static void SearchMenu() {
System.out.println("Search by price - \n" +
"Enter the minimum price:");
int minimumPrice = scanner.nextInt();
System.out.println("Enter the maximum price:");
int maximumPrice = scanner.nextInt();
ArrayList<Toy> search = new ArrayList<>(toys);
}
}
I will be very grateful if you help
>Solution :
Try using a filter
System.out.println("Search by price - \n" +
"Enter the minimum price:");
int minimumPrice = scanner.nextInt();
System.out.println("Enter the maximum price:");
int maximumPrice = scanner.nextInt();
List<Toy> filtered = toys
.stream()
.filter(t -> t.getCost() <= maximumPrice && t.getCost() >= minimumPrice)
.collect(Collectors.toList());
System.out.println(filtered);