在餐厅OOP计划中添加菜单小计

时间:2016-03-13 01:02:26

标签: java oop

好吧,所以我已经有点陷入这个问题,你需要得到小计并添加它们,但每当我输入它们时,我最终得到最后输入的值。

示例输入: 1,早餐 然后 1-59.00 2,无 (返回bfast菜单) 3-35.00 (应该是59 + 35) 然后退出,然后当我输入现金并获得更改时,我总是得到35.00输入。

package finalsProject;

import java.text.DecimalFormat;
import javax.swing.*;
/* Program Name: Menu Bar
 * Program Version: 1.2b
 * Programmed By: Lance Cyril L. Luchico
 * Modified By: Ivan Aldwin A. Cristobal
 * 
 * This Program enables the user to select from a wide variety
 * of food choices using the provided menus in the program.
 * The program is created using OOP, and is divided into 4 methods.
 * First-Breakfast, Second-Lunch, Third Dinner, and fourth, The Checkout.
 * 
 * This Program has its parts interchanged from two menu programs
 * for a better functionality.
 * 
 */

public class Menu {

    static float  sub , vat = 0.12f , total , pay , change ;
    static int meal , choice , order , add, addorders, comporders ;
    static DecimalFormat df=new DecimalFormat("#.00");

    public static void showMenu(){


    {
        meal = Integer.parseInt(JOptionPane.showInputDialog(null,"Mcdonalds"
                + "\nChoose your Meal"
                + "\n[1] Breakfast"
                + "\n[2] Lunch"
                + "\n[3] Dinner"));

        if (meal == 1)
            Breakfast(meal);
        else if (meal == 2)
            Lunch(meal);
        else if (meal == 3)
            Dinner(meal);
    }
}
    public static void Breakfast(int meal){
        choice = Integer.parseInt(JOptionPane.showInputDialog(null,"Breakfast Choices"
                + "\n[1] Hamburger Mcdo     \t59.00"
                + "\n[2] Breakfast Deluxe   \t99.00"
                + "\n[3] Chicken Muffin     \t35.00"
                + "\n[4] Egg McMuffin       \t49.00"
                + "\n[5] Apple Pie          \t39.00"));

        order = Integer.parseInt(JOptionPane.showInputDialog(null,"How many orders?"));


        if (choice == 1){
            sub = 59 * order;
        }
        else if (choice == 2){
            sub = 99 * order;
        }
        else if (choice == 3){
            sub = 35 * order;
        }
        else if (choice == 4){
            sub = 49 * order;
        }
        else if (choice == 5){
            sub = 39 * order;
        }

        addorders = Integer.parseInt(JOptionPane.showInputDialog("Any Additional Orders?:"
                + "\n[1]Yes"
                + "\n[2]No"
                + "\nChoice: "));
        if(addorders == 1){
            showMenu();
        }
        else if(addorders == 2){
            comporders = Integer.parseInt(JOptionPane.showInputDialog("Orders Complete? "
                    + "\n[1]Yes"
                    + "\n[2]No"));

            if(comporders == 1)
                Checkout();
            if(comporders == 2)
                Breakfast(meal);
        }
    }
    public static void Lunch(int meal){
        choice = Integer.parseInt(JOptionPane.showInputDialog(null,"Lunch Choices"
                + "\n[1] Chicken McNuggets  \t59.00"
                + "\n[2] Double McSpicy     \t59.00"
                + "\n[3] French Fries       \t99.00"
                + "\n[4] McWings            \t79.00"
                + "\n[5] Coke               \t29.00"));

        order = Integer.parseInt(JOptionPane.showInputDialog(null,"How many orders?"));


        if (choice == 1){
            sub = 59 * order;
        }
        else if (choice == 2){
            sub = 59 * order;
        }
        else if (choice == 3){
            sub = 99 * order;
        }
        else if (choice == 4){
            sub = 79 * order;
        }
        else if (choice == 5){
            sub = 35 * order;
        }
        addorders = Integer.parseInt(JOptionPane.showInputDialog("Any Additional Orders?:"
                + "\n[1]Yes"
                + "\n[2]No"
                + "\nChoice: "));
        if(addorders == 1){
            showMenu();
        }
        else if(addorders == 2){
            comporders = Integer.parseInt(JOptionPane.showInputDialog("Orders Complete? "
                    + "\n[1]Yes"
                    + "\n[2]No"));

            if(comporders == 1)
                Checkout();
            if(comporders == 2)
                Lunch(meal);
        }
    }
    public static void Dinner(int meal){
        choice = Integer.parseInt(JOptionPane.showInputDialog("Dinner Choices"
                + "\n[1] Mocha Frappe       \t59.00"
                + "\n[2] Garden Side Salad  \t29.00"
                + "\n[3] McDouble           \t89.00"
                + "\n[4] McSpicy            \t99.00"
                + "\n[5] CheeseBurger       \t35.00"));

    order = Integer.parseInt(JOptionPane.showInputDialog("How many orders?"));

        if (choice == 1){
            sub = 59 * order;
        }
        else if (choice == 2){
            sub = 29 * order;
        }
        else if (choice == 3){
            sub = 89 * order;
        }
        else if (choice == 4){
            sub = 99 * order;
        }
        else if (choice == 5){
            sub = 35 * order;
        }

        addorders = Integer.parseInt(JOptionPane.showInputDialog("Any Additional Orders?:"
                + "\n[1]Yes"
                + "\n[2]No"
                + "\nChoice: "));
        if(addorders == 1){
            showMenu();
        }
        else if(addorders == 2){
            comporders = Integer.parseInt(JOptionPane.showInputDialog("Orders Complete? "
                    + "\n[1]Yes"
                    + "\n[2]No"));

            if(comporders == 1)
                Checkout();
            if(comporders == 2)
                Dinner(meal);
        }
    }

    public static void Checkout(){
        pay = Integer.parseInt(JOptionPane.showInputDialog("Enter your Money"));

        vat = sub * vat;
        total = sub + total;
        change = pay - total;

        if (pay > total){
            JOptionPane.showMessageDialog(null,"Subtotal : " + df.format(sub)
                    + "\nVAT : " + df.format(vat)
                    + "\nTotal : " + df.format(total)
                    + "\nCash : " + df.format(pay)
                    + "\nChange : " + df.format(change));
        }
        else if (pay < total){
            JOptionPane.showMessageDialog(null,"Unable to Continue Transaction, Insufficient Funds");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在为sub分配值,而不是添加它们而非执行

sub = ...

使用

sub += ...

这会将价格加到小计

在旁注上使所有方法保持静态基本上违背了OOP的目的。