-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample9.java
218 lines (188 loc) · 6.13 KB
/
sample9.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.List;
public class sample9 {
public static void selectOption(ArrayList arr)
{
System.out.println("1.Delete by Account id " + "\n" + "2. Delete By Acc name" + "\n" + "3. Delete By Acc Ids" + "\n" + "4. Delete By Acc bal range" +"\n" + "5.exit");
System.out.println("enter option");
Scanner scanner=new Scanner(System.in);
int i= scanner.nextInt();
switch (i) {
case 1:
deleteByAccid(arr);
break;
case 2:
deleteByAccName(arr);
break;
case 3:
deleteByAccIds(arr);
break;
case 4:
deleteByBalanceRange(arr);
break;
case 5:
exit();
break;
default:
break;
}
}
public static void deleteByAccid(ArrayList<Bank> arr)
{
System.out.println("Enter account id");
Scanner scanner=new Scanner(System.in);
int acid=scanner.nextInt();
System.out.println("Entered acid is:"+acid);
Iterator itr=arr.iterator();
while(itr.hasNext())
{
Object o=itr.next();
Bank b=(Bank)o;
if(b.getAccid() == acid)
{
System.out.println("are you sure :(y/n)");
char s=scanner.next().charAt(0);
System.out.println(s);
if(s == 'y')
{
arr.remove(b);
System.out.println("Id Removed");
break;
}
else
{
System.out.println("Id not removed");
}
}
}
display(arr);
}
public static void deleteByAccName(ArrayList arr)
{
System.out.println("Enter account Name");
Scanner scanner=new Scanner(System.in);
String acname=scanner.nextLine();
System.out.println("Entered acName is:"+acname);
Iterator itr=arr.iterator();
while(itr.hasNext())
{
Object o=itr.next();
Bank b=(Bank)o;
if(b.getAccName().equals(acname))
{
System.out.println("are you sure :(y/n)");
char s=scanner.next().charAt(0);
if(s == 'y')
{
arr.remove(b);
System.out.println("Id Removed");
break;
}
else
{
System.out.println("Id not removed");
}
}
}
display(arr);
}
public static void deleteByAccIds(ArrayList arr)
{
System.out.println("Enter Account ids");
Scanner scanner=new Scanner(System.in);
String str= scanner.next();
String[] svalues=str.split(",");
Integer[] values=new Integer[svalues.length];
int i=0;
for(String s:svalues)
{
values[i]=Integer.parseInt(s.trim());
i++;
}
for( i=0;i<values.length;i++)
{
Iterator itr=arr.iterator();
while(itr.hasNext())
{
Object o= itr.next();
Bank b=(Bank)o;
if(b.getAccid() == values[i])
{
System.out.println("Id is:" + values[i]);
System.out.println("are you sure :(y/n)");
char s=scanner.next().charAt(0);
System.out.println(s);
if(s == 'y')
{
arr.remove(b);
System.out.println("Id Removed");
break;
}
else
{
System.out.println("Id not removed");
}
}
}
}
display(arr);
}
public static void deleteByBalanceRange(ArrayList arr)
{
System.out.println("Enter balance range");
Scanner scanner=new Scanner(System.in);
int range=scanner.nextInt();
for(int i=0;i<arr.size();i++)
{
Object o= arr.get(i);
Bank b=(Bank) o;
if(b.getAccBal() < range)
{
arr.remove(b);
System.out.println(b.getAccid()+ " " + b.getAccName() + " " + b.getAccBal());
}
}
// Iterator itr=arr.iterator();
// while(itr.hasNext())
// {
// Object o=itr.next();
// Bank b=(Bank) o;
// if(b.getAccBal() < range)
// {
// arr.remove(b);
// System.out.println(b.getAccid()+ " " + b.getAccName() + " " + b.getAccBal());
// }
// }
}
public static void exit(){
System.exit(0);
}
public static void display(ArrayList arr)
{
Iterator itr=arr.iterator();
while(itr.hasNext())
{
Object o=itr.next();
Bank b=(Bank)o;
System.out.println(b.getAccid() + " " +b.getAccName() + " " +b.getAccBal());
}
}
public static void main(String[] args)
{
ArrayList<Bank> ar=new ArrayList<Bank>();
Bank b1=new Bank();
Bank b2=new Bank();
Bank b3=new Bank();
Bank b4=new Bank();
Bank b5=new Bank();
b1.setAccid(101);b1.setAccName("surya");b1.setAccBal(500);
b2.setAccid(102);b2.setAccName("satya");b2.setAccBal(2500);
b3.setAccid(103);b3.setAccName("Tiwari");b3.setAccBal(3456);
b4.setAccid(104);b4.setAccName("Priya");b4.setAccBal(1122);
b5.setAccid(105);b5.setAccName("Akhil");b5.setAccBal(2341);
ar.add(b1);ar.add(b2);ar.add(b3);ar.add(b4);ar.add(b5);
selectOption(ar);
}
}