Java Application with JOptionPane

Below is the implementation of the topic mentioned:

Java




// Java Program to demonstrate
// Java Application
import javax.swing.*;
import java.io.*;
  
// Driver Class
public class GFG {
    // main function
    public static void main(String[] args) {
        // Infinite Loop
        while(true){
            // Exception Handling
            try{
                String initial, arithmetic;
                int choice, choose1;
  
                // Welcome Screen
                JOptionPane.showMessageDialog(null, "Welcome to MyApplication");
  
                // Screen with Options Showing
                initial = JOptionPane.showInputDialog(null, "1. Arithmetic Operation\n2. Even or Odd Number\n3. Shopping Discount\n4. Student Grading\n5. Exit");
  
                // Taking the Input and Storing the
                // Choice Taken
                choice = Integer.parseInt(initial);
  
                switch (choice) {
                    case 1:
                        //  Arithmetic operations section
                        arithmetic = JOptionPane.showInputDialog(null, "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division");
                        choose1 = Integer.parseInt(arithmetic);
                        switch (choose1) {
                            // Addition Operation
                            case 1:
                                String addA = JOptionPane.showInputDialog(null, "Input first number");
                                int add1 = Integer.parseInt(addA);
                                String addB = JOptionPane.showInputDialog(null, "Input second number");
                                int add2 = Integer.parseInt(addB);
                                int addResult = add1 + add2;
                                JOptionPane.showMessageDialog(null, "The answer is " + addResult);
                                break;
                            // Subtraction Opertion
                            case 2:
                                String subA = JOptionPane.showInputDialog(null, "Input first number");
                                int sub1 = Integer.parseInt(subA);
                                String subB = JOptionPane.showInputDialog(null, "Input second number");
                                int sub2 = Integer.parseInt(subB);
                                int subResult = sub1 - sub2;
                                JOptionPane.showMessageDialog(null, "The answer is " + subResult);
                                break;
                            // Multiplication Operation
                            case 3:
                                String mulA = JOptionPane.showInputDialog(null, "Input first number");
                                int mul1 = Integer.parseInt(mulA);
                                String mulB = JOptionPane.showInputDialog(null, "Input second number");
                                int mul2 = Integer.parseInt(mulB);
                                int mulResult = mul1 * mul2;
                                JOptionPane.showMessageDialog(null, "The answer is " + mulResult);
                                break;
                            // Division Operation
                            case 4:
                                String divA = JOptionPane.showInputDialog(null, "Input first number");
                                int div1 = Integer.parseInt(divA);
                                String divB = JOptionPane.showInputDialog(null, "Input second number");
                                int div2 = Integer.parseInt(divB);
                                int divResult = div1 / div2;
                                JOptionPane.showMessageDialog(null, "The answer is " + divResult);
                                break;
                        }
                        break;
                    // Even or Odd Section
                    case 2:
                        String eo = JOptionPane.showInputDialog(null,"Input the number to check");
                        int check = Integer.parseInt(eo);
  
                        // Number is Even
                        if(check % 2 == 0){
                            JOptionPane.showMessageDialog(null,"The number "+check+" is even");
                        }
                        // Number is Odd
                        else{
                            JOptionPane.showMessageDialog(null,"The number "+check+" is odd");
                        }
                        break;
                    // Shopping Section
                    case 3:
                        String shopping = JOptionPane.showInputDialog(null,"Enter your total shopping amount");
                        int discount = Integer.parseInt(shopping);
  
                        // Discount Given according to the Discount
                        if((discount >= 500000) && (discount <= 1000000)){
                            int finalAmount = discount * 5 / 100;
                            int price = discount - finalAmount;
                            JOptionPane.showMessageDialog(null,"You get a 5% discount");
                            JOptionPane.showMessageDialog(null,"Your total shopping amount is "+price);
                        }
                        else if((discount > 1000000) && (discount <= 1500000)){
                            int finalAmount = discount * 10 / 100;
                            int price = discount - finalAmount;
                            JOptionPane.showMessageDialog(null,"You get a 10% discount");
                            JOptionPane.showMessageDialog(null,"Your total shopping amount is "+price);
                        }
                        else if((discount > 1500000) && (discount < 2000000)){
                            int finalAmount = discount * 15 / 100;
                            int price = discount - finalAmount;
                            JOptionPane.showMessageDialog(null,"You get a 15% discount");
                            JOptionPane.showMessageDialog(null,"Your total shopping amount is "+price);
                        }
                        else if(discount >= 2000000){
                            int finalAmount = discount * 25 / 100;
                            int price = discount - finalAmount;
                            JOptionPane.showMessageDialog(null,"You get a 25% discount");
                            JOptionPane.showMessageDialog(null,"Your total shopping amount is "+price);
                        }
                        else if(discount >= 0){
                            JOptionPane.showMessageDialog(null,"Your total shopping amount is "+discount);
                        }
                        else{
                            JOptionPane.showMessageDialog(null,"Value cannot be negative");
                        }
                        break;
                    //  Student Grading
                    case 4:
                        String name = JOptionPane.showInputDialog(null,"Enter student's name");
                        String age = JOptionPane.showInputDialog(null,"Enter student's age");
                        int studentAge = Integer.parseInt(age);
                        if(studentAge >= 0){
                            JOptionPane.showMessageDialog(null,"The student's name is "+name+" and is "+studentAge+" years old");
                        }
                        else{
                            JOptionPane.showMessageDialog(null,"Age cannot be negative");
                        }
                        break;
                    // Exit
                    case 5:
                        JOptionPane.showMessageDialog(null, "Program will exit");
                        System.exit(0);
                        break;
                }
            }
            // Exception
            catch(NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "Input error occurred", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}


Executing the Program:

Outputs:

1. Welcome Screen

2. Taking Input for Operation to be Performed

3. Taking Input Inside for Further Operation

Example: If Selection of Arithmetic Operation then which operation to be performed.

Taking of Input 1 for Addition Operation

Taking of Input 2 for Addition Operation

4. Result of the Operations Performed



Java Application with JOptionPane: Calculator, Numbers, Discounts, and Student Assessment

In this article, we will create a simple application that is created using the Java programming language with the help of the JOptionPane class. This application has various features that can assist users in various situations.

Similar Reads

Available Features:

1. Arithmetic Operations (Calculator):...

Java Application with JOptionPane

Below is the implementation of the topic mentioned:...