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.