Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sorting objects of an array by String name

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting objects of an array by String name

    Hello all, I am new to the forums and fairly new to Java so please take it easy on me

    I am given this assignment to create an Inventory Program and this is part 2 of the assignment. Here are the 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.

    Create a method to calculate the value of the entire inventory.

    Create another method to sort the array items by the name of the product.


    I am having a heck of a time trying to figure out how to sort this array by the name of the objects. I have tried both a bubble sort (commented out) and using the compareTo but keep getting errors. I got it down to one error saying I need to make the class InventoryConstruct abstract, but when I do that I get even more errors that pop up. Can someone please help me understand this? I have already looked online for hours trying to figure out what I am doing wrong and I cannot seem to get it right. The assignment is also asking us to do something that is not covered in our materials and this is an online course... can't get a real timely response from my instructor. Here is the code:

    import java.util.Arrays;
     
    public class InventoryProduct
    {
    	//main method starts execution
    	public static void main (String[] args)
    	{
    		//welcome message
    		System.out.println("Welcome to the inventory tracker!\n ");
     
    		//inventory information
    		InventoryConstruct[] software = new InventoryConstruct[4];
     
    		InventoryConstruct p1 = new InventoryConstruct(1, "FL Studio ", 99.99, 14);
    		InventoryConstruct p2 = new InventoryConstruct(2, "Reason ", 149.99, 5);
    		InventoryConstruct p3 = new InventoryConstruct(3, "Ableton Live ", 399.99, 8);
    		InventoryConstruct p4 = new InventoryConstruct(4, "Pro Tools ", 299.99, 6);
     
    		software[0] = p1;
    		software[1] = p2;
    		software[2] = p3;
    		software[3] = p4;
     
    		Arrays.sort(software, InventoryConstruct.InventorySort);
     
    		for (int i = 0; i < 4; i++ )
    		{
    			System.out.println("ID#: " + ++i + "   Name: " + InventoryConstruct.getName() 
    					+ "   Price: " + InventoryConstruct.getPrice() + "   In Stock: " + InventoryConstruct.getStock());
    		}
    		/*sort (software[] software)
    		{
    			for (int a = 1; a < software.length; a++)
    			{
    				for (int b = 0; b < software.length - a; b++)
    				{
    					if (((software[b].getName()).compareToIgnoreCase((software[b+1].getName()))) > 0)
    					{
    						InventoryConstruct temp = software[b];
    						software[b] = software[b + 1];
    						software[b + 1] = temp;
    					}
    				}
    			}
    		}
     
    		//display ID number
    		System.out.printf("Product ID number: %d%n", software.getName());
    		//display product name
    		System.out.printf("Product name: %s%n", p1.getName());
    		//display product price
    		System.out.printf("The item's price is: $%.2f%n", p1.getPrice());
    		//display amount in stock
    		System.out.printf("Amount in stock: %d%n%n", p1.getStock());
    		//display inventory total
    		System.out.printf("The total value of inventory is: $%.2f", p1.getTotal());
    	}*/
    	}//end main
    }//end class

    import java.util.Comparator;
     
    public class InventoryConstruct implements Comparable<InventoryConstruct>
    {
    	private int itemNum;//declare ID number
    	private static String itemName;//declare item name
    	private static double itemPrice;//declare item price
    	private static int itemInStock;//declare amount in stock
    	private static double inventoryTotal;//declare total of inventory product
     
    	//initialize constructor
    	public InventoryConstruct(int itemNum, String itemName,             
    			double itemPrice, int itemInStock)
    	{
    		this.itemNum = itemNum;
    		InventoryConstruct.itemName = itemName;
    		InventoryConstruct.itemPrice = itemPrice;
    		InventoryConstruct.itemInStock = itemInStock;
    		setTotal();
    	}
    	//return ID number
    	public int getNum()
    	{
    		return itemNum;
    	}
    	//return item name
    	public static String getName()
    	{
    		return itemName;
    	}
    	//return item price
    	public static double getPrice()
    	{
    		return itemPrice;
    	}
    	//return amount in stock
    	public static int getStock()
    	{
    		return itemInStock;
    	}
    	//calculate total
    	public void setTotal()
    	{
    		inventoryTotal = itemPrice * itemInStock;
    	}
    	//return total
    	public double getTotal()
    	{
    		return inventoryTotal;
    	}
     
    	public static Comparator<InventoryConstruct> InventorySort
    		= new Comparator<InventoryConstruct>()
    	{
    		public int compare(InventoryConstruct software1, InventoryConstruct software2)
    		{
    			String itemName1 = InventoryConstruct.getName().toUpperCase();
    			String itemName2 = InventoryConstruct.getName().toUpperCase();
     
    			return itemName1.compareTo(itemName2);
    		}
    	};
     
    }//end class

    Any assistance would be GREATLY appreciated...

    This is what is printing out in the compiler when I run it (with errors)

    Welcome to the inventory tracker!

    ID#: 1 Name: Pro Tools Price: 299.99 In Stock: 6
    ID#: 3 Name: Pro Tools Price: 299.99 In Stock: 6

    Edit: I got it to print this out now (still with errors):

    Welcome to the inventory tracker!

    ID#: 1 Name: Pro Tools Price: 299.99 In Stock: 6
    ID#: 2 Name: Pro Tools Price: 299.99 In Stock: 6
    ID#: 3 Name: Pro Tools Price: 299.99 In Stock: 6
    ID#: 4 Name: Pro Tools Price: 299.99 In Stock: 6

    by changing it to for(i = 1; i <= 4; i++) and taking the increment off the print statement, but it is still printing only one of the object's info and I have no clue if the sort code is correct because of this.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Sorting objects of an array by String name

    Does it compile?

    In either case post the full text of the error messages

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting objects of an array by String name

    Quote Originally Posted by jps View Post
    Does it compile?

    In either case post the full text of the error messages
    Yes it does compile, but it doesn't print correctly. The only error I am getting is that I need to add unimplimented methods to InventoryConstruct (I know my naming sucks and I will change it and redo everything once I figure this out) so when I click the line to add that it adds this at the end:



    	@Override
    	public int compareTo(InventoryConstruct o) {
    		// TODO Auto-generated method stub
    		return 0;

    which leads me to believe there is something wrong with this line of my code:

     public int compare(InventoryConstruct software1, InventoryConstruct software2)

Similar Threads

  1. STRING sorting plz correct it
    By Rawezh Frya in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 27th, 2012, 12:05 PM
  2. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  3. help in sorting the array
    By alloka in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 15th, 2012, 01:54 PM
  4. Help with sorting Calendar objects!
    By leao in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 11:20 PM
  5. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM