You must write a small application in JAVA for items and their prices. Your program must:

  1. Create an array of item names. Use the following item names: Bread, Milk, Butter, Chips,
    Honey, Soap, Towel, Carrots, Beans, Samp, Dress, Pants, Shoes, Socks.
  2. Create an array of item prices. Use the following item prices: 15.50, 12.00, 56.00, 20.00, 45.00,
    7.00, 75.00, 8.00, 5.00, 12.00, 200.00, 150.00, 400.00, 25.00.
  3. Display every item name and its corresponding price. See the sample output given.
  4. Print the average price of all the items.
  5. Find and display the price of a specific item
    a. Read in the name of the item.
    b. Loop through the items array and look for the specific item.
    c. If the item is found, display the price of the item.
    SAMPLE OUTPUT
    Display every item name and its corresponding price.
    Items in stock with prices

    Bread - R15.5
    Milk - R12.0
    Butter - R56.0
    Chips - R20.0
    Honey - R45.0
    Soap - R7.0
    Towel - R75.0
    Carrots - R8.0
    Beans - R5.0
    Samp - R12.0
    Dress - R200.0
    Pants - R150.0
    Shoes - R400.0
    Socks - R25.0
    Display the average price of all the items
    The average price of all the items is R73.6
    Find and display the price of a specific item – Sample input and output
    Sample run 1 Sample run 2
    Item Look-up
    What item are you looking for?
    Dress
    The price of item Dress is R200.0
    Item Look-up
    What item are you looking for?
    hjgjg
    Item hjgjg is not stocked in the shop.

What I have:

public class ShopApp


public static void main(String args[])


Shop items[]=new Shop[14];
items[0]=new Shop("Bread", (int) 15.00);
items[1]=new Shop("Milk", (int) 12.00);
items[2]=new Shop("Butter", (int) 56.00);
items[4]=new Shop("Chips", (int) 20.00);
items[5]=new Shop("Honey", (int) 45.00);
items[6]=new Shop("Soap", (int) 7.00);
items[7]=new Shop("Towel", (int) 75.00);
items[8]=new Shop("Carrots", (int) 8.00);
items[9]=new Shop("Beans", (int) 5.00);
items[10]=new Shop("Samp", (int) 12.00);
items[11]=new Shop("Dress", (int) 200.00);
items[12]=new Shop("Pants", (int) 150.00);
items[13]=new Shop("Shoes", (int) 400.00);
items[14]=new Shop("Socks", (int) 25.00);

    for (Shop item : items) {
        System.out.println("Item: " + item.item + ", Price:" + item.price);
    } 

    }

class Shop


String item;
int price;

public Shop(String item,int price)

this.item=item;
this.price=price;

Dani AI

Generated

The specification wants two parallel arrays (names + prices). The code posted by instantiates a Shop[] and has several practical and syntax problems — missing braces, using (int) which truncates cents, skipped an index (leaving a null element), and an out-of-range assignment (items[14] on an array of length 14). As points out, follow the array approach for the assignment; in real code a small Item class or a Map is preferable (the point makes about real requirements vs exam specs).

Key fixes and pitfalls to avoid:

  • Use double for prices (not int) so cents aren’t lost.
  • If new Shop[14] is used, valid indices are 0..13 — assigning items[14] throws ArrayIndexOutOfBoundsException.
  • Leaving an array slot uninitialized leads to a null element and a NullPointerException when accessed.
  • For lookups, read a line with Scanner.nextLine().trim() and compare with equalsIgnoreCase.
  • Format money with System.out.printf("R%.2f%n", value) to show two decimals.

A compact, correct approach using the required parallel arrays (replace the placeholder comments with the 14 names/prices from the spec if needed):

import java.util.Scanner;

public class ShopApp {
    public static void main(String[] args) {
        String[] items = {"Bread","Milk","Butter","Chips","Honey","Soap","Towel","Carrots","Beans","Samp","Dress","Pants","Shoes","Socks"};
        double[] prices = {15.50,12.00,56.00,20.00,45.00,7.00,75.00,8.00,5.00,12.00,200.00,150.00,400.00,25.00};

        for (int i = 0; i < items.length; i++)
            System.out.printf("%s - R%.2f%n", items[i], prices[i]);

        double sum = 0;
        for (double p : prices) sum += p;
        System.out.printf("The average price of all the items is R%.2f%n", sum / prices.length);

        Scanner sc = new Scanner(System.in);
        System.out.println("Item Look-up\nWhat item are you looking for?");
        String lookup = sc.nextLine().trim();
        boolean found = false;
        for (int i = 0; i < items.length; i++) {
            if (items[i].equalsIgnoreCase(lookup)) {
                System.out.printf("The price of item %s is R%.2f%n", items[i], prices[i]);
                found = true;
                break;
            }
        }
        if (!found) System.out.printf("Item %s is not stocked in the shop.%n", lookup);
        sc.close();
    }
}

Testing notes: run the program and try a missing product to confirm the “not stocked” message, and test case-insensitive lookups. For production or later coursework, refactor to a small Item class or use an ArrayList/Map for easier maintenance.

Recommended Answers

All 2 Replies

You have created a little class to hold the item names and prices. That's what any Java programmer would instinctively do. The problem is that the specification you have been given tells you to create two parallel arrays. Presumably you teacher is assuming that you haven't learned about classes yet?
In any case, no matter how stupid the spec is, you have to write what you are asked to write if you want to get a good grade.

If you should continue until you become a professional (employed) programmer you will find out the ugly truth about users. What they ask for is seldom what they want, or need. Sometimes it will be your (probably) unpleasant task to explain that to them.

commented: +1. A client decided they would supply the changes in writing. All good but one day I wanted to talk about a change."do it per the document" I did. +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.