Okey, so I've got a feeling that explaining what I mean can be super difficult, however, I'm gonna try. So, basically what I want to do is to CREATE a next OBJECT and PLACE it in an ARRAYLIST using USER INPUT.
Is it possible to achieve it using count of the ArrayList? Assuming my constructor would allow to pass a name in order to create an object would I be able to ask a user for the name and later use it to do so and then pass it to already created ArrayList?
I feel like I can't explain what I mean properly, English isn't my first language, sorry. :)
public class Voter
{
private String name;
private ArrayList<Voter> voters;
public Voter(){
this.name = "";
voters = new ArrayList();
}
public Voter(String name)
{
this.name = name;
voters = new ArrayList<>();
}
public void add(Voter v)
{
if (!this.voters.contains(v))
{
this.voters.add(v);
}
}
}
public static void main(String[] args)
{
Voter v1 = new Voter("Admin");
Voter v = new Voter();
v.add(v1);
displayMenu();
int option = getInt("Enter Option:", 0, 3);
while (option != END)
{
if (option == 1)
{
doOption1();
int count = v.countArray(v);
}
}
}
public static void displayMenu()
{
System.out.println("\nSample Menu of Options");
System.out.println("0. Exit this menu");
System.out.println("1. Option 1");
}
public static void doOption1()
{
System.out.println("Please, enter the below details to register for
voting system.");
System.out.println("Full name");
String name = keyboard.nextLine();
//THIS IS WHERE I WANT TO CREATE A NEW OBJECT AND PUT IT AS AN ELEMENT OF
//MY Voter ARRAYLIST
}
public static int getInt(String prompt, int min, int max)
{
System.out.print(prompt);
int value = keyboard.nextInt();
while ((value < min) || (value > max))
{
System.out.println("Invalid - [" + min + "," + max + "] only");
System.out.print(prompt);
value = keyboard.nextInt();
}
keyboard.nextLine();
return value;
}