Knowee
Questions
Features
Study Tools

Happy Mart is a supermarket with wide range of products. The manager wants to add the products and sort them based on id and price. As a java developer, create a java application to sort the products based on id and price.Component Specification: ProductType (Class)AttributesMethodsProductint productidString productNamedouble priceInclude a public parametrized constructor of three  arguments in the following order - productId, productName and price to intialize the values for the Product object. Note: The class and constructor and all the attributes should be declared as public. Component NameType (Class)MethodsResponsibilitiesOverride the toString methodProductPublic String toString()This method should return String by overriding the productid, productName and price with space.Component Specification: SortByIdType (Class)MethodsResponsibilitiesSortByIdpublic int compare(Product a, Product b)This method should return the difference between productId of the object a and object bNote: This class implements the comparator interface of type Product.Component Specification: SortByPriceType (Class)MethodsResponsibilitiesSortByPricepublic int compare(Product a, Product b)This method should return the difference between price of the object a and object bNote: This class implements the comparator interface of type Product.In the UserInterface class,-       Get the products count from user. If the count is negative or zero, display "Invalid count".-       Get the product details as shown in the sample input.-       Create an object for each product and assign the values through 3 argument constructor.-       Get the choice from user as given in the sample input.-       Based on the choice entered, the corresponding comparator is invoked in sort method and the output is displayed as given in sample output.-       If the choice is other than 1 or 2, display "Invalid choice". Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user and the rest of the text represents the output. Ensure to follow the object-oriented specifications provided in the question. Ensure to provide the names for classes, attributes, and methods as specified in the question. Adhere to the code template, if provided. Please don't use System.exit(0) to terminate the program.  Sample Input/Output 1:Enter the products count3Enter Product details1006:Pen:501009:Eraser:401004:Ruler:401.Sort By Id2.Sort By PriceEnter your choice1After Sorting By Id1004 Ruler 40.01006 Pen 50.01009 Eraser 40.0 Sample Input/Output 2:Enter the products count4Enter Product details2503:Onion:802508:Tomato:502501:Carrot:1402506:Potato:1201.Sort By Id2.Sort By PriceEnter your choice2After Sorting By Price2508 Tomato 50.02503 Onion 80.02506 Potato 120.02501 Carrot 140.0 Sample Input/Output 3:Enter the products count2Enter Product details4002:Bread:2404006:Biscuit:1601.Sort By Id2.Sort By PriceEnter your choice5Invalid choice Sample Input/Output 4:Enter the products count-5Invalid count

Question

Happy Mart is a supermarket with wide range of products. The manager wants to add the products and sort them based on id and price. As a java developer, create a java application to sort the products based on id and price.Component Specification: ProductType (Class)AttributesMethodsProductint productidString productNamedouble priceInclude a public parametrized constructor of three  arguments in the following order - productId, productName and price to intialize the values for the Product object. Note: The class and constructor and all the attributes should be declared as public. Component NameType (Class)MethodsResponsibilitiesOverride the toString methodProductPublic String toString()This method should return String by overriding the productid, productName and price with space.Component Specification: SortByIdType (Class)MethodsResponsibilitiesSortByIdpublic int compare(Product a, Product b)This method should return the difference between productId of the object a and object bNote: This class implements the comparator interface of type Product.Component Specification: SortByPriceType (Class)MethodsResponsibilitiesSortByPricepublic int compare(Product a, Product b)This method should return the difference between price of the object a and object bNote: This class implements the comparator interface of type Product.In the UserInterface class,-       Get the products count from user. If the count is negative or zero, display "Invalid count".-       Get the product details as shown in the sample input.-       Create an object for each product and assign the values through 3 argument constructor.-       Get the choice from user as given in the sample input.-       Based on the choice entered, the corresponding comparator is invoked in sort method and the output is displayed as given in sample output.-       If the choice is other than 1 or 2, display "Invalid choice". Note:In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user and the rest of the text represents the output. Ensure to follow the object-oriented specifications provided in the question. Ensure to provide the names for classes, attributes, and methods as specified in the question. Adhere to the code template, if provided. Please don't use System.exit(0) to terminate the program.  Sample Input/Output 1:Enter the products count3Enter Product details1006:Pen:501009:Eraser:401004:Ruler:401.Sort By Id2.Sort By PriceEnter your choice1After Sorting By Id1004 Ruler 40.01006 Pen 50.01009 Eraser 40.0 Sample Input/Output 2:Enter the products count4Enter Product details2503:Onion:802508:Tomato:502501:Carrot:1402506:Potato:1201.Sort By Id2.Sort By PriceEnter your choice2After Sorting By Price2508 Tomato 50.02503 Onion 80.02506 Potato 120.02501 Carrot 140.0 Sample Input/Output 3:Enter the products count2Enter Product details4002:Bread:2404006:Biscuit:1601.Sort By Id2.Sort By PriceEnter your choice5Invalid choice Sample Input/Output 4:Enter the products count-5Invalid count

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The task requires creating a Java application for a supermarket named "Happy Mart". The application should be able to add products and sort them based on their id and price. Here is a step-by-step guide on how to create this application:

  1. Create a class named Product with the following attributes: productid, productName, and price. All attributes should be public. Also, create a public parameterized constructor that takes three arguments in the following order: productId, productName, and price. This constructor will be used to initialize the values for the Product object.
public class Product {
    public int productid;
    public String productName;
    public double price;

    public Product(int productid, String productName, double price) {
        this.productid = productid;
        this.productName = productName;
        this.price = price;
    }

    @Override
    public String toString() {
        return productid + " " + productName + " " + price;
    }
}
  1. Create a class named SortById that implements the Comparator interface of type Product. This class should have a method named compare that takes two Product objects as arguments and returns the difference between their productId.
import java.util.Comparator;

public class SortById implements Comparator<Product> {
    public int compare(Product a, Product b) {
        return a.productid - b.productid;
    }
}
  1. Similarly, create a class named SortByPrice that also implements the Comparator interface of type Product. This class should have a method named compare that takes two Product objects as arguments and returns the difference between their price.
import java.util.Comparator;

public class SortByPrice implements Comparator<Product> {
    public int compare(Product a, Product b) {
        return Double.compare(a.price, b.price);
    }
}
  1. In the UserInterface class, get the products count from the user. If the count is negative or zero, display "Invalid count". Get the product details as shown in the sample input. Create an object for each product and assign the values through the 3 argument constructor. Get the choice from the user as given in the sample input. Based on the choice entered, the corresponding comparator is invoked in the sort method and the output is displayed as given in the sample output. If the choice is other than 1 or 2, display "Invalid choice".
import java.util.*;

public class UserInterface {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the products count");
        int count = sc.nextInt();
        if (count <= 0) {
            System.out.println("Invalid count");
            return;
        }
        List<Product> products = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String[] details = sc.next().split(":");
            products.add(new Product(Integer.parseInt(details[0]), details[1], Double.parseDouble(details[2])));
        }
        System.out.println("1.Sort By Id\n2.Sort By Price");
        System.out.println("Enter your choice");
        int choice = sc.nextInt();
        if (choice == 1) {
            Collections.sort(products, new SortById());
        } else if (choice == 2) {
            Collections.sort(products, new SortByPrice());
        } else {
            System.out.println("Invalid choice");
            return;
        }
        for (Product product : products) {
            System.out.println(product);
        }
    }
}

This Java application will now sort the products based on id and price as per the user's choice.

This problem has been solved

Similar Questions

write a java program to define a class Item having item namepricequantityfind the total cost of the itemuse accept method to get the data from the user and toString method to display the detailscreate two objects and display the name of the item which is ordered more quantitydisplay total amount of items purchased.edited 10:37 AM

Develop a java application to handle the menu details of Sandy Food Inc. Company. Your application will handle the Sandwich product details. There are three types of Sandwiches under Sandwich class, they are Sweet, Sour, Hot. Each has two sub types of Sandwiches like small and medium. The members are ProductName – String (Sweet Small / Sweet Medium / Sour Small…) Price – Integer ProductCode - Integer GetOrder() - method to get the Product code and number of quantities. DisplayPrice() - method to display the Product name, number of quantities and cost. Your application should calculate the cost of the ordered product based on the quantity and price. Single order will not have more than one type of product. The price range of different products with the product code and product name are tabulated below. S.No Product name Product Code Price 1 Sweet Small 101 100/- 2 Sweet Medium 102 150/- 3 Sour Small 103 150/- 4 Sour Medium 104 200/- 5 Hot Small 105 200/- 6 Hot Medium 106 250/- Ensure that the bill value is calculated only on the respective class. The inputs are the product code and the quantity of the product. The expected output is the product name, quantity and price. If the entry is wrong product code, then handle with exception and display the message as “Invalid”.

Observe the below class and match the output for the code snippets provided.public class Product{    //Attributes    private int productId;    private String productName;    private float price;    private char category;     //Constructor public Product(){ } public Product(int productId,String productName,float price) {     this.productId=productId;     this.productName=productName;     this.price=price; } public Product(int productId,String productName,float price,char category) { this(productId,productName,price); this.category=category;      } public void display()  { System.out.println(productId+"  "+productName+" "+price+" "+category); }    }Product p = new Product()p.display();Answer 1Product p = new Product(71,"Mobile",80000,'A');p.display();Answer 2Product p = new Product(501,"Wallet",520);p.display();Answer 3

Objective: The objective of this activity is to implement a basic inventory management system using Java programming concepts such as user-defined methods, scanner, arrays, loops, and conditions.Task Overview:1. Create a new Java class named `InventorySystem`.2. Declare the following static variables inside the `InventorySystem` class:   - `MAX_ITEMS`: an integer constant to define the maximum number of items in the inventory (set it to 10).   - `items`: a string array to store the names of the items in the inventory (initialize it with the size of `MAX_ITEMS`).   - `quantities`: an integer array to store the quantities of the items in the inventory (initialize it with the size of `MAX_ITEMS`).3. Implement the `main` method inside the `InventorySystem` class:   - Create a `Scanner` object named `scanner` to read user input from the console.   - Use a `while` loop to keep the program running until the user chooses to exit.   - Inside the loop, display a menu of options to the user:     1. Add item to inventory     2. Sell item from inventory     3. Display inventory     4. Exit   - Use a `switch` statement to execute the corresponding action based on the user's choice (1, 2, 3, or 4).   - If the user selects option 1, call the `addItem` method.   - If the user selects option 2, call the `sellItem` method.   - If the user selects option 3, call the `displayInventory` method.   - If the user selects option 4, exit the program.Sample Output 4. Implement the `displayInventory` method:   - Iterate through the `items` and `quantities` arrays and display the name and quantity of each item in the inventory.Sample OutputNote: For other displayInventory functions, check the sample output in the addItem and in the sellItem methods. 5. Implement the `addItem` method:   - Prompt the user to enter the name of the item to be added.   - Check if the item already exists in the inventory by searching for its name in the `items` array.   - If the item does not exist, add it to the list. Automatically it will have value of 1.   - If the item already exists, prompt the user to enter the quantity to be added.   - Update the quantity of the existing item in the `quantities` array.Sample Output6. Implement the `sellItem` method:   - Prompt the user to enter the name of the item to be sold.   - Check if the item exists in the inventory by searching for its name in the `items` array.   - If the item does not exist or if its quantity is 0, display an error message.   - If the item exists and its quantity is greater than 0, prompt the user to enter the quantity to be sold.   - Update the quantity of the sold item in the `quantities` array.Sample Output7. Compile and run the `InventorySystem` class to test the inventory management system.8. Test the system by adding items to the inventory, selling items, and displaying the current inventory.9. When exit is chosen, the program will terminate

Objective: The objective of this activity is to implement a basic inventory management system using Java programming concepts such as user-defined methods, scanner, arrays, loops, and conditions.Task Overview:1. Create a new Java class named `InventorySystem`.2. Declare the following static variables inside the `InventorySystem` class:   - `MAX_ITEMS`: an integer constant to define the maximum number of items in the inventory (set it to 10).   - `items`: a string array to store the names of the items in the inventory (initialize it with the size of `MAX_ITEMS`).   - `quantities`: an integer array to store the quantities of the items in the inventory (initialize it with the size of `MAX_ITEMS`).3. Implement the `main` method inside the `InventorySystem` class:   - Create a `Scanner` object named `scanner` to read user input from the console.   - Use a `while` loop to keep the program running until the user chooses to exit.   - Inside the loop, display a menu of options to the user:     1. Add item to inventory     2. Sell item from inventory     3. Display inventory     4. Exit   - Use a `switch` statement to execute the corresponding action based on the user's choice (1, 2, 3, or 4).   - If the user selects option 1, call the `addItem` method.   - If the user selects option 2, call the `sellItem` method.   - If the user selects option 3, call the `displayInventory` method.   - If the user selects option 4, exit the program.Sample Output 4. Implement the `displayInventory` method:   - Iterate through the `items` and `quantities` arrays and display the name and quantity of each item in the inventory.Sample OutputNote: For other displayInventory functions, check the sample output in the addItem and in the sellItem methods. 5. Implement the `addItem` method:   - Prompt the user to enter the name of the item to be added.   - Check if the item already exists in the inventory by searching for its name in the `items` array.   - If the item does not exist, add it to the list. Automatically it will have value of 1.   - If the item already exists, prompt the user to enter the quantity to be added.   - Update the quantity of the existing item in the `quantities` array.Sample Output6. Implement the `sellItem` method:   - Prompt the user to enter the name of the item to be sold.   - Check if the item exists in the inventory by searching for its name in the `items` array.   - If the item does not exist or if its quantity is 0, display an error message.   - If the item exists and its quantity is greater than 0, prompt the user to enter the quantity to be sold.   - Update the quantity of the sold item in the `quantities` array.Sample Output7. Compile and run the `InventorySystem` class to test the inventory management system.8. Test the system by adding items to the inventory, selling items, and displaying the current inventory.9. When exit is chosen, the program will terminate.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.