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");
}
}
-
1StackOverflow 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).Turing85– Turing852015年06月06日 06:23:12 +00:00Commented 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"); } }ranjit kumar– ranjit kumar2015年06月06日 06:26:01 +00:00Commented Jun 6, 2015 at 6:26
-
1Update your question with the code, indented 4 spaces. Difficult to read in a comment.Roger Gustavsson– Roger Gustavsson2015年06月06日 06:27:37 +00:00Commented 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 customerranjit kumar– ranjit kumar2015年06月06日 06:28:25 +00:00Commented 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 indexesranjit kumar– ranjit kumar2015年06月06日 06:33:36 +00:00Commented Jun 6, 2015 at 6:33
1 Answer 1
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