-1

I have an array of objects and I am trying to change the values of the objects and then re-update the storage file where the object-in-file are stored. When I try changing the storage file it either clears the file or does nothing. Here is my code..

public class InventoryManager implements Serializable{

String name;
String code;
double price;
int inventory;
String expireDate;
boolean frozen;
String type;
int shoeSize;
double screenSize;
boolean medical;
Object myObject;


Controller l;

FileOutputStream storage = new FileOutputStream("Storage.dat");
ObjectOutputStream out = new ObjectOutputStream(storage);

Pharmacy ph;
TVS tv;
Shoes shoe;
Fruit fruit;

Scanner input = new Scanner(new File("inp.dat"));


public InventoryManager(Object x) throws IOException{

    myObject = x;


    if(x instanceof Item){
        name = ((Item) x).getName();
        code = ((Item) x).getCode();
        price = ((Item) x).getPrice();
        inventory = ((Item) x).getInventory();

        }

    out.writeObject(x);
    out.close();
}

public class Controller implements ActionListener {

GUI myGui;
InventoryManager[] myManager;
JButton myButton;
int quantity;
String nameofproduct = "";

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub



    if(arg0.getSource() instanceof JButton){


        myButton = (JButton) arg0.getSource();
        String x = myButton.getText();

        for(int i = 0; i<myManager.length; i++){
            if(x.equals(myManager[i].name)){
                nameofproduct = myManager[i].name;
            }
        }

        if(x.equals("Purchase")){
            try{
                quantity = Integer.parseInt(myGui.qtnInput.getText());
            if(nameofproduct == ""){
                JOptionPane.showMessageDialog(null, "Select a Product");
            }
            else{

                for(int i = 0; i<myManager.length; i++){
                    if(nameofproduct.equals(myManager[i].name)){

                        myManager[i].inventory = myManager[i].inventory - quantity;
                        myGui.qtnLeft.setText("Quantity: "+myManager[i].inventory);
                        myGui.CartInv.setText(myGui.CartInv.getText() + myManager[i].name + " " + quantity+"\n");
                        myGui.description.setText(myManager[i].toString());

                        }
                    }



                }



            }
            catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null, "That is not a number");
                myGui.qtnInput.setText("");
            }
        }

        for(int i = 0; i<myManager.length; i++){
            if(x.equals(myManager[i].name)){
                myGui.qtnLeft.setText("Quantity: "+myManager[i].inventory);
                myGui.description.setText(myManager[i].toString());
            }
        }


    }


}


}

public void getIM(InventoryManager[] myManager){
    this.myManager = myManager;
}

}

Bear
  • 1
  • 2
    Welcome to Stack Overflow! I would imagine that 95% of this code is not relevant to your question. Please create a [**Minimal**, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your issue. – Joe C Apr 22 '17 at 20:18

1 Answers1

0

imaging bytes in a file as LegoTM blocks on a base plate each close to each other.

If you want to change some blocks in the middle you have to move all the blocks after the changed position and move them backwards by the number of bytes the changed content part is longer than the old one.

Almost the same is true for the byte on your hard disk.

This means the easiest way is to overwrite the whole file in the first place.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51