-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionSet.java
More file actions
372 lines (335 loc) · 10.7 KB
/
OptionSet.java
File metadata and controls
372 lines (335 loc) · 10.7 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package Model;
import java.io.*;
import java.util.Arrays;
public class OptionSet implements Serializable
{
private String OptionSetName;
private Options[] opt;
//////// Constructors ////////////
public OptionSet(){
this.OptionSetName = "";
this.opt = new Options[0];
}
public OptionSet(String OptionSetName, int size)
{
this.OptionSetName = OptionSetName;
this.opt = new Options[size];
}
/////////////////Methods/////////
public void addOption(Options NewOpt)//aka buildOption
{
int length = this.opt.length;
Options[] NewArr = new Options[length+1];//New bigger array
for(int i = 0; i < length; i++)//Process of copying the old array
{
NewArr[i] = opt[i];
}
NewArr[length] = NewOpt;//Adding new option
this.setOpt(NewArr);//Making old array become new one
}
//OverLoad
public void addOption(String SetName, String Oname, double Oprice)//aka build option
{
Options NewOpt = new Options(Oname, Oprice);//New Option
int length = this.opt.length;
Options[] NewArr = new Options[length+1];//New bigger array
for(int i = 0; i < length; i++)//Process of copying the old array
{
NewArr[i] = opt[i];
}
NewArr[length] = NewOpt;//Adding new option
this.setOpt(NewArr);//Making old array become new one
}
public void deleteOption(String name)
{
boolean found = false;// To indicate when Option is found
int length = this.opt.length;
int ind = 0;//index
Options del; // = new Options();
for (int i = 0; i < length - 1; i++)
{
if (opt[i].getOptName().equals(name))//To find
{
System.out.println(name + " Option with name found successfully to delete");
del = opt[i];//We don't use it but let it be here
ind = i;
found = true;
break;
}
ind++;
}
if (!found){
System.out.println("Option with name " + name + " didn't found to delete");
}
else{
//We create temp Options to swap Option we need to delete with last element
// Options temp = new Options();
Options temp = opt[length-1];
opt[length-1] = opt[ind];
opt[ind] = temp;
Options[] NewArr = new Options[length-1];//New Array
for(int i = 0; i < NewArr.length; i++)//Process of deletion
{
NewArr[i] = opt[i];
}
this.setOpt(NewArr);
}
}
public void UpdateOption(String oldName, String NewName, int NewPrice)
{
boolean found = false;
for (Options j : this.getOpt())
{
if (j.getOptName().equals(oldName))
{
j.setOptName(NewName);
j.setOptPrice(NewPrice);
System.out.println(oldName + " Option updated successfully");
found = true;
}
}
if (!found)
{
System.out.println("No such Option with that name");
}
}
public void UpdateOptionWithoutMessage(String oldName, String NewName, int NewPrice)
{
boolean found = false;
for (Options j : this.getOpt())
{
if (j.getOptName().equals(oldName))
{
j.setOptName(NewName);
j.setOptPrice(NewPrice);
// System.out.println(oldName + " Option updated successfully"); //Commented this since I don;t want this line
found = true;
}
}
if (!found)
{
System.out.println("No such Option with that name");
}
}
protected void printOneOption(int x)
{
int count = 1;
for(Options i : opt)
{
if(count == x){
System.out.println(i);
}
count++;
}
System.out.println("No such Option, printOneOption failed");
}
//Overload
protected void printOneOption(String Oname)
{
boolean found = false;// To indicate when Option is found
int length = this.opt.length;
for (int i = 0; i < length - 1; i++)
{
if (opt[i].getOptName().equals(Oname))//To find
{
System.out.println(opt[i]);
}
}
System.out.println("Option with name " + Oname + " No such Option, printOneOption failed");
}
@Override
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("OptionSet name: ");
sb.append(getOptionSetName());
sb.append(", OptionSet's Options(Array): ");
sb.append(Arrays.toString(getOpt()));
return sb.toString();
}
////////////////// GETTERS AND SETTERS ////////////////////////////////////////////////////////////////////////////////
protected Options[] getOpt() {
return opt;
}
protected void setOpt(Options[] opt) {
this.opt = opt;
}
protected String getOptionSetName() {
return OptionSetName;
}
protected void setOptionSetName(String optionName) {
OptionSetName = optionName;
}
protected String getOptionName(int x){
int count = 1;
for(Options i : opt)
{
if(count == x){
return i.getOptName();
}
count++;
}
System.out.println("No such Option, getOptionName failed");
String o = null;
return o;
}
protected void setOptionName(int x, String OName){
int count = 1;
for(Options i : opt)
{
if(count == x){
i.setOptName(OName);
}
count++;
}
System.out.println("No such Option, setOptionName failed");
}
protected double getOptionPrice(int x){
int count = 1;
for(Options i : opt)
{
if(count == x){
return i.getOptPrice();
}
count++;
}
System.out.println("No such Option, getOptionPrice failed");
return 0;
}
//Overloaded
protected double getOptionPrice(String Oname){
boolean found = false;// To indicate when Option is found
int length = this.opt.length;
for (int i = 0; i < length - 1; i++)
{
if (opt[i].getOptName().equals(Oname))//To find
{
System.out.println(opt[i].getOptPrice());
return opt[i].getOptPrice();
}
}
System.out.println("Option with name " + Oname + " No such Option, getOptionPrice failed");
return 0;
}
protected void setOptionPrice(int x, double OPrice){
int count = 1;
for(Options i : opt)
{
if(count == x){
i.setOptPrice(OPrice);
}
count++;
}
System.out.println("No such Option, setOptionPrice failed");
}
protected Options getOneOpt(String Oname){
boolean found = false;// To indicate when Option is found
int length = this.opt.length;
for (int i = 0; i < length - 1; i++)
{
if (opt[i].getOptName().equals(Oname))//To find
{
System.out.println(Oname + " Option with name found successfully");
return opt[i];
}
}
System.out.println("Option with name " + Oname + " didn't found, getOneOption failed");
return null;
}
//Overload
protected Options getOneOpt(int x){
int count = 1;
for(Options i : opt)
{
if(count == x){
return i;
}
count++;
}
System.out.println("No such Option, getOneOption failed");
return null;
}
protected void setOneOpt(String Oname, Options option){
String nameToSet = option.getOptName();
double priceToSet = option.getOptPrice();
boolean found = false;// To indicate when Option is found
int length = this.opt.length;
for (int i = 0; i < length - 1; i++)
{
if (opt[i].getOptName().equals(Oname))//To find
{
System.out.println(Oname + " Option with name found successfully");
opt[i].setOptName(nameToSet);
opt[i].setOptPrice(priceToSet);
found = true;
}
}
if(!found){
System.out.println("Option with name " + Oname + " didn't found, getOneOption failed");
}
}
protected void setOneOpt(int x, Options option){
boolean found = false;
String nameToSet = option.getOptName();
double priceToSet = option.getOptPrice();
int count = 1;
for(Options i : opt)
{
if(count == x){
System.out.println(x + " Option found successfully");
i.setOptName(nameToSet);
i.setOptPrice(priceToSet);
found = true;
}
count++;
}
if(!found){
System.out.println("No such Option, setOneOption failed");
}
}
protected int getOptionsLength(){
return opt.length;
}
///////////////////////////////////////New Inner Class/////////////////////////////////////////////////////////////////
public class Options implements Serializable
{
private String OptName;
private double OptPrice;
private static int size; //not used
//////// Constructors ////////////
public Options()
{
}
public Options (String OptName, double OptPrice)
{
this.OptName = OptName;
this.OptPrice = OptPrice;
size++;
}
//Methods
@Override
public String toString()
{
StringBuffer sb1 = new StringBuffer();
sb1.append("Name: ");
sb1.append(getOptName());
sb1.append(", Price: ");
sb1.append(getOptPrice());
return sb1.toString();
}
////////////////// GETTERS AND SETTERS ////////////////////////////////////////////////////////////////////////////////
protected String getOptName() {
return OptName;
}
protected void setOptName(String optName) {
OptName = optName;
}
protected double getOptPrice() {
return OptPrice;
}
protected void setOptPrice(double optPrice) {
OptPrice = optPrice;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}