I've been working on this project for the past couple of days. I can get the code to run, however, i'm suppose to be able to drop the java if I type -1 into the spot. I don't know how or where to place it. Here is the assignment and what I have so far:
1) Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and
then an integer quantity of units sold (this is two separate prompts for input values). You must
use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out
of range value, such as -1, is input). All 15 items below are for a single purchase. There are five
sets of inputs as follows:
Product 1 1 unit (cost is $2.98 per unit)
Product 2 2 units (cost is $4.50 per unit)
Product 3 3 units (cost is $9.98 per unit)
Product 4 4 units (cost is $4.49 per unit)
Product 5 5 units (cost is $6.87 per unit)
Your application must calculate and display the total retail value of all products sold, after all 5
pairs of inputs are completed. You must also display the total after each new pair of input values
is entered.
(This program was taken from Exercise 5.17 on page 228 of Deitel & Deitel's "Java How to
Program (Sixth Edition)" (2005 by Pearson Publishing Co.)
and here is the pseudocode we are offered as a potential guide:
6) Here is some pseudocode/skeleton Java code for one possible solution to the program to get you
started (this shows procedural code, but an object-oriented solution would have been better, since
Java is a pure object-oriented language):
import the classes you need
main
declare productNo and quantity variables
declare and initialize lineAmount and orderAmount variables
set up a String for your output via the Scanner class (or you may use the JTextArea
GUI component – this will require additional research beyond the textbook!)
start filling the String (or JTextArea) with the headers for Product, Quantity, Line
Cost, and Total Cost
prompt the user for the first productNo
while the productNo is not the sentinel value of -1
get the quantity
if the quantity is -1 then exit
switch on productNo
in each case, determine the new lineAmount
add the lineAmount to the orderAmount
add the new subtotal/order line information to the output String (or JTextArea)
get the next productNo
output the total orderAmount
Here is what I have so far:
import java.util.Scanner;
public class Product
{
public static void main(String args[])
{
int cntr = 0;
int product = 0;
int units = 0;
double totalcost = 0;
Scanner MK = new Scanner(System.in);
cntr=0;
while (cntr >= 0 && cntr <=5)
{
System.out.println("Enter Product no.(1-5) or -1 to Quit");
product = MK.nextInt();
switch(product) {
case 1:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 2.98;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 2:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 4.50;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 3:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 9.98;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 4:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 4.49;
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
break;
case 5:
{
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
double cost = 6.87;
totalcost = totalcost + (cost*product);
System.out.println("Current total cost: " + totalcost);
cntr++;
}
}
System.out.println("Total cost-->" +totalcost);
}
}
}
any help will be awesome!
Thanks!