-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayEnum.java
97 lines (84 loc) · 2.58 KB
/
ArrayEnum.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
import java.util.Locale;
import java.util.Scanner;
enum Command {QUIT, ADD, LIST, SUM, MIN, MAX, INVALID};
public class ArrayEnum {
public static void main(String[] args) {
final Scanner sc = new Scanner(System.in);
int values[] = new int[100];
int i = 0;
while(true) {
final Command command = getCommand(sc);
if (command == Command.QUIT){
System.out.println("Bye!");
break;
}
if(command == Command.INVALID) {
System.out.println("Invalid Command");
continue;
}
switch(command) {
case ADD :
final int newValue = getValue(sc);
values[i] = newValue;
i++;
break;
case LIST:
printList(values, i);
break;
case SUM :
System.out.println(getSum(values, i));
break;
case MIN :
System.out.println(getMin(values, i));
break;
case MAX :
System.out.println(getMax(values,i));
break;
}
}
sc.close();
}
private static Command getCommand(Scanner sc) {
Command cmd;
String input = sc.next();
try {
cmd = Command.valueOf(input.toUpperCase(Locale.ROOT));
}
catch(IllegalArgumentException e) {cmd = Command.INVALID;}
finally{}
return cmd;
}
private static int getValue(Scanner sc) {
int value = sc.nextInt();
return value;
}
private static void printList(int[] values, int i) {
for(int j = 0; j < i; j++) {
System.out.print(values[j] + " ");
}
System.out.printf("\n");
}
private static int getSum(int[] values, int i) {
int sum = 0;
for(int j = 0; j < i; j++) {
sum+= values[j];
}
return sum;
}
private static int getMax(int[] values, int i) {
int max = -1;
for(int j = 0; j < i; j++) {
if(values[j] > max)
max = values[j];
}
return max;
}
private static int getMin(int[] values, int i) {
int min = Integer.MAX_VALUE;
for(int j = 0; j < i; j++) {
if(values[j] < min)
min = values[j];
}
return min;
}
}