samedi 5 mars 2016

Java Loop Quit Failure

As a disclaimer, I'm a newbie to programming with java. I'm having an issue with quitting my program when it should quit. Once the user hits (Q)uit, it shows the quit dialog, but then shows the dialog as if an invalid selection was entered. To make sure the selection was staying the same, I even printed it at three different locations in the code and it stays Q, as it is supposed to. I seem to be missing something big, and I'm assuming it's something very technical about comparing Strings.

Program as follows:

import javax.swing.*;

public class SalonReport 
{
public static void main(String[] args)
{
    //Creates and stores Service array
    Service[] services = new Service[6];
    services[0] = new Service("Cut", 8.00, 15);
    services[1] = new Service("Shampoo", 4.00, 10);
    services[2] = new Service("Manicure", 18.00, 30);
    services[3] = new Service("Style", 48.00, 55);
    services[4] = new Service("Permanent", 18.00, 35);
    services[5] = new Service("Trim", 6.00, 5);

    //Asks user for sort selection and converts to upper case
    String selection = JOptionPane.showInputDialog(null, "Sort services by" + 
            "\n(S)ervice, (P)rice, or (T)ime" +
            "\nOr (Q) to Quit Program").toUpperCase();

    //Runs sortService method
    sortService(selection, services);
}

private static void sortService(String selection, Service[]services) 
{
    //Selects sorting option based on user selection
    switch(selection)
    {
    case "S":
    {
        //Builds string to show Services by service
        StringBuilder message = new StringBuilder("Sorted by Services");

        //Sorts services by service alphabetically
        int x;
        for (x = 0; x < services.length - 1; x++)
        {
            for (int y = services.length - 1; y >= x + 1; y--)
                if (services[y - 1].getService().charAt(0) > services[y].getService().charAt(0))
                {
                    Service temp = services[y - 1];
                    services[y - 1] = services[y];
                    services[y] = temp;
                }

            //Adds services sorted by service to string
            message.append("\n" + services[x].getService() + "     $" + 
                    services[x].getPrice() + "     " + services[x].getTime() + " minutes");
        }
        //Prints services sorted by service
        JOptionPane.showMessageDialog(null, message);

        //Requests user selection again
        selection = JOptionPane.showInputDialog(null, "Sort services by" + 
                "\n(S)ervice, (P)rice, or (T)ime" +
                "\nOr (Q) to Quit Program").toUpperCase();

        //runs through method again to sort by selection
        sortService(selection, services);
        break;
    }

    case "P":
    {
        //Builds string to show Services by price
        StringBuilder message = new StringBuilder("Sorted by Price");

        //Sorts services by service price descending
        int x;
        for (x = 0; x < services.length - 1; x++)
        {
            for (int y = services.length - 1; y >= x + 1; y--)
                if (services[y - 1].getPrice() > services[y].getPrice())
                {
                    Service temp = services[y - 1];
                    services[y - 1] = services[y];
                    services[y] = temp;

                }

            //Adds services sorted by price descending to string
            message.append("\n" + services[x].getService() + "     $" + 
                    services[x].getPrice() + "     " + services[x].getTime() + " minutes");
        }

        //Prints services sorted by price
        JOptionPane.showMessageDialog(null, message);

        //Requests user selection again
        selection = JOptionPane.showInputDialog(null, "Sort services by" + 
                "\n(S)ervice, (P)rice, or (T)ime" +
                "\nOr (Q) to Quit Program").toUpperCase();

        //Runs through method again to sort by selection
        sortService(selection, services);
        break;
    }

    case "T":
    {
        //Builds string to show Services by time
        StringBuilder message = new StringBuilder("Sorted by Time");

        //Sorts services by service time descending
        int x;
        for (x = 0; x < services.length - 1; x++)
        {
            for (int y = services.length - 1; y >= x + 1; y--)
                if (services[y - 1].getTime() > services[y].getTime())
                {
                    Service temp = services[y - 1];
                    services[y - 1] = services[y];
                    services[y] = temp;

                }

            //Adds services sorted by time descending to string
            message.append("\n" + services[x].getService() + "     $" + 
                    services[x].getPrice() + "     " + services[x].getTime() + " minutes");
        }

        //Prints services sorted by time
        JOptionPane.showMessageDialog(null, message);

        //Requests user selection again
        selection = JOptionPane.showInputDialog(null, "Sort services by" + 
                "\n(S)ervice, (P)rice, or (T)ime" +
                "\nOr (Q) to Quit Program").toUpperCase();

        //Runs through method again to sort by selection
        sortService(selection, services);
        break;
    }
    case "Q":
    {
        //Shows termination dialog and quits the program
        JOptionPane.showMessageDialog(null, "Program Terminated");
        break;
    }
    }
    //Checks whether user selection is valid
    if(selection != "S" || selection != "P" || selection != "T" || selection != "Q")
    {
        //Tells user why program failed to sort
        JOptionPane.showMessageDialog(null, "Invalid Entry\nTry Again");

        //Requests user sort selection again
        selection = JOptionPane.showInputDialog(null, "Sort services by" + 
                "\n(S)ervice, (P)rice, or (T)ime" +
                "\nOr (Q) to Quit Program").toUpperCase();

        //Runs sortServices method against user selection
        sortService(selection, services);
    }
}

}

Aucun commentaire:

Enregistrer un commentaire