Open In App

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

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Available Features:

1. Arithmetic Operations (Calculator):

This application allows you to perform various arithmetic operations such as addition, subtraction, multiplication, and division. You only need to enter the numbers you want to calculate through the dialog boxes provided by JOptionPane, and the results will be displayed instantly.

2. Even-Odd Numbers:

This feature allows you to check whether a number is even or odd. You simply enter the number through a dialog box, and the application will inform you whether the number falls into the even or odd category.

3. Shopping Discounts:

For those who enjoy shopping, this feature will be very useful. This application enables you to calculate discounts on a product or total purchases. You only need to enter the product price and discount amount through dialog boxes, and the application will provide the total amount after the discount.

4. Student Grading:

This feature will help educators or students calculate final grades based on grading components. You can enter values for assignments, exams, and projects through dialog boxes, and the application will calculate the final grade according to the specified weights.

5. Exit:

Finally, this application also has an option to exit. If you have finished using the application, you can choose this option to comfortably close the application.

With the help of the JOptionPane class, this application offers an interactive and user-friendly experience. You can use the various provided features by following the instructions within the application. Whether you want to perform mathematical calculations, check numbers, calculate discounts, or grade students, this application is ready to assist you.

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:

Execution of the Program

Outputs:

1. Welcome Screen

Welcome Screen

2. Taking Input for Operation to be Performed

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.

Project4

Taking of Input 1 for Addition Operation

Taking Input 1 for Addition

Taking of Input 2 for Addition Operation

Taking Input 2 for Addition

4. Result of the Operations Performed

Result of the Operation



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads