0

I am working on my beginners project for learning java. The project is to a add, list and delete person. like

type 1 to add a person type 2 to list all person type 3 to delete a person.

I am using ArrayList collections to work on this project.

The first question is is using the collections is a good aproach? if yes i am declaring the ArrayList globally but while iterating it I can see it's not storing any data.

my code is as follows.

import java.util.*;  

public class OperationClass implements OperationInterface {  
    String id;  
    String fname;  
    String lname;  
    ArrayList<String> Person=new ArrayList<String>();  

    public static void main(String args[]) {  
        new OperationClass().operation();  
    }  

    public static void operation() {  
        int a;  
        Scanner in = new Scanner(System.in);  
        System.out.println("Enter 1 for Adding new Record");  
        System.out.println("Enter 2 to see List of Records");  
        System.out.println("Enter 3 to Delete a record");  
        System.out.println("Enter 4 to Exit!!");  

        a = in.nextInt();  

        if (a == 1) {  
            new OperationClass().addPerson();  
        } if (a == 2) {  
            new OperationClass().listPerson();  
        } if (a == 3) {  
            new OperationClass().deletePerson();  
        } if (a == 4) {  
            System.out.println("Thanks for using the program");  
        }  
    }  

    /** 
     * adds a new Person and stores in the collection 
     */  
    @Override  
    public void addPerson() {  


        Scanner in = new Scanner(System.in);  
new OperationClass().operation();  
        System.out.println("Enter a the id");  
        id = in.next();  

        System.out.println("Enter a the First Name");  
        fname = in.next();  

        System.out.println("Enter a the Last Name");  
        lname = in.next();  


        Person.add(id);  
        Person.add(fname);  
        Person.add(lname);  



        new OperationClass().operation();  
    }  

    @Override  
    public void listPerson() {  
        Iterator itr=Person.iterator();  
        while(itr.hasNext()){  
            System.out.println(itr.next());  
        }  
     new OperationClass().operation();  

    }  

    @Override  
    public void deletePerson() {  
        System.out.println("Delete under construction");  
    }  

    @Override  
    public void quit() {  

    }  
}  

first output is

Enter 1 for Adding new Record.

Enter 2 to see List of Records.

Enter 3 to Delete a record .

Enter 4 to Exit!!

while entering 1 its coming like this

Enter 1 for Adding new Record

Enter 2 to see List of Records

Enter 3 to Delete a record

Enter 4 to Exit!!

1

Enter a the id

1

Enter a the First Name

Indranil

Enter a the Last Name

Sinha

Then after hitting enter again the first four options are coming and after going to 2 its not showing anything.

Enter 1 for Adding new Record

Enter 2 to see List of Records

Enter 3 to Delete a record

Enter 4 to Exit!!

1

Enter a the id

1

Enter a the First Name

asdasd

Enter a the Last Name

dsffdgdfg

Enter 1 for Adding new Record

Enter 2 to see List of Records

Enter 3 to Delete a record

Enter 4 to Exit!!

2

Enter 1 for Adding new Record

Enter 2 to see List of Records

Enter 3 to Delete a record

Enter 4 to Exit!!

4 Answers 4

3

You are creating a new OperationClass object every time you enter a number:

if (a == 1) {  
    new OperationClass().addPerson(); //Creating a new object
} if (a == 2) {  
    new OperationClass().listPerson(); //Creating a new object
} if (a == 3) {  
    new OperationClass().deletePerson(); //Creating a new object
} if (a == 4) {  
    System.out.println("Thanks for using the program");  
}  

Try calling the methods on the object you already have:

if (a == 1) {  
    addPerson();  
} if (a == 2) {  
    listPerson();  
} if (a == 3) {  
    deletePerson();  
} if (a == 4) {  
    System.out.println("Thanks for using the program");  
}  

This will call the methods on the object you create in your main().

edit:

As pointed out, this will not quite work since your operation() method is static. I would suggest to either make this method non-static (and therefore an instance method):

public void operation() { 
    //Your operation() code
}

or move operation()s code to a constructor of OperationClass alltogether so that it's code is ran right when you create an object of that class:

public OperationClass() {
    //Your operation() code
}
Sign up to request clarification or add additional context in comments.

2 Comments

public static void operation() The operation() method is declared to be static therefore, you won't be able to call the instance methods from there directly like you say, addPerson(). OP will need an object to do so.
or the alternate will be to make the operation() method to be instance method. i.e. public void operation()
2

You have declared your ArrayList as an instance variable.

In simplest words it means for a new object a new ArrayList will be cretaed.

Now while calling methods you are doing, new OperationClass().addPerson();

In all these statements new OperationClass(); creates a new object and hence creates a new ArrayList.

Therefore in the last you don't get anything in one list.

I would suggest the following:

  1. As your public static void operation() method is static, so you don't need to create an object to call it. You can call it directly by class name.

Therefore new OperationClass().operation(); can be replaced by OperationClass.operation();. However it won't solve your problem.

Solutions

  1. What you need to do is have one common object of OperationClass and store it in some reference variable then use this only one object to call your other methods i.e. Don't call new OperationClass() again and again.

  2. The alternate which can work with your same code will be declare your ArrayList to be static i.e. static ArrayList<String> Person=new ArrayList<String>();.

By doing this it will be common for all the objects and you will be adding data to a single common ArrayList.

1 Comment

Thanks a lot....for this wonderful answer. It not only solved my problem but taught me new lessons.
2

You have new OperationClass().operation()/add/list...; almost everywhere, you should know that, when you new ... a new instance was created, thus, with an empty ArrayList<String> Person List.

You may want to use the same instance, instead of creating a new one every time.

Comments

2

here is working and modified code for you:

import java.util.*;  

public class OperationClass implements OperationInterface {  
    String id;  
    String fname;  
    String lname;  
    static ArrayList<String> Person=new ArrayList<String>();  

    public static void main(String args[]) {  
        new OperationClass().operation();  
    }  

    public static void operation() {  
        int a;  
        Scanner in = new Scanner(System.in);  
        System.out.println("Enter 1 for Adding new Record");  
        System.out.println("Enter 2 to see List of Records");  
        System.out.println("Enter 3 to Delete a record");  
        System.out.println("Enter 4 to Exit!!");  

        a = in.nextInt();  

        if (a == 1) {  
            new OperationClass().addPerson();  
        } if (a == 2) {  
            new OperationClass().listPerson();  
        } if (a == 3) {  
            new OperationClass().deletePerson();  
        } if (a == 4) {  
            System.out.println("Thanks for using the program");  
        }  
    }  

    /** 
     * adds a new Person and stores in the collection 
     */  
    @Override  
    public void addPerson() {  


        Scanner in = new Scanner(System.in);   
        System.out.println("Enter a the id");  
        id = in.next();  

        System.out.println("Enter a the First Name");  
        fname = in.next();  

        System.out.println("Enter a the Last Name");  
        lname = in.next();  


        Person.add(id);  
        Person.add(fname);  
        Person.add(lname);  



        new OperationClass().operation();  
    }  

    @Override  
    public void listPerson() {  
        Iterator itr=Person.iterator();  
        while(itr.hasNext()){  
            System.out.println(itr.next());  
        }  
     new OperationClass().operation();  

    }  

    @Override  
    public void deletePerson() {  
        System.out.println("Delete under construction");  
    }  

    @Override  
    public void quit() {  

    }  
}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.