problem with writting RetailItem program

writting a program and facing some problems!!

Page 1 of 1

10 Replies - 883 Views - Last Post: 02 January 2010 - 01:03 PM Rate Topic: -----

#1 Georgiena  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-December 09

problem with writting RetailItem program

Posted 01 January 2010 - 08:55 AM

Hi everyone,

first of all I really liked the topics here ..

will I'm writing here to ask for help.

I'm doing a JAVA program and there is this error that I really tried to fix but couldn't.
so can u help me please.

the program I'm writing is to accept data from the user " Item serial, units, price " then calculate the retail price of an item and the total price. After that it prints the result on the screen and on a file.
I should use methods and arrays.
Itemserial , units, price are 1D array item description is a 2D array and should be initialized with the following:
Item # Description
Item 1 Jacket
Item 2 Designer Jeans
Item 3 Shirt

validation the units between 1 and 1000 and no negative number.
Items serial numbers should not contain more than 6 digits.

The arrays should be parallel.

This is the code I wrote :
import java.util.Scanner;  // needed for scanner
import java.io.*; // needed for classes
import java.text.DecimalFormat;  // for formating nums

public class RetailItem
{
	public static void main(String [] args) throws IOException
	{

		int itemSerial[];
		int units[];
		Double price[];
		String Desc [][];
		Double retailPrice;
		Double totalPrice;
		int count;
		int row;
		int col;


		int itemSerial = new int[3];
		int units = new int[3];
		double price = new double [3];
		String Desc = new String [2][3];

		//create a decimal format object
		DecimalFormat formatter = new DecimalFormat("#,##0.00");




// Use a loop to ask the user to input data and store them in the arrays
		for ( int count=0; count <3; count++ )
		{
			Scanner keyboard = new Scanner(System.in);
			System.out.println("Enter the Item Serial ");
			itemSerial[count] = keyboard.nextInt();

			Scanner keyboard = new Scanner(System.in);
			System.out.println("Enter the units ");
			units[count] = keyboard.nextInt();

			Scanner keyboard = new Scanner(System.in);
			System.out.println("Enter the price ");
			price[count] = keyboard.nextInt();
// insialize the 2Darray with the following
			String Desc [][] = { { Item1, Jacket} , { Item2, Jeans} , { Item3, Shirt} };
		}

		// valdate unit
				while (unit < 0 && unit > 1000)
				{
					System.out.println(" Invalide Input " );
					unit = reader.nextInt();
				}

				//valdate itemSerial
				while(itemSerial < 0 && itemSerial > 1000000 )
				{
					System.out.println(" Invalide Input " );
					itemSerial = reader.nextInt();
				}

				//valdate price
				while(price < 0  )
				{
					System.out.println(" Invalide Input " );
					price = reader.nextInt();
				}

// call the methods
			retailPrice(price);
			totalPrice(price,retailPrice);

		//}

/** a method to calc the retail price it accepts parameters @parm
then return the value @return */
			public static double retailePrice(double price )
			{
				String Desc [][] = { { Item1, Jacket} , { Item2, Jeans} , { Item3, Shirt} };

				if(String Desc [0][0] = { { Item1 , Jacket}})

					double retailPrice = price * 1.12;

				else if ( String Desc [0][1] = 	{ { Item2, Jeans}})

					double retailPrice = price * 1.16;

				else if ( String Desc [0][2] = { { Item3, Shirt}})

					double retailPrice = price * 1.18;

				else

					double retailPrice = "invaled data";

				return retailPrice;
			 }


/** a method to calc the total price it accepts parameters @parm
then return the value @return */
			public static double totalPrice(double retailePrice, Price)
			{
				return double totalPrice = retailePrice + Price;

			}



		// call the printing methods
		 printingOnFile(Desc,itemSerial, units, price, retailPrice, totalPrice);
		 printingOnScreen(Desc,itemSerial, units, price, retailPrice, totalPrice);

		// Print on txt file

	public static  priningtOnFile (String Desc, int itemSerial,int units, double price, double retailPrice, double totalPrice) throws IOException
	{
		FileWriter fwriter = new FileWriter("Report.txt"); // Creat a file
		PrintWriter outputFile = new PrintWriter(fwriter); // to write on file

		outputFile.println("Item Name \t Serial Number \t Units \t Price \t Retail Price \t Total Price"); // write to the file
		outputFile.println("==========================================================================="); // write to the file

		// loop to print the description first
		for (int row = 0; row < 2; row++)
		{
	 		 for (int col = 0; col < 3; col++)
			{
				System.out.println(Desc[row][col]);
			  }
		}

		// loop to print the serial,units,price,retailprice,and totalprice
		for (int count = 0; count < itemSerial.length; count++)
		{
			outputFile.println("\t" + itemSerial[count] + " \t" + units[count]  + " \t" +
			"$"+ (formatter.format(price[count]))+ " \t" +"$"+ (formatter.format(retailPrice)) + " \t" + "$"+ (formatter.format(totalPrice)) );
			outputFile.close();
			System.out.println( " Data Written to the file " );
		}
	}


	public static  printingOnScreen(String Desc, int itemSerial,int units, double price, double retailPrice, double totalPrice)
	{
		// To write on screen
		System.out.println();
		System.out.println("Item Name \t Serial Number \t Units \t Price \t Retail Price \t Total Price");
		System.out.println("===========================================================================");

		// loop to print the description first
		for (int row = 0; row < 2; row++)
		{
	 		 for (int col = 0; col < 3; col++)
			{
				System.out.println(Desc[row][col]);
			  }
		}

		// loop to print the serial,units,price,retailprice,and totalprice
		for (int count = 0; count < itemSerial.length; count++)
		{
			System.out.println("\t" + itemSerial[count] + " \t" + units[count]  + " \t" +
			"$"+(formatter.format(price[count]))+ " \t" +"$"+ (formatter.format(retailPrice))+ " \t" + "$"+ (formatter.format(totalPrice)));
		}
	}
;
  }
}



and this is the error that the compiler gives me
C:\Users\user\Documents\Uni\COS 260 S1\JAVA\RetailItem.java:86: illegal start of expression
			public static double retailePrice(double price )
						^
C:\Users\user\Documents\Uni\COS 260 S1\JAVA\RetailItem.java:126: illegal start of expression
	public static  priningtOnFile (String Desc, int itemSerial,int units, double price, double retailPrice, double totalPrice) throws IOException
		^
2 errors

Tool completed with exit code 1


I hope you can help me to understand the problem.
please feel free to give your comments.


Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: problem with writting RetailItem program

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,193
  • Joined: 20-September 08

Re: problem with writting RetailItem program

Posted 01 January 2010 - 10:20 AM

'retailePrice' (which should be called something like getRetailPrice) doesn't really make sense. You need to rethink that, along the lines of how to check which items are to be priced and where the reference points are found, and what the user entered
Was This Post Helpful? 0
  • +
  • -

#3 Georgiena  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-December 09

Re: problem with writting RetailItem program

Posted 01 January 2010 - 10:42 AM

g00se

Thank you for replaying
I'll go through it again hoping for better result :)
I have a question can I pass two different types of array to a method? a double and a string?
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,193
  • Joined: 20-September 08

Re: problem with writting RetailItem program

Posted 01 January 2010 - 10:50 AM

Quote

I have a question can I pass two different types of array to a method? a double and a string?


You probably shouldn't be doing that. More likely, you need to validate two types of input in one method, which of course you can do by passing two different types, which would both be checked against their respective reference data
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10393
  • View blog
  • Posts: 38,459
  • Joined: 27-December 08

Re: problem with writting RetailItem program

Posted 01 January 2010 - 01:30 PM

View PostGeorgiena, on 1 Jan, 2010 - 01:42 PM, said:

I have a question can I pass two different types of array to a method? a double and a string?


Really, it depends on the method definition. Syntactically, you can define a method to except an Object[] (if it isn't defined like this, you won't be able to pass either array), which will in turn allow you to pass both a String and a Double (note that the array has to be of type java.lang.Double vs. the primitive type double). However, just because you can doesn't always mean you should. So in other words, it depends on what you are trying to accomplish. If the task you are trying to accomplish is type specific, I would steer away from doing this. However, if it is more generic, like swapping elements in the array, then I don't see a problem.
Was This Post Helpful? 0
  • +
  • -

#6 Georgiena  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-December 09

Re: problem with writting RetailItem program

Posted 01 January 2010 - 02:48 PM

g00se, macosxnerd101

Thank u for replaying
as for now I managed to fix almost all the errors
the problem is in one method

I would like to write a method that accepts a string array that is already initialized at the beginning and a double array that is taken from the user.
later on it compare both arrays and do calculation according to the result of comparison.
hope u can help me with this one.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10393
  • View blog
  • Posts: 38,459
  • Joined: 27-December 08

Re: problem with writting RetailItem program

Posted 01 January 2010 - 03:01 PM

View PostGeorgiena, on 1 Jan, 2010 - 05:48 PM, said:

I would like to write a method that accepts a string array that is already initialized at the beginning and a double array that is taken from the user.
later on it compare both arrays and do calculation according to the result of comparison.
hope u can help me with this one.

Compare how? What are you looking for? What are you trying to accomplish? You are being very ambiguous about what you want, which isn't a good trait in a programmer.

Also, we won't help you out with your code until you attemp to implement the problem.

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
Was This Post Helpful? 0
  • +
  • -

#8 Georgiena  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-December 09

Re: problem with writting RetailItem program

Posted 01 January 2010 - 03:22 PM

Sorry if I wasn't clear.

as you can see in the previous code I initialized a string 2D array to hold the following
String (Desc [0][0]="Item1",
Desc [0][1]="Item2",
Desc [0][2]="Item3" ,
Desc [1][0]="Jacket" ,
Desc [1][1]="Jeans" ,
Desc [1][2]="Shirt");
and asked the user to input a price
I'm trying to write a method
that will do the following
retailprice = price * 1.12 if its a Jacket
retailprice = price * 1.16 if its a Jeans
retailprice = price * 1.18 if its Shirt
and return the result.

here is the method
 public static getRetailePrice(double price, String Desc )
						 {


							 if(String Desc [0][0] = "Item1")

								 double retailPrice = price * 1.12;

							 else if ( String Desc [0][1] = "Item2"	)
								 double retailPrice = price * 1.16;

							 else if ( String Desc [0][2] = " Item3")

								 double retailPrice = price * 1.18;

							 else

								 double retailPrice = "invaled data";

							 return retailPrice;
						  }


the other thing I thought about that since the arrays are parallel array I may write the code as following

 public double getRetailPrice(double price[])
			 {
				if (price[0])
				{
				 	retailPrice = price[0] * 1.12;

				else
					if (price[1])
					{
					retailPrice = price[1] * 1.16;

					else
						if (price[2])
						retailPrice = price[2] * 1.18;
					}
				}
				return retailPrice;
			}


but still not working it gives me the message illegal start of expression although I'm sure no brackets or semicolon are missing.

hope I made myself clear now.
and thank you anyway.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10393
  • View blog
  • Posts: 38,459
  • Joined: 27-December 08

Re: problem with writting RetailItem program

Posted 01 January 2010 - 04:08 PM

Let's start by examining what we need to correct with these methods.

public static getRetailePrice(double price, String Desc){
   if(String Desc [0][0] = "Item1")
		double retailPrice = price * 1.12;
   else if (String Desc [0][1] = "Item2")
		double retailPrice = price * 1.16;
   else if ( String Desc [0][2] = " Item3")
		 double retailPrice = price * 1.18;
   else double retailPrice = "invaled data";
   return retailPrice;
}


I see a lot wrong in this method. First, you tell this method to accept a String parameter, but you reference that param as if it were a 2D array. Remember, a String is not a 2 Dimmensional Array.

Next, let's look at how you reference Desc String Desc [0][0] = "Item1". First, it has already been declared as a String in the method header, so you don't need the String keyword with Desc anywhere else.

Next, you need to change where retailPrice is declared in the method. Rather than declaring a local retailPrice method within each conditional, declare and initialize it at the beginning of the class, then assign it values in the conditionals and return.

Lastly, the statement double retailPrice = "invaled data"; is invalid b/c you cannot assign a String literal to a double variable.

Now let's move onto the next method:
public double getRetailPrice(double price[]){
	if (price[0]){
		retailPrice = price[0] * 1.12;
		else if (price[1]){
		  retailPrice = price[1] * 1.16;
		  else if (price[2])
			 retailPrice = price[2] * 1.18;
		  }//end price1
		}//end price0
	return retailPrice;
  }//end method


The first thing that jumped out at me after I indented your code is that the curly braces messed up your logic. So first, you have if(price[0]), and inside that, you have everything else except the return statement. I'm actually suprised that you didn't get an "else without if" error at compile time, because both else if(price[1]) and else if(price[2]) are "else without if" conditionals.

Next, the conditional if(price[0]) and the corresponding else-if statements are illegal because a boolean value is required, not a double. Since price[] is a double[], price[0] will be of type double. You have to make a comparison or feed in a boolean variable or literal directly in order for that to go away.

Lastly, retailPrice isn't declared in the method, so you can't assign values to it and you can't return it. In other words, you can't use something that doesn't exist.


Now I'm guessing what you want to do is, given the name of the item, calculate the price using methods. So let's re-work the logic a little bit using.
/*
   @params-
	  1) String item- item name
	  2) double[] markupList- 1.12 for Jeans, etc. array
	  3) double price- given price for this item
*/
public static double getRetailPrice(String item, double[] markupList, double price){ 
   double retailPrice = price; //initialize to base price
   //if item is valid, markup
   if(item.equals("Jacket")) retailPrice *= priceList[0];
   else if(item.equals("Jeans")) retailPrice *= priceList[1];
   else if(item.equals("Shirt") retailPrice *= priceList[2];

   //if invalid, set to -1 to flag as invalid
   else retailPrice = -1;
   return retailPrice;
}



Honestly, I'm guessing at what you want to do here and how you want it organized. I'm a little confused myself because the design needs to be completely overhauled (no offense).

If I may make a suggestion- there is no need to use a 2D array to store items in a table format. Since arrays are indexed from 0 to length-1, you know that element 0 (in a 1D array) is the first item. So I would store a String[] shoppingCart, double[] markup, and double[] priceList if you are going to use parallel arrays.

I hope that I've helped to clear up some of the mess. Good luck with your program! :)
Was This Post Helpful? 1
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,193
  • Joined: 20-September 08

Re: problem with writting RetailItem program

Posted 02 January 2010 - 04:39 AM

Quote

Desc [0][1]="Item2",
Desc [0][2]="Item3" ,
Desc [1][0]="Jacket" ,
Desc [1][1]="Jeans" ,



If those data don't change, you might as well store them in a static array as your reference data. You don't need to involve them as parameters. Just compare them to your input data
Was This Post Helpful? 0
  • +
  • -

#11 Georgiena  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-December 09

Re: problem with writting RetailItem program

Posted 02 January 2010 - 01:03 PM

Both of you
Thanks a lot.

I guess I still have a loge way with java programming.
it's my first program so thanks for the help.

I'll be back after fixing it :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1