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