Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

I am dealing with ArrayLists and want to check my code

Posted on 2006-04-19
15
Medium Priority
?
198 Views
Last Modified: 2012-05-05
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){
         }
    }
 }
             
         
 
         


     
   
                           
                       
                     
               
           
             


0
Comment
Question by:java63
  • 7
  • 5
  • 3
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16494600
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
 

Author Comment

by:java63
ID: 16494625
Would you be albe to make some amendment to existing code? This is only the part I am using ArrayList in.
0
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 16494683
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:java63
ID: 16496609
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
 
LVL 92

Expert Comment

by:objects
ID: 16496777
almost, just get rid of:

     ArrayList pending = new ArrayList();
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16496802
>> 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
 

Author Comment

by:java63
ID: 16497145
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
 

Author Comment

by:java63
ID: 16497285

<<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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16497370
pending should be declared as a member variable of the class. Have you done that?
0
 

Author Comment

by:java63
ID: 16497386
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16497439
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
 

Author Comment

by:java63
ID: 16497508
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16497533
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
 

Author Comment

by:java63
ID: 16497824
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16497925
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question