So I have a homework assignment where a program needs to be designed to allow the user to input the Number of bags sold (of coffee) as well as the Weight per bag and output the information in a table as I've shown in my code and as I've tried to outline below:
Coffee Table
Number of bags sold Weight per Bag Price per pound Total Price Price with Tax
You have to calculate the total price using Total price= (unit weight) * num of units sold * 5.99
You calculate price with tax using = (total price) + (total price) * 0.0725
The only problem I'm having is that she's given use a table of values for Number of bags sold and weight per bag (that the user should be able to enter) and I know I have to incorporate a loop so that the values successfully output into the table. Here's my code:
main method
import java.util.*; public class CalcMain { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); double weight, units, totalPrice, taxPrice, pricePerPound; System.out.println("\t\t\t\t" + "Coffee Table"); System.out.println("Enter Weight of Bag: "); weight = scanner.nextDouble(); System.out.println("Enter number of units: "); units = scanner.nextDouble(); Calculator calculator = new Calculator(); totalPrice = calculator.getTotalPrice(weight, units); pricePerPound = totalPrice/weight; taxPrice = calculator.getTaxPrice(totalPrice); System.out.println("Number of bags sold" + "\t" + "Weight per bag" + "\t" + "Price per pound" + "\t" + "Total Price" + "\t" + "Price with tax"); System.out.println(units + "\t\t\t" + weight + "\t\t" + "$5.99" + "\t\t" + totalPrice + "\t\t" + taxPrice); } }
calculator method
public class Calculator { //set global variables public static final double PRICE = 5.99; public static final double TAX = 0.0725; // instance variables - replace the example below with your own private double totalPrice; private double taxPrice; /** * Constructor for the calculator setting total/tax prices at 0. */ public Calculator() { // initialise instance variables totalPrice = 0.0; taxPrice = 0.0; } /** * getTotalPrice and getTaxPrice methods */ public double getTotalPrice(double weight, double units){ totalPrice = weight * units * PRICE; return totalPrice; } public double getTaxPrice(double totalPrice){ taxPrice = totalPrice + (totalPrice * TAX); return taxPrice; } }
I'm not sure in the least bit, how to incorporate a loop to allow multiple inputs of # of bags sold and Weight per bag and have it output as I've designated in the code. Could anyone possibly clue me in? I understand basic for-and-while loops.