Consider a program to enter codes of one to eight characters along with an assoc
ID: 3687962 • Letter: C
Question
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 phonebook are to be represented with a simple class:
class Entry { public:
string name, number, notes;
};
Use an array to store the entries. The array should allow for up to 200 entries.
Entry entryList[200];
Use a function to read the entries into the program from a file, and a function to store the entries back into the file when the program is exited.
void readInventory () void storeInventory ()
Also, use a function to list all contacts:
void listAllEntries()
Final Submission:
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.
Note: You may wish to use
string strToUpper (string S) { for (size_t i=0; i < S.length(); i++) S[i] = toupper(S[i]); return S;
}
Writing to a File void storeInventory () { fstream F("PhoneData.txt"); rec_num = 0;
while (rec_num < num_entries){
F << inventoryList[rec_num].name << " "
<< inventoryList[rec_num].quantity << " "
<< inventoryList[rec_num].notes << " " << endl; rec_num++;
}
cout << "Inventory stored." << endl; return;
}
Explanation / Answer
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;
public class phoneEntry
{
public static Entry[] entryList = new Entry[200];
static Scanner fileReader, stdin;
static int numEntries;
public static void main(String[] args) throws Exception
{
Scanner stdin;
String command;
String[] name;
final String FILE;
char commandChar;
int index;
stdin = new Scanner(System.in);
FILE = "entries.txt";
index = -1;
numEntries = readPhoneBook(FILE);
if (numEntries == 200)
{
System.out.println("Error: Phonebook is full.");
System.exit(0);
}
System.out.println("Codes are entered as 1 to 8 characters.");
System.out.println("Use "e" for enter, "f" for find, "l" for list, "q" for quit.");
System.out.println();
System.out.print("Command: ");
command = stdin.nextLine();
commandChar = command.charAt(0);
while(!command.matches("q") && !command.matches("Q"))
{
//Creates new entries
if (commandChar == 'e' || commandChar == 'E')
{
entryList[numEntries] = new Entry();
name = command.split(" ");
//Checks to see if the code is of the specified length
while (name[1].length() > 8)
{
System.out.println("Error: codes must contain 1-8 characters.");
System.out.print("Command: ");
command = stdin.nextLine();
name = command.split(" ");
}
entryList[numEntries].name = name[1];
System.out.print("Enter number: ");
entryList[numEntries].number = stdin.nextLine();
System.out.print("Enter notes: ");
entryList[numEntries].notes = stdin.nextLine();
if (entryList[numEntries].notes.matches(""))
{
entryList[numEntries].notes = "No notes";
}
System.out.println();
numEntries++;
}
//Finds an entry
else if (commandChar == 'f' || commandChar == 'F')
{
name = command.split(" ");
for (int x = 0; x < numEntries; x++)
{
if (entryList[x].name.equalsIgnoreCase(name[1]))
{
index = x;
break;
}
else
{
index = -1;
}
}
if (index >= 0)
{
System.out.println("-- " + entryList[index].name);
System.out.println("-- " + entryList[index].number);
System.out.println("-- " + entryList[index].notes);
}
else if (index == -1)
{
System.out.println("** No entry with code " + name[1]);
System.out.println();
}
}
//Lists all entries
else if (commandChar == 'l' || commandChar == 'L')
{
for (int i = 0; i < numEntries; i++)
{
System.out.println("-- " + entryList[i].name);
System.out.println("-- " + entryList[i].number);
System.out.println("-- " + entryList[i]. notes);
System.out.println();
}
}
else
{
System.out.println("Command " + command + " is an invalid command.");
}
System.out.print("Command: ");
command = stdin.nextLine();
commandChar = command.charAt(0);
}
storePhoneBook(FILE);
stdin.close();
}
//Stores entries into the file
public static void storePhoneBook(String file) throws Exception
{
PrintStream print;
print = new PrintStream(file);
for (int i = 0; i < numEntries; i++)
{
print.println(entryList[i].name + " " + entryList[i].number + " " + entryList[i].notes);
}
System.out.println("Phonebook stored");
print.close();
}
//Reads entries into the program from the file
public static int readPhoneBook(String file) throws Exception
{
int i;
i = 0;
try
{
fileReader = new Scanner(new File(file));
fileReader.useDelimiter(" | ");
while (fileReader.hasNext())
{
entryList[i] = new Entry();
entryList[i].name = fileReader.next();
entryList[i].number = fileReader.next();
entryList[i].notes = fileReader.next();
i++;
}
}
catch (Exception NoSuchElementException)
{
}
return i;
}
}
Entry.java
// Creates new object "Entry". Entries consist of three Strings (name, number, and notes)
public class Entry
{
public String name, number, notes;
}
sample output
Compile and Execute Java-8 Online
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 number: 20
Enter notes: chilled storage
Command: q
Phonebook stored
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.