0

I'm writing a program in which it will asks the user to enter some entries like Name, Surname etc and then store all those entries in one location of an arraylist. In other words variables ( name, surname, telephone ) will be added to location 0 of arraylist.

The difficulty I'm finding is when it comes to get all those variables and store them into the location of the array list. Can someone help me ? Thanks.

public class Loans
{
    ArrayList lns = new ArrayList();

    void LoansInput ()
    {
           // Add New Loan Button  

           // Requesting DVD ID
                    String dvdid;
                    dvdid =JOptionPane.showInputDialog("Enter DVD ID Number");

            // Requesting Member Name & Surname
                    String namesurname;
                    namesurname = JOptionPane.showInputDialog("Client Name & Surname");

            // Requesting Member ID Number
                    String mid;
                    mid =JOptionPane.showInputDialog("Client ID Number");

            // Requesting Member ID Number
                    String telephone;
                    telephone =JOptionPane.showInputDialog("Client Telephone Number");

            // Requesting Rental Date
                    String rentaldate;
                    rentaldate =JOptionPane.showInputDialog("Rental Date (DD-MM-YY)");                                    

            // Requesting Return Date
                    String returndate;
                    returndate =JOptionPane.showInputDialog("Return Date (DD-MM-YY)");

What should I add here in order to get all this variables and put them in the Arraylist lns in one location? (Not every entry in different locations)

5 Answers 5

1

A good approach would be to create a User object, e.g., (getters and setters left out)

public class User {
   private Stringg surname;
   private String memberId;
   private String telephone;
   // etc. 
}

Then you can store a list of User objects instead

List<User> users = new LinkedList<User>();

You create your user object and populate it from the input you get, e.g.,

User user = new User();
String surname = JOptionPane.showInputDialog("...");
user.setSurname(surname);

users.add(user);
Sign up to request clarification or add additional context in comments.

Comments

0

First create DVD object as below...

class DVD {

    private int dvdId;
    .
    .
    .
    .

    //now set getter setters
    public void setdvdId(int dvdId) {
    this.dvdId = dvdId
    }

    public int getdvdId() {
    return dvdId;
    }

    // similarly for all field
}

Then use DVD object in arraylist as below

class MyProgram {
    public static void main (String args[]) {


    List<DVD> arrayList = new ArrayList<DVD>();

    DVD myDVD = new DVD();
    myDVD.setdvdId(Integer.parseInt(JOPtionPane.showInputDialog("Enter ID")));
    // similarly add other
    .
    .
    .
    .
    .

    arrayList.add(myDVD)

    // similarly add other
    }
}

Good Luck!!!

6 Comments

public void setdvdId(int dvdId) { this.dvdId = dvdId; } it tells Incompatible types
see my updated answer... note you will also need to add try.. catch now as if you don't enter number then that statement will throw exception...
It still says incompatible types in DVD Class Line 10 this.dvdId = dvdId
goshhh... because mistakenly I had dvdId as string.. see now my updated answer... it should work
How should I print the arraylist then ? Because when I try the output is [DVD@ca64df]
|
0

Why don't you create a class Dvd containing all the fields that you need and store this in the ArrayList?

1 Comment

You mean I create a class DVD and ask the user to input the data in there ? But then how will I put them in the array list ?
0

You shoul definitely create class for these variables, let's say DVD, then you can use it in your code like

List<DVD> arrayList = new ArrayList<DVD>(); //always use a raw types (List)
DVD dvd = new DVD();
dvd.setId(JOPtionPane.showInputDialog("Enter ID");
//and so on, when you finish, just put the object in the array
arrayList.add(dvd);

And your DVD object should override the hashCode and equals method, so you can efectivelly search DVD objects in your ArrayList

Comments

0

Crate a class with all the variables you need and crate objects for each your and put them into the ArrayList. I think it solves your problem.

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.