226 lines
8.5 KiB
Plaintext
226 lines
8.5 KiB
Plaintext
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.Scanner;
|
||
|
import java.util.Comparator;
|
||
|
|
||
|
class ToyRoom {
|
||
|
|
||
|
public static void variant() {
|
||
|
int C13 = 2430 % 13;
|
||
|
System.out.println("\n ---------------------------------------------------------------------------------------------------------------------");
|
||
|
System.out.println(" C13 = " + C13 + ", So, the task is: Prepare the toy room for children of different age groups.");
|
||
|
System.out.println(" ---------------------------------------------------------------------------------------------------------------------");
|
||
|
}
|
||
|
|
||
|
private List<Toy> toys;
|
||
|
|
||
|
public ToyRoom() {
|
||
|
this.toys = new ArrayList<>();
|
||
|
}
|
||
|
|
||
|
public void addToy(Toy toy) {
|
||
|
toys.add(toy);
|
||
|
}
|
||
|
|
||
|
public void displayToys() {
|
||
|
System.out.println(" -------------------------------------------------------");
|
||
|
System.out.printf(" | %-20s | %-10s | %-15s |\n", "Name", "Price ($)", "Age Group");
|
||
|
System.out.println(" |----------------------|------------|-----------------|");
|
||
|
for (Toy toy : toys) {
|
||
|
System.out.printf(" | %-20s | %-10.2f | %-15s |\n", toy.name, toy.price, toy.ageGroup);
|
||
|
}
|
||
|
System.out.println(" -------------------------------------------------------");
|
||
|
}
|
||
|
|
||
|
public void sortToys(boolean ascending) {
|
||
|
if (ascending) {
|
||
|
toys.sort(Comparator.comparingDouble(Toy::getPrice));
|
||
|
} else {
|
||
|
toys.sort(Comparator.comparingDouble(Toy::getPrice).reversed());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public List<Toy> findToysByPriceRange(double minPrice, double maxPrice) {
|
||
|
List<Toy> foundToys = new ArrayList<>();
|
||
|
for (Toy toy : toys) {
|
||
|
if (toy.getPrice() >= minPrice && toy.getPrice() <= maxPrice) {
|
||
|
foundToys.add(toy);
|
||
|
}
|
||
|
}
|
||
|
return foundToys;
|
||
|
}
|
||
|
|
||
|
public void findToysByName(String name) {
|
||
|
List<Toy> foundToys = new ArrayList<>();
|
||
|
for (Toy toy : toys) {
|
||
|
if (toy.name.toLowerCase().contains(name.toLowerCase())) {
|
||
|
foundToys.add(toy);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!foundToys.isEmpty()) {
|
||
|
System.out.println("\n Toys with matching name \"" + name + "\":");
|
||
|
System.out.println(" -------------------------------------------------------");
|
||
|
System.out.printf(" | %-20s | %-10s | %-15s |\n", "Name", "Price ($)", "Age Group");
|
||
|
System.out.println(" |----------------------|------------|-----------------|");
|
||
|
for (Toy toy : foundToys) {
|
||
|
System.out.printf(" | %-20s | %-10.2f | %-15s |\n", toy.name, toy.price, toy.ageGroup);
|
||
|
}
|
||
|
System.out.println(" -------------------------------------------------------");
|
||
|
} else {
|
||
|
System.out.println(" No toys found with a matching name.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
abstract class Toy {
|
||
|
protected String name;
|
||
|
protected double price;
|
||
|
protected String ageGroup;
|
||
|
|
||
|
public Toy(String name, double price, String ageGroup) {
|
||
|
this.name = name;
|
||
|
this.price = price;
|
||
|
this.ageGroup = ageGroup;
|
||
|
|
||
|
}
|
||
|
|
||
|
public double getPrice() {
|
||
|
return price;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String toString() {
|
||
|
return name + " - $" + price + ageGroup;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Car extends Toy {
|
||
|
public Car(String name, double price, String ageGroup) {
|
||
|
super(name, price, ageGroup);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Doll extends Toy {
|
||
|
public Doll(String name, double price, String ageGroup) {
|
||
|
super(name, price, ageGroup);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Ball extends Toy {
|
||
|
public Ball(String name, double price, String ageGroup) {
|
||
|
super(name, price, ageGroup);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Cube extends Toy {
|
||
|
public Cube(String name, double price, String ageGroup) {
|
||
|
super(name, price, ageGroup);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class Lab6 {
|
||
|
public static void main(String[] args) {
|
||
|
ToyRoom toyRoom = new ToyRoom();
|
||
|
ToyRoom.variant();
|
||
|
|
||
|
toyRoom.addToy(new Car("Small car 'Mcqueen'", 10.0, "1-3 years"));
|
||
|
toyRoom.addToy(new Car("Small Car 'Audi'", 12.0, "1-3 years"));
|
||
|
toyRoom.addToy(new Car("Medium Yellow Car", 15.0, "3-6 years"));
|
||
|
toyRoom.addToy(new Car("Medium Blue Car", 18.0, "3-6 years"));
|
||
|
toyRoom.addToy(new Car("Car on R/C", 30.0, "6-10 years"));
|
||
|
toyRoom.addToy(new Doll("Doll that can talk", 25.0, "1-3 years"));
|
||
|
toyRoom.addToy(new Doll("Doll 'Barbie'", 10.0, "3-6 years"));
|
||
|
toyRoom.addToy(new Doll("Bear Teddy", 14.5, "6-12 months"));
|
||
|
toyRoom.addToy(new Ball("Yellow Ball", 5.0, "6-12 months"));
|
||
|
toyRoom.addToy(new Ball("Big Blue Ball", 7.0, "1-3 years"));
|
||
|
toyRoom.addToy(new Ball("Football Ball", 12.5, "6-10 years"));
|
||
|
toyRoom.addToy(new Cube("Cubes with pictures", 6.5, "6-12 months"));
|
||
|
toyRoom.addToy(new Cube("Cubes with letters", 9.0, "3-6 years"));
|
||
|
toyRoom.addToy(new Cube("Rubik's Cube", 14.0, "6-10 years"));
|
||
|
|
||
|
boolean exit = false;
|
||
|
Scanner scanner = new Scanner(System.in);
|
||
|
|
||
|
while (!exit) {
|
||
|
System.out.println("\n Select an option:");
|
||
|
System.out.println(" 1 - Display all toys;");
|
||
|
System.out.println(" 2 - Find toy by price range;");
|
||
|
System.out.println(" 3 - Sort toys by price;");
|
||
|
System.out.println(" 4 - Find a toy by name;");
|
||
|
System.out.println(" Q - Quit;\n");
|
||
|
|
||
|
String option = scanner.nextLine();
|
||
|
|
||
|
switch (option.toLowerCase()) {
|
||
|
case "1":
|
||
|
System.out.println(" Toys in the Toy Room:");
|
||
|
toyRoom.displayToys();
|
||
|
break;
|
||
|
case "2":
|
||
|
System.out.print(" Enter the minimum price for the toy: ");
|
||
|
double minPrice = scanner.nextDouble();
|
||
|
scanner.nextLine();
|
||
|
|
||
|
System.out.print(" Enter the maximum price for the toy: ");
|
||
|
double maxPrice;
|
||
|
do {
|
||
|
maxPrice = scanner.nextDouble();
|
||
|
scanner.nextLine();
|
||
|
|
||
|
if (minPrice >= maxPrice) {
|
||
|
System.out.println(" Error: The minimum price must be less than the maximum price. Please try again.");
|
||
|
System.out.print(" Enter the maximum price for the toy: ");
|
||
|
}
|
||
|
} while (minPrice >= maxPrice);
|
||
|
|
||
|
List<Toy> foundToys = toyRoom.findToysByPriceRange(minPrice, maxPrice);
|
||
|
if (!foundToys.isEmpty()) {
|
||
|
System.out.println("\n Found toys within the price range:");
|
||
|
System.out.println(" -------------------------------------------------------");
|
||
|
System.out.printf(" | %-20s | %-10s | %-15s |\n", "Name", "Price ($)", "Age Group");
|
||
|
System.out.println(" |----------------------|------------|-----------------|");
|
||
|
for (Toy toy : foundToys) {
|
||
|
System.out.printf(" | %-20s | %-10.2f | %-15s |\n", toy.name, toy.price, toy.ageGroup);
|
||
|
}
|
||
|
System.out.println(" -------------------------------------------------------");
|
||
|
} else {
|
||
|
System.out.println("\n No toys found within the price range.");
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case "3":
|
||
|
System.out.println(" Choose the sorting order:");
|
||
|
System.out.println(" A - Ascending");
|
||
|
System.out.println(" D - Descending");
|
||
|
String sortOrder = scanner.nextLine();
|
||
|
|
||
|
boolean ascending = true;
|
||
|
if (sortOrder.equalsIgnoreCase("D")) {
|
||
|
ascending = false;
|
||
|
}
|
||
|
|
||
|
toyRoom.sortToys(ascending);
|
||
|
System.out.println("\n Toys in the Toy Room after sorting:");
|
||
|
toyRoom.displayToys();
|
||
|
break;
|
||
|
|
||
|
case "4":
|
||
|
System.out.print(" Enter the toy name: ");
|
||
|
String toyName = scanner.nextLine();
|
||
|
|
||
|
toyRoom.findToysByName(toyName);
|
||
|
break;
|
||
|
|
||
|
case "q":
|
||
|
exit = true;
|
||
|
System.out.println(" Program exited. Thank you!");
|
||
|
break;
|
||
|
default:
|
||
|
System.out.println(" Invalid option. Please try again.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|