-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUD.java
More file actions
133 lines (111 loc) · 3.2 KB
/
CRUD.java
File metadata and controls
133 lines (111 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package Codes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class SortByName implements Comparator<Customer> {
public int compare(Customer first, Customer second) {
return first.getName().compareToIgnoreCase(second.getName());
}
}
class SortById implements Comparator<Customer> {
public int compare(Customer first, Customer second) {
return ((Integer) first.getId()).compareTo(second.getId());
}
}
class Customer implements Comparable<Customer> {
private int id;
private String name;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
Customer(int id, String name, String city) {
this.id = id;
this.name = name;
this.city = city;
}
@Override
public int compareTo(Customer o) {
// Customer c = (Customer)o;
return o.name.compareToIgnoreCase(this.name);
// return this.name.compareToIgnoreCase(o.name);
// return ((Integer)this.id).compareTo(o.id);
}
/*
* @Override public int compareTo(Object o){ Customer c = (Customer)o;
* return ((Integer)this.id).compareTo(c.id); }
*/
public boolean equals(Object o) {
Customer c = (Customer) o;
if (this.id == c.id && this.name.equals(c.name) && this.city.equals(c.city)) {
return true;
} else {
return false;
}
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", city=" + city + "]";
}
}
public class CRUD {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("mike");
list.add("tom");
/*
* if(list.contains("mike")){ System.out.println("Mike Found..."); }
* else {
*
* System.out.println("Not Found.."); }
*/
// System.out.println(list);
String a = "Hello";
System.out.println(a.toString());
System.out.println(a);
ArrayList<Customer> customerList = new ArrayList<>();
Customer amit = new Customer(1000, "amit", "IN");
customerList.add(amit);
Customer tom = new Customer(1002, "Tom", "NY");
customerList.add(tom);
Customer mike = new Customer(1001, "Mike", "LA");
// System.out.println(mike.toString());
// System.out.println(mike);
customerList.add(mike);
System.out.println(customerList);
System.out.println("After Sort");
// Collections.sort(customerList,new SortByName());
/*
* Collections.sort(customerList ,new Comparator<Customer>() { public
* int compare(Customer first,Customer second){ return first.getCity().
* compareTo(second.getCity()); } });
*/
Collections.sort(customerList, (first, second) -> {
return first.getCity().compareTo(second.getCity());
});
System.out.println("Now " + customerList);
/*
* Customer searchCustomer = new Customer(0,"Tom",null); int index =
* customerList.indexOf(searchCustomer);
* //if(customerList.contains(searchCustomer)){ if(index>=0){
* //customerList.remove(index); customerList.set(index, new
* Customer(1001,"Kim","Delhi")); System.out.println("Found.."); } else
* { System.out.println("Not Found.."); }
*/
}
}