Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //method
- public static void insertCustomer(Customer[] customers, Customer customer) {
- Customer cus1 = null;
- Customer cus2 = null;
- for (int i = 0; i < customers.length - 1; i++) {
- if (customers[i] != null) {
- if (customers[i].compareTo(customer) < 0 && customers[i + 1].compareTo(customer) > 0) {
- for (int j = i + 1; j < customers.length - 1; j++) {
- cus1 = customers[j];
- cus2 = customers[j + 1];
- customers[j] = cus2;
- customers[j + 1] = cus1;
- }
- }
- }
- }
- }
- //mainapp
- public static void main(String[] args) {
- Customer[] customers = new Customer[10];
- Customer c1 = new Customer("Allan", "Birch", 32);
- Customer c2 = new Customer("fn1", "ln1", 35);
- Customer c3 = new Customer("fn2", "ln2", 29);
- Customer c4 = new Customer("fn3", "ln3", 30);
- Customer c5 = new Customer("fn4", "ln4", 31);
- Customer c6 = new Customer("fn5", "ln5", 34);
- Customer c7 = new Customer("fn6", "ln6", 33);
- customers[0] = c1;
- customers[1] = c2;
- customers[2] = c3;
- customers[3] = c4;
- customers[4] = c5;
- customers[5] = c6;
- customers[6] = c7;
- System.out.println();
- System.out.println("Print array here: " + Arrays.toString(customers));
- Algoritmer.insertCustomer(customers, c8);
- System.out.println();
- System.out.println("Print array here: " + Arrays.toString(customers));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement