0

I'm trying to create a simple shopping cart application. The problem is with the totalPrice that is displayed in the end (startScanning() method). I'm trying to to make the user input one of the four items as many times as they want(thus the do-while loop) and through the startScanning method, I am trying to figure out the number of times the user entered that item and thus later on calculating the totalPrice. Also the processItem method is used to give discount and tax based on items. Can you help me correct the for loops for the startScanning method in order to display the correct 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());

    }
}
  • You've stated that you're having a problem, but I don't see where you've told us exactly what problem you're having. Also what steps have you taken to debug your code? If none, then please have a look at: [How to Debug Small Programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). It won't solve your direct problem, but it will give you steps that you can follow that should help you solve it yourself, or even if that is not successful, then at least help you to better isolate your problem so that your question can be more focused and easier to answer. – Hovercraft Full Of Eels Jun 14 '18 at 22:31
  • Your startScanning method ***returns*** the totalPrice information, but you're ignoring what is returned suggesting that you don't yet know how to handle information returned from a method, a most basic Java concept and suggests that you should review your notes/book/tutorials. You can find the tutorials here: [The Really Big Index](http://docs.oracle.com/javase/tutorial/reallybigindex.html) – Hovercraft Full Of Eels Jun 14 '18 at 22:34
  • Here's the tutorial that you want: [Returning values from a method](https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html) – Hovercraft Full Of Eels Jun 14 '18 at 22:36
  • I will look up what you suggested. And the problem I am having is that the totalPrice always displays $0.0 as the final result regardless of any input. – Premanshu Yadav Jun 14 '18 at 22:39
  • Regarding the `for` loops in the `startScanning()` method (which, by the way, doesn't appear to either **start** or **scan** anything...), can you explain 1) what were they _intended_ to do? and 2) what do they _actually_ do? and 3) why are you using them in preference to, say, some `if`s or a `switch`? – Kevin Anderson Jun 14 '18 at 22:49

0 Answers0