Link to home
Start Free TrialLog in
Avatar of java63
java63

asked on

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){
         }
    }
 }
             
         
 
         


     
   
                           
                       
                     
               
           
             


Avatar of Mick Barry
Mick Barry
Flag of Australia image

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.
Avatar of java63
java63

ASKER

Would you be albe to make some amendment to existing code? This is only the part I am using ArrayList in.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of java63

ASKER

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();
            }    
            
            
      }
 }
almost, just get rid of:

     ArrayList pending = new ArrayList();
>> 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.
Avatar of java63

ASKER

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();
            }    
            
            
      }
 }
                
Avatar of java63

ASKER


<<almost, just get rid of: >>

     ArrayList pending = new ArrayList();

You cannot get rid of it from within the method as pending cannot be understand
pending should be declared as a member variable of the class. Have you done that?
Avatar of java63

ASKER

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();
            }    
            
            
      }
 }
                
          
 
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
Avatar of java63

ASKER

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.
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.
Avatar of java63

ASKER

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
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?