Hi,
I'm a current, beggining java student and I am completely stumped! i have been searching the web for help but i just cant find a solution, nor can I tell if i'm on the right path with my code. For this weeks assignment I have to write a Java Application that prompts the user for input. Here is the exact Assignement:
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
and here is the code i have come up with so far:
import java.util.Scanner;
public class Test {
public static void main(String args[]){
int cntr, product, units, cost, totalcost = 0;
Scanner MK = new Scanner(System.in);
cntr=0;
System.out.println("Enter Product No.(1-5)or -1 to Quit");
product = MK.nextInt();
switch(product) {
case 1:
while(cntr <5){
System.out.println("");
System.out.println("Product " + (cntr+1));
System.out.println("Enter Quantity or -1 to Quit");
product = MK.nextInt();
System.out.println("Enter Cost");
cost = MK.nextInt();
totalcost = totalcost + cost*product;
System.out.println("Current total cost: " + totalcost);
cntr++;
}
System.out.println("Total Cost-->"+totalcost);
}
}
}
I have no clue how to associate a specific cost with a specific product, and looking at her examples, that seems to be what she wants. As of right now, my program asks the User to input the cost of the product because I am clueless on how to write the code i need. It is only supposed to ask for the product number, the number of units being sold, and then output the total after each new product is added to the list.
- Product 1 is $2.98 per unit
- Product 2 is $4.50 per unit
- Product 3 is $9.98 per unit
- Product 4 is $4.49 per unit
Product 5 is $6.87 per unit
If there is anyone willing to help me out, at least pointing me in the right direction, i would greatly appriciate it.