|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | + |
| 4 | +import com.ibm.broker.plugin.MbElement; |
| 5 | +import com.ibm.broker.plugin.MbException; |
| 6 | +import com.ibm.broker.plugin.MbJSON; |
| 7 | +import com.ibm.broker.plugin.MbMessage; |
| 8 | + |
| 9 | +public abstract class CustomerDatabase { |
| 10 | + |
| 11 | + private static List<Customer> customers = new ArrayList<>(); |
| 12 | + |
| 13 | + static { |
| 14 | + customers.add(new Customer("Denis", "Darner", "1 The Street, The Town")); |
| 15 | + customers.add(new Customer("Elana", "Ericson", "2 The Street, The Town")); |
| 16 | + customers.add(new Customer("Lyle", "Longino", "3 The Street, The Town")); |
| 17 | + customers.add(new Customer("Gerri", "Gaertner", "4 The Street, The Town")); |
| 18 | + customers.add(new Customer("Willis", "Wicks", "5 The Street, The Town")); |
| 19 | + customers.add(new Customer("Jessika", "Jeon", "6 The Street, The Town")); |
| 20 | + customers.add(new Customer("Neil", "Newton", "7 The Street, The Town")); |
| 21 | + customers.add(new Customer("Ferne", "Foye", "8 The Street, The Town")); |
| 22 | + } |
| 23 | + |
| 24 | + public static void getAllCustomers(Long max, MbElement[] output) { |
| 25 | + try { |
| 26 | + MbElement root = output[0]; |
| 27 | + long counter = 0; |
| 28 | + for (Customer customer : customers) { |
| 29 | + if (max > -1 && counter >= max) { |
| 30 | + break; |
| 31 | + } |
| 32 | + MbElement item = root.createElementAsLastChild(MbJSON.OBJECT, MbJSON.ARRAY_ITEM_NAME, null); |
| 33 | + item.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "id", customer.getID()); |
| 34 | + item.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "firstname", customer.getFirstName()); |
| 35 | + item.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "lastname", customer.getLastName()); |
| 36 | + item.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "address", customer.getAddress()); |
| 37 | + counter++; |
| 38 | + } |
| 39 | + } catch (MbException e) { |
| 40 | + throw new RuntimeException(e); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static MbElement getCustomer(int id) throws MbException { |
| 45 | + for (Customer customer : customers) { |
| 46 | + if (customer.getID() == id) { |
| 47 | + MbMessage message = new MbMessage(); |
| 48 | + MbElement result = message.getRootElement().createElementAsLastChild(MbJSON.OBJECT, MbJSON.DATA_ELEMENT_NAME, null); |
| 49 | + result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "id", customer.getID()); |
| 50 | + result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "firstname", customer.getFirstName()); |
| 51 | + result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "lastname", customer.getLastName()); |
| 52 | + result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "address", customer.getAddress()); |
| 53 | + return result; |
| 54 | + } |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + public static boolean deleteCustomer(int id) { |
| 60 | + for (Customer customer : customers) { |
| 61 | + if (customer.getID() == id) { |
| 62 | + customers.remove(customer); |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + public static boolean customerExists(int id) { |
| 70 | + for (Customer customer : customers) { |
| 71 | + if (customer.getID() == id) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + public static MbElement addCustomer(String firstname, String lastname, String address) throws MbException { |
| 79 | + Customer customer = new Customer(firstname, lastname, address); |
| 80 | + customers.add(customer); |
| 81 | + MbMessage message = new MbMessage(); |
| 82 | + MbElement result = message.getRootElement().createElementAsLastChild(MbJSON.OBJECT, MbJSON.DATA_ELEMENT_NAME, null); |
| 83 | + result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "message", "A new customer with ID '" + customer.getID() + "' was successfully added to the database."); |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + public static MbElement updateCustomer(int id, String firstname, String lastname, String address) throws MbException { |
| 88 | + for (Customer customer : customers) { |
| 89 | + if (customer.getID() == id) { |
| 90 | + customer.setFirstName(firstname); |
| 91 | + customer.setLastName(lastname); |
| 92 | + customer.setAddress(address); |
| 93 | + MbMessage message = new MbMessage(); |
| 94 | + MbElement result = message.getRootElement().createElementAsLastChild(MbJSON.OBJECT, MbJSON.DATA_ELEMENT_NAME, null); |
| 95 | + result.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "message", "An existing customer with ID '" + customer.getID() + "' was successfully updated in the database."); |
| 96 | + return result; |
| 97 | + } |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments