-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCRUDDemo.java
More file actions
211 lines (177 loc) · 5.36 KB
/
CRUDDemo.java
File metadata and controls
211 lines (177 loc) · 5.36 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Scanner;
import javax.sound.midi.Synthesizer;
// What is Utility Class
// All methods are static and Constructor is private
// and class is final
// Utility Interface
//interface CustomerOperations{
final class CustomerOperations{
private CustomerOperations(){}
// It is a generic ArrayList , it hold the customer objects
static final ArrayList<Customer> customerList = new ArrayList<>();
public static void addNewCustomer(int id , String name, double salary){
Customer customer = new Customer(id ,name ,salary);
customerList.add(customer);
System.out.println("Record Added...");
}
public static boolean deleteRecord(Customer deleteObject){
int index = customerList.indexOf(deleteObject);
if(index>=0){
customerList.remove(index);
return true;
}
return false;
}
public static boolean isFound(Customer searchObject){
return customerList.contains(searchObject);
}
static double totalSalary = 0.0;
public static double getTotalSalary(){
// Traverse the ArrayList
// 1st Way (Traditional Style)
//int ee [] = new int[10];
//System.out.println(ee.length);
// for(int i = 0 ; i<customerList.size(); i++){
// Customer customerCurrentObject = customerList.get(i);
// totalSalary = totalSalary + customerCurrentObject.getSalary();
// }
// 1.4 (Iterator) 2nd Way
Iterator<Customer> customerIterator = customerList.iterator();
while(customerIterator.hasNext()){
// next() - get the current object and move to next object
Customer customerObject = customerIterator.next();
totalSalary = totalSalary + customerObject.getSalary();
//customerIterator.remove();
}
// 1.4 ListIterator 3rd Way
ListIterator<Customer> customerListIterator = customerList.listIterator();
while(customerListIterator.hasNext()){
Customer customerObject = customerListIterator.next();
totalSalary = totalSalary + customerObject.getSalary();
}
while(customerListIterator.hasPrevious()){
// give current object and move to previous object
Customer customerObject = customerListIterator.previous();
totalSalary = totalSalary + customerObject.getSalary();
}
// 4th Way (Enhance For Loop (Java 1.5))
for(Customer customerObject : customerList){
totalSalary = totalSalary + customerObject.getSalary();
}
//5th Way forEach function (Java 1.8)
customerList.forEach((customerObject)
->totalSalary+=customerObject.getSalary());
return totalSalary;
}
}
class Customer{
private int id;
private String name;
private double salary;
Customer(int id, String name , double salary){
this.id = id;
this.name = name;
this.salary = salary;
}
@Override
public boolean equals(Object object){
boolean isFound = false;
if(object instanceof Customer){
Customer customer = (Customer) object; // Downcasting
if(this.id == customer.id &&
this.name.equals(customer.name)
&& this.salary == customer.salary){
isFound = true;
}
}
return isFound;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Id "+id + "Name "+name+" Salary "+salary+"\n";
}
}
public class CRUDDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner (System.in);
while(true){
System.out.println("1. Add");
System.out.println("2. Delete");
System.out.println("3. Search");
System.out.println("4. Update");
System.out.println("6. Read");
System.out.println("7. Total Salary");
System.out.println("5. Exit");
int choice = scanner.nextInt();
if(choice ==1){
System.out.println("Enter the Id");
int id = scanner.nextInt();
System.out.println("Enter the Name");
String name = scanner.next();
System.out.println("Enter the Salary");
double salary = scanner.nextDouble();
CustomerOperations.addNewCustomer(id, name, salary);
}
else
if(choice == 6){
System.out.println(CustomerOperations.customerList);
}
else
if(choice ==2){
System.out.println("Enter the Id To Delete");
int id = scanner.nextInt();
System.out.println("Enter the Name to Delete");
String name = scanner.next();
System.out.println("Enter the Salary to Delete");
double salary = scanner.nextDouble();
Customer deleteObject = new Customer(id ,name, salary);
if(CustomerOperations.deleteRecord(deleteObject)){
System.out.println("Record Delete");
System.out.println(CustomerOperations.customerList);
}
else
System.out.println("Record Not Found Can't Delete");
}
else
if(choice == 7){
System.out.println(CustomerOperations.getTotalSalary());
}
else
if(choice == 3){
System.out.println("Enter the Id To Search");
int id = scanner.nextInt();
System.out.println("Enter the Name to Search");
String name = scanner.next();
System.out.println("Enter the Salary to Search");
double salary = scanner.nextDouble();
Customer searchCustomerObject = new Customer(id, name,salary);
if(CustomerOperations.isFound(searchCustomerObject)){
System.out.println("Customer Found...");
}
else
{
System.out.println("Customer Not Found...");
}
}
}
}
}