Jump to content

Object Arrays and sorting them

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
CodeWalker23

CodeWalker23

    Newbie

  • Members
  • Pip
  • 2 posts
I am trying to build an object array from this class.

 public class Items 

{ 

// * A String instance variable to hold the item name 

private String itemName; 

// * A double instance variable to hold the price 

private double itemPrice; 



// * A constructor that takes a String and double to initialize the instance variables 


public Items (String itemName, double itemPrice) 

{ 

this.itemPrice = itemPrice; 

this.itemName = itemName; 

} 

public Items()

{

}

// * A get and set method for each instance variable 


public String getName()

{ 

return itemName; 

} 

public double getPrice()

{ 

return itemPrice; 

} 


public void setItemName(String someItem) 

{ 

itemName = someItem; 

} 

public void setItemPrice(double somePrice) 

{ 

itemPrice = somePrice; 

} 

public String toString()

{

 return "Item: " + itemName + " price:" + itemPrice;

} 

}



This is my tester class but it doesn't display the output correctly. Any help would be appreciated. I am getting confused and I know it has to be something I overlooked and can't find a good answer for on Google. I hope you guys can help me out.



// Once you have this class created, you write a second class named CoffeeDriver.   This class has the following methods: 

public class CoffeeDriver

{


// sortName – this method sorts the array of items by item name and then displays the name and price of all items on the screen 

public static void sortName(Items arr[])

{

// sorts array by name and displays the items and prices


int in, out;

 for(out=1; out< arr.length; out++) // out is dividing line

  { Items temp = arr[out]; // remove marked person

   in = out; // start shifting at out

	 while(in>0 && // until smaller one found,

	  arr[in - 1].getName().compareTo(temp.getName())>0)

	   { arr[in] = arr[in-1]; // shift item to the right

		 --in; // go left one position

		  }

		   arr[in] = temp; // insert marked item


				JOptionPane.showMessageDialog(null,(arr[in]));


			} // end for 



		      }

 


	 

// sortPrice – this method sorts the array of items by item price and then displays the name and price of all items on the screen 

public static void sortPrice(Items arr[]) 


{

//  sorts the array by price and displays the list

int a,b;

Items temp;


for (a=0; a < arr.length;++a)

for (b=0; b< arr.length;++b)

if (arr[b].getPrice() > arr[b +1].getPrice())

{

  temp = arr[b];

  arr[b] = arr[b +1];

  arr[b+1] = temp;


JOptionPane.showMessageDialog(null, (arr[b]));

}

} 

// main - It creates an array of Item objects using the data above to set each Item's information. 


public static void main(String []args)

{


String userSorted; 

int x;

 

  Items arr[] = new Items[5];

   arr[0] = new Items();

	arr[0].setItemName("Coffee");

	arr[0].setItemPrice(1.00); 

	arr[1] = new Items();

	arr[1].setItemName("Water");

	 arr[1].setItemPrice(2.00);

	 arr[2] = new Items();

	 arr[2].setItemName("Milk");

	 arr[2].setItemPrice(1.50);

	  arr[3] = new Items();

	  arr[3].setItemName("Bagel");

	  arr[3].setItemPrice(1.25);

	   arr[4] = new Items();

		arr[4].setItemName("Donut");

		arr[4].setItemPrice(0.75);


		int selectSort;

		JOptionPane.showMessageDialog(null, "Welcome to The Coffee Shop!");

		userSorted = JOptionPane.showInputDialog(null, "Please indicate how you would like to sort the menu. To sort by price, press 1. To sort by name, press 2. To exit the program, press 3.");

		 selectSort = Integer.parseInt(userSorted);

	// insert do while loop here

	

	// 	  if (selectSort == 1);

// 		   sortPrice(arr);

		 		  if (selectSort == 2);

	       sortName(arr);

		    if (selectSort == 3);

		   JOptionPane.showMessageDialog(null, "Thank you for using Wings Coffee Shop. Have a great day!"); 

System.exit(0);

				 

}

}




Thanks

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
if you put ' ; ' behind the if line, it allready terminates the if and will run trough whatever comes behind it wether the if was true or false.
if (selectSort == 2);
	       sortName(arr);

//This must be:
if (selectSort == 2)
    sortName(arr);

On a side note, You're making it yourself pretty hard by manually sorting the array as Java has the Arrays.sort( ..) method
CoffeeDriver class (these don't have to be static by the way):
public static void sortName(Items arr[]) {
        Arrays.sort(arr, Items.NAME_ORDER);
    }

    public static void sortPrice(Items arr[])
    {
        Arrays.sort(arr, Items.PRICE_ORDER);
    }

Items class (these do^^)
static final Comparator<Items> NAME_ORDER = new Comparator<Items>() {
        public int compare(Items e1, Items e2) {
            return e1.getName().compareTo(e2.getName());
        }
    };

    static final Comparator<Items> PRICE_ORDER = new Comparator<Items>() {
        public int compare(Items e1, Items e2) {
            return Double.compare(e1.getPrice(), e2.getPrice());
        }
    };

Items will need: import java.util.Comparator;

And the sorting is done.

For the output in the end i placed
String out="";
        for(Items item : arr){
            out+= item.toString()+"\n";
        }

        JOptionPane.showMessageDialog(null, out);

At the end of the main loop. It's a bit more user friendly to show it in 1 box, rather than have to click trough all of them :D

#3
CodeWalker23

CodeWalker23

    Newbie

  • Members
  • Pip
  • 2 posts
I can not thank you enough..:thumbup1: I am going to add the final step by trying to figure out how to format my out to currency. Never tried this using the JOptionPane before. but thanks again.