0

Need help with this java program

Write a Java program that maintains a price catalog for a grocery
store. Each catalog entry is an object of the class Item defined
exactly as shown:

    public class Item {
        private int id;
        private String name;
        private double price;

        public Item(int i, String n, double p) {
            id = i;
            name = n;
            price = p;
        }

        public int getKey() {return id;}

        public void print() {
            System.out.printf("Item number: %d%n", id);
            System.out.printf("Description: %s%n", name);
            System.out.printf("Price:       %.2f%n", price);
        }
    }

The catalog is maintained in an object of class Catalog, which
may store up to a specified number of items. The items are
stored in the array "itemList". The "size" field contains the
number of items currently stored. The items are stored at
subscripts 0 through size-1.

    public class Catalog {
        private Item[] itemList;
        private int size;

        public Catalog(int max) {
            // Initialize an empty catalog with the capacity to
            // store up to "max" items. Create the "itemList"
            // array to hold "max" elements. Initialize "size"
            // to 0.
        }

        public void insert(Item item) {
            // Insert the item passed as parameter into the
            // catalog at the next available array subscript.
            // Increment "size".
        }

        public Item find(int key) {
            // Search the catalog for an item having the item
            // number "key". Return the Item object if the
            // search is successful. Return the null reference
            // if the search fails.
        }
    }

Class Program4 contains a main method that creates and
populates the catalog as shown and then processes a series
of user interactions. The user enters an item number at the
keyboard. The program displays the corresponding item or an
error message if the item is not found. The program terminates
when the user enters 0 as the item number.

    import java.util.*;

    public class Program4 {
        public static void main(String[] args) {
            Scanner kbd = new Scanner(System.in);
            Catalog store;

            store = new Catalog(8);
            store.insert(new Item(1111, "Hamburger", 2.40));
            store.insert(new Item(2222, "Chicken", 4.20));
            store.insert(new Item(3333, "Milk", 0.85));
            store.insert(new Item(4444, "Butter", 2.15));
            store.insert(new Item(5555, "Bread", 1.30));

            // Process a series of user interactions where the
            // user enters an item number at the keyboard and the
            // program displays the corresponding item or an
            // error message if the item is not found. The program
            // terminates when the user enters 0 as the item number.
        }
    }

Base your code on the following sample run:

    Item number? 1111

    Item number: 1111
    Description: Hamburger
    Price:       2.40

    Item number? 8888

    No item found for 8888.

    Item number? 2222

    Item number: 2222
    Description: Chicken
    Price:       4.20

    Item number? 0

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by bibiki
0

I hate to be the bearer of bad news, but nobody is going to do your homework. nobody did mine, either, so no hard feelings, ok :P

seriously, you are expected to give the code you wrote and that does not work before anyone will give you any help. show some effort.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.