Skip to content

Commit b206f71

Browse files
authored
Merge pull request #3 from flexycode/flexycode-patch-3
☕ Refactor Activity 1
2 parents 71531a0 + 9901b41 commit b206f71

File tree

1 file changed

+70
-96
lines changed
  • activity/activity-1/Java/FlippingFavorites

1 file changed

+70
-96
lines changed

activity/activity-1/Java/FlippingFavorites/App.java

+70-96
Original file line numberDiff line numberDiff line change
@@ -4,117 +4,91 @@
44
* Main application class implementing a snack distribution algorithm.
55
* When a child has a favorite snack number, we "flip" it by incrementing it by 1.
66
* If the incremented value exceeds k (the maximum snack number), we reset it to 1.
7-
* Handles user input processing and manages snack quantity increments.
7+
* Exits silently on any invalid input without error messages.
88
*/
99
public class App {
1010
/**
1111
* Main entry point of the application.
1212
* Processes user input and coordinates snack distribution logic.
13-
* Reads input, processes snack numbers according to requirements, and displays the result.
13+
* Exits silently on any invalid input.
1414
* @param args Command line arguments (not used)
15-
* @throws Exception If input processing fails
1615
*/
17-
public static void main(String[] args) throws Exception {
18-
boolean looper = true; // Controls main program loop
16+
public static void main(String[] args) {
17+
Scanner userInput = new Scanner(System.in);
1918

20-
while(looper) {
19+
try {
20+
// Get initial values (n and k)
21+
String firstLine = userInput.nextLine();
22+
String[] parts = firstLine.split("\\s+");
23+
24+
// Exit silently if format is invalid
25+
if (parts.length != 2) {
26+
return;
27+
}
28+
29+
// Parse input values and exit silently on any parsing error
30+
int n, k;
2131
try {
22-
int k = 0; // Maximum snack number limit
23-
int n = 0; // Number of children
24-
String stringInput = ""; // Temporary storage for input
25-
26-
// Initialize arrays for tracking snacks
27-
int[] a = new int[40]; // Stores initial snack quantities
28-
int[] snack = new int[40]; // Tracks updated snack counts
29-
30-
Scanner userInput = new Scanner(System.in);
31-
32-
// Get initial values (n and k)
33-
while(true) {
34-
stringInput = userInput.nextLine();
35-
String[] parts = stringInput.split("\\s+");
36-
37-
// Parse input values
38-
n = Integer.parseInt(parts[0]); // Number of children
39-
k = Integer.parseInt(parts[1]); // Maximum snack limit
40-
41-
boolean invalidInput = false; // Track invalid input
42-
43-
// Validate input ranges
44-
if (!(n <= 100 && n >= 1)) {
45-
System.out.println("Invalid Input for n, try again");
46-
invalidInput = true;
47-
}
48-
49-
if (!(k <= 100 && k >= 1)) {
50-
System.out.println("Invalid Input for k, try again");
51-
invalidInput = true;
52-
}
32+
n = Integer.parseInt(parts[0]); // Number of children
33+
k = Integer.parseInt(parts[1]); // Maximum snack limit
34+
} catch (NumberFormatException e) {
35+
return;
36+
}
37+
38+
// Validate input ranges silently
39+
if (n < 1 || n > 100 || k < 1 || k > 100) {
40+
return;
41+
}
42+
43+
// Process second line of input (individual snack quantities)
44+
String secondLine = userInput.nextLine();
45+
String[] snackValues = secondLine.split("\\s+");
46+
47+
// Exit silently if input count doesn't match expected
48+
if (snackValues.length != n) {
49+
return;
50+
}
51+
52+
int[] snacks = new int[n];
53+
54+
// Load and validate snack values silently
55+
for (int i = 0; i < n; i++) {
56+
try {
57+
snacks[i] = Integer.parseInt(snackValues[i]);
5358

54-
// Exit loop if both inputs are valid
55-
if (!invalidInput) {
56-
break;
59+
// Exit silently if value is out of range
60+
if (snacks[i] < 1 || snacks[i] > k) {
61+
return;
5762
}
58-
}
59-
60-
// Process second line of input (individual snack quantities)
61-
stringInput = userInput.nextLine();
62-
String[] parts2 = stringInput.split("\\s+");
63-
64-
boolean hasInvalidInput = false; // Track invalid input
65-
66-
// Load initial snack values
67-
for (int i = 0; i < n; i++) {
68-
try {
69-
a[i] = Integer.parseInt(parts2[i]);
70-
71-
// Throw if out of range (1 to k)
72-
if (a[i] < 1 || a[i] > k) {
73-
throw new IllegalArgumentException("Invalid Input for a[" + (i + 1) + "], must be between 1 and " + k);
74-
}
75-
} catch (IllegalArgumentException e) {
76-
if (!hasInvalidInput) {
77-
System.out.println(); // Add space when first error occurs
78-
}
79-
System.out.println(e.getMessage()); // Prints range error
80-
hasInvalidInput = true;
81-
}
82-
}
83-
84-
// Exit program if any invalid input was found
85-
if (hasInvalidInput) {
63+
} catch (NumberFormatException e) {
8664
return;
8765
}
88-
89-
// Filter snacks within valid range (1 to k)
90-
for(int i = 0; i < n; i++) {
91-
if (a[i] >= 1 && a[i] <= k) {
92-
snack[i] = a[i];
93-
}
94-
}
95-
96-
// Process the "flip" operation: increment each snack number or reset to 1 if exceeds k
97-
for(int i = 0; i < n; i++) {
98-
if (snack[i] >= k) {
99-
snack[i] = 1; // Reset to minimum if exceeds maximum
100-
} else {
101-
snack[i] += 1; // Otherwise increment normally
102-
}
66+
}
67+
68+
// Process the "flip" operation: increment each snack number or reset to 1 if equals k
69+
for (int i = 0; i < n; i++) {
70+
if (snacks[i] == k) {
71+
snacks[i] = 1; // Reset to 1 if exactly equals maximum
72+
} else {
73+
snacks[i]++; // Otherwise increment normally
10374
}
104-
105-
// Output the result
106-
System.out.println(); // Add space before output only if there was an error
107-
for(int i = 0; i < n; i++) {
108-
System.out.print(snack[i] + " ");
75+
}
76+
77+
// Output the result
78+
StringBuilder result = new StringBuilder();
79+
for (int i = 0; i < n; i++) {
80+
result.append(snacks[i]);
81+
if (i < n - 1) {
82+
result.append(" ");
10983
}
110-
System.out.println();
111-
112-
looper = false; // Exit main loop
113-
} catch(Exception e) {
114-
// Handle invalid input or processing errors
115-
System.out.println("\nError: " + e);
116-
System.out.println("Make sure your inputs are correct. Repeating program...\n");
11784
}
85+
System.out.println(result.toString());
86+
87+
} catch (Exception e) {
88+
// Silently exit on any exception
89+
return;
90+
} finally {
91+
userInput.close();
11892
}
11993
}
120-
}
94+
}

0 commit comments

Comments
 (0)