Skip to content

☕ Refactor Activity 1 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 70 additions & 96 deletions activity/activity-1/Java/FlippingFavorites/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,117 +4,91 @@
* Main application class implementing a snack distribution algorithm.
* When a child has a favorite snack number, we "flip" it by incrementing it by 1.
* If the incremented value exceeds k (the maximum snack number), we reset it to 1.
* Handles user input processing and manages snack quantity increments.
* Exits silently on any invalid input without error messages.
*/
public class App {
/**
* Main entry point of the application.
* Processes user input and coordinates snack distribution logic.
* Reads input, processes snack numbers according to requirements, and displays the result.
* Exits silently on any invalid input.
* @param args Command line arguments (not used)
* @throws Exception If input processing fails
*/
public static void main(String[] args) throws Exception {
boolean looper = true; // Controls main program loop
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);

while(looper) {
try {
// Get initial values (n and k)
String firstLine = userInput.nextLine();
String[] parts = firstLine.split("\\s+");

// Exit silently if format is invalid
if (parts.length != 2) {
return;
}

// Parse input values and exit silently on any parsing error
int n, k;
try {
int k = 0; // Maximum snack number limit
int n = 0; // Number of children
String stringInput = ""; // Temporary storage for input

// Initialize arrays for tracking snacks
int[] a = new int[40]; // Stores initial snack quantities
int[] snack = new int[40]; // Tracks updated snack counts

Scanner userInput = new Scanner(System.in);

// Get initial values (n and k)
while(true) {
stringInput = userInput.nextLine();
String[] parts = stringInput.split("\\s+");

// Parse input values
n = Integer.parseInt(parts[0]); // Number of children
k = Integer.parseInt(parts[1]); // Maximum snack limit

boolean invalidInput = false; // Track invalid input

// Validate input ranges
if (!(n <= 100 && n >= 1)) {
System.out.println("Invalid Input for n, try again");
invalidInput = true;
}

if (!(k <= 100 && k >= 1)) {
System.out.println("Invalid Input for k, try again");
invalidInput = true;
}
n = Integer.parseInt(parts[0]); // Number of children
k = Integer.parseInt(parts[1]); // Maximum snack limit
} catch (NumberFormatException e) {
return;
}

// Validate input ranges silently
if (n < 1 || n > 100 || k < 1 || k > 100) {
return;
}

// Process second line of input (individual snack quantities)
String secondLine = userInput.nextLine();
String[] snackValues = secondLine.split("\\s+");

// Exit silently if input count doesn't match expected
if (snackValues.length != n) {
return;
}

int[] snacks = new int[n];

// Load and validate snack values silently
for (int i = 0; i < n; i++) {
try {
snacks[i] = Integer.parseInt(snackValues[i]);

// Exit loop if both inputs are valid
if (!invalidInput) {
break;
// Exit silently if value is out of range
if (snacks[i] < 1 || snacks[i] > k) {
return;
}
}

// Process second line of input (individual snack quantities)
stringInput = userInput.nextLine();
String[] parts2 = stringInput.split("\\s+");

boolean hasInvalidInput = false; // Track invalid input

// Load initial snack values
for (int i = 0; i < n; i++) {
try {
a[i] = Integer.parseInt(parts2[i]);

// Throw if out of range (1 to k)
if (a[i] < 1 || a[i] > k) {
throw new IllegalArgumentException("Invalid Input for a[" + (i + 1) + "], must be between 1 and " + k);
}
} catch (IllegalArgumentException e) {
if (!hasInvalidInput) {
System.out.println(); // Add space when first error occurs
}
System.out.println(e.getMessage()); // Prints range error
hasInvalidInput = true;
}
}

// Exit program if any invalid input was found
if (hasInvalidInput) {
} catch (NumberFormatException e) {
return;
}

// Filter snacks within valid range (1 to k)
for(int i = 0; i < n; i++) {
if (a[i] >= 1 && a[i] <= k) {
snack[i] = a[i];
}
}

// Process the "flip" operation: increment each snack number or reset to 1 if exceeds k
for(int i = 0; i < n; i++) {
if (snack[i] >= k) {
snack[i] = 1; // Reset to minimum if exceeds maximum
} else {
snack[i] += 1; // Otherwise increment normally
}
}

// Process the "flip" operation: increment each snack number or reset to 1 if equals k
for (int i = 0; i < n; i++) {
if (snacks[i] == k) {
snacks[i] = 1; // Reset to 1 if exactly equals maximum
} else {
snacks[i]++; // Otherwise increment normally
}

// Output the result
System.out.println(); // Add space before output only if there was an error
for(int i = 0; i < n; i++) {
System.out.print(snack[i] + " ");
}

// Output the result
StringBuilder result = new StringBuilder();
for (int i = 0; i < n; i++) {
result.append(snacks[i]);
if (i < n - 1) {
result.append(" ");
}
System.out.println();

looper = false; // Exit main loop
} catch(Exception e) {
// Handle invalid input or processing errors
System.out.println("\nError: " + e);
System.out.println("Make sure your inputs are correct. Repeating program...\n");
}
System.out.println(result.toString());

} catch (Exception e) {
// Silently exit on any exception
return;
} finally {
userInput.close();
}
}
}
}