public static void main(String[] args) throws Exception { System.out.println("hello from Customer.java"); EntityManagerFactory factory = Persistence.createEntityManagerFactory("customerPU"); EntityManager manager = factory.createEntityManager(); Query q1 = manager.createQuery("SELECT COUNT(c) FROM Customer c"); Long count = (Long) q1.getSingleResult(); if (count == 0) { // record is empty, read from data.txst System.out.println("record empty, read from data.txt..."); // try { FileReader fr = new FileReader("data.txt"); BufferedReader br = new BufferedReader(fr); String s; while ((s = br.readLine()) != null) { // System.out.println(s); // split the string s Object[] items = s.split("\\|"); // store in string list // List<String> itemList= new ArrayList<String>(Arrays.asList(items)); // string list converted to array // Object[] itemArray = itemList.toArray(); // insert data into database table manager.getTransaction().begin(); Customer c = new Customer(); // add email c.setEmail((String) items[0]); // add pass c.setPass((String) items[1]); // add name c.setName((String) items[2]); // add address c.setAddress((String) items[3]); // add yob c.setYob((String) items[4]); // change to managed state manager.persist(c); manager.getTransaction().commit(); } fr.close(); } // display the records Query q2 = manager.createNamedQuery("Customer.findAll"); List<Customer> customers = q2.getResultList(); for (Customer c : customers) { System.out.println(c.getName() + ", " + c.getEmail()); } manager.close(); factory.close(); }
public void printAllAccounts(String customerID) { // Sorts the customer based on ID, then prints the accounts information // change the withdrawal funcitons and deposit functions too ajf.dispose(); ajf = new AdminRunningFrame(this); Collections.sort(cust, Customer.CompareIDs); String searchString = ajf.getCustomerID(); StringBuilder stringResults = new StringBuilder(); String header = header(); stringResults.append(header); for (Customer customer : cust) { String id = customer.returnID(); String name = customer.getName().toUpperCase(); String pin = customer.returnPin(); if (searchString.equals(id)) { ArrayList<Account> accounts = customer.getAccounts(); if (!accounts.isEmpty()) { for (Account account : accounts) { if (account.checkActive()) { String accountNumber = account.returnNumber(); double balance = account.returnBalance(); String balanceAsString = formatter.format(balance); String customerInfo = printAdminInfo(name, id, accountNumber, pin, balanceAsString); stringResults.append(customerInfo); } } } } } String resultsAsString = stringResults.toString(); ajf.printInfo(resultsAsString); }
/** * Lookup a Customer and return it or return a new one if not found. * * @param name the name of the customer to lookup * @return a new or existing Customer associated with this Company */ public Customer lookupOrCreateCustomer(String name) { Customer c = (Customer) lnkCustomer.get(name); if (c == null) { c = new Customer(name); lnkCustomer.put(c.getName(), c); } return c; }
public void printHighestBalance() { // prints the accounts from highest amount to lowest, with those who have empty accounts last ajf.dispose(); ajf = new AdminRunningFrame(this); StringBuilder stringResults = new StringBuilder(); String header = header(); stringResults.append(header); if (!cust.isEmpty()) { ArrayList<Account> allAccounts = new ArrayList<Account>(); ArrayList<Customer> CustomersNoAccounts = new ArrayList<Customer>(); for (Customer c : cust) { ArrayList<Account> accounts = c.getAccounts(); if (!accounts.isEmpty()) { // Concatenates all the accounts together for easy sorting allAccounts.addAll(accounts); } // Adds customers without accounts to a separate list to be printed at the end of all the // others else { CustomersNoAccounts.add(c); } } if (!allAccounts.isEmpty()) { Collections.sort(allAccounts, Account.CompareBalances); for (Account a : allAccounts) { if (a.checkActive()) { String id = a.getID(); String name = a.getName().toUpperCase(); String pin = a.getPin(); String accountNumber = a.returnNumber(); double balance = a.returnBalance(); String balanceAsString = formatter.format(balance); String customerInfo = printAdminInfo(name, id, accountNumber, pin, balanceAsString); stringResults.append(customerInfo); } } } if (!CustomersNoAccounts.isEmpty()) { Collections.sort(CustomersNoAccounts, Customer.CompareName); for (Customer c : CustomersNoAccounts) { String id = c.returnID(); String name = c.getName().toUpperCase(); String pin = c.returnPin(); String noAccounts = String.format("%-20s %-9s %-10s\n", name, id, pin); stringResults.append(noAccounts); } } String resultsAsString = stringResults.toString(); ajf.printInfo(resultsAsString); } }
public void printByCustomerName() { // Prints the database info by customer's name in ascending order Collections.sort(cust, Customer.CompareName); ajf.dispose(); ajf = new AdminRunningFrame(this); StringBuilder stringResults = new StringBuilder(); String header = header(); stringResults.append(header); if (!cust.isEmpty()) { for (Customer customer : cust) { String id = customer.returnID(); String name = customer.getName().toUpperCase(); String pin = customer.returnPin(); ArrayList<Account> accounts = customer.getAccounts(); if (!accounts.isEmpty()) { for (Account account : accounts) { if (account.checkActive()) { String accountNumber = account.returnNumber(); double balance = account.returnBalance(); String balanceAsString = formatter.format(balance); String customerInfo = printAdminInfo(name, id, accountNumber, pin, balanceAsString); stringResults.append(customerInfo); } } } else { // Still prints customers who have created customer accounts but not set up any // checking/savings String noAccounts = String.format("%-20s %-9s %-10s\n", name, id, pin); stringResults.append(noAccounts); } } } String resultsAsString = stringResults.toString(); ajf.printInfo(resultsAsString); }
@SuppressWarnings("unchecked") @Test public void testReadNextCustomer() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("readDomainsAndGuids") .createStrictMock(); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); List<ICustomer> customerList = new LinkedList<ICustomer>(); int custID = 34; int state = 3; String name = "myName"; String fromAddress = "myFromAddr"; String clientID = "myClientId"; String clientKey = "myClientKey"; String backendId = "myBackendId"; String templateId = "myTemplateId"; String externalID = "myExternalId"; int estMailboxCount = 34453; int estCloudMailboxCount = 345234; int estOnpremisesJournalMailboxCount = 3523; int estWelcomedCount = 938423; int estNotificationCount = 3423; int estAdminCount = 45345; Date countsUpdatedTime = new Date(); int estTotalContacts = 345234; int estTotalUsersWithContacts = 87432; int estTotalCalendarEntries = 343460; int estTotalUsersWithCalendar = 9892; int estTotalDLists = 8932; int estTotalDListMembers = 34982; int estTotalPDLs = 12342142; int estTotalPDLMembers = 8732324; int estNoWelcomeResponseCount = 734232; int estNoLoginCount = 90832; int estNoActiveLoginCount = 89923; int estIgnoredCount = 349823; String channel = "myChannel"; EasyMock.expect(resultSet.getInt(1)).andReturn(custID); EasyMock.expect(resultSet.getInt(2)).andReturn(state); EasyMock.expect(resultSet.getString(3)).andReturn(name); EasyMock.expect(resultSet.getString(4)).andReturn(fromAddress); EasyMock.expect(resultSet.getString(5)).andReturn(clientID); EasyMock.expect(resultSet.getString(6)).andReturn(clientKey); EasyMock.expect(resultSet.getString(7)).andReturn(backendId); EasyMock.expect(resultSet.getString(8)).andReturn(templateId); EasyMock.expect(resultSet.getString(9)).andReturn(externalID); EasyMock.expect(resultSet.getInt(14)).andReturn(estMailboxCount); EasyMock.expect(resultSet.getInt(15)).andReturn(estCloudMailboxCount); EasyMock.expect(resultSet.getInt(16)).andReturn(estOnpremisesJournalMailboxCount); EasyMock.expect(resultSet.getInt(17)).andReturn(estWelcomedCount); EasyMock.expect(resultSet.getInt(18)).andReturn(estNotificationCount); EasyMock.expect(resultSet.getInt(19)).andReturn(estAdminCount); EasyMock.expect(resultSet.getTimestamp(20)) .andReturn(new Timestamp(countsUpdatedTime.getTime())); EasyMock.expect(resultSet.getInt(21)).andReturn(estTotalContacts); EasyMock.expect(resultSet.getInt(22)).andReturn(estTotalUsersWithContacts); EasyMock.expect(resultSet.getInt(23)).andReturn(estTotalCalendarEntries); EasyMock.expect(resultSet.getInt(24)).andReturn(estTotalUsersWithCalendar); EasyMock.expect(resultSet.getInt(25)).andReturn(estTotalDLists); EasyMock.expect(resultSet.getInt(26)).andReturn(estTotalDListMembers); EasyMock.expect(resultSet.getInt(27)).andReturn(estTotalPDLs); EasyMock.expect(resultSet.getInt(28)).andReturn(estTotalPDLMembers); EasyMock.expect(resultSet.getInt(29)).andReturn(estNoWelcomeResponseCount); EasyMock.expect(resultSet.getInt(30)).andReturn(estNoLoginCount); EasyMock.expect(resultSet.getInt(31)).andReturn(estNoActiveLoginCount); EasyMock.expect(resultSet.getInt(32)).andReturn(estIgnoredCount); EasyMock.expect(resultSet.getString(33)).andReturn(channel); EasyMock.expect( dao.readDomainsAndGuids( EasyMock.eq(resultSet), EasyMock.eq(custID), EasyMock.isA(Set.class), EasyMock.isA(Set.class))) .andReturn(true); EasyMock.replay(dao, resultSet); assertTrue( "Should have found another customer.", dao.readNextCustomer(resultSet, customerList)); EasyMock.verify(dao, resultSet); assertEquals("Should have customer in list.", 1, customerList.size()); Customer cust = (Customer) customerList.get(0); assertEquals("Wrong customer id.", custID, cust.getCustID()); assertEquals("Wrong customer state.", state, cust.getState().toInt()); assertEquals("Wrong customer name.", name, cust.getName()); assertEquals("Wrong from address.", fromAddress, cust.getFromAddress()); assertEquals("Wrong backend id.", backendId, cust.getBackendHostname()); assertEquals("Wrong template id.", templateId, cust.getTemplateId()); assertEquals("Wrong channel.", channel, cust.getChannel()); assertEquals("Wrong client id.", clientID, cust.getClientID()); assertEquals("Wrong client key.", clientKey, cust.getClientKey()); assertEquals("Wrong channel.", channel, cust.getChannel()); assertEquals("Wrong est mailbox count.", estMailboxCount, cust.getEstMailboxCount()); assertEquals( "Wrong est cloud mailbox count.", estCloudMailboxCount, cust.getEstCloudMailboxCount()); assertEquals( "Wrong est on premises journal mailbox count.", estOnpremisesJournalMailboxCount, cust.getEstOnpremisesJournalMailboxCount()); assertEquals("Wrong est not welcomed count.", estWelcomedCount, cust.getEstNotWelcomedCount()); assertEquals( "Wrong est notification not set count.", estNotificationCount, cust.getEstNotificationNotSetCount()); assertEquals("Wrong est admin count.", estAdminCount, cust.getEstAdminCount()); assertEquals("Wrong counts updated time.", countsUpdatedTime, cust.getCountsUpdatedTime()); assertEquals("Wrong total contacts.", estTotalContacts, cust.getTotalContacts()); assertEquals( "Wrong total users with contacts.", estTotalUsersWithContacts, cust.getTotalUsersWithContacts()); assertEquals( "Wrong total calendar entries.", estTotalCalendarEntries, cust.getTotalCalendarEntries()); assertEquals( "Wrong total users with calendar entries.", estTotalUsersWithCalendar, cust.getTotalUsersWithCalendarEntries()); assertEquals("Wrong total dlists.", estTotalDLists, cust.getTotalDlists()); assertEquals("Wrong total dlist members.", estTotalDListMembers, cust.getTotalDlistMembers()); assertEquals("Wrong total pdls.", estTotalPDLs, cust.getTotalPDLs()); assertEquals("Wrong total pdl members.", estTotalPDLMembers, cust.getTotalPDLMembers()); assertEquals( "Wrong est no welcome reponse count.", estNoWelcomeResponseCount, cust.getEstNoWelcomeResponseCount()); assertEquals("Wrong est no login count.", estNoLoginCount, cust.getEstNoLoginCount()); assertEquals( "Wrong est no active login count.", estNoActiveLoginCount, cust.getEstNoActiveLoginCount()); assertEquals("Wrong est ignored count.", estIgnoredCount, cust.getEstIgnoredCount()); assertEquals("Wrong external id.", externalID, cust.getExternalID()); }