diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index e571935..7711989 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,3 +6,4 @@
  • Owaniyi Oluwafemi Micheal, (@Chuckycipher), Cybersecurity_department, U23CYS1072
  • Ahmad Muhammad Idris, (@LuffytheGoat), Physics with Electronics, U23PE1008
  • +
  • Godwin Arege, (godwin849), U24EEE2004, Electrical and electronics engineering diff --git a/SwingCalculator.java b/SwingCalculator.java new file mode 100644 index 0000000..b0ee714 --- /dev/null +++ b/SwingCalculator.java @@ -0,0 +1,83 @@ +import java.util.Scanner; +import java.util.InputMismatchException; + +public class ConsoleCalculator { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + boolean running = true; + + System.out.println("Java Console Calculator"); + System.out.println("======================="); + + while (running) { + displayMenu(); + int choice = getIntInput(scanner, "Enter your choice: "); + + if (choice == 5) { + running = false; + System.out.println("Exiting calculator. Goodbye!"); + continue; + } + + if (choice < 1 || choice > 4) { + System.out.println("Invalid choice! Please try again."); + continue; + } + + double num1 = getDoubleInput(scanner, "Enter first number: "); + double num2 = getDoubleInput(scanner, "Enter second number: "); + performOperation(choice, num1, num2); + } + + scanner.close(); + } + + private static void displayMenu() { + System.out.println("\nAvailable operations:"); + System.out.println("1. Addition (+)"); + System.out.println("2. Subtraction (-)"); + System.out.println("3. Multiplication (*)"); + System.out.println("4. Division (/)"); + System.out.println("5. Exit"); + } + + private static void performOperation(int choice, double num1, double num2) { + switch (choice) { + case 1 -> System.out.printf("Result: %.2f + %.2f = %.2f%n", num1, num2, num1 + num2); + case 2 -> System.out.printf("Result: %.2f - %.2f = %.2f%n", num1, num2, num1 - num2); + case 3 -> System.out.printf("Result: %.2f * %.2f = %.2f%n", num1, num2, num1 * num2); + case 4 -> { + if (num2 == 0) { + System.out.println("Error: Cannot divide by zero!"); + } else { + System.out.printf("Result: %.2f / %.2f = %.2f%n", num1, num2, num1 / num2); + } + } + } + } + + private static int getIntInput(Scanner scanner, String prompt) { + while (true) { + try { + System.out.print(prompt); + return scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Invalid input! Please enter an integer."); + scanner.nextLine(); // Clear invalid input + } + } + } + + private static double getDoubleInput(Scanner scanner, String prompt) { + while (true) { + try { + System.out.print(prompt); + return scanner.nextDouble(); + } catch (InputMismatchException e) { + System.out.println("Invalid input! Please enter a number."); + scanner.nextLine(); // Clear invalid input + } + } + } +}