Results 1 to 3 of 3
- 04-20-2010, 06:07 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
read and write arraylists to a file
Hi,
Im working on a stock system for a project and im fairly new to java.
I need help with my saveFile, readFile and displayReadFile methods, I cant display the file in cmd when i choose the displayReadFile method, i get null values instead. So im not sure if ive done soemthing wrong with saving, reading or displaying the file.
Urgent help would be much appreciated.
Heres my program (i also have two other classes not pasted in here, one with main and the other realting to input from the keyboard):
import java.io.*;
import java.util.ArrayList;
class Stock implements Serializable
{
int maxStock; //intialising values
int current = 0;
String name [];
int quantity [];
double cost [];
double totalStockValue = 0.0;
boolean success;
final double gst = 1.125;
String n ="";
int tempId = 1000;
int itemId [];
public ArrayList<Integer> itemIds;
public ArrayList<String> names;
public ArrayList<Integer> quantities;
public ArrayList<Double> costs;
public Stock()
{
ArrayList<Integer> itemIds = new ArrayList<Integer>();
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> quantities = new ArrayList<Integer>();
ArrayList<Double> costs = new ArrayList<Double>();
}
public void storeItem(int itemId, String name, int quantity, double cost)
{
itemIds.add(itemId);
names.add(name);
quantities.add(quantity);
costs.add(cost);
}
public void setMaxStock(int maxStock) //a setter for maxStock
{
if (maxStock > 0)
{
this.maxStock = maxStock;
}
else
{
this.maxStock = 0;
}
}
public int getMaxStock() //a getter for max stock
{
return maxStock;
}
public void setItemId(int itemId[]) //setter for array name
{
this.itemId = itemId;
}
public void setName(String name[]) //setter for array name
{
this.name = name;
}
public void setQuantity(int quantity[]) //setter for array quantity
{
this.quantity = quantity;
}
public void setCost(double cost[]) //setter for array cost
{
this.cost = cost;
}
public int getItemId(int itemId) //getter for array name
{
return itemId;
}
public String getName(String name) //getter for array name
{
return name;
}
public int getQuantity(int quantity) //getther for array quantity
{
return quantity;
}
public double getCost(double cost) //getter for array cost
{
return cost;
}
Stock(String commandArgs[]) //custom constructor
{
if (commandArgs.length != 0)
{
maxStock = validateArg(commandArgs[0]); //calls validate to check if the correct data has been entered
}
else
{
System.out.println("\n\nInvalid or missing maximum stock level supplied."); //error message for invalid maxStock entry
System.out.println("Usage: java Inventory <n> where <n> is the maximum number of stock items.\n");
System.out.println("============================== ================================================") ;
System.exit(0); //exits the program
}
itemId = new int [maxStock];
name = new String [maxStock];
quantity = new int [maxStock];
cost = new double [maxStock];
}
public int validateArg(String theArg) //called by stock to check for valid maxstock values
{
for (int i = 0; i < theArg.length(); i++)
{
if ( !Character.isDigit(theArg.charAt(i)))
{
System.out.println("\n\nYou need to supply a numeric value."); //error message for invalid input from user
System.out.println("Usage: java Inventory <n> where <n> is the maximum number of stock items.\n");
System.out.println("============================== ================================================") ;
System.exit(0);
return -1;
}
}
Integer tempInt = new Integer (theArg); //assigns tempInt with the value of the argument entered as a new int
return tempInt.intValue();
}
public void displayMenu() //displays a menu to add items, view stock or exit the program
{
boolean success;
int option =99;
String tmp = "";
if (maxStock != -1)
{
do
{
System.out.println("\n\n\nMax number of stock items is: "+maxStock);
System.out.println("\nThe current number of items entered is : "+ current);
System.out.println("\n\nStock Menu");
System.out.println("============================== ");
System.out.println("<1> Add an item");
System.out.println("<2> Edit a stock item");
System.out.println("<3> Delete a stock item");
System.out.println("<4> Find a stock item");
System.out.println("<5> View Inventory");
System.out.println("<0> Exit");
System.out.print("\nSelect an option <1>, <2>, <3>, <4>, <5> or <0>: ");
tmp = KeyInput.readString(); //reads keyboard input
success = true;
try
{
option = Integer.parseInt(tmp); //parses the input into an integer that can be testes against menu options
}
catch(Exception a) //catches exceptions
{
System.out.println("\n\nInvalid menu option, must be 1, 2 or 0."); // error message for exceptions eg a string instead of a number
success = false;
}
}
while(!success);
}
if (option == 1 ) //if the user typed option 1 this if statement would execute by calling setName
{
if (current != maxStock) //current items have to be less than max stock for it to run
{
setName(); //calls set name methods
}
else
{
System.out.println("\n\nDatabase is full please choose another option\n"); //if a user trys to enter data when the database is full this error will display
System.out.println("----------------------------------------------\n");
displayMenu();
}
}
else if (option == 2)
{
editItem();
}
else if (option == 3)
{
deleteItem();
}
else if (option == 4)
{
findItem();
}
else if (option == 5)
{
if (current != 0)
{
readFile();
displayReadFile();
}
else
{
System.out.println("\n\nNo stock has been entered please add an item or exit.");
System.out.println("----------------------------------------------------------\n\n");
displayMenu();
}
}
else if (option == 0)
{
System.out.println("\n\n\nThank you for using a Corney Flakes (C) 2010 production :)\n\n");
System.out.println("****************************** *********************************************\n\n" );
System.out.println("Written by: \tCourtenay Beckwith, KeyInput file credited to Karl Dodds.");
System.out.println("Produced: \tMarch 2010");
System.out.println("Course: \tBCPR212");
System.out.println("\n\n************************** *************************************************\ n\n");
System.exit(0);
}
else
{
System.out.println("\n\n\nInvalid menu option, must be 1, 2, 3, 4, 5 or 0.");
System.out.println("----------------------------------------");
displayMenu();
}
}
public void setName()
{
boolean success;
String tmp = "";
do
{
System.out.print("\nEnter the name of the item: "); //prompts the user for an item name
success = true;
try
{
tmp = KeyInput.readString(); // reads keyboard input from the user
if(tmp.length() > 0)
{
this.itemId[current] = tempId;
this.name[current] = tmp; //assigns the current name to the value of tmp from the keyboard entry
}
else
{
System.out.println("\nName cannot be null a name must be entered.");
setName();
}
}
catch (Exception b)
{
System.out.println("Not a valid string name"); //exception error for not a valid string name eg no input.
success = false;
}
}
while(!success);
System.out.print("\nNow enter the quantity of "+ name[current] +"'s: "); //prompts for quantity
setQuantity(); //calls set quantity
}
public void setQuantity()
{
boolean success;
int quantity = 0;
String tmp = "";
do
{
tmp = KeyInput.readString(); //reads the quantity
success = true;
try
{
Integer I = new Integer(tmp); //stores the input as tmp
quantity = I.intValue(); //assigns quantity to the int value entered
if (I.intValue() > 0) //only if the quantity is greater than zero will it prompy for a cost
{
System.out.print("\nNow enter the cost price of "+ name[current] +"'s: $");
}
}
catch (Exception c)
{
System.out.println("\nNot a valid input, should be a whole number and greater than zero eg 9");
System.out.print("\nPlease re-enter the quantity of "+ name[current] +"'s: ");
success = false;
}
}
while(!success);
this.quantity[current] = quantity;
setCost();
}
public void setCost()
{
boolean success;
double cost = 0.0;
String tmp = "";
do
{
tmp = KeyInput.readString();
success = true;
try
{
Double D = new Double(tmp);
cost = D.doubleValue();
if (D.doubleValue() > 0.0)
{
this.cost[current] = cost;
saveFile();
current++;
tempId++;
displayMenu();
}
else
{
System.out.println("\nNot a valid input, should be a real number and greater than zero eg. 10.65");
System.out.print("\nPlease re-enter the cost price of "+ name[current] +"'s: ");
setCost();
}
}
catch (Exception d)
{
System.out.println("\nNot a valid input, should be a real number and greater than zero eg. 10.65");
System.out.print("\nPlease re-enter the cost price of "+ name[current] +"'s: ");
success = false;
}
}
while(!success);
}
public void findItem()
{
boolean success;
String tmp = "";
do
{
System.out.println("\nPlease enter the name of the search item: ");
success = true;
try
{
tmp = KeyInput.readString();
if(tmp.length() > 0)
{
readFile();
int locationIndex = names.indexOf(tmp);
System.out.print("Index location of search name is: " + locationIndex);
}
else
{
System.out.print("\nSearch name cannot be null");
findItem();
}
}
catch (Exception e)
{
System.out.println("Not a valid string name");
success = false;
}
}
while(!success);
displayMenu();
}
public void saveFile()
{
try
{
ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream("stock.inv"));
outFile.writeObject(itemIds);
outFile.writeObject(names);
outFile.writeObject(quantities);
outFile.writeObject(costs);
outFile.close();
}
catch (IOException e)
{
System.out.println("\n\nFile exception error:" + e);
}
}
public void readFile()
{
try
{
ObjectInputStream inFile = new ObjectInputStream(new FileInputStream("stock.inv"));
itemIds = (ArrayList<Integer>)inFile.readObject();
names = (ArrayList<String>)inFile.readObject();
quantities = (ArrayList<Integer>)inFile.readObject();
costs = (ArrayList<Double>)inFile.readObject();
inFile.close();
}
catch (IOException f)
{
System.out.println("\n\nFile exception error1:" + f);
}
catch (ClassNotFoundException g)
{
System.out.println("\n\nFile exception error2:" + g);
}
catch (Exception h)
{
System.out.println("\n\nFile exception error3:" + h);
}
}
public void displayReadFile()
{
System.out.println("\n\nThere are "+ current +" items stored in the warehouse.\n");
System.out.println("ItemID\t Name\t\t Quantity \tCost Price \tSell Price inc GST \tTotal Selling Value");
System.out.println("============================== ================================================== ===================");
System.out.println(itemIds + "\t" + names + "\t\t" + quantities + "\t\t$" + costs);
displayMenu();
}
public void editItem()
{
findItem();
displayMenu();
}
public void deleteItem()
{
findItem();
}
}
- 04-20-2010, 10:18 AM #2
your query is not clear.Put proper debugging statement and printStackTrace() inside catch.
Ramya:cool:
- 04-20-2010, 11:50 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Using Read/Write From File Using Scanner
By javaisntcoffee123 in forum New To JavaReplies: 4Last Post: 04-15-2010, 03:35 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to read from and write to .properties file from a jsp
By MAGNUM in forum New To JavaReplies: 5Last Post: 01-20-2009, 09:08 AM -
Read and Write file
By mrdestroy in forum New To JavaReplies: 13Last Post: 10-31-2008, 12:11 PM -
File read/write problems
By p900128 in forum New To JavaReplies: 4Last Post: 06-27-2008, 12:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks