Results 1 to 6 of 6

Thread: Sorting Arrays

  1. #1
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Sorting Arrays

    Hello all... I was given a homework assignment and it is already a day late. I am just not understanding this. I have looked all over online and I have found different sort options, but maybe it has something to do with sorting multiple items... idk. Here is the assignment requirements:

    Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

    Here is my code:

    Java Code:
    //import array methods
    
    import java.util.Arrays;
    
    //Main class
    public class Inventory 
    {
    	//Calls main method to execute program
    	public static void main (String[] args)
    	{
    		//Welcome message
    		System.out.println("Welcome! \n This program will keep " +
    				"track of software programs in stock. \n");
    		
    		Inventory myInventory[] = new Inventory[3];
    		
    		InventoryProduct flStudio = new InventoryProduct("FL Studio", 99.99, 10);
    		InventoryProduct reason = new InventoryProduct("Reason", 159.99, 5);
    		InventoryProduct proTools = new InventoryProduct("Pro Tools", 349.99, 13);
    		
    		Arrays.sort(myInventory);
    		
    		//Display results
    		System.out.printf("Product ID number: %d%n", myInventory.length);
    		System.out.printf("Product name: %d%n", myInventory.length);
    		System.out.printf("Product price: %d%n", myInventory.length);
    		System.out.printf("Amount in stock: %d%n", myInventory.length);
    		
    		//Display total value of inventory
    		System.out.printf("The total value of the inventory is: %d%n", myInventory.length);
    	}
    }
    Java Code:
    //Constructor class
    class InventoryProduct 
    {
    	//Declorations
    	private int itemNum; //ID number
    	private String itemName; //Name of product
    	private double itemPrice; //Price of one unit
    	private int itemInStock; //Units currently in stock
    	public double inventoryTotal; //Total value of units in inventory
    	
    	//Initialize constructor
    	public InventoryProduct (String itemName,
    			double itemPrice, int itemInStock)
    	{
    		this.itemName = itemName;
    		this.itemPrice = itemPrice;
    		this.itemInStock = itemInStock;
    		setTotal();
    	}
    	//Return ID number
    	public int getNum()
    	{
    		return itemNum;
    	}
    	//Return item name
    	public String getName()
    	{
    		return itemName;
    	}
    	//Return unit price
    	public double getPrice()
    	{
    		return itemPrice;
    	}
    	//Return items in stock
    	public int getStock()
    	{
    		return itemInStock;
    	}
    	//Set the total value of inventory
    	private void setTotal()
    	{
    		inventoryTotal = itemPrice + itemInStock;
    	}
    	//Return inventory total
    	public double getTotal()
    	{
    		return inventoryTotal;
    	}
    }
    I am trying to sort the items in the array by name and display each item. Can someone point me in the right direction or at least tell me where I am going wrong?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Sorting Arrays

    There are no items in your array and it should be of type InventoryProduct[], not Inventory[].

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

  4. #4
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Sorting Arrays

    Thanks for the replies... I'm going over it again today along with the materials from my class and the internet to look at examples and try to figure this out. Regarding the .length... I just got frustrated after trying many different things and before I posted this I went through those lines of code in my IDE and it said there were errors and it gave that as the only option to correct it. I might be back after I make adjustments to my code if I still cant figure it out... without taking suggestions from Eclipse :)

  5. #5
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Sorting Arrays

    Ok, I gave it another shot after doing more reading. I hope I am at least getting close... I deleted about half my code to try this.

    Java Code:
    import java.util.Arrays;
    
    //Main class
    public class Inventory 
    {
    	//Calls main method to execute program
    	public static void main (String[] args)
    	{
    		//Welcome message
    		System.out.println("Welcome! \n This program will keep " +
    				"track of software programs in stock. \n");
    		
    		InventoryProduct[] myInventoryProduct = new InventoryProduct[3];
    		
    		InventoryProduct flStudio = new InventoryProduct("FL Studio", 99.99, 10);
    		InventoryProduct reason = new InventoryProduct("Reason", 159.99, 5);
    		InventoryProduct proTools = new InventoryProduct("Pro Tools", 349.99, 13);
    		
    		myInventoryProduct[0] = flStudio;
    		myInventoryProduct[1] = reason;
    		myInventoryProduct[2] = proTools;
    		
    		Arrays.sort(myInventoryProduct);
    		
    		int i = 0;
    		for(InventoryProduct temp: myInventoryProduct)
    		{
    			System.out.println("Product " + ++i + " : " + temp.getName() +
    					", Price : " + temp.getPrice() + ", In Stock : " + 
    					temp.getStock());
    		}
    	}
    }

    Java Code:
    public abstract class InventorySort implements Comparable<InventorySort> 
    {
    	private String name;
    	private double price;
    	private int stock;
    	
    	public InventorySort (String name, double price, int stock)
    	{
    		super();
    		this.name = name;
    		this.price = price;
    		this.stock = stock;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	
    	public void setName(String name)
    	{
    		this.name = name;
    	}
    	
    	public double getPrice()
    	{
    		return price;
    	}
    	
    	public void setPrice(double price)
    	{
    		this.price = price;
    	}
    	
    	public int getStock()
    	{
    		return stock;
    	}
    	
    	public void setStock(int stock)
    	{
    		this.stock = stock;
    	}
    	
    	public Inventory compareTo(Inventory compareInventory)
    	{
    		Inventory compareName = ((Inventory) compareInventory);
    		return compareName;
    	}
    }
    There are no errors in Eclipse that are showing so I don't know where I am going wrong. This is the error I am getting when trying to compile:

    Exception in thread "main" java.lang.ClassCastException: InventoryProduct cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscendi ng(Unknown Source)
    at java.util.ComparableTimSort.sort(Unknown Source)
    at java.util.ComparableTimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at Inventory.main(Inventory.java:31)

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Sorting Arrays

    Hello,

    I didn't see any relation between your main Inventory class, InventoryProduct with the InventorySort class. I you want to create create a natural order for your InventoryProduct why don't you just implements the Comparable interface in the InventoryProduct class. This interface has one method that you need to implement, and the signature of this method is:

    Java Code:
    public int compareTo(T o) {
    ...
    }
    As you can see this method return an int. That indicates the compare result either 0, a negative integer or a positive integer.

Similar Threads

  1. Sorting arrays
    By Blondedude092 in forum New To Java
    Replies: 2
    Last Post: 12-01-2012, 01:57 AM
  2. Sorting arrays
    By Lex in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 02:19 AM
  3. Help with sorting arrays
    By Joycey in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 08:35 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 01:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •