Good day!
My codes stated don't have errors, it run well, BUT, though this program is saving in notepad as .out and .txt, when i closed the dos console and then run the program again, it is not reading the added items in .out and .txt file. That's the problem, it should read/view/edit/searched/delete the previous record. This only reads the real-time running of program like the adding, viewing, searching, viewing functions.
Any suggestions will be much appreciated. Thank you.
import java.io.*;
import java.util.InputMismatchException;
public class www
{
public static String categI[] = new String[100];
public static String itemsI[] = new String[100];
public static String empNameI[] = new String[100];
public static String IDI[] = new String[100];
public static String positI[] = new String[100];
public static double priceI[] = new double[100];
public static double itemsUsedI[] = new double[100];
public static double remItemsI[] = new double[100];
public static double totRemItemsI[] = new double[100];
public static String dateI[] = new String[100];
public static String timeI[] = new String[100];
public static String acategI,aitemsI,aempNameI,aIDI,apositI;
public static double apriceI,aitemsUsedI,aremItemsI,atotRemItemsI;
public static String adateI,atimeI;
public static int actr=0;
public static char query;
public static void main(String[] args) throws Exception
{
menu();
}
public static void menu() throws Exception
{
char c;
do
{
System.out.println("Menu");
System.out.println("A = Add");
System.out.println("B = View/Print");
System.out.println("C = Edit");
System.out.println("D = Delete");
System.out.println("E = Search");
System.out.println("F = Exit");
System.out.print("Enter your choice: ");
c=Character.toUpperCase((char) System.in.read());
System.in.read(); System.in.read();
switch(c)
{
case 'A':
p1();
break;
case 'B':
p2();
break;
case 'C':
p3();
break;
case 'D':
p4();
break;
case 'E':
p5();
break;
default: System.out.println("Exiting...");
break;
}
}while(c!='F');
}
public static void p1() throws Exception
{
try
{
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String y;
PrintWriter outputStream = null;
FileWriter fStream = new FileWriter("C:/inventory.txt", true);
BufferedWriter out = new BufferedWriter(fStream);
try
{
outputStream = new PrintWriter(new FileOutputStream("C:/inventory.out", true));
}
catch(FileNotFoundException excep)
{
System.err.printf("Exception", excep);
System.exit(0);
}
System.out.print("Category: ");
acategI = x.readLine();
System.out.print("Name of item: ");
aitemsI = x.readLine();
System.out.print("Price each item: ");
y = x.readLine();
apriceI = Double.parseDouble(y);
System.out.print("Number of items used: ");
y = x.readLine();
aitemsUsedI = Double.parseDouble(y);
System.out.print("Remaining items: ");
y = x.readLine();
aremItemsI = Double.parseDouble(y);
atotRemItemsI = aremItemsI - aitemsUsedI;
System.out.print("Inventorer's name: ");
aempNameI = x.readLine();
System.out.print("ID combination: ");
aIDI = x.readLine();
System.out.print("Position: ");
apositI = x.readLine();
System.out.print("Date of inventory [mm/dd/yy]: ");
adateI = x.readLine();
System.out.print("Time of inventory [hr:min:sec]: ");
atimeI = x.readLine();
System.out.println("Are you sure you want to add this inventory record [y/n]: ");
query = Character.toUpperCase((char) System.in.read());
System.in.read(); System.in.read();
if(query=='Y')
{
categI[actr] = acategI;
itemsI[actr] = aitemsI;
empNameI[actr] = aempNameI;
IDI[actr] = aIDI;
positI[actr] = apositI;
priceI[actr] = apriceI;
itemsUsedI[actr] = aitemsUsedI;
remItemsI[actr] = aremItemsI;
dateI[actr] = adateI;
timeI[actr] = atimeI;
totRemItemsI[actr] = atotRemItemsI;
actr++;
outputStream.println("Category: " +acategI);
out.write("Category: " +acategI);
outputStream.println("Name of item: " +aitemsI);
out.write("Name of item: " +aitemsI);
outputStream.println("Price each item: " +apriceI);
out.write("Price each item: " +apriceI);
outputStream.println("Number of items used: " +aitemsUsedI);
out.write("Number of items used: " +aitemsUsedI);
outputStream.println("Remaining items: " +aremItemsI);
out.write("Remaining items: " +aremItemsI);
System.out.println("Total remaining items: " +atotRemItemsI);
outputStream.println("Total remaining items: " +atotRemItemsI);
out.write("Total remaining items: " +atotRemItemsI);
outputStream.println("Inventorer's name: " +aempNameI);
out.write("Inventorer's name: " +aempNameI);
outputStream.println("ID combination: " +aIDI);
out.write("ID combination: " +aIDI);
outputStream.println("Position: " +apositI);
out.write("Position: " +apositI);
outputStream.println("Date of inventory [mm/dd/yy]: " +adateI);
out.write("Date of inventory [mm/dd/yy]: " +adateI);
outputStream.println("Time of inventory [hr:min:sec]: " +atimeI);
out.write("Time of inventory [hr:min:sec]: " +atimeI);
outputStream.close();
out.close();
}
else
{
spacing();
}
outputStream.close();
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void spacing()
{
System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
public static void p2() throws Exception
{
try
{
FileReader fr = new FileReader( "c:/inventory.out" );
BufferedReader br = new BufferedReader( fr );
String stringRead = br.readLine( );
while( stringRead != null )
{
System.out.println( stringRead );
stringRead = br.readLine( );
}
br.close( );
}
catch( FileNotFoundException fnfe )
{
System.out.println( "Unable to find inventory.out, exiting" );
}
catch( IOException ioe )
{
ioe.printStackTrace( );
}
}
public static void p3()throws Exception
{
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String y;
PrintWriter outputStream = null;
FileWriter fStream = new FileWriter("C:/inventory.txt", true);
BufferedWriter out = new BufferedWriter(fStream);
try
{
outputStream = new PrintWriter(new FileOutputStream("C:/inventory.out", true));
}
catch(FileNotFoundException excep)
{
System.err.printf("Exception", excep);
System.exit(0);
}
boolean retbol = false;
int indx=0;
spacing();
System.out.print("-->EDIT RECORDS<--\n\n\n");
System.out.print("Search name of item to be edited: ");
String edititemsI = x.readLine();
for(int i=0;i<itemsI.length;i++)
{
if(itemsI[i] !=null)
{
if(edititemsI.equalsIgnoreCase(itemsI[i]))
{
retbol = true;
indx = 0;
}
}
}
if(retbol==true)
{
System.out.print("Category: ");
String ecategI = x.readLine();
System.out.print("Name of item: ");
String eitemsI = x.readLine();
System.out.print("Price each item: ");
y = x.readLine();
double epriceI = Double.parseDouble(y);
System.out.print("Number of items used: ");
y = x.readLine();
double eitemsUsedI = Double.parseDouble(y);
System.out.print("Remaining items: ");
y = x.readLine();
double eremItemsI = Double.parseDouble(y);
double etotRemItemsI = eremItemsI - eitemsUsedI;
System.out.print("Inventorer's name: ");
String eempNameI = x.readLine();
System.out.print("ID combination: ");
String eIDI = x.readLine();
System.out.print("Position: ");
String epositI = x.readLine();
System.out.print("Date of inventory [mm/dd/yy]: ");
String edateI = x.readLine();
System.out.print("Time of inventory [hr:min:sec]: ");
String etimeI = x.readLine();
System.out.println("Are you sure you want to edit this inventory record [y/n]: ");
query = Character.toUpperCase((char) System.in.read());
System.in.read(); System.in.read();
if(query=='Y')
{
categI[actr] = ecategI;
itemsI[actr] = eitemsI;
empNameI[actr] = eempNameI;
IDI[actr] = eIDI;
positI[actr] = epositI;
priceI[actr] = epriceI;
itemsUsedI[actr] = eitemsUsedI;
remItemsI[actr] = eremItemsI;
dateI[actr] = edateI;
timeI[actr] = etimeI;
totRemItemsI[actr] = etotRemItemsI;
actr++;
outputStream.println("Category: " +ecategI);
out.write("Category: " +ecategI);
outputStream.println("Name of item: " +eitemsI);
out.write("Name of item: " +eitemsI);
outputStream.println("Price each item: " +epriceI);
out.write("Price each item: " +epriceI);
outputStream.println("Number of items used: " +eitemsUsedI);
out.write("Number of items used: " +eitemsUsedI);
outputStream.println("Remaining items: " +eremItemsI);
out.write("Remaining items: " +eremItemsI);
System.out.println("Total remaining items: " +etotRemItemsI);
outputStream.println("Total remaining items: " +etotRemItemsI);
out.write("Total remaining items: " +etotRemItemsI);
outputStream.println("Inventorer's name: " +eempNameI);
out.write("Inventorer's name: " +eempNameI);
outputStream.println("ID combination: " +eIDI);
out.write("ID combination: " +eIDI);
outputStream.println("Position: " +epositI);
out.write("Position: " +epositI);
outputStream.println("Date of inventory [mm/dd/yy]: " +edateI);
out.write("Date of inventory [mm/dd/yy]: " +edateI);
outputStream.println("Time of inventory [hr:min:sec]: " +etimeI);
out.write("Time of inventory [hr:min:sec]: " +etimeI);
}
else
{
System.out.print("Item not found!");
}
outputStream.close();
out.close();
}
}
public static void p4()throws Exception
{
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String y;
try
{
FileReader fr = new FileReader( "c:/inventory.out" );
BufferedReader br = new BufferedReader( fr );
String stringRead = br.readLine( );
while( stringRead != null )
{
System.out.println( stringRead );
stringRead = br.readLine( );
}
boolean retbol = false;
int indx=0;
System.out.print("-->DELETE RECORDS<--\n\n\n");
System.out.print("Name of item to be deleted: ");
String deitems = x.readLine();
for(int i=0;i<itemsI.length;i++)
{
if(itemsI[i] !=null)
{
if(deitems.equalsIgnoreCase(itemsI[i]))
{
retbol = true;
indx = i;
}
}
if(retbol==true)
{
System.out.println("Category: " +categI[i]);
System.out.println("Name of item: " +itemsI[i]);
System.out.println("Price each item: " +priceI[i]);
System.out.println("Number of items used: " +itemsUsedI[i]);
System.out.println("Remaining items: " +remItemsI[i]);
System.out.println("Total remaining items: " +totRemItemsI[i]);
System.out.println("Inventorer's name: " +empNameI[i]);
System.out.println("ID combination: " +IDI[i]);
System.out.println("Position: " +positI[i]);
System.out.println("Date of inventory [mm/dd/yy]: " +dateI[i]);
System.out.println("Time of inventory [hr:min:sec]: " +timeI[i]);
System.out.println("");
System.out.print("Are you sure to delete this record?[y]-y/[n]-no: ");
char query = Character.toUpperCase((char) System.in.read());
System.in.read(); System.in.read();
if(query=='Y')
{
categI[i] = null;
itemsI[i] = null;
empNameI[i] = null;
IDI[i] = null;
positI[i] = null;
priceI = null;
itemsUsedI = null;
remItemsI = null;
dateI[i] = null;
timeI[i] = null;
totRemItemsI = null;
for(int j=indx+1;j<itemsI.length;j++)
{
categI[j-1] = categI[j];
itemsI[j-1] = itemsI[j];
empNameI[j-1] = empNameI[j];
IDI[j-1] = IDI[j];
positI[j-1] = positI[j];
priceI[j-1] = priceI[j];
itemsUsedI[j-1] = itemsUsedI[j];
remItemsI[j-1] = remItemsI[j];
dateI[j-1] = dateI[j];
timeI[j-1] = timeI[j];
totRemItemsI[j-1] = totRemItemsI[j];
}
System.out.println("Record succesfully deleted!");
}
}
else
{
System.out.print("Item not found!");
}
br.close( );
}
}
catch( FileNotFoundException fnfe )
{
System.out.println( "Unable to find inventory.out, exiting" );
}
catch( IOException ioe )
{
ioe.printStackTrace( );
}
}
public static void p5()throws Exception
{
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String y;
try
{
FileReader fr = new FileReader( "c:/inventory.txt" );
BufferedReader br = new BufferedReader( fr );
String stringRead = br.readLine( );
while( stringRead != null )
{
System.out.println( stringRead );
stringRead = br.readLine( );
}
boolean retbol = false;
int indx=0;
spacing();
System.out.print("-->SEARCH RECORDS<--\n\n\n");
System.out.print("Name of item to be searched: ");
String seitemsI = x.readLine();
for(int i=0;i<itemsI.length;i++)
{
if(itemsI[i] !=null)
{
if(seitemsI.equalsIgnoreCase(itemsI[i]))
{
retbol = true;
indx = i;
}
}
}
if(retbol==true)
{
System.out.println("Category: " +categI[indx]);
System.out.println("Name of item: " +itemsI[indx]);
System.out.println("Price each item: " +priceI[indx]);
System.out.println("Number of items used: " +itemsUsedI[indx]);
System.out.println("Remaining items: " +remItemsI[indx]);
totRemItemsI[indx] = remItemsI[indx] - itemsUsedI[indx];
System.out.println("Total remaining items: " +totRemItemsI[indx]);
System.out.println("Inventorer's name: " +empNameI[indx]);
System.out.println("ID combination: " +IDI);
System.out.println("Position: " +positI[indx]);
System.out.println("Date of inventory [mm/dd/yy]: " +dateI[indx]);
System.out.println("Time of inventory [hr:min:sec]: " +timeI[indx]);
}
else
{
System.out.print("Item not found!");
}
br.close( );
}
catch( FileNotFoundException fnfe )
{
System.out.println( "Unable to find inventory.out, exiting" );
}
catch( IOException ioe )
{
ioe.printStackTrace( );
}
}
}