4 Replies - 654 Views - Last Post: 18 June 2011 - 02:29 PM Rate Topic: -----

#1 ITStudent02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 11

how to calculate 5 double values in my array?

Posted 18 June 2011 - 11:33 AM

Hello, I am trying to figure out where and how to get my array to calculate the "total inventory Cost" of all my printers. I have gotten them to printer out but they are not in ascending order and I can't get them to calculate ALL the values. So far my program prints out all the printers and their item Number, product name, price for each unit, and the number of units in stock. It even calculates each individual printer by units In Stock * unit price. However, I need all of them to calculate in an array. I guess I would also need to do a second array to put my product names in ascending order. The problem is my code won't allow for any changes. Where am I suppose to put this array? Did I do my inventory class wrong? Should each printers output be an array rather then what I have? OR should I take what I have and just add the array's at the end of my main method? Maybe outside the main but inside the class? Can anyone shed some light on this for me? Thank you. Here is my code:

public class InventoryPart2
{   //main method begins to execute
	Scanner input = new Scanner(System.in);

	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

	public static void main (String args [])
	{
		System.out.println("Welcome to the Inventory Program");

 		Printer hp = new Printer("P1104","HP LaserJet" , 5, 149.99);
 		System.out.println(hp);

 		Printer dell = new Printer("P1103", "Dell Multi-Function Laser Printer", 3, 149.99);
 		System.out.println(dell);

		 Printer lexmar = new Printer("P1105","Lexmar CLP", 10, 299.98);
 		System.out.println(lexmar);

 		Printer brother = new Printer("P1102", "Brother Inkjet Muliti-function Printer", 12, 217.60);
 		System.out.println(brother);

 		Printer canon = new Printer("P1101","Canon Bubble Jet Photo Printer", 13, 1249.98);
 		System.out.println(canon);

    }//end main


}//end 
Inventory class public Printer(String itemNumber, String productName, int unitsInStock, double unitPrice)
	{
		// call to object Constructor
		this.itemNumber = itemNumber;
		this.productName = productName;
		this.unitsInStock = unitsInStock;
		this.unitPrice = unitPrice;
		this.value = unitsInStock*unitPrice;
		this.totalInventory = value;
		}//end four argument constructor

		//Attributes
		public String itemNumber;
		public String productName;
		public int unitsInStock;
		public double unitPrice;
		public double value;
		public double totalInventory;
		//get and set methods for each variable

		public void setItemNumber( String ItemNumber )
		{
			this.itemNumber = itemNumber;
		}
		public String getItemNumber()
		{
			return itemNumber;
		}
		public void setProductName( String ProductName )
		{
			this.productName = productName;
		}

		public String getProductName()
		{
			return productName;
		}
		public void setUnitsInStock( int UnitsInStock )
		{
			this.unitsInStock = unitsInStock;
		}
		public int getUnitsInStock()
		{
			return unitsInStock;
		}
		public void setUnitPrice( double UnitPrice )
		{
			this.unitPrice = unitPrice;
		}
		public double getUnitPrice()
		{
			return unitPrice;
		}
		public double getValue()
		{
			return value;
		}
		public String toString()
		{
			NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
			return "\n\nItem Number: "+ itemNumber + "\nProduct Name: " + productName + "\nNumber of Units In Stock: " +  unitsInStock + "\nUnit Price: " + unitPrice + "\nValue: " + value;
		}


}//end class


Is This A Good Question/Topic? 0
  • +

Replies To: how to calculate 5 double values in my array?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to calculate 5 double values in my array?

Posted 18 June 2011 - 11:42 AM

public class InventoryPart2
{   //main method begins to execute
	Scanner input = new Scanner(System.in);

	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

	public static void main (String args [])
	{
		System.out.println("Welcome to the Inventory Program");
                Printer[] printer = new Printer[5];

 		printer[0] = new Printer("P1104","HP LaserJet" , 5, 149.99);
 		printer[1] = new Printer("P1103", "Dell Multi-Function Laser Printer", 3, 149.99);
 		printer[2] = new Printer("P1105","Lexmar CLP", 10, 299.98);
 		printer[3] = new Printer("P1102", "Brother Inkjet Muliti-function Printer", 12, 217.60);
 		printer[4] = new Printer("P1101","Canon Bubble Jet Photo Printer", 13, 1249.98);

          double total = 0;
          for(int i = 0; i < printer.length; ++i) {
            System.out.println(printer[i]);
            tottal += printer[i].getUnitPrice();
          }
          System.out.println("Total: " + total);
    }//end main



Was This Post Helpful? 1
  • +
  • -

#3 ITStudent02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 11

Re: how to calculate 5 double values in my array?

Posted 18 June 2011 - 12:07 PM

I knew I had to turn my program into an array! Thank you so much! I was in tears over this. I am getting getting some errors and although it is not the same errors I was getting it is saying "can't find symbol: method getunitPrice" My next question is this. Since java is case sensitive I know that we have to put our Class name in all caps, in other works InventoryProgram (capital I capital P.) the same goes for our variables and so forth. Now in my printer class if you will notice I names my variables as such:

itemName
productNumber
unitPrice
etc. So I made a lower u and an upper P in for unitPrice, do I need to change all my variables to be getUnitPrice (both caps) or can I do this getunitPrice (lower case u and upper p). I think this is my problem.

Also, I keep getting found <nulltype> required double error message too so I am assuming this has to do with some syntax errors right? I will keep looking and work on this....thank you so very very much for all your help!!!
Was This Post Helpful? 0
  • +
  • -

#4 ITStudent02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 11

Re: how to calculate 5 double values in my array?

Posted 18 June 2011 - 01:27 PM

Thank you! I got most of it working as it should be. I just need to figure out how to get my array in ascending order and I am good to go. Thank you so much for reply and assistance in getting me in the right direction. Wish I had come here in the beginning of my course I might have learned more and avoided so many issues at this point of the game....but better learned late then never, right? Thanks a ton!

Cheers!
Was This Post Helpful? 0
  • +
  • -

#5 ITStudent02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 11

Re: how to calculate 5 double values in my array?

Posted 18 June 2011 - 02:29 PM

Okay, so I figured out why I was struggling to do an array with my program and came to the conclusion that I need a nested for loop. Unfortunately I am not sure how to do this and the way I am doing it is obviously wrong this is what I was trying to do:

double totalInventory = 0;
 		for(int i = 0; i < printer.length; ++i)
 		{
 			System.out.println(printer[i]);
 			totalInventory += printer[i].getValue();

 			for (int j = 0; j < 5; j++)
 			{
 				System.out.println(itemName[j]);
 				itemName += itemName[j].getItemName();
 			}
 		}
		System.out.println("\n\nTotal Inventory Value: " + totalInventory)

I know the code is wrong because java won't allow an = sign to compare Strings so I think I would use some kind of name.equals but I have not found any information with nested for loops using doubles on the first and strings on the second so I am confused on what the proper syntax would be. When I take this line of code out
for (int j = 0; j < 5; j++)
 			{
 				System.out.println(itemName[j]);
 				itemName += itemName[j].getItemName();
 			}

My program compiles and runs fine, except it does not loop through the printers names and put them in ascending order. I actually moved my printers positions so that they would print out in alphabetical order, but that is not the requirements. I really need this nested loop to move through the printers and names array. If the Product Name in the printers
array is equal to the printer name in the names array, it need to copy this value to the array. I can't find anything on passing doubles and strings through a nested for loop. Any direction would be greatly appreciated. Thank you! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1