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

    Example_1
    If there is anyone willing to help me out, at least pointing me in the right direction, i would greatly appriciate it.

how to associate a specific cost with a specific product,

The Map class is one way to associate one value(the product id) with another (the cost).
You may not be using the Map class yet, so another solution would be an array.
That could work for you if the product ids are integers in a small range, for example 1-5 .
Define an array to hold the product costs and have the product id be the index into that array.

Edited 4 Years Ago by NormR1

for a simple approach:
As I look at the pseudocode your suppose to ask first the user the product number then the quantity.

Next
first put the switch statement inside a loop instead of a loop inside the switch case
next use a diferent variable for the product id and the counter for the switch case

you can use a variable for each product,cost
ex. case 1 uses variables quantity1,cost1,totalcost1 next case 2 uses quantity2, cost2, etc.
or
you could use an array of each variable and use the counter from the switch statement (product number) as the index
ex. quantity[counter], cost[counter], totalcost[counter]

as for the quantity of each item just multiply this to the cost to get the total cost assuming their starting values are 1 or use a loop for repeated addition

I can see that the pseudocode is already provided for this so there's probably no need to show more of the same

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):

If you want we could help you too with the object oriented approach of the program too, just say so :)

Edited 4 Years Ago by zeroliken

You can use a two dimentional array to store all the data you need.
You can even keep the total in the same array.

Yes, you could.
But
you (and anyone reading the code) would need to know what each column in the array represented, whereas if they were different 1D arrays then their name would tell you what they were. For this reason, I think a 2D array is a bad idea.

This article has been dead for over six months. Start a new discussion instead.