writing array of objects to a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DA AD
    New Member
    • Jan 2012
    • 1

    writing array of objects to a file

    The first class
    import java.io.*;
    import java.io.IOExcep tion;
    import java.util.Scann er;
    import javax.swing.*;

    import java.util.Forma tter;
    public class Project implements Serializable

    {

    TestProject s=new TestProject();
    Scanner sc=new Scanner(System. in);
    private int accNo;
    private String company;
    private int type;
    private int quantity;
    private double price;
    private int newQuantity;
    private double newPrice;
    static int x=0;
    private int eq;
    private Formatter output;




    public Project()
    {
    // save(TestProjec t[] accessory);
    }


    public static void save(TestProjec t accessory [])
    {
    try {

    ObjectOutputStr eam objOut = new ObjectOutputStr eam(new FileOutputStrea m("student.txt" ));

    objOut.writeObj ect(accessory);
    objOut.close();
    }
    catch(Exception e)
    {
    System.out.prin tln("E");
    }
    }

    public Project(int update)
    {
    getaccNo();

    }
    public void setaccNo(int acc_No)
    {
    accNo=acc_No;
    }


    public void setCompany(Stri ng company1)
    {
    company=company 1;
    }


    public void setType(int type1)
    {
    type=type1;
    }


    public void setQuantity(int quantity1)
    {
    quantity=quanti ty1;
    }


    public void setPrice(double price1)
    {
    price=price1;
    }


    public int getaccNo()
    {
    return accNo;
    }
    public int getEq()
    {
    return eq;
    }


    public void startj()
    {
    System.out.prin tln("** Computer Accessories Store **\n"+"How Much Equipments do you have");
    int eq=sc.nextInt() ;
    }

    public String getCompany()
    {
    return company;
    }


    public int getQuantity()
    {
    return quantity;
    }


    public double getPrice()
    {
    return price;
    }


    public int getType()
    {
    return type;
    }


    String show()
    {


    return(" "+accNo+" "+" "+company+" "+type+" "+quantity+
    " "+" "+price);
    }
    public void add()
    {
    Scanner sc=new Scanner(System. in);
    System.out.prin tln("What is the Serial ID: ");
    accNo=sc.nextIn t();
    System.out.prin tln("What is the Company name: ");
    company=sc.next ();
    System.out.prin tln("What is the type of the equipment:\n1-Mouse\n2-KeyBoard\n3-Monitor\n4-Printer ");
    type=sc.nextInt ();
    while((type>4)| |(type<1))
    {
    System.out.prin tln("Sorry, there's no such a type");
    System.out.prin tln("What is the type of the equipment:\n1-Mouse\n2-KeyBoard\n3-Monitor\n4-Printer ");
    type=sc.nextInt ();
    }
    System.out.prin tln("What is the quantity of the equipment: ");
    quantity=sc.nex tInt();
    System.out.prin tln("How much it costs for each equipment: ");
    price=sc.nextDo uble();
    }
    }




    The second class

    import java.util.Scann er;
    import java.io.*;
    import java.io.File;
    import javax.swing.*;
    import java.io.ObjectO utputStream;
    import java.io.FileOut putStream;
    import java.io.Seriali zable;
    //import java.io.PrintWr iter;





    public class TestProjectSeri alizable
    {
    static int c=0;
    static int eq=0;
    static Project accessory[]=new Project[100];

    public static void main(String args[])
    {

    Scanner sc=new Scanner(System. in);
    int arraylength=acc essory.length;
    try{
    objOut = new ObjectOutputStr eam(new FileOutputStrea m("AA.txt"));


    }
    catch(Exception e)
    {
    System.out.prin tln("E");
    }
    int choose=0;
    System.out.prin tln("** Computer Accessories Store **\n");


    //arraylength-eq;
    do{
    System.out.prin tln("1-Add new Accessory\n2-Update Accessory(Quant ity,Price)\n3-Sell Accessory\n4-View Reports\n5-Exit");
    choose=sc.nextI nt();
    switch(choose)
    {

    case(1):
    {
    System.out.prin tln("How Much Equipments do you have");
    eq=sc.nextInt() ;

    //File file=new File("AAA.txt") ;
    //OutputStream w=new ObjectOutputStr eam(file);
    //OutputStream output = null;
    for(int i=0;i<eq;i++)
    {

    System.out.prin tln("Please Fill the information for the equipment number: "+(i+1));
    accessory[c]=new Project();
    accessory[c].add();

    try{

    objOut.writeObj ect(accessory);
    objOut.flush();
    objOut.close();
    }
    catch(Exception e)
    {
    System.out.prin tln(e);
    }
    c++;
    }
    }
    }
    }
    }

    There's an error says that TestProjectSeri alizable is not Serializable.
    please help :(
Working...