Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I need a little bit help about finalCost += cost; it is not working even if i placed it somewhere possible

+3 votes
asked Oct 8, 2022 by Lexus Guevara (1,010 points)
edited Oct 8, 2022 by Lexus Guevara
I also tried making a method like repeatOrder(){ just to fix the problem, still im a little bit confused why it is not still getting the value once i repeated the order. I want to put the first cost in finalCost then add again if the user wants to put another product. thank you in advance for helping me!

import java.util.Scanner;

public class Act04_BeautySaloon {

    

Scanner s = new Scanner(System.in);

    public class Customer{

        public class getProduct{

        public String[] products = new String[]{"brush", "makeup", "perfume", "skincare", "cologne"};;

        public double[] prices = new double[]{20.00, 50.00, 10.00, 5.00, 3.00};

        boolean orderPlaced = false;

        double cost;

        double finalCost;

        int ans;

        

            public void placeOrder(String productName, int quantity){

           

                for(int i = 0; i < products.length; i++){

                    if(products[i].equals(productName)){

                        cost = prices[i] * quantity;

                        

                        System.out.println("The cost of " + quantity + " " + productName + " product(s) is : $" + cost + ".");

                        finalCost += cost;

                        System.out.println("Is there anything you want to buy? Please type 1 if Yes and 2 if No.");

                        ans = s.nextInt();

                    if(ans  == 1) {

                    getProduct gP = new getProduct();

                    gP.selectProduct();

                   

                    }

                    if(ans == 2) {

                    orderPlaced = true;

                   

                            System.out.println("The cost of your purchased products is $" + finalCost);

                    }

                    //getProduct gP = new getProduct();

                //gP.repeatPurchase();

                    }

                }

            }

            

            /*public void repeatPurchase() {

           

           

            }*/

public void selectProduct() {

System.out.println("Type the products then put the quantity of the product that you want to buy");

            String prdName = s.next();

            int qty = s.nextInt();

            getProduct gP = new getProduct();     

            gP.placeOrder(prdName, qty);

            

}

}

       

        public void welcomeCustomer(){

       

        System.out.println("Good Day! Welcome to our Beauty Saloon.");

            System.out.println("- Type 1 if you want to buy PRODUCTS");

            System.out.println("- Type 2 if you want SERVICES");

            System.out.println("- Type 3 if BOTH");

            int option = s.nextInt();

                

            switch (option) {

                case 1:

                    System.out.println("PRODUCTS: ");

                    System.out.println("1. brush ");

                    System.out.println("2. make up");

                    System.out.println("3. perfume");

                    System.out.println("4. skincare");

                    System.out.println("5. cologne");

                    getProduct gP = new getProduct();

                    gP.selectProduct();

                break;

               // }

               // case 2 -> {

               // }

               // case 3 -> {

               // }

                default:

                System.out.println("INVALID INPUT!");

                

            }

        }

    

    }

    

    

    public class Discount{

        

        private static double sDcPremium = 0.2;

        private static double sDcGold = 0.15;

        private static double sDcSilver = 0.1;

        private static double pDcPremium, pDcGold, pDcSilver = 0.1;

        

        public static double get_pDcRate(String type){

            switch(type){

                case "Premium" -> {

                    return pDcPremium;

                }

                case "Gold" -> {

                    return pDcGold;

                }

                case "Silver" -> {

                    return pDcSilver;

                }

                default ->{

                    throw new IllegalArgumentException("Wrong membership type!");

                }

            }

        }

        

        public static double get_sDcRate(String type){

            switch(type){

                case "Premium" -> {

                    return sDcPremium;

                }

                case "Gold" -> {

                    return sDcGold;

                }

                case "Silver" -> {

                    return sDcSilver;

                }

                default ->{

                    throw new IllegalArgumentException("Wrong membership type!");

                }

            }

        }

    }

    

    public class Visit{

   

        

    }

    public static void main(String[] args) {

        Act04_BeautySaloon bs = new Act04_BeautySaloon();

        Act04_BeautySaloon.Customer c = bs.new Customer();

        c.welcomeCustomer();

    }

}

1 Answer

0 votes
answered Oct 9, 2022 by Peter Minarik (86,160 points)

Your code does not compile. It took me quite a while to make it compile in OnlineGDB. It would be easier to help if they could focus on the problem in question (and not fixing other things first).

I found that you often instantiate the getProduct class (, which has a name, as if it would be a function, so pretty misleading -- classes should have nouns for names), yet you already have an instance.

Because of this, you have many instances of the getProduct class and instances do not share states (such as the cost).

I'd recommend keeping only one instance of the getProduct class in the Act04_BeautySaloon class. Do not instantiate the getProduct class multiple times.

Good luck! :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...