|
1 | | -package com.iluwatar.dao; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
5 | | - |
6 | | -/** |
7 | | - * |
8 | | - * With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly |
9 | | - * interacting with the data. The below example demonstrates basic operations(CRUD): select, add, update, and delete. |
10 | | - */ |
11 | | -public class App { |
12 | | - |
13 | | - public static void main(String[] args) { |
14 | | - |
15 | | - CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers()); |
16 | | - |
17 | | - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
18 | | - System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2)); |
19 | | - |
20 | | - Customer customer = new Customer(4, "Dan", "Danson"); |
21 | | - customerDao.addCustomer(customer); |
22 | | - |
23 | | - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
24 | | - |
25 | | - customer.setFirstName("Daniel"); |
26 | | - customer.setLastName("Danielson"); |
27 | | - customerDao.updateCustomer(customer); |
28 | | - |
29 | | - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
30 | | - |
31 | | - customerDao.deleteCustomer(customer); |
32 | | - |
33 | | - System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
34 | | - } |
35 | | - |
36 | | - public static List<Customer> generateSampleCustomers() { |
37 | | - Customer customer1 = new Customer(1, "Adam", "Adamson"); |
38 | | - Customer customer2 = new Customer(2, "Bob", "Bobson"); |
39 | | - Customer customer3 = new Customer(3, "Carl", "Carlson"); |
40 | | - |
41 | | - List<Customer> customers = new ArrayList<Customer>(); |
42 | | - customers.add(customer1); |
43 | | - customers.add(customer2); |
44 | | - customers.add(customer3); |
45 | | - return customers; |
46 | | - } |
47 | | -} |
| 1 | +package com.iluwatar.dao; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * |
| 8 | + * With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly |
| 9 | + * interacting with the data. The below example demonstrates basic operations(CRUD): select, add, update, and delete. |
| 10 | + * |
| 11 | + */ |
| 12 | +public class App { |
| 13 | + |
| 14 | + /** |
| 15 | + * Program entry point |
| 16 | + * @param args command line args |
| 17 | + */ |
| 18 | + public static void main(String[] args) { |
| 19 | + |
| 20 | + CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers()); |
| 21 | + |
| 22 | + System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
| 23 | + System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2)); |
| 24 | + |
| 25 | + Customer customer = new Customer(4, "Dan", "Danson"); |
| 26 | + customerDao.addCustomer(customer); |
| 27 | + |
| 28 | + System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
| 29 | + |
| 30 | + customer.setFirstName("Daniel"); |
| 31 | + customer.setLastName("Danielson"); |
| 32 | + customerDao.updateCustomer(customer); |
| 33 | + |
| 34 | + System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
| 35 | + |
| 36 | + customerDao.deleteCustomer(customer); |
| 37 | + |
| 38 | + System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Generate customers |
| 43 | + * @return list of customers |
| 44 | + */ |
| 45 | + public static List<Customer> generateSampleCustomers() { |
| 46 | + Customer customer1 = new Customer(1, "Adam", "Adamson"); |
| 47 | + Customer customer2 = new Customer(2, "Bob", "Bobson"); |
| 48 | + Customer customer3 = new Customer(3, "Carl", "Carlson"); |
| 49 | + |
| 50 | + List<Customer> customers = new ArrayList<Customer>(); |
| 51 | + customers.add(customer1); |
| 52 | + customers.add(customer2); |
| 53 | + customers.add(customer3); |
| 54 | + return customers; |
| 55 | + } |
| 56 | +} |
0 commit comments