Получение правильных выпадающих списков в цикле - PullRequest
0 голосов
/ 08 апреля 2020

Я пытаюсь создать программу, в которой, когда пользователь нажимает лекарство определенного типа, например «Электраль», он предлагает пользователю ввести необходимые значения, а затем при следующем подсчете он снова показывает раскрывающийся список. до тех пор, пока не истечет счет; это заканчивается таблицей. Когда я запустил его, он показал мне выпадающие списки в странных местах.

import javax.swing.JOptionPane;

public class MedStore {

    public static void main(String[] args) {
        String inputStr;
        String outputStr;
        int count;

        inputStr = JOptionPane.showInputDialog("Enter number of medicines in "
                + "transaction today: ");

        count = Integer.parseInt(inputStr);
        int NoOfUnitsBought[] = new int[count];
        int NoOfUnitsSold[] = new int[count];
        int GST[] = new int[count];
        int ProfitMargin[] = new int[count];
        double SellingPrice[] = new double[count];
        double Profit[] = new double[count];
        double CP[] = new double[count];
        String MedType = MedTypeMenu();
        int CostPrice = CP(MedType);

        for (int i = 0; i < count; i++) {
            MedTypeMenu();
            CP(MedType);

            inputStr = JOptionPane.showInputDialog("Enter number of units of " + 
                    MedType + " bought: ");
            NoOfUnitsBought[i] = Integer.parseInt(inputStr);
            if (NoOfUnitsBought[i] < 0) {
                JOptionPane.showInputDialog("Error 02: Number of units bought "
                        + "cannot be below 0!");
                inputStr = JOptionPane.showInputDialog(
                        "Re-enter number of units of " + MedType + " bought: ");
                NoOfUnitsBought[i] = Integer.parseInt(inputStr);
            }
            inputStr = JOptionPane.showInputDialog("Enter number of units of " + 
                    MedType + " sold: ");
            NoOfUnitsSold[i] = Integer.parseInt(inputStr);
            if (NoOfUnitsBought[i] < 0) {
                JOptionPane.showInputDialog("Error 03: Number of units sold "
                        + "cannot be below 0!");
                inputStr = JOptionPane.showInputDialog(
                        "Re-enter number of units of " + MedType + " sold: ");
                NoOfUnitsSold[i] = Integer.parseInt(inputStr);
            }

            inputStr = JOptionPane.showInputDialog("Enter GST Percentage: ");
            GST[i] = Integer.parseInt(inputStr);
            if (GST[i] < 0 && GST[i] > 100) {
                JOptionPane.showInputDialog("Error 04: GST % Cannot be "
                        + "below 0 or above 100");
                inputStr = JOptionPane.showInputDialog(
                        "Re-enter GST percentage: ");
                GST[i] = Integer.parseInt(inputStr);
            }

            inputStr = JOptionPane.showInputDialog(
                    "Enter profit margin amount desired in INR: ");
            ProfitMargin[i] = Integer.parseInt(inputStr);

            CP[i] = calculateCP(CostPrice, NoOfUnitsBought[i]);
            SellingPrice[i] = calculateSP(CostPrice, NoOfUnitsBought[i], 
                    NoOfUnitsSold[i], GST[i], ProfitMargin[i]);
            Profit[i] = calculatePROFIT(SellingPrice[i], CP[i]);

        }

        outputStr = "<html><style>table{width:100%;border-spacing: 0px;}"
                + "th{font-weight: bold;border:1px solid black}"
                + "td{text-align: center;border:1px solid black}</style>"
                + "<body><table><tr><th>Name </th><th>Cost Of 1 Unit "
                + "</th><th> Number bought </th><th>Number Sold "
                + "</th><th>GST </th></th>Margin "
                + "</th><th>CP </th><th>SP "
                + "</th><th>Profit </th></tr>";
        for (int j = 0; j < count; j++) {
            outputStr += "<tr><td>" + MedType + 
                    "</td><td>" + "INR " + CostPrice + 
                    "</td><td>" + NoOfUnitsBought[j] + 
                    "</td><td>" + NoOfUnitsSold[j] + 
                    "</td><td>" + GST[j] + "%" + 
                    "</td></td>" + "INR " + ProfitMargin[j] + 
                    "</td></td>" + "INR " + CP[j] + 
                    "</td></td>" + "INR" + SellingPrice[j] + 
                    "</td></td>" + "INR" + Profit[j] + "</td></tr>";
        }

        JOptionPane.showMessageDialog(null, 
                outputStr, "Medical Expenses", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }

    public static String MedTypeMenu() {
        Object[] options = {"Electral", "Azithromycin", "Ibugesic+", "Soframycin"};
        Object selectionObject = JOptionPane.showInputDialog(null, 
                "Choose the medicine name", "Input Medicine Name", 
                JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
        String selectionString = selectionObject.toString();
        return selectionString;
    }

    public static int CP(String MedType) {
        int CostPrice = 0;

        if (MedType.equals("Electral")) {
            CostPrice = 15;
        } else if (MedType.equals("Azithromycin")) {
            CostPrice = 50;
        } else if (MedType.equals("Ibugesic+")) {
            CostPrice = 14;
        } else if (MedType.equals("Soframycin")) {
            CostPrice = 24;
        } else {
            JOptionPane.showInputDialog("Error");
        }

        return CostPrice;
    }

    static double calculateSP(int CostPrice, int NoOfUnitsBought, 
            int NoOfUnitsSold, int GST, int ProfitMargin) {
        double SellingPrice = (CostPrice * NoOfUnitsSold * (1 + (GST / 100)) + 
                ProfitMargin);

        return SellingPrice;
    }

    static double calculateCP(int CostPrice, int NoOfUnitsBought) {
        double CP = (CostPrice * NoOfUnitsBought);
        return CP;
    }

    static double calculatePROFIT(double SellingPrice, double CP) {
        double Profit = SellingPrice - CP;
        return Profit;
    }
}
...