Click here to Skip to main content
15,616,232 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that prompts the user to enter the weight of the package and the price of the
product, then display the shipping cost and total bill. If the weight is greater than 20, display a
message “the package cannot be shipped”. If the user input invalid numbers (negative numbers
for the weight and price), display the message “The weight/Price cannot be negative”.

Here is a sample run:
Please input the weight of the package (in pounds): 4
Please input the price of the product (in dollars): 10
The shipping cost (in dollars) is: 8.5
The total amount is (in dollars): 18.5

What I have tried:

I have used java to find the shipping cost section but no the total bill. I do not understand what to do for the total bill.

Where does the total bill go in this project?
Java
import java.util.Scanner;

public class PracticeLab3 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		// Prompt the user to enter the weight of the package
				System.out.print("Enter the weight of the package: ");
				double weight = input.nextDouble();

				// Calculate cost of shipping
				if (weight > 20)
					System.out.println("The package cannot be shipped.");
				else
				{
					double costPerPound; 
					if (weight > 0 && weight <= 1)
						costPerPound = 3.5;
					else if (weight <= 3)
						costPerPound = 5.5;
					else if (weight <= 10)
						costPerPound = 8.5;
					else //if (weight <= 20)
						costPerPound = 10.5;
					System.out.println("Shipping cost of package is $" +
						costPerPound * weight);
				}
			}
		
}
Posted
Updated 8-Feb-22 20:20pm
v2

Read the question carefully, and look at the sample you have been given - it tells you exactly what to do: get the price and weight from the user, then calculate the shipping cost shipping cost and print that, then add the price to that and print it as the total.

How you calculate the shipping cost isn't defined, but from the example, it's maybe $0.85 per kilo (which is spectacularly cheap).

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Your inputs lack of the price of the product. Try, for instance:
Java
import java.util.Scanner;
  
public class PracticeLab3 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
        
        // Prompt the user to enter the price of the product
        System.out.print("Enter the price of the product: ");
        double costProduct = input.nextDouble();
        // Prompt the user to enter the weight of the package
        System.out.print("Enter the weight of the package: ");
        double weight = input.nextDouble();

        // Calculate cost of shipping
        if (weight > 20)
          System.out.println("The package cannot be shipped.");
        else
        {
          double costPerPound;
          double costShipping;
          double costTotal;
          if (weight > 0 && weight <= 1)
            costPerPound = 3.5;
          else if (weight <= 3)
            costPerPound = 5.5;
          else if (weight <= 10)
            costPerPound = 8.5;
          else //if (weight <= 20)
            costPerPound = 10.5;
          costShipping = costPerPound * weight;
          System.out.println("Shipping cost of package is $" +
            costShipping);
          costTotal = costProduct + costShipping;
          System.out.println("Total amount is $" +
            costTotal);
        }
      }

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900