Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.4 kB
2
Indexable
Never
package org.example;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.InputMismatchException;
import java.util.Scanner;

public class LoggingExample2 {
    final static Logger logger =  LogManager.getLogger(LoggingExample2.class.getName());
    public static void main(String[] args) {
//        DOMConfigurator.configure("log4j.xml");
        System.out.println(" Welcome to Java Calculator v0.1 \n");
        Scanner scanner = new Scanner(System.in);
        System.out.println("\n Please enter two numbers");
        System.out.print("\n First number: ");
        int firstNumber = 0;
        try {
            firstNumber = scanner.nextInt();
        }catch (InputMismatchException inputException){
            logger.error("Please enter integer number");
        }
        System.out.print("\n Second number: ");
        int secondNumber = 0;
        try {
            secondNumber = scanner.nextInt();
        }catch (InputMismatchException inputException){
            logger.error("Please enter integer number");
        }
        System.out.println("\n Select between (*,/,+,-)\n Type out the character in a single letter: ");
        String operation = scanner.next();

        String eo = "You have selected ";

        switch (operation) {
            case "*" -> {
                System.out.println(eo + "*");
                mul(firstNumber, secondNumber);
            }
            case "/" -> {
                try {
                    System.out.println(eo + "/ \n Your Result: " + (firstNumber / secondNumber));
                }catch (ArithmeticException e){
                    logger.error("Divide by 2 is forbidden");
                }
            }
            case "+" -> System.out.println(eo + "+ \n Your Result: " + (firstNumber + secondNumber));
            case "-" -> System.out.println(eo + "- \n Your Result: " + (firstNumber - secondNumber));
            default -> System.out.println("\n Please select a valid character");
        }

        scanner.close();
        System.out.println(" Closing Application ");
    }

    public static void mul(int num1, int num2){
        logger.info("this is an info log");
        int result = num1 * num2;
        System.out.println("Your Result: "+result);
//        logger.debug("this is an info log");
    }
}