Consider the inventory management application of Project 6. A solution to this p
ID: 3828499 • Letter: C
Question
Consider the inventory management application of Project 6. A solution to this project is given at the end of this document. You can use this code or your own code in Project 6 as a basis for this assignment. Either way is fine.
For the fixed part of this project (the 220 points) you must extend the program as follows:
Add a delete command.
Keep the inventory in alphabetical order. When a new item is to be added, put the entry into the appropriate location in the array.
Construct a Graphical User Interface (GUI) for the user. Do not use JOptionPane. You must use JavaFX.
Make the program object oriented. This means having a separate class for the internal representation of the inventory, with methods like add, list, or delete. It also means having a separate class for the GUI aspects of the program.
The program must follow the 10 Program Standards given with Project 4 (all previous projects are still posted in the folder “Previous Projects”). These standards are reposted in a file called
Program Standards for Java Course.doc
Final Submission: Submitting a portfolio or dossier of sorts (a stapled or bound set of pages) containing
A separate cover page (or pages) itemizing the grounds for extra credit. It is your responsibility to itemize all grounds for extra credit.
A printout of your code,
A printout from several consecutive runs, illustrating the various features of your program. For example, you must show that the file I/O works.
A printout from the list command
In the sample runs, each of the commands "e", "f", "l", “d”, and "q" should be illustrated.
A solution similar to Project 6.
import java.io.*;
import java.util.*;
class Entry {
public String name, quantity, note;
}
public class InventoryFor1510 {
public static Entry[] entryList;
public static int num_entries;
public static Scanner stdin = new Scanner(System.in);
public static void main(String args[]) throws Exception{
int i;
char C;
String code, Command;
entryList = new Entry[200];
num_entries = 0;
Command = null;
C = ' ';
readInventory("inventory.txt");
System.out.println("Codes are entered as 1 to 8 characters. Use" +
" "e" for enter," +
" "f" for find," +
" "l" for listing all the entries," +
" "q" to quit.");
while(C != 'q'){
System.out.print("Command: ");
Command = stdin.next();
C = Command.charAt(0);
switch (C) {
case 'e':
addItem(); break;
case 'f':
code = stdin.next();
stdin.nextLine();
i = index(code);
if (i >= 0) displayEntry(entryList [i]);
else System.out.println("**No entry with code " + code); break;
case 'l':
listAllItems(); break;
case 's':
sortList(); break;
case 'q':
CopyInventoryToFile("inventory.txt");
System.out.println("Quitting the application. All the entries are "
+ "stored in the file inventory.txt"); break;
default:
System.out.println("Invalid command. Please enter the command again!!!"); }
}
}
public static void readInventory(String FileName) throws Exception {
File F;
F = new File(FileName);
Scanner S = new Scanner(F);
while (S.hasNextLine()) {
entryList [num_entries]= new Entry();
entryList [num_entries].name = S.next();
entryList [num_entries].quantity = S.next();
entryList [num_entries].note = S.nextLine();
num_entries++;
}
S.close();
}
public static void addItem() {
String name = stdin.next();
String quantity;
stdin.nextLine();
entryList [num_entries] = new Entry();
entryList [num_entries].name = name;
System.out.print("Enter Quantity: ");
quantity = stdin.nextLine();
entryList [num_entries].quantity = quantity;
System.out.print("Enter Notes: ");
entryList [num_entries].note = stdin.nextLine();
num_entries++;
}
public static int index(String Key) {
// Function to get the index of a key from an array
// if not found, returns -1
for (int i=0; i < num_entries; i++) {
if (entryList [i].name.equalsIgnoreCase(Key))
return i; // Found the Key, return index.
}
return -1;
}
public static void displayEntry(Entry item) {
System.out.println("--"+ item.name+" "+
item.quantity+" "+
item.note);
}
public static void listAllItems() {
int i = 0;
while (i < num_entries) {
displayEntry(entryList [i]);
i++;
}
}
public static void CopyInventoryToFile(String FileName) throws Exception{
FileOutputStream out = new FileOutputStream(FileName);
PrintStream P = new PrintStream( out );
for (int i=0; i < num_entries; i++) {
P.println(entryList [i].name + " " + entryList [i].number +
" " + entryList [i].note);
}
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
class Entry {
public String name, quantity, note;
}
public class InventoryFor1510 {
public static Entry[] entryList;
public static int num_entries;
public static Scanner stdin = new Scanner(System.in);
public static void main(String args[]) throws Exception{
int i;
char C;
String code, Command;
entryList = new Entry[200];
num_entries = 0;
Command = null;
C = ' ';
readInventory("inventory.txt");
System.out.println("Codes are entered as 1 to 8 characters. Use" +
" "e" for enter," +
" "f" for find," +
" "l" for listing all the entries," +
" "q" to quit.");
while(C != 'q'){
System.out.print("Command: ");
Command = stdin.next();
C = Command.charAt(0);
switch (C) {
case 'e':
addItem(); break;
case 'f':
code = stdin.next();
stdin.nextLine();
i = index(code);
if (i >= 0) displayEntry(entryList [i]);
else System.out.println("**No entry with code " + code); break;
case 'l':
listAllItems(); break;
case 's':
sortList(); break;
case 'q':
CopyInventoryToFile("inventory.txt");
System.out.println("Quitting the application. All the entries are "
+ "stored in the file inventory.txt"); break;
default:
System.out.println("Invalid command. Please enter the command again!!!"); }
}
}
public static void readInventory(String FileName) throws Exception {
File F;
F = new File(FileName);
Scanner S = new Scanner(F);
while (S.hasNextLine()) {
entryList [num_entries]= new Entry();
entryList [num_entries].name = S.next();
entryList [num_entries].quantity = S.next();
entryList [num_entries].note = S.nextLine();
num_entries++;
}
S.close();
}
public static void addItem() {
String name = stdin.next();
String quantity;
stdin.nextLine();
entryList [num_entries] = new Entry();
entryList [num_entries].name = name;
System.out.print("Enter Quantity: ");
quantity = stdin.nextLine();
entryList [num_entries].quantity = quantity;
System.out.print("Enter Notes: ");
entryList [num_entries].note = stdin.nextLine();
num_entries++;
}
public static int index(String Key) {
// Function to get the index of a key from an array
// if not found, returns -1
for (int i=0; i < num_entries; i++) {
if (entryList [i].name.equalsIgnoreCase(Key))
return i; // Found the Key, return index.
}
return -1;
}
public static void displayEntry(Entry item) {
System.out.println("--"+ item.name+" "+
item.quantity+" "+
item.note);
}
public static void listAllItems() {
int i = 0;
while (i < num_entries) {
displayEntry(entryList [i]);
i++;
}
}
public static void CopyInventoryToFile(String FileName) throws Exception{
FileOutputStream out = new FileOutputStream(FileName);
PrintStream P = new PrintStream( out );
for (int i=0; i < num_entries; i++) {
P.println(entryList [i].name + " " + entryList [i].number +
" " + entryList [i].note);
}
}
}