Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

EECS 1510 Object Oriented Programming Consider a program to enter codes of one t

ID: 3814782 • Letter: E

Question

EECS 1510 Object Oriented Programming
Consider a program to enter codes of one to eight characters along with an associated item number and associated notes. A code can represent an item, package, or product’s name.

            By using such a program, we could enter product quantities and additional notes (i.e. storage considerations) before associating them with some brief code (i.e. string), which would indicate the product’s ID. We could then retrieve a product’s information by entering the product’s ID. This might be a useful application for a small store’s inventory and stock management.

      Codes are entered as 1 to 8 characters.

      Use "e" for enter, "f" for find, "l" for list, "q" for quit.

      Command: e Soda

      Enter quantity: 20

      Enter notes:

      Command: e Milk

      Enter quantity: 10

      Enter notes: Chilled storage

      Command: e Chips

      Enter quantity: 25

      Enter notes: Easily crushed

      Command: f Milk

      -- Milk

      -- 10

      -- Chilled storage

      Command: f Popcorn

      ** No entry with code Popcorn

      Command: . . .

There is also an "l" command, which will list all entries on the screen.

For the input to this program, upper and lower case letters are considered equivalent. For example, if a product name with the code "Soda" is entered, then the codes "SODA", "soda", and "SOda" will all retrieve the same entry with "Soda" associated.

            The entries are to be stored in a file from run to run. When the program begins, the entries in the file are to be read into an array. The array should allow for up to 200 entries. The inventory need not be kept in alphabetical order. You may use a simple sequential search to retrieve entries.   When the program is exited, the entries should be stored back in a file for use when the program is run again.

Required Program Characteristics: The assignment is to write a program that incorporates the above features. The entries in the inventory manager are to be represented with a class:

                class Entry {

          public String name, number, notes;

       }

Use an array to store the entries. The array should allow for up to 200 entries.

     public Entry[] entryList = new Entry[200];

Use a static method to read the entries into the program from a file, and a static method to store the entries back into the file when the program is exited.  

       public static void readInventory (String FileName)

              throws Exception

       public static void storeInventory (String FileName)

              throws Exception

Also, use a static method to list all entries:

            public static void listAllEntries()

Final Submission: See the posted file “Project Submissions.doc”.

In the printouts of the sample runs, each of the commands "e", "f", "l" and "q" should be illustrated.

You must also show that the external file is updated with entries added from a previous run.

Writing to a File

public static void WriteInventory(String FileName) throws Exception{

   PrintStream P = new PrintStream(FileName);

                             

   for (int i=0; i < num_entries; i++) {

      P.println(entryList[i].name      + " " +

                entryList[i].quantity + " " +

                entryList[i].notes);

   }

   P.close();

   System.out.println(“Inventory stored.”)           

}

Explanation / Answer

Here you go champ. Here is teh code just tailored for you!!

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.april;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;

/**
*
* @author Sam
*/
public class ProductList {

    public static Entry[] entryList = new Entry[200];
    public static int size;

    public static void main(String[] args) throws IOException, Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        readInventory("D:\Prog\Java\data.txt");
        System.out.print("Command: ");
        String line = br.readLine();
        String[] tokens =line.split(" ");
        while (!tokens[0].equalsIgnoreCase("q")){
            if (tokens[0].equalsIgnoreCase("e")){
                System.out.println("Enter quantity: ");
                String quantity = br.readLine();
                System.out.println("Enter notes: ");
                String notes = br.readLine();
                entryList[size++] = new Entry(tokens[1], quantity, notes);
            }
            else if (tokens[0].equalsIgnoreCase("l"))
                printList();
            else if (tokens[0].equalsIgnoreCase("f"))
                findtList(tokens[1]);
            else
                System.out.println("INVALID COMMAND");
            System.out.print("Command: ");
            line = br.readLine();
            tokens =line.split(" ");
          
        }
      
        writeInventory("D:\Prog\Java\data1.txt");
    }
  
    public static void readInventory(String FileName) throws FileNotFoundException, IOException { //read inventory from file
        BufferedReader br = new BufferedReader(new FileReader(FileName));
        String line;
        String[] tokens;
        size = 0;
        while ((line = br.readLine()) != null) {
            tokens = line.split("\t");
            entryList[size++] = new Entry(tokens[0].trim(), tokens[1].trim(), tokens[2].trim());
        }
    }

    public static void writeInventory(String FileName) throws Exception { //to simply write the array EntityList
        try (PrintStream P = new PrintStream(FileName)) {
            for (int i = 0; i < size; i++) {
                P.println(entryList[i].name + " "
                        + entryList[i].number + " "
                        + entryList[i].notes);
            }
        }
        System.out.println("Inventory stored.");
    }

    private static void printList() { //to print list
        for (int i = 0; i<size; i++){
                System.out.println("-" +entryList[i].name);
                System.out.println("----" +entryList[i].number);
                System.out.println("----" +entryList[i].notes);
        }
    }

    private static void findtList(String name) { //to find ignoring case
        for (int i = 0; i<size; i++){
            if ((entryList[i].name).equalsIgnoreCase(name)){
                System.out.println("--" +entryList[i].name);
                System.out.println("--" +entryList[i].number);
                System.out.println("--" +entryList[i].notes);
            }
        }
    }

}

class Entry {

    public String name, number, notes;

    public Entry(String name, String number, String notes) {
        this.name = name;
        this.number = number;
        this.notes = notes;
    }

}

I hope you understood the code and comments present. If you still face any problem regarding the methodology, please let me know the reason. I shall be glad to help you with your problem.