I am dealing with ArrayLists and want to check my code

I want to ask you one more question if you want I can set a new one. Anyway whatever you suggest.
I am going to print some coding which contain method run, within the server part. By the way the error with regard to socket I fixed it, it was basically one unnecessary read method that caused the problem. THe question I want you to ask is this the following code does some logic. Has this logic being implemented in my design looking at the code? After I run this code and check logs, it seems to be fine. So the Received Log does not contain the request being save but Processed Log does. How would I deal with more objects being stored in list?

1. Save request to file called Received Log as To_be_Processed request. It means all request that were not executed yet, because of crash or other things.
2. Write that request to list of ArrayList class and then write that list to Received Log. This means I can have many objects stored within the file.
3. Get the object to be processed and then process it
4. Write that request to another log called Processed Log it means all the request that has being exectued.
5. Overwrite Received Log minus Processed request

THIS IS MY CODE:

public void run(){
         
         try {
           
            AccountInfo user1 = (AccountInfo) ois.readObject();
            int amount = user1.getAmount(100);
            String message = user1.setMessage("1ZA");
           
           
           //String message = null;
               // ship the object to the client
            AccountInfo user = new AccountInfo(100, message);
            oos.writeObject(user);
            oos.flush();
            // close connections
            //ois.close();
            oos.close();
            client.close();
        }catch(Exception e) {}      
     

         
         try{
             
             
             
             
              AccountInfo user1 = new AccountInfo ();
             
              //save request to file called ReceivedLog
              ObjectOutputStream oos = new ObjectOutputStream(new
               FileOutputStream("C:\\Logs\\ReceivedServerLog\\To_Be_Processed.txt"));          
             ArrayList list = new ArrayList();
             list.add(user1);
             oos.writeObject(list);                              
             oos.flush();
             oos.close();
                       
                       
            ObjectInputStream ois = new ObjectInputStream(new
               FileInputStream("C:\\Logs\\ReceivedServerLog\\To_Be_Processed.txt"));
            list = ( ArrayList ) ois.readObject () ;
            ois.close () ;
            AccountInfo info = ( AccountInfo ) list.get ( 0 ) ; // get the  object to be processed
            //info.printInfo(); // process it
           
           
            oos = new ObjectOutputStream(new
              FileOutputStream("C:\\Logs\\ProcessedServerLog\\Already_Processed.txt"));
            ArrayList list1 = new ArrayList();
            list1.add ( info ) ; // add the processed request to ProcessedLog
            oos.writeObject ( list1 ) ;
            oos.flush () ;
            oos.close () ;
                       
            oos = new ObjectOutputStream(new
               FileOutputStream("C:\\Logs\\ReceivedServerLog\\To_Be_Processed.txt"));
            list.remove(0);//remove request
            oos.writeObject(list); // overite the receivedLog - minus the processed request
            oos.flush();
            oos.close();
                                                                                         
                       
                       
           
         }
         catch(IOException ex){
              ex.printStackTrace();
         }catch(ClassNotFoundException e){
         }
    }
 }
             
         
 
         


     
   
                           
                       
                     
               
           
             


java63Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
The list(s) should probably me a member variable which you update as objects are recieved and processed.
So instead of reading it from file you would update the member variable and save the list to relevant file.
0
java63Author Commented:
Would you be albe to make some amendment to existing code? This is only the part I am using ArrayList in.
0
objectsCommented:
private List pending = new ArrayList();
private List processed = new ArrayList();

public void run(){
         
         try {
           
            AccountInfo user1 = (AccountInfo) ois.readObject();

            // add recieved object to pending list
            pending.add(user1);
            savePending();    // this will save pending list to file

            int amount = user1.getAmount(100);
            String message = user1.setMessage("1ZA");
           
           
           //String message = null;
               // ship the object to the client
            AccountInfo user = new AccountInfo(100, message);
            oos.writeObject(user);
            oos.flush();
            // close connections
            //ois.close();
            oos.close();
            client.close();

            // remove from pending, and add to processed

            pending.remove(user1);
            savePending();    // this will save pending list to file

            processed.add(user);
            saveProcessed();    // this will save processed list to file

        }catch(Exception e) {}      
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

java63Author Commented:
I like the idea, what about two methods savePending and saveProcessed. Should I place them within the run method or outside. Is this what should be in the save method.

private void savePending() {
            
            try {
                  
                  oos = new ObjectOutputStream(new
                             FileOutputStream("C:\\Logs\\ReceivedServerLog\\To_Be_Processed.txt"));
                  ArrayList pending = new ArrayList();
                                                oos.writeObject(pending);                              
                                                oos.flush();
                                                oos.close();
            } catch (FileNotFoundException e) {
                  
                  e.printStackTrace();
            } catch (IOException e) {
                  
                  e.printStackTrace();
            }    
            
            
      }
 }
0
objectsCommented:
almost, just get rid of:

     ArrayList pending = new ArrayList();
0
Mayank SAssociate Director - Product EngineeringCommented:
>> Should I place them within the run method or outside

Outside, as you cannot place a method inside a method.

Instead of 2 methods, you can also write one method called save ( ArrayList list, String filePath ) and use the same method for saving both.
0
java63Author Commented:
I did get rid of this,  ArrayList pending = new ArrayList();   but now is complaining about pending instance, the same with the other mehtod. Anyway this is my code amended. Please have a look something is wrong




 public void run(){
          
             
             try {
               
                  
             AccountInfo user = (AccountInfo) ois.readObject();
                  
                  
      pending = null;
      pending.add(user);
                  savePending();    // this will save pending list to file
                  
                  int amount = user.getAmount(100);
                String message = user.setMessage("1ZA");
               
//              add recieved object to pending list
                pending.add(user);
                savePending();    // this will save pending list to file
               
               
               
                // ship the object to the client
                AccountInfo user1 = new AccountInfo(100, message);
                oos.writeObject(user1);
                oos.flush();
                // close connections
                ois.close();
                oos.close();
                client.close();

                // remove from pending, and add to processed

                pending.remove(user1);
                savePending();    // this will save pending list to file

                List processed = null;
                        processed.add(user1);
                saveProcessed();    // this will save processed list to file
               
             }catch(Exception e) {
                   }
             }
   

    private void saveProcessed() {
            
            try {
                  oos = new ObjectOutputStream(new
                              FileOutputStream("C:\\Logs\\ProcessedServerLog\\Already_Processed.txt"));
                  
            oos.writeObject(processed);                              
                                oos.flush();
                                oos.close();
            } catch (FileNotFoundException e) {
                  
                  e.printStackTrace();
            } catch (IOException e) {
                  
                  e.printStackTrace();
            }
            
            
      }

      private void savePending() {
            
            try {
                  
                  oos = new ObjectOutputStream(new
                             FileOutputStream("C:\\Logs\\ReceivedServerLog\\To_Be_Processed.txt"));
                  oos.writeObject(pending);                              
                                                oos.flush();
                                                oos.close();
            } catch (FileNotFoundException e) {
                  
                  e.printStackTrace();
            } catch (IOException e) {
                  
                  e.printStackTrace();
            }    
            
            
      }
 }
                
0
java63Author Commented:

<<almost, just get rid of: >>

     ArrayList pending = new ArrayList();

You cannot get rid of it from within the method as pending cannot be understand
0
Mayank SAssociate Director - Product EngineeringCommented:
pending should be declared as a member variable of the class. Have you done that?
0
java63Author Commented:
I think I got it right. For some reason it did not like two methods  saveProcessed();   and savePending(); I got rid of them and put just try and catch statement. this is my code;

private List pending = new ArrayList();
       private List processed = new ArrayList();
   
       public void run(){
          
             
             try {
               
                  
                   AccountInfo user1 = (AccountInfo) ois.readObject();

                 // add recieved object to pending list
                 pending.add(user1);
                 

                 int amount = user1.getAmount(100);
                 String message = user1.setMessage("1ZA");
               
                // ship the object to the client
                 AccountInfo user = new AccountInfo(100, message);
                 oos.writeObject(user);
                 oos.flush();
                 
                 // close connections
                 ois.close();
                 oos.close();
                 client.close();

                 // remove from pending, and add to processed

                 pending.remove(user1);
                 processed.add(user);
                     

               
             }catch(Exception e) {
                   }
             
   

   
            
            try {
                  oos = new ObjectOutputStream(new
                              FileOutputStream("C:\\Logs\\ProcessedServerLog\\Already_Processed.txt"));
                  
                oos.writeObject(processed);                              
            oos.flush();
            oos.close();
            } catch (FileNotFoundException ex) {
                  
                  ex.printStackTrace();
            } catch (IOException e) {
                  
                  e.printStackTrace();
            }
            
            
            try {
                  
                  oos = new ObjectOutputStream(new
                             FileOutputStream("C:\\Logs\\ReceivedServerLog\\To_Be_Processed.txt"));
                  
                  oos.writeObject(pending);                              
            oos.flush();
            oos.close();
            } catch (FileNotFoundException e) {
                  
                  e.printStackTrace();
            } catch (IOException ex) {
                  
                  ex.printStackTrace();
            }    
            
            
      }
 }
                
          
 
0
Mayank SAssociate Director - Product EngineeringCommented:
No, that is not right. You are not calling saveProcessed () and savePending () anywhere. Where did you define those 2 methods? You have to define them in the same class
0
java63Author Commented:
Why do I have to call save method? As soon as it reaches another try statement it passes pending which contain user1 object received from client. Then pending(List) is save to the file. so this way I do not have to read from the file as you suggested before, but as soon as I received the object user1 I return it to client and save it to file.
0
Mayank SAssociate Director - Product EngineeringCommented:
Oh sorry, I missed the last lines. Yes, if you write to the files in run () itself, there won't be any problems, though putting it in a method makes it more reusable.

>> so this way I do not have to read from the file as you suggested before

It might be better that the 2 array-lists are initially populated from the file (e.g., consider a case where the application crashes - in that case, all the values they have will be lost). So when you re-start the application, you can read the values from the file and again populate the array-lists as they were before crashing.
0
java63Author Commented:
I think we cannot implement save methods in this cirumstances as soon as I do this the request is not pass to the output and client cannot display the message . Why I have no idea, somehow this interfere? I can save the request in the file but is not passing this before it saves  to the client
0
Mayank SAssociate Director - Product EngineeringCommented:
I'm sorry I did not understand that properly. Can you tell what exactly is the problem? If its a new one, do you mind opening a new question for it since you closed this one already?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.