Need array help

I am writing a program that needs to have a subclass that adds a new constructor and adds in a 5% restocking fee. I have everything working except the new constructor. My new constructor should add the manufacturer name. My question is, now when I sort my array alphabetically by product name (folders, glue, markers, etc.) the newly added manufacturer name (Bic, Mead, Stanley, etc.) does not coincide with the right product. How do I get the newly added constructor in my subclass to stay with the right product?

Here is my first set of output with correct information:

Product name: pens

Manufacturer: Bic

Item number: 1

Piece's in stock: 346.0

Unit price : $1.59

Product name: pencils

Manufacturer: Mead

Item number: 2

Piece's in stock: 487.0

Unit price : $0.59

Product name: markers

Manufacturer: Sharpie

Item number: 3

Piece's in stock: 168.0

Unit price : $1.29

Product name: paperclips

Manufacturer: Dennison

Item number: 4

Piece's in stock: 136.0

Unit price : $1.19

Product name: glue

Manufacturer: Elmer's

Item number: 5

Piece's in stock: 72.0

Unit price : $0.79

Product name: tape

Manufacturer: 3m

Item number: 6

Piece's in stock: 127.0

Unit price : $0.49

Product name: paper

Manufacturer: Mead

Item number: 7

Piece's in stock: 203.0

Unit price : $1.79

Product name: staples

Manufacturer: Pentech

Item number: 8

Piece's in stock: 164.0

Unit price : $1.19

Product name: folders

Manufacturer: Mead

Item number: 9

Piece's in stock: 238.0

Unit price : $0.49

Product name: rulers

Manufacturer: Stanley

Item number: 10

Piece's in stock: 123.0

Unit price : $0.17

_

Here is my output after sorting by product name:

Product name: folders

Manufacturer: Bic

Item number: 9

Unit price: $0.49

The value of folders left in the inventory, plus a 5% restocking fee is: $122.45

Product name: glue

Manufacturer: Mead

Item number: 5

Unit price: $0.79

The value of glue left in the inventory, plus a 5% restocking fee is: $59.72

Product name: markers

Manufacturer: Sharpie

Item number: 3

Unit price: $1.29

The value of markers left in the inventory, plus a 5% restocking fee is: $227.56

Product name: paper

Manufacturer: Dennison

Item number: 7

Unit price: $1.79

The value of paper left in the inventory, plus a 5% restocking fee is: $381.54

Product name: paperclips

Manufacturer: Elmer's

Item number: 4

Unit price: $1.19

The value of paperclips left in the inventory, plus a 5% restocking fee is: $169.93

Product name: pencils

Manufacturer: 3m

Item number: 2

Unit price: $0.59

The value of pencils left in the inventory, plus a 5% restocking fee is: $301.70

Product name: pens

Manufacturer: Mead

Item number: 1

Unit price: $1.59

The value of pens left in the inventory, plus a 5% restocking fee is: $577.65

Product name: rulers

Manufacturer: Pentech

Item number: 10

Unit price: $0.17

The value of rulers left in the inventory, plus a 5% restocking fee is: $21.96

Product name: staples

Manufacturer: Mead

Item number: 8

Unit price: $1.19

The value of staples left in the inventory, plus a 5% restocking fee is: $204.92

Product name: tape

Manufacturer: Stanley

Item number: 6

Unit price: $0.49

The value of tape left in the inventory, plus a 5% restocking fee is: $65.34

The value of all merchandise in inventory is: $1,166.20

Here is my program:

InventoryMain.java

package inventorymain;

import java.text.NumberFormat;// used to format currency

import inventorymain.Maker;

publicclass InventoryMain

{

// main method begins execution of java application

publicstaticvoid main(String[] args)

{

//variable for formatting currency

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

//create array for products in inventory

Maker []proMaker =new Maker[10];//initializes product array

//enter elements into array

proMaker[0] =new Maker( 1,"pens",1.59,346,"Bic");

proMaker[1] =new Maker( 2,"pencils", .59, 487,"Mead");

proMaker[2] =new Maker( 3,"markers", 1.29, 168,"Sharpie");

proMaker[3] =new Maker( 4,"paperclips", 1.19, 136,"Dennison");

proMaker[4] =new Maker( 5,"glue", .79, 72,"Elmer's");

proMaker[5] =new Maker( 6,"tape", .49, 127,"3m");

proMaker[6] =new Maker( 7,"paper", 1.79, 203,"Mead");

proMaker[7] =new Maker( 8,"staples", 1.19, 164,"Pentech");

proMaker[8] =new Maker( 9,"folders", .49, 238,"Mead");

proMaker[9] =new Maker( 10,"rulers", .17, 123,"Stanley");

for (int i = 0; i < proMaker.length; i++)// for loop displays array elements

{

System.out.println("Product name: " + proMaker[i].getName());//displays product name

System.out.println("Manufacturer: " + proMaker[i].getManufact());//displays product manufacturer

System.out.println("Item number: " + proMaker[i].getNumberCode());//displays product number

System.out.println("Piece's in stock: " + proMaker[i].getUnits());

System.out.println("Unit price : " + nf.format(proMaker[i].getPrice()));//displays price per unit

System.out.println();//enters blank line

}// end for loop

//Sort elements in array in alphabetical order by product name

proMaker[0].sortItems(proMaker);

System.out.println();//inserts blank line

System.out.println();//inserts blank line

System.out.println("**********Alphabetical by product name**********");

System.out.println();//inserts blank line

System.out.println();//inserts blank line

for (int c = 0; c < proMaker.length; c++)

{

System.out.println("Product name: " + proMaker[c].getName());//displays product name

System.out.println("Manufacturer: " + proMaker[c].getManufact());//displays product manufacturer

System.out.println("Item number: " + proMaker[c].getNumberCode());//displays item number

System.out.println("Unit price: " + nf.format (proMaker[c].getPrice()));//displays price per unit

System.out.println("The value of " + proMaker[c].getName() +" left in the inventory, plus a 5% restocking fee is: "+nf.format (proMaker[c].getSum()));//displays inventory product value

System.out.println();//inserts blank line

}//end for loop

System.out.println();//enters blank line

System.out.println("The value of all merchandise in inventory is: " + nf.format (proMaker[0].totalAllInventory(proMaker)));

}//end Main

}//end class InventoryMain

Here is my Inventory.java

package inventorymain;//file assigned to inventorymain package

import java.text.NumberFormat;

import inventorymain.Maker;

publicclass Inventory

{

// set variables

protectedlong numberCode;

protected String productName;

protectedlong productUnits;

protecteddouble unitPrice;

protectedstaticdouble totalAllInventory;

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

public Inventory(long itemNum, String name,double price,long units)//varibles for constructor

{

numberCode = itemNum;//variable initialized

productName = name;//variable initialized

productUnits = units;//variable initialized

unitPrice = price;//variable initialized

}

// All setters and getters

public Inventory()

{

numberCode = 0;

}

publicvoid setnumberCode(long itemNum)

{

numberCode = itemNum;

}

publicvoid setName(String name)

{

productName = name;

}

publicvoid setProductUnits(long units)

{

productUnits = units;

}

publicvoid setUnitPrice(double price)

{

unitPrice = price;

}

publiclong getNumberCode()

{

return numberCode;

}

public String getName()

{

return productName;

}

publicdouble getUnits()

{

return productUnits;

}

publicdouble getPrice()

{

return unitPrice;

}

publicdouble totalAllInventory( Inventory[] myProduct1)//method totals value of all merchandise

{

double retotal = 0;

for (int i = 0; i < myProduct1.length; i++)// for loop for totaling

{

retotal = retotal + (getUnits() * getPrice());

}

return retotal;

}//end totalAllInventory

publicvoid sortItems(Inventory[] myProducts)

{

int a;

int b;

int sortNames = myProducts.length - 1;

String tempName;

long tempItemNum;

long tempUnits;

double tempPrice;

for (a = 0; a < sortNames; ++a)

for (b = 0; b < sortNames; ++b)

if(myProducts[b].productName.compareTo(myProducts[b + 1].productName) >0)

{

tempName = myProducts[b].productName;

myProducts[b].productName = myProducts[b + 1].productName;

myProducts[b + 1].productName = tempName;

tempItemNum = myProducts[b].numberCode;

myProducts[b].numberCode = myProducts[b + 1].numberCode;

myProducts[b + 1].numberCode = tempItemNum;

tempUnits = myProducts[b].productUnits;

myProducts[b].productUnits = myProducts[b + 1].productUnits;

myProducts[b + 1].productUnits = tempUnits;

tempPrice = myProducts[b].unitPrice;

myProducts[b].unitPrice = myProducts[b + 1].unitPrice;

myProducts[b + 1].unitPrice = tempPrice;

}//end if

}// end class Inventory

}// end class Inventory

_

Here is my subclass Maker.java where the new constructor manufact has been added.

package inventorymain;

publicclass Makerextends Inventory

{

protected String manufact;

public Maker(long itemNum, String name,double price,long units, String manufact)// Constructor for varibles

{

super(itemNum, name, price, units);

this.manufact = manufact;

}

publicvoid setManufact(String manufact)

{

this.manufact = manufact;

}

public String getManufact()

{

return manufact;

}

publicdouble getSum()//method to calculate sum

{

double itemTotal;

double stockFee;

itemTotal = (productUnits * unitPrice);

stockFee = itemTotal + (itemTotal * .05);

return stockFee;

}

}// end of class Maker

__

Hopefully someone can give me some direction. Thank you in advance!

[17801 byte] By [javahelp44a] at [2007-11-27 8:45:52]
# 1

1, your sort is all wrong. Instead of swapping even property, you should just move teh entire object in the array.

Maker temp;

for (a = 0; a < sortNames; ++a)

{

for (b = 0; b < sortNames; ++b)

{

if(myProducts[b].productName.compareTo(myProducts[b + 1].productName) >0)

{

temp = myProducts[b];

myProducts[b] = myProducts[b + 1]

myProducts[b + 1]= temp;

}//end if

}

}

2, you really should rethink the entire structure, maybe use an array list instead of arrays, change the total and sort methods to static class methods, etc.

~Tim

I did not notice the first time thru, but your total method is wrong anyway, as it will only add the total for the item you called the method on (at index 0), and at that same amount every time thru the loop. you need to change it. Test it by having 0 quantity for the first item, and running it.

Message was edited by:

SomeoneElse

SomeoneElsea at 2007-7-12 20:47:21 > top of Java-index,Java Essentials,Java Programming...
# 2
ok, Thanks for your advice Tim, I will try it!!!
javahelp44a at 2007-7-12 20:47:21 > top of Java-index,Java Essentials,Java Programming...
# 3
Tim;I got the problem worked out with my total method, but I still can not get my array to operate right and coincide the manufacturer with the correct product name. Do you have any other ideas?
javahelp44a at 2007-7-12 20:47:21 > top of Java-index,Java Essentials,Java Programming...
# 4

I made the change to sort that I suggested to you, and fixed the total method by making it static, and grabbing the correct item count and unit price for each element, and the program works as you described it should and as I would expect it to. paste your sort method only here again, and the output you are getting.

~Tim

SomeoneElsea at 2007-7-12 20:47:21 > top of Java-index,Java Essentials,Java Programming...
# 5

I'm sorry Tim, maybe I do not understand what you are advising me to do with the sort. I took out my whole sort and put yours in and I got "can't find symbol" errors so I took it back out. Here's how I fixed my total method though:

double retotal = 0;

for (int i = 0; i < products.length; i++)

{

retotal = retotal + (products[i].getUnits() * products[i].getPrice());

}

return retotal;

javahelp44a at 2007-7-12 20:47:21 > top of Java-index,Java Essentials,Java Programming...
# 6

OK, The total method looks good. Hereis the revised sort method.

public void sortItems(Inventory[] myProducts)

{

int a;

int b;

int sortNames = myProducts.length - 1;

Inventory temp;

for (a = 0; a < sortNames; ++a)

{

for (b = 0; b < sortNames; ++b)

{

if(myProducts[b].productName.compareTo(myProducts[b + 1].productName) >0)

{

temp = myProducts[b];

myProducts[b] = myProducts[b + 1];

myProducts[b + 1] = temp;

}

}

}

}

SomeoneElsea at 2007-7-12 20:47:21 > top of Java-index,Java Essentials,Java Programming...
# 7
Hey, please don't double post. It's not fair to the folks answering you. http://forum.java.sun.com/thread.jspa?threadID=5188409&tstart=0
petes1234a at 2007-7-12 20:47:21 > top of Java-index,Java Essentials,Java Programming...