简体   繁体   中英

Arraylist java program

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");
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM