Added DAO implementation, modified readme, and added UML diagram
This commit is contained in:
47
dao/src/main/java/com/iluwatar/App.java
Normal file
47
dao/src/main/java/com/iluwatar/App.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without directly
|
||||
* interacting with the data directly. The below example demonstrates basic operations: select, add, update, and delete.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
CustomerDaoImpl customerDao = new CustomerDaoImpl(generateSampleCustomers());
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
System.out.println("customerDao.getCusterById(2): " + customerDao.getCusterById(2));
|
||||
|
||||
Customer customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.addCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.updateCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
|
||||
customerDao.deleteCustomer(customer);
|
||||
|
||||
System.out.println("customerDao.getAllCustomers(): " + customerDao.getAllCustomers());
|
||||
}
|
||||
|
||||
public static List<Customer> generateSampleCustomers() {
|
||||
Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
|
||||
List<Customer> customers = new ArrayList<Customer>();
|
||||
customers.add(customer1);
|
||||
customers.add(customer2);
|
||||
customers.add(customer3);
|
||||
return customers;
|
||||
}
|
||||
}
|
64
dao/src/main/java/com/iluwatar/Customer.java
Normal file
64
dao/src/main/java/com/iluwatar/Customer.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class Customer {
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Customer(int id, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Customer customer = (Customer) o;
|
||||
|
||||
if (id != customer.id) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id;
|
||||
return result;
|
||||
}
|
||||
}
|
11
dao/src/main/java/com/iluwatar/CustomerDao.java
Normal file
11
dao/src/main/java/com/iluwatar/CustomerDao.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CustomerDao {
|
||||
public List<Customer> getAllCustomers();
|
||||
public Customer getCusterById(int id);
|
||||
public void addCustomer(Customer customer);
|
||||
public void updateCustomer(Customer customer);
|
||||
public void deleteCustomer(Customer customer);
|
||||
}
|
55
dao/src/main/java/com/iluwatar/CustomerDaoImpl.java
Normal file
55
dao/src/main/java/com/iluwatar/CustomerDaoImpl.java
Normal file
@ -0,0 +1,55 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism.
|
||||
* By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.
|
||||
* This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of
|
||||
* domain-specific objects and data types (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
|
||||
* database schema, etc.
|
||||
*/
|
||||
public class CustomerDaoImpl implements CustomerDao {
|
||||
|
||||
// Represents the DB structure for our example so we don't have to managed it ourselves
|
||||
// Note: Normally this would be in the form of an actual database and not part of the Dao Impl.
|
||||
private List<Customer> customers;
|
||||
|
||||
public CustomerDaoImpl(List<Customer> customers) {
|
||||
this.customers = customers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> getAllCustomers() {
|
||||
return customers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getCusterById(int id) {
|
||||
for (int i = 0; i < customers.size(); i++) {
|
||||
if (customers.get(i).getId() == id) {
|
||||
return customers.get(i);
|
||||
}
|
||||
}
|
||||
// No customer found
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCustomer(Customer customer) {
|
||||
customers.add(customer);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateCustomer(Customer customer) {
|
||||
if (customers.contains(customer)) {
|
||||
customers.set(customers.indexOf(customer), customer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteCustomer(Customer customer) {
|
||||
customers.remove(customer);
|
||||
}
|
||||
}
|
12
dao/src/test/java/com/iluwatar/AppTest.java
Normal file
12
dao/src/test/java/com/iluwatar/AppTest.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user