-1

I still am a student and still learning java and I just recently learned how if...else... works but I don't know how to properly use it. How do I compute and display the amount of the total price using the if...else....

The problem:

A rice store nearby is on sale. All rice is sold at 10% (.1) off per kilo if the customer buys at least 11 kilos. For anything less than that, the regular price applies.

Here is the list of prices:

Lion - Php48/kilo

Ganador - Php50/kilo

Conchita - Php47/kilo

Other types of rice - Php45/kilo

Let the user input the rice purchased and the kilos and then compute and display the total amount the customer needs to pay.

Example: If a customer buys 3 kilos of Lion rice, your program is expected to only compute 3 kilos with the regular price.

Note: Customers may buy rice in any quantity, even half a kilo.

My code:

import java.util.Scanner;

public class Main
{
    public static void main (String[] args) 
    {
        System.out.println("List of prices: ");
        System.out.println("Lion - Php48/kilo");
        System.out.println("Ganador - Php50/kilo");
        System.out.println("Conchita - Php47/kilo");
        System.out.println("Other types of rice - Php45/kilo");
        System.out.println(" ");
        
        Scanner input = new Scanner(System.in);
        System.out.println("Type of rice: ");
        String rice = input.nextLine();
        System.out.println(" ");

        System.out.println("Kilos of rice: ");
        int kilo = input.nextInt();
        System.out.println(" ");
        
        System.out.println("Type of rice: " +rice);
        System.out.println("Kilos of rice: " +kilo);
        
        if(kilo>11)
        {
            System.out.println("You have a discount of 10%.");
        }
        else if(kilo<11)
        {
            System.out.println("You have no discount of 10%.");
        }
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
emjay04
  • 3
  • 1
  • What exactly is your problem? Even though your code is incomplete, I can see these problems with your code: 1. It should be `>= 11` rather than `> 11` since for exactly 11 kilos you get a discount as well. 2. `kilo` should be of type `double` since the customer may pick fractional amounts. – Daniel Junglas Dec 10 '21 at 07:52

2 Answers2

1

The problem statement says that any amount of rice can be bought, even half a kilo. Computers cannot accept vulgar fractions, like ½, but they can accept decimal fractions so the type for variable kilo should be double (and not int).

Think how you would do the calculation. You need to get the price for the type of rice that the user entered. I would use a switch statement or a Map but since you may not have already learned those and since you state that this is an exercise in using if...else, the below code uses if...else to get the price for the requested type of rice.

After you get the price, you simply multiply by the entered amount. Now you apply the discount. A discount of 10% means that you multiply the price by 0.9.

Here is the code.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("List of prices: ");
        System.out.println("Lion - Php48/kilo");
        System.out.println("Ganador - Php50/kilo");
        System.out.println("Conchita - Php47/kilo");
        System.out.println("Other types of rice - Php45/kilo");
        System.out.println(" ");

        Scanner input = new Scanner(System.in);
        System.out.println("Type of rice: ");
        String rice = input.nextLine();
        System.out.println(" ");

        System.out.println("Kilos of rice: ");
        double kilo = input.nextDouble();
        System.out.println(" ");

        System.out.println("Type of rice: " + rice);
        System.out.println("Kilos of rice: " + kilo);
        int price;
        if ("Lion".equals(rice)) {
            price = 48;
        }
        else if ("Ganador".equals(rice)) {
            price = 50;
        }
        else if ("Conchita".equals(rice)) {
            price = 47;
        }
        else {
            price = 45;
        }
        double cost = price * kilo;

        if (kilo >= 11) {
            System.out.println("You have a discount of 10%.");
            cost *= 0.9;
        }
        else {
            System.out.println("You have no discount of 10%.");
        }
        System.out.println("Cost: " + cost);
    }
}

Note that you need to compare strings using method equals. Refer to How do I compare strings in Java?

Here is output of a sample run of the above code.

List of prices: 
Lion - Php48/kilo
Ganador - Php50/kilo
Conchita - Php47/kilo
Other types of rice - Php45/kilo
 
Type of rice: 
Ganador
 
Kilos of rice: 
11.5
 
Type of rice: Ganador
Kilos of rice: 11.5
You have a discount of 10%.
Cost: 517.5
Abra
  • 19,142
  • 7
  • 29
  • 41
0

The key to this problem is utilizing conditional(e.g:if) statements or a HashMap to determine the value of the type of rice the customer selected.

  1. Step 1: determine the type of rice.

  2. Step 2: determine the price of the rice from Step 1

  3. Step 3: determine whether or not the quantity of rice the customer selected is >=11 kilos

  4. Step 4: if Step 3 is true, then apply a 90% discount on the totalPrice

Please also consider that if any other type of rice is provided the default price is 45.

Code Example below: utilizing a HashMap.

public class Main {
    public static void main (String[] args) 
    {
        //declaring local variables 
        int priceOfOtherTypesOfRice = 45;
        double discountValue = 0.9;
        HashMap<String,Integer> ricePriceMap  = new HashMap<>();
        ricePriceMap.put("Lion", 48);
        ricePriceMap.put("Ganador", 50);
        ricePriceMap.put("Conchita", 47);
        double totalPrice =0.0;             
        
        System.out.println("List of prices: ");
        System.out.println("Lion - Php48/kilo");
        System.out.println("Ganador - Php50/kilo");
        System.out.println("Conchita - Php47/kilo");
        System.out.println("Other types of rice - Php45/kilo");
        System.out.println(" ");
        
        Scanner input = new Scanner(System.in);
        System.out.println("Type of rice: ");
        String rice = input.nextLine();
        System.out.println(" ");

        System.out.println("Kilos of rice: ");
        int kilo = input.nextInt();
        System.out.println(" ");
        
        System.out.println("Type of rice: " +rice);
        System.out.println("Kilos of rice: " +kilo);
        if(kilo>11)
        {
            System.out.println("You have a discount of 10%.");
            totalPrice  =kilo * ((discountValue)*ricePriceMap.getOrDefault(rice, priceOfOtherTypesOfRice));
        }
        else if(kilo<11)
        {
            System.out.println("You have no discount of 10%.");
            totalPrice  =kilo * ricePriceMap.getOrDefault(rice, priceOfOtherTypesOfRice);
        }
        System.out.println("Final total cost of rice is " + totalPrice);

    }
}

Console output: [1]: https://i.stack.imgur.com/0sfa1.png

Mayyar Al-Atari
  • 199
  • 1
  • 1
  • 9