0

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;
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
G. Martyna
  • 35
  • 7
  • okay, so you want is allow a user to define the object properties by user input then add the constructed object into an ArrayList? – Ousmane D. Apr 06 '17 at 01:15
  • First, you'll need a **SCANNER** to read the **USER INPUT**, and then you'll need to **STORE** the **USER INPUT** in a **VARIABLE**, or add it to the **ARRAYLIST** directly. – Jacob G. Apr 06 '17 at 01:17
  • It's indeed unclear what you are asking. The best thing to do: show your current code and explain where you are stuck. Read "how to create an [mcve]" first. – Erwin Bolwidt Apr 06 '17 at 01:17
  • I want to create a completely new object using the user's input (in this case name) as an argument for the constructor and then add this newly created class instance to an ArrayList. – G. Martyna Apr 06 '17 at 01:17
  • @G.Martyna you must provide at least some code in order for people to help you. The code doesn't have to be working of course but at least provide what you've attempted. – Ousmane D. Apr 06 '17 at 01:21
  • @OusmaneMahyDiaw I added some of my code. – G. Martyna Apr 06 '17 at 01:34
  • @G.Martyna okay i will have a look. – Ousmane D. Apr 06 '17 at 01:35
  • @G.Martyna see my answer. – Ousmane D. Apr 06 '17 at 01:50

1 Answers1

0

With your current algorithm already in place, all you'd have to do in order to add items to the ArrayList of a Voter object is to first reconstruct the method definition of doOption1 to the method below.

public static void doOption1(Voter voter){
      System.out.println("Please, enter the below details to register for voting system.");
      System.out.println("Full name");
      String name = keyboard.nextLine();
      voter.add(new Voter(name)); // you could validate name before constructing the object if you see fit.
}

After you've done that, you'll need to change this code:

while (option != END){
    if (option == 1){
       doOption1();
       int count = v.countArray(v);
    }
}

to this:

while (option != END){
    if (option == 1){
       doOption1(v); // notice the argument
       int count = v.countArray(v);
    }
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Awesome! I understand where you're coming from. Now, if I had more fields in my Voter class, I just basically need to add more arguments to this line voter.getVoters().add(new Voter(name)); right? As I said it was just a tiny bit of my code, so I realise there's a lot of missing bits in it. – G. Martyna Apr 06 '17 at 01:58
  • @G.Martyna that's correct, if you have more parameters within you constructor definition, then you'd have to add more arguments. – Ousmane D. Apr 06 '17 at 02:10
  • Thank you very much! It indeed had fixed my issue! :) – G. Martyna Apr 06 '17 at 02:12
  • I've got one more question (I know it's been a while). What if I want to save this new voter and keep it in my array list for use some other time? For now it adds the voter to the array list, but whenever I close the application and restart it, the new voter I just created is gone. – G. Martyna Apr 25 '17 at 13:37
  • @G.Martyna you'll want to look into using databases to store the data. there is a really simple database system add-on from [mozilla](https://addons.mozilla.org/en-GB/firefox/addon/sqlite-manager/) which you can use, also you'll need to download the [JDBC driver](http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/) for that. Follow the links I have provided and read everything step by step then you should be able to store the data even if the program is closed. However, you'll need to learn [SQL](https://www.w3schools.com/sql/) in order to perform queries on the database. – Ousmane D. Apr 25 '17 at 13:44
  • you'll only need to know the simple [CRUD](https://www.w3schools.com/sql/sql_select.asp) methods and you'll be good to go. when you click the link you should look for the chapters `INSERT` , `SELECT` , `UPDATE` , `DELETE`. good luck ^^. – Ousmane D. Apr 25 '17 at 13:47
  • if you don't want to go into all of that database stuff then have a read at the following link: [save changes (permanently) in an arraylist?](http://stackoverflow.com/questions/22323959/save-changes-permanently-in-an-arraylist). – Ousmane D. Apr 25 '17 at 14:01
  • 1
    Thank you very much! I do have basic experience with SQL, but I think I'm gonna use the latter solution! Thank you so, so much again! :) – G. Martyna Apr 27 '17 at 10:40