In need of some help please

Working on a basic java application

Page 1 of 1

3 Replies - 3186 Views - Last Post: 28 May 2009 - 09:03 PM Rate Topic: -----

#1 sw33tr05e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-May 09

In need of some help please

Posted 28 May 2009 - 08:30 PM

I am working on my current assignment

A mail order house sells five products whose retail prices are as follows: Product 1 $2.98; product 2, 4.50; product 3, 9.98; product 4, 4.49; product 5, 6.87. Write an application that reads a series of pairs of numbers as follows:

a) product number
B) quantity sold

Your program should use a swich statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.


Right now I know I have a lot of work left to do on my code, but I am stuck at a part that hopefully someone can bring a little light on. I cannot for the life of me figure out how to get the cases to be read within the switch statement with the given values. After I prompt the user to enter in the product number, I dont know what to get and if I have enough variables declared above. Again I know that there is still a lot to do afterwards, and I am not asking for assistance on that right now because I am stuck at this particular point and cannot get the basic portion of this application to run properly. Any assistance would be appreciated! Below is my code as is right now.

import java.util.Scanner;

public class TotalRetailValue
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner (System.in);
        int product = 0;
        double product1;
        double product2;
        double product3;
        double product4;
        double product5;
        int quantity;
        String done = "y";
        double totalSales = 0;
        
        while (done.equals("y"))
        {
            System.out.println("Enter product number: ");
            
            System.out.println("Enter how many were sold: ");
            quantity = input.nextInt();

            switch (product)
            {
                case 1: product1 = 2.98;
                    break;
                case 2: product2 = 4.50;
                    break;
                case 3: product3 = 9.98;
                    break;
                case 4: product4 = 4.49;
                    break;
                case 5: product5 = 6.87;
                    break;
            }
            totalSales = product * quantity;

            System.out.println("Would you like to enter another sale? (y/n): ");
            done = input.next();
        }  

    }
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 28 May 2009 - 08:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: In need of some help please

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: In need of some help please

Posted 28 May 2009 - 08:55 PM

First of all, no need for a variable for each product. The only reason they are mentioning the different products is to let you know how to setup the switch statement. Next, you just need a variable to hold the current price of an item.

double price = 0.0;

while (done.equals("y"))
{
	 System.out.println("Enter product number: ");
	 product = input.nextInt();
			
	 System.out.println("Enter how many were sold: ");
	 quantity = input.nextInt();

	 switch (product)
	 {
		  case 1: 
			   price = 2.98;
			   break;
		  case 2: 
			   price = 4.50;
			   break;
		  case 3: 
			   price = 9.98;
			   break;
		  case 4: 
			   price = 4.49;
			   break;
		  case 5: 
			   price = 6.87;
			   break;
		  default:
			   price = 0.0;
	 }
			
	 totalsales += price * quantity;

	 System.out.println("Would you like to enter another sale? (y/n): ");
	 done = input.next();
}  



We evaluate the product number chosen using the switch statement. Based on the product, we know what to set the current price at. At the end we add to our total sales the price times the quantity.

It is smart to always put in a default case in case the user enters some different number. Here we set the price to 0 which thus adds zero to the totalsales (not altering it).

Now watch out how you use next and nextInt together like that, it may lead to your loop giving you funny things.

Enjoy!

"At DIC we be switch case fixing code ninjas... and sometimes it is the case where it fixes us!" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 sw33tr05e  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-May 09

Re: In need of some help please

Posted 28 May 2009 - 08:56 PM

Okay so I had a thought and changed some things, thinking it would work, to ask the user for the product and how many where sold and then print out how much the initial total is, but it still is not doing it.

Again any insight please would help greatly!!!



import java.util.Scanner;

public class TotalRetailValue
{

	public static void main(String[] args)
	{
		Scanner input = new Scanner (System.in);
		int product = 0;
		double price;
		double product1;
		double product2;
		double product3;
		double product4;
		double product5;
		int quantity;
		String done = "y";
		double totalSales = 0;
		
		while (done.equals("y"))
		{
			System.out.println("Enter product number: ");
			price = input.nextDouble();
			System.out.println("Enter how many were sold: ");
			quantity = input.nextInt();

			switch (product)
			{
				case 1: price = 2.98;
					break;
				case 2: price = 4.50;
					break;
				case 3: price = 9.98;
					break;
				case 4: price = 4.49;
					break;
				case 5: price = 6.87;
					break;
			}
			totalSales = price * quantity;

			System.out.println("Would you like to enter another sale? (y/n): ");
			done = input.next();
		}  

	}
}


Was This Post Helpful? 0
  • +
  • -

#4 blaqkhavok  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-May 09

Re: In need of some help please

Posted 28 May 2009 - 09:03 PM

Ok, i'm not sure if this is exactly what you want but I think it's the general idea.

*edit: Martyr2 beat me to it ;)


import java.util.Scanner;

public class TotalCostHelp
{

	public static void main(String[] args)
	{
		Scanner input = new Scanner (System.in);
		int product = 0;
		double product1;
		double product2;
		double product3;
		double product4;
		double product5;
		int quantity;
		String done = "y";
		double totalSales = 0;
		
		while (done.equals("y"))
		{
			System.out.print("Enter product number: ");
			product=input.nextInt();
			
			System.out.print("Enter how many were sold: ");
			quantity = input.nextInt();

			switch (product)
			{
				case 1: product1 = 2.98;
						totalSales+=(2.98*quantity);
						break;
				case 2: product2 = 4.50;
						totalSales+=(4.50*quantity);
				   		 break;
				case 3: product3 = 9.98;
						totalSales+=(9.98*quantity);
						break;
				case 4: product4 = 4.49;
						totalSales+=(4.49*quantity);
						break;
				case 5: product5 = 6.87;
						totalSales+=(6.87*quantity);
						break;
			}

			System.out.println("Would you like to enter another sale? (y/n): ");
			done = input.next();
		}
		System.out.println(totalSales);

	}
}







This post has been edited by blaqkhavok: 28 May 2009 - 09:08 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1