Question

The Exercise Write a Java program that stores and retrieves details about a ``basket'' of items...

The Exercise Write a Java program that stores and retrieves details about a ``basket'' of items bought in a supermarket. Items should have a code, a name, and a price. Once created, it should be possible to change the price of an item, but not its name or code.

1. Write a class for individual items. Do not forget to include constructors, accessors and mutator methods as required.

2. Create another class to store a list of items (the shopping basket).This should include the use of a single collection for the storage of all individual items. This class should have a methods to:

a. add an item to the collection (this method should have multiple parameters - one per Item attribute);

b. return the total number of items in the basket;

c. print the index, name, item code, and price of each item to the console window.

3. Write a method to calculate and return the total cost of the shopping (the total price of all items in the basket).

4. Write a method that removes all items whose price is greater than some maximum (a parameter to the method).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

/*package whatever //do not write package name here */

import java.util.*;

class Item
{
    final String itemName;
    final int code;
    double price;
    Item(String name,int code,double price)
    {
        this.itemName=name;
        this.code=code;
        this.price=price;
    }
  
}
class Basket
{
    ArrayList<Item> list=new ArrayList<Item>();
    public void add(String name,int code,double price)
    {
        list.add(new Item(name,code,price));
    }
  
    public int countItems()
    {
        return list.size();
    }
    public void printItems()
    {
        int index=1;
        for(Item i:list)
        {
            System.out.print("Index= "+index++);
            System.out.print(" Name= "+i.itemName);
            System.out.print(" Price= "+i.price);
            System.out.println(" CODE= "+i.code);
        }
    }
  
    public double totalCost()
    {
        double cost=0;
        for(Item i:list)
        {
            cost+=i.price;
        }
        return cost;
    }
  
    public void removeGT(double maxPrice)
    {
        for(int i=0;i<list.size();i++)
        {
           if(list.get(i).price>maxPrice)
            list.remove(i);
        }
    }
    public static void main(String[] args)
    {
        Basket b=new Basket();
        b.add("orange",123,10.00);
        b.add("grape",124,8.00);
        b.add("apple",125,40.50);
        b.add("banana",126,4.56);
        b.printItems();
      
      
        System.out.println("There are "+b.countItems()+" items in the basket");
        System.out.println("The total cost is "+b.totalCost());
        b.removeGT(20);
        System.out.println("Removed items greater than 20 rupees.");

        b.printItems();

    }
}

=====================================

SAMPLE OUTPUT:

Index= 1  Name= orange  Price= 10.0  CODE= 123
Index= 2  Name= grape  Price= 8.0  CODE= 124
Index= 3  Name= apple  Price= 40.5  CODE= 125
Index= 4  Name= banana  Price= 4.56  CODE= 126
There are 4 items in the basket
The total cost is 63.06
Removed items greater than 20 rupees.
Index= 1  Name= orange  Price= 10.0  CODE= 123
Index= 2  Name= grape  Price= 8.0  CODE= 124
Index= 3  Name= banana  Price= 4.56  CODE= 126

=============================================

SCREENSHOT FOR CODE:

=======

SCREENSHOT FOR OUTPUT:

====

if you have any doubts please comment below.

HIT A LIKE if you like the solution.

======

Add a comment
Know the answer?
Add Answer to:
The Exercise Write a Java program that stores and retrieves details about a ``basket'' of items...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the following program in Java using Eclipse. A cash register is used in retail stores...

    Write the following program in Java using Eclipse. A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class: numberOfItems: this variable will keep track of the number of items added to...

  • Write code in Java programming language. The ShoppingCart class will be composed with an array of...

    Write code in Java programming language. The ShoppingCart class will be composed with an array of Item objects. You do not need to implement the Item class for this question, only use it. Item has a getPrice() method that returns a float and a one-argument constructor that takes a float that specifies the price. The ShoppingCart class should have: Constructors: A no-argument and a single argument that takes an array of Items. Fields: • Items: an array of Item objects...

  • Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides...

    Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides the following. • Two attributes: lastName and first Name of type String. • Appropriate initialization, accessors/mutator methods. 2. (6 marks) Write a Student class that is a subclass of Person class (in the previous question) which provides the following. • Two attributes: A unique student ID of type String and GPA of type float; the student ID is initialized when a Student object gets...

  • Java 1. Develop a program to emulate a purchase transaction at a retail store. This program...

    Java 1. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual line item of merchandise that a customer is purchasing. The Transaction class will combine several LineItem objects and calculate an overall total price for the line item within the transaction. There will also be two test classes, one for the LineItem class and one for the...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress,...

    DESCRIPTION You have to design an e-commerce shopping cart. These require classes Item, Electronics, Food, Dress, Cart and Main. ITEM CLASS Your Item class should contain: Attributes (protected) String name - name of the Item double price - price of the item Methods (public) void setName(String n) - sets the name of Item void setPrice(double p) - sets the price of Item String getName() - retrieves name double getPrice() - retrieves price String formattedOutput() returns a string containing detail of...

  • Java Program Please help me with this. It should be pretty basic and easy but I...

    Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • You are required to write a program that implements the computerized maintenance of your monthly expenditure record keeping.

    OO Object AssociationsAggregation Vs. Composition Built-in libraries and functions You are required to write a program that implements the computerized maintenance of your monthly expenditure record keeping. You will have to build two classes 1. ExpenseItem, ExpenseLedger The class ExpenseLedger will record all the expenses you make. You record an expense by giving it a serial number, a description (that tells what it is about) and the money you spent on this item. So you need to create a class ExpenseItem with serial,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT