public static boolean insertCustomer(Customer c) { // Flag if record is inserted or not boolean isInserted = false; // Check if array is full, prompt and leave method if so if (numOfCustomers >= MAX_CUSTOMER) { System.out.println(c.getName() + " NOT ADDED! Maximum number of Customers present"); return false; } // validate if customer object is already in array // if findCustomer returns null, not in array if (!((findCustomer(c)) == null)) { System.out.println(c.getName() + " NOT ADDED! Duplicate record found in database"); return false; } // Insert into array for (int i = 0; i < numOfCustomers; i++) { // If next item customerArray[i] is larger, insert before current item // alternatively if at the end of the array already, insert at the end if (c.compareTo(customerArray[i]) < 0) { Customer swappedOutCustomer = customerArray[i]; customerArray[i] = c; isInserted = true; // Push all the rest 1 value back for (int j = i; j < numOfCustomers; j++) { Customer toInsert = swappedOutCustomer; swappedOutCustomer = customerArray[j + 1]; customerArray[j + 1] = toInsert; } // Inserted, so break out of loop break; } } // If not inserted by now, means c belongs at the end if (!isInserted) { customerArray[numOfCustomers] = c; } // Increment number of customers numOfCustomers++; return true; }
public void customerSorting() { for (int p = 0; p < this.customer.size(); p++) { Customer customer1 = this.customer.get(p); for (int i = 0; i < this.customer.size(); i++) { Customer customer2 = this.customer.get(i); int compare = customer1.compareTo(customer2); if (compare % 10 < 2) { int customer1Index = this.customer.indexOf(customer1); this.customer.remove(i); this.customer.add(i, customer1); this.customer.remove(customer1Index); this.customer.add(customer1Index, customer2); customer1 = customer2; } } } }