JAVA基本购物车计划

时间:2018-06-14 22:27:59

标签: java loops for-loop methods do-while

我正在尝试创建一个简单的购物车应用程序。问题在于最后显示的totalPricestartScanning()方法)。我试图让用户输入四个项目中的一个(因为do-while循环),并通过startScanning方法,我试图找出数量用户输入该项目的次数,因此稍后计算totalPrice。 此外,processItem方法用于根据项目提供折扣和税。 您能否帮我纠正for loops方法的startScanning,以便显示正确的totalPrice

import javax.swing.JOptionPane;

public class Shopping {
    static String itemScan;
    static String firstName; 
    static String surName;
    static int age;
    static double priceBanana = 0.75;
    static double priceApple = 0.75;
    static double priceMilk = 1.25;
    static double priceOrange = 1.50;
    public static String printWelcomeMessage () {
        firstName = JOptionPane.showInputDialog("Enter your name:");
        surName = JOptionPane.showInputDialog("Enter your surname:");
        String sAge = JOptionPane.showInputDialog("Enter your age:");
        age = Integer.parseInt(sAge);
            do {
            itemScan = JOptionPane.showInputDialog(surName + ", " + firstName + "("+age+"). Welcome to our supermarket! Start Scanning items. To stop, enter \'EXIT\'");
            }
        while (!itemScan.equalsIgnoreCase("Exit"));
            return itemScan;
    }
    public static double startScanning() {
        double newpriceBanana = 0;
        double newpriceMilk = 0;
        double newpriceApple = 0;
        double newpriceOrange = 0;
            for (int i = 0; itemScan.equalsIgnoreCase("Banana"); i++ ) {
                newpriceBanana = i*priceBanana;
            }
            for (int i = 0; itemScan.equalsIgnoreCase("Apple"); i++) {
                newpriceApple = i*priceApple;

            }
            for (int i = 0; itemScan.equalsIgnoreCase("Milk"); i++) {
                newpriceMilk = i*priceMilk;

            }
            for (int i = 0; itemScan.equalsIgnoreCase("Orange Juice"); i++) {
                newpriceOrange = i*priceOrange;
                } 


        double totalPrice = newpriceBanana + newpriceApple + newpriceMilk + newpriceOrange;
        return totalPrice;
    }
    public static void processItem() {
        // Note: I haven't considered orange juice as a fruit but a drink like milk.
        // Thus, no discount for orange juice.
        if (itemScan.equalsIgnoreCase("Apple") || itemScan.equalsIgnoreCase("Banana")) {
            priceBanana = priceBanana- (0.1*priceBanana);
            priceApple = priceApple- (0.1*priceApple);
        }
        else {
            priceMilk = priceMilk + (0.2*priceMilk);
            priceOrange = priceOrange + (0.2*priceOrange);
        }

    }

    public static String printReceipt() {
        String cardNumber = JOptionPane.showInputDialog("Please enter your 16-digit Credit Card Number:");
        int cardNum = Integer.parseInt(cardNumber.substring(12,16));
        String information =  "Printing Receipt...\n"+"Credit Card Number ends in: "+ cardNum + "\nTotal Amount: $"+ startScanning();

        return information;
    }
    public static void main(String[] args) {
    printWelcomeMessage();
    processItem();
    startScanning();
    JOptionPane.showMessageDialog(null, printReceipt());

    }
}

0 个答案:

没有答案