Untitled

 avatar
unknown
java
5 months ago
4.6 kB
6
Indexable
import javax.swing.*;
import java.util.Scanner;

public class ConvertStringToInteger {

    public static void main(String[] args) {
        // Prompt the user to choose between GUI or Scanner
        String[] options = {"Use GUI", "Use Scanner"};
        int choice = JOptionPane.showOptionDialog(null,
                "Choose input method:",
                "Input Method Selection",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                options,
                options[0]);

        // Execute the selected method
        if (choice == 0) {
            // Use GUI
            showGUI();
        } else if (choice == 1) {
            // Use Scanner
            useScanner();
        } else {
            System.out.println("No option selected.");
        }
    }

    // Method to show GUI for input
    private static void showGUI() {
        // Create the frame
        JFrame frame = new JFrame("Pay Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 250);
        frame.setLayout(null);

        // Create labels and text fields
        JLabel hoursLabel = new JLabel("Hours worked:");
        hoursLabel.setBounds(10, 20, 150, 25);
        JTextField hoursField = new JTextField();
        hoursField.setBounds(160, 20, 100, 25);

        JLabel payRateLabel = new JLabel("Hourly pay rate:");
        payRateLabel.setBounds(10, 60, 150, 25);
        JTextField payRateField = new JTextField();
        payRateField.setBounds(160, 60, 100, 25);

        JLabel taxRateLabel = new JLabel("Tax rate (%):");
        taxRateLabel.setBounds(10, 100, 150, 25);
        JTextField taxRateField = new JTextField();
        taxRateField.setBounds(160, 100, 100, 25);

        // Create submit button
        JButton submitButton = new JButton("Calculate");
        submitButton.setBounds(90, 140, 120, 30);

        // Add action listener to the button
        submitButton.addActionListener(e -> {
            // Get values from text fields
            String stringHours = hoursField.getText();
            String stringPayRate = payRateField.getText();
            String stringTaxRate = taxRateField.getText();

            // Process the inputs
            try {
                int hours = Integer.parseInt(stringHours);
                double payRate = Double.parseDouble(stringPayRate);
                double taxRate = Double.parseDouble(stringTaxRate) / 100; // Convert to decimal

                // Calculate gross pay, tax amount, and net pay
                double grossPay = hours * payRate;
                double taxAmount = grossPay * taxRate;
                double netPay = grossPay - taxAmount;

                // Display results in the console
                System.out.println("You worked " + hours + " hours at ₱" + payRate + " per hour.");
                System.out.println("Gross Pay: ₱" + grossPay);
                System.out.println("Tax Amount: ₱" + taxAmount);
                System.out.println("Net Pay: ₱" + netPay);
            } catch (NumberFormatException ex) {
                System.out.println("Invalid input. Please enter numeric values.");
            }
        });

        // Add components to frame
        frame.add(hoursLabel);
        frame.add(hoursField);
        frame.add(payRateLabel);
        frame.add(payRateField);
        frame.add(taxRateLabel);
        frame.add(taxRateField);
        frame.add(submitButton);

        // Set frame visibility
        frame.setVisible(true);
    }

    // Method to use Scanner for input
    private static void useScanner() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("How many hours did you work this week? ");
        int hours = scanner.nextInt();

        System.out.print("What is your hourly pay rate? ");
        double payRate = scanner.nextDouble();

        System.out.print("What is your tax rate (in percentage)? ");
        double taxRate = scanner.nextDouble() / 100; // Convert to decimal

        // Calculate gross pay, tax amount, and net pay
        double grossPay = hours * payRate;
        double taxAmount = grossPay * taxRate;
        double netPay = grossPay - taxAmount;

        // Display results in the console
        System.out.println("You worked " + hours + " hours at ₱" + payRate + " per hour.");
        System.out.println("Gross Pay: ₱" + grossPay);
        System.out.println("Tax Amount: ₱" + taxAmount);
        System.out.println("Net Pay: ₱" + netPay);

        // Close the scanner
        scanner.close();
    }
}
Editor is loading...
Leave a Comment