0

Help in writing a program using the array list which stores the values of name, address, phone number, date and time (for each customer) and later I need to retrieve the specific information like all the customer's name on a specified date. any help is appreciated.

Code:

public class Details {
    public static void main(String args[]) throws IOException {
        InputStreamReader rdr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(rdr);
        String s;
        s = br.readLine();
        System.out.println("PLEASE ENTER CLIENT NAME");
        String name = br.readLine();
        System.out.println("PLEASE ENTER CLIENT ADDRESS");
        String add = br.readLine();
        System.out.println("PLEASE ENTER CLIENT CONTACT PHONE NUMBER");
        String pnum = br.readLine();
        List list = new ArrayList();
        list.add("name");
        list.add("add");
        list.add("pnum");
        list.add("food");
    }
}
5
  • 1
    StackOverflow is not a website to generate code for you. Show us what you got, ask specific questions about the things you do not understand (after looking up the question on Google/Stackoverflow). Commented Jun 6, 2015 at 6:23
  • public class Details { public static void main(String args[]) throws IOException { InputStreamReader rdr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(rdr); String s; s = br.readLine(); System.out.println("PLEASE ENTER CLIENT NAME"); String name= br.readLine(); System.out.println("PLEASE ENTER CLIENT ADDRESS"); String add= br.readLine(); System.out.println("PLEASE ENTER CLIENT CONTACT PHONE NUMBER"); String pnum= br.readLine(); List list = new ArrayList(); list.add("name"); list.add("add"); list.add("pnum"); list.add("food"); } } Commented Jun 6, 2015 at 6:26
  • 1
    Update your question with the code, indented 4 spaces. Difficult to read in a comment. Commented Jun 6, 2015 at 6:27
  • i accepted the data dynamically from user and added to array list but can i add all the details in the same index like the index 0 contains all info of a particular customer Commented Jun 6, 2015 at 6:28
  • after creating a array list --> List list = new ArrayList() i added the details of name, address pnum and food for only one customer like list.add ("name") is there a way i can add name, address, pnum, food in the index zero of arraylist but not in different indexes Commented Jun 6, 2015 at 6:33

1 Answer 1

3

Create a class Customer like this -

public class Customer{
   private String name; 
   private String address;
   private String phoneNumber;
   private Date date;

  public Customer(name, address, phoneNumber, date){

     this.name = name;
     this.address = address;
     this.phoneNumber = phoneNumber;
     this.date = date;
  }

   //getters and setters method

}

After that you create an ArrayList of Customer like this -

List<Customer> `customerList` = new ArrayList<Customer>();  

Now create an object/instance of Customer like this -

Customer aCustomer = new Customer("ranjit", "someAddress", "023-859 74", new Date() );

Then add the Customer object/instance aCustomer to ArrayList of Customer - customerList like this:

customerList.add(aCustomer);

In the given way you can more easily handle a Customer. Now you have a single entity containing all the customer attributes (name, address, phoneNumber etc). So you don't need store all the attributes/property in separate ArrayList

Sign up to request clarification or add additional context in comments.

1 Comment

System.out.println("Enter the appointment ID to see the full details :"); int y=in.nextInt(); int r; for(r=0;r<count;r++) { if(all.get(r).getID()==y) { all.get(r).display(); } }i am using this code to retrieve the full details that have been entered using the get statement and display function. this is a small part of my program. i was wondering is there any other way to do it

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.