1

I'm doing a train ticket program. I want to convert certain text in the string array to a certain price using JOptionPane.

ex.

String[] option_1 = {"location1","location2", "location3","location4","location5"};

where location1 = $10, location2 = $23, etc.

all I know is I could use Interger.parseInt or double, but I don't know where to go next. Should I go with a loop or make a whole new array and make it equal to the string array. I'm wondering if there is a more easier method to execute this.

code :

public static void main (String [] args) {
        String name;
                    
        String[] option_1 = {"North Avenue","Quezon Avenue", "Kamuning","Cubao","Santolan","Ortigas","Shaw","Boni","Guadalupe","Buendia","Ayala","Magallanes","Taft"};
        String[] option_2 = {"North Avenue","Quezon Avenue", "Kamuning","Cubao","Santolan","Ortigas","Shaw","Boni","Guadalupe","Buendia","Ayala","Magallanes","Taft"};
        
        name = JOptionPane.showInputDialog("Welcome to DME Ticketing System!\n\nEnter your name:");
        String leave = (String)JOptionPane.showInputDialog(null, "Leaving from", 
                "Train Station", JOptionPane.QUESTION_MESSAGE, null, option_1, option_1[0]);
        
        String  going = (String)JOptionPane.showInputDialog(null, "Leaving from", 
                "Train Station", JOptionPane.QUESTION_MESSAGE, null, option_2, option_2[0]);
       // int  pay = (Integer)JOptionPane.showInputDialog(null, "From: "+leave+"\nTo: "+going+"\nFare Price: "+"\n\nEnter your payment amount:", 
           // null, JOptionPane.PLAIN_MESSAGE, null, null,null);
   
       // int op1 = Integer.parseInt(going); 
       // int op2 = Integer.parseInt(leave);         
        

        JOptionPane.showMessageDialog(null, "DME Ticketing System\nMalolos, Bulacan\n\n"
                + "Name: "+name
                +"\nLocation: "+leave
                +"\nDestination: "+going
                +"\nFare: "
                +"\nPayment: "//+pay
                +"\nChange: "
                , "RECEIPT",JOptionPane.INFORMATION_MESSAGE);
    }

-The codes I turned into comments are giving me errors

3
  • 1
    I believe this will help: Creating a GUI With JFC/Swing Commented Jul 1, 2021 at 18:26
  • Is the user entering the payment amount? I am asking this because in your comments seems like there is a JOptionPane requesting the payment from the user. If, on the other hand, you are trying to map the locations (Strings) to their value (ints), then a HashMap<String,Integer> should suffice (or maybe some arrays, or lists). Commented Jul 2, 2021 at 12:52
  • Yes, the user is going to be entering a payment amount because I'll be calculating the change in the receipt. And Thanks, I'll be trying those methods out. Commented Jul 2, 2021 at 14:10

1 Answer 1

1

showInputDialog always returns the input of the user as a String object, except from a variation where it returns the selected Object from the supplied ones. I am talking for Java version 8 for the sake of example.

Based on your question and comments, you basically want to:

  1. Match String objects (their name) to integers (their value), so that you can calculate how much they will have to pay, plus the change afterwards.
  2. Receive as input from the user the amount they will give, in order to calculate the change.

If so, then:

  1. For the first case you can use some data structure(s) from which you will be able to match the locations with their value. For example a HashMap<String, Integer> can match the locations with their value, like so:
//Set up the origin options, along with their value:
HashMap<String, Integer> leavingOptions = new HashMap<>();
leavingOptions.put("North Avenue", 10);
leavingOptions.put("Quezon Avenue", 11);
leavingOptions.put("Kamuning", 12);
leavingOptions.put("Cubao", 13);
leavingOptions.put("Santolan", 14);
leavingOptions.put("Ortigas", 15);
leavingOptions.put("Shaw", 16);
leavingOptions.put("Boni", 17);
leavingOptions.put("Guadalupe", 18);
leavingOptions.put("Buendia", 19);
leavingOptions.put("Ayala", 20);
leavingOptions.put("Magallanes", 21);
leavingOptions.put("Taft", 22);

//Get user input for origin:
Object[] leavingOptionsArray = leavingOptions.keySet().toArray();
String leaving = (String) JOptionPane.showInputDialog(null, "Leaving from:", "Train Station", JOptionPane.QUESTION_MESSAGE, null, leavingOptionsArray, leavingOptionsArray[0]);

//Show output depending on user's input or cancel:
if (leaving != null)
    JOptionPane.showMessageDialog(null, "You shall pay: " + leavingOptions.get(leaving));
else
    JOptionPane.showMessageDialog(null, "Quiting...");

(note here that the casting of the returned value of the showInputDialog to a String reference is safe, just because the way we set up the options' array to contain only String objects)

Or you can use a String array with the locations along with an int array with the corresponding values, like so:

//Set up the origin options, along with their price:
String[] leavingOptions2 = new String[]{"North Avenue","Quezon Avenue", "Kamuning","Cubao","Santolan","Ortigas","Shaw","Boni","Guadalupe","Buendia","Ayala","Magallanes","Taft"};
int[] price = new int[]{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22};

//Get user input for origin:
Object leaving2 = JOptionPane.showInputDialog(null, "Leaving from:", "Train Station", JOptionPane.QUESTION_MESSAGE, null, leavingOptions2, leavingOptions2[0]);

//Show output depending on user's input or cancel:
if (leaving2 != null) {

    //Find the price of the selected location:
    int p = -1;
    for (int i = 0; i < price.length; ++i)
        if (leaving2.equals(leavingOptions2[i])) {
            p = price[i];
            break;
        }

    JOptionPane.showMessageDialog(null, "You shall pay: " + p);
}
else
    JOptionPane.showMessageDialog(null, "Quiting...");

Or even better, you can use another object type other than String for the options array, for example a class named Location which will have the name and the price as properties, the toString method of which will return the name.

  1. For the second part, obtain the user's input as a String from a JOptionPane and use Integer.parseInt(...) method on the user's input so as to be able to calculate the change afterwards, like so:
String paymentString = JOptionPane.showInputDialog(null, "Enter payment amount:", "Payment", JOptionPane.QUESTION_MESSAGE);

if (paymentString != null) {
    try {
        int payment = Integer.parseInt(paymentString);
        JOptionPane.showMessageDialog(null, "You chose to pay: " + payment);
        //Calculate change here...
    }
    catch (final NumberFormatException exception) {
        JOptionPane.showMessageDialog(null, "The input was invalid (not an 'int').");
    }
}
else
    JOptionPane.showMessageDialog(null, "Quiting...");

Remember that parseInt will throw a NumberFormatException (which is a RuntimeException) if its input String is not an int, so you will have to check for that using a try-catch block.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.