Results 1 to 6 of 6
Thread: Sorting Arrays
- 02-24-2013, 06:41 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
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); } }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?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; } }
- 02-24-2013, 07:33 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Sorting Arrays
There are no items in your array and it should be of type InventoryProduct[], not Inventory[].
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-24-2013, 09:38 AM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Sorting Arrays
...and there is no loop and you only print the length :P
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
- 02-24-2013, 07:27 PM #4
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
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 :)
- 02-25-2013, 05:46 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
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()); } } }
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: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; } }
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)
- 02-25-2013, 09:02 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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:
As you can see this method return an int. That indicates the compare result either 0, a negative integer or a positive integer.Java Code:public int compareTo(T o) { ... }Website: Learn Java by Examples
Similar Threads
-
Sorting arrays
By Blondedude092 in forum New To JavaReplies: 2Last Post: 12-01-2012, 01:57 AM -
Sorting arrays
By Lex in forum New To JavaReplies: 4Last Post: 08-09-2011, 02:19 AM -
Help with sorting arrays
By Joycey in forum New To JavaReplies: 4Last Post: 03-30-2010, 08:35 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Sorting Two Arrays
By Faye Rett in forum New To JavaReplies: 4Last Post: 03-07-2010, 01:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks