Reputation: 1
I am practicing out of a book, self study. It is blowing my mind. If someone could help me ?
How do I subtract the amount of the first, second and third bag as well as collect the total number of bag?
The project asks these criteria > Assignment details The company sells coffee only in 2 pound bags. The price for each bags is 5.50. When a customer places an order, they are shipped the coffee in boxe. The boxes come in three sizes. Large(Contains 20 bags, medium(contains 10 bags, and small(contains 5 bags).
The cost of a large box is $1.80 medium $1.00 small $0.60 The order is shipped in the least expensive manner, for examples. the fule for packing is to fill the large and medium boxes completely. that is, the box is fuly packed. Only the small boxes can have empty spaces. but that would not leave the third box fully packed. Develop a program that computes the total cost of an order. Display the following format:
Number of bags ordered: 52 - $ 286.00
Boxes used 2 Large - 3.60 1 medium - 1.00 1 small - 0.60
Your total cost is: $291.20
sample code
import javax.swing.*;
import java.lang.*;
import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
import java.math.*;
public class CoffeeBags {
public static void main(String [] args) throws IOException
{
DecimalFormat df = new DecimalFormat ("#,##0.00");
BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));
int numberOfBags;
int largeBags = 2;
int mediumBags = 1;
int smallBags = 1;
double costOfLargeBags = 3.60;
double costOfMediumBags = 1.00;
double costOfSmallBags = 0.60;
double costPerBag = 5.50;
double totalCost;
double totalCostWithBags;
System.out.print("Enter number of bags to order: ");
String numberOfBagsStr = bufReader.readLine();
numberOfBags = Integer.parseInt(numberOfBagsStr);
totalCost = numberOfBags * costPerBag;
System.out.println("Number of bags ordered: " + numberOfBags + " - $" + df.format(totalCost));
System.out.println("Boxes used:");
System.out.println(" " + largeBags + " Large - $" + df.format(costOfLargeBags));
System.out.println(" " + mediumBags + " Medium - $" + df.format(costOfMediumBags));
System.out.println(" " + smallBags + " Small - $" + df.format(costOfSmallBags));
//System.out.print("Your total cost is: $ " + (totalCostWithBags));
//String numberOfUnitsStr = bufReader.readLine();
//numberOfUnits = Integer.parseInt(numberOfUnitsStr);
System.out.println("\nPress any key to continue. . .");
System.exit(0);
}
}
Upvotes: 0
Views: 488
Reputation: 342
I came up with the following code that achieves your goal. Using ints to round down is very useful in this situation. You also need to name your variables correctly. They are bags going into boxes, not bags going into bags.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class CoffeeBags {
public static void main(String [] args) throws IOException {
DecimalFormat df = new DecimalFormat ("#,##0.00");
BufferedReader bufReader = new BufferedReader(
new InputStreamReader(System.in));
int numberOfBags = 0;
int numberOfBoxes = 0;
int numberLargeBoxes = 0;
int numberMediumBoxes = 0;
int numberSmallBoxes = 0;
double costOfLargeBox = 1.80;
double costOfMediumBox = 1.00;
double costOfSmallBox = 0.60;
double totalCostLargeBoxes;
double totalCostMediumBoxes;
double totalCostSmallBoxes;
double totalBoxCost;
double costPerBag = 5.50;
double totalBagCost;
double totalCostWithBags;
System.out.print("Enter number of bags to order: ");
String numberOfBagsStr = bufReader.readLine();
try {
numberOfBags = Integer.parseInt(numberOfBagsStr);
} catch (NumberFormatException e) {
System.out.println("Error: Enter digits only");
}
totalBagCost = numberOfBags * costPerBag;
if (numberOfBags > 20) {
numberLargeBoxes = numberOfBags/20;
}
if (numberOfBags - (numberLargeBoxes*20) < 20 || numberLargeBoxes == 0) {
numberMediumBoxes = (numberOfBags - (numberLargeBoxes*20))/10;
}
if (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) < 10 ||
numberLargeBoxes == 0 || numberMediumBoxes == 0) {
numberSmallBoxes = (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10))/5;
}
if (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) -
(numberSmallBoxes*5) < 5 && numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) -
(numberSmallBoxes*5) > 0 || numberLargeBoxes == 0 || numberMediumBoxes == 0) {
numberSmallBoxes++;
}
totalCostLargeBoxes = numberLargeBoxes*costOfLargeBox;
totalCostMediumBoxes = numberMediumBoxes*costOfMediumBox;
totalCostSmallBoxes = numberSmallBoxes*costOfSmallBox;
totalBoxCost = totalCostLargeBoxes + totalCostMediumBoxes + totalCostSmallBoxes;
totalCostWithBags = totalBoxCost + totalBagCost;
System.out.println("Number of bags ordered: " + numberOfBags + " - $" + df.format(totalBagCost));
System.out.println("Boxes used: ");
System.out.println(" " + numberLargeBoxes + " Large - $" + df.format(totalCostLargeBoxes));
System.out.println(" " + numberMediumBoxes + " Medium - $" + df.format(totalCostMediumBoxes));
System.out.println(" " + numberSmallBoxes + " Small - $" + df.format(totalCostSmallBoxes));
System.out.println("Your total cost is: $ " + df.format(totalCostWithBags));
}
}
Upvotes: 1
Reputation: 713
You're misunderstanding the exercise, based on your variable names. You have -boxes- which can fit 20/10/5 bags, depending on the size; large and medium boxes must be full, small ones do not need to be.
So you take the whole number of bags, divide by 20, put that many in large boxes; then take the remainder, divide by 10, then the remainder of that goes into small boxes.
Look here for operators; dividing an int
by another int
will always round down.
Upvotes: 2