Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

Here is a simple Java program that meets your requirements:

import java.util.Scanner;

class Item {
    String name;
    int price;
    int quantity;

    void accept() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter item name: ");
        name = sc.nextLine();
        System.out.println("Enter item price: ");
        price = sc.nextInt();
        System.out.println("Enter item quantity: ");
        quantity = sc.nextInt();
    }

    int totalCost() {
        return price * quantity;
    }

    public String toString() {
        return "Item Name: " + name + ", Price: " + price + ", Quantity: " + quantity;
    }
}

public class Main {
    public static void main(String[] args) {
        Item item1 = new Item();
        item1.accept();
        Item item2 = new Item();
        item2.accept();

        System.out.println(item1);
        System.out.println("Total cost: " + item1.totalCost());

        System.out.println(item2);
        System.out.println("Total cost: " + item2.totalCost());

        if (item1.quantity > item2.quantity) {
            System.out.println(item1.name + " is ordered in more quantity.");
        } else if (item2.quantity > item1.quantity) {
            System.out.println(item2.name + " is ordered in more quantity.");
        } else {
            System.out.println("Both items are ordered in equal quantity.");
        }

        int totalAmount = item1.totalCost() + item2.totalCost();
        System.out.println("Total amount of items purchased: " + totalAmount);
    }
}

This program defines a class Item with fields name, price, and quantity. The accept method is used to get the data from the user. The totalCost method calculates the total cost of the item. The toString method is overridden to display the details of the item.

In the main method, two objects of the Item class are created and their details are accepted from the user. The total cost of each item is calculated and displayed. The name of the item which is ordered in more quantity is displayed. The total amount of items purchased is also calculated and displayed.

This problem has been solved

Similar Questions

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”.

Ramesh went to a general Store and picked two items of x and y prices.Write a java program to calculate the total amount for Ramesh to pay.Sample Test Case:Enter x value: 10Enter y value: 20Sum: 30Sample Test CasesTest Case 1:Expected Output:Enter·x·value:·10Enter·y·value:·20Sum:·30Test Case 2:Expected Output:Enter·x·value:·12Enter·y·value:·50Sum:·62Test Case 3:Expected Output:Enter·x·value:·700Enter·y·value:·654Sum:·1354Test Case 4:Expected Output:Enter·x·value:·99Enter·y·value:·100Sum:·199

Write a C program to store the Item number, quantity sold, and Price of one item in toi.remctf file by reading values from the keyboard.Save your program as lBQ4a.cSample inputItem number quantity sold Price of oneitem(Rs)Ilo 2 12000233 6 5000320 2 10000100 5 1500b) Write a program to read the details from i.femft£ 5°/o discount is given if more than 3 itemsare purchased from an item. Display the total amount paid for each item.` Display the itemnumber and the total amount paid for each item.Save your program as lBQ4b.cSample outputItem No. Total AmountIlo233320loo24000.0028500.0020000.007125.00

/** A simulated cash register that tracks the item count and the total amount due.*/public class CashRegister{ // private data--see the "Designing the Data Representation" section /** Adds an item to this cash register. @param price the price of this item */ public void addItem(double price) { // implementation--see the "Implementing Instance Methods" section } /** Gets the price of all items in the current sale. @return the total amount */ public double getTotal() { // implementation--see the "Implementing Instance Methods" section } /** Gets the number of items in the current sale. @return the item count */ public int getCount() { // implementation--see the "Implementing Instance Methods" section } /** Clears the item count and the total. */ public void clear() { // implementation--see the "Implementing Instance Methods" section }}

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/3

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.