Hello,
I am having trouble sorting my array. I have gotten it to read out just the names in ABC order, but I need it to display all of the attributes. I have read several sites trying to figure out how to do it and I keep getting errors. Here is my code, can anyone help me atleast understand if I am putting the code in the right spot. Most websites show sort() in its own file not being added to one. Thanks.
package inventory;
import java.text.DecimalFormat;
import java.util.Arrays;
/**
*
* @author Mel
*/
class Pizza {
// Private variables
private String name;
private int quantity;
private double price;
private int productNumber = 0;
// Default constructor uses other constructor to set the
// default state of the object.
public Pizza() {
this(0,"Unknown",0,0.00);
}
// Constructor that user can specify a name, quantity, and price for items.
public Pizza(int productNumber, String itemname, int quantityOnHand, double itemprice) {
productNumber = productNumber;
setName(itemname);
setQuantityOnHand(quantityOnHand);
setPrice(itemprice);
}
// Public mutators (changes state of the object)
public void setName(String itemname) {
name = itemname;
}
// Sets quantity on hand and if negative, defaults to zero.
public void setQuantityOnHand(int quantityOnHand) {
if (quantityOnHand > 0) {
quantity = quantityOnHand;
}
else { quantity = 0; }
}
// Set price of a product and defaults it to zero if negative.
public void setPrice(double itemPrice) {
if (itemPrice > 0.00) {
price = itemPrice;
}
else { price = 0.00; }
}
// Get the product's name
public String getName() {
return name;
}
public int getQuantityOnHand() {
return quantity;
}
public double getPrice() {
return price;
}
// Calculate the value of stock on this particular item.
public double getItemValue() {
return (price * (double)quantity);
}
// String representation of the product
public String toString() {
return name + " - " + price;
}
}
public class Inventory {
// Setup an array of Products (set it to hold 30 items)
int inventorySize = 30;
private Pizza items[] = new Pizza[inventorySize];
// Set the formatter to format values into currency form.
DecimalFormat formatter = new DecimalFormat("$##,###.00");
// Adds a product to the array of pizza. Adds to first empty slot found.
public void addPizza(Pizza item) {
for (int i = 0; i < inventorySize; i++) {
if (items[i] == null) {
items[i] = item;
return;
}
}
}
// Loop through our array of pizza and add up the total value.
// Go item by item adding the quantity on hand x its price.
// Add that value to a running total variable.
public double getTotalInvValue() {
double sumOfInventory = 0.0;
// Uses a for loop which iterates the array of items.
for (Pizza item : items) {
if (item != null) {
sumOfInventory += item.getItemValue();
}
}
return sumOfInventory;
}
// Prints the inventory list including name, quantity, price, and total stock value for each item.
public void printInventory() {
System.out.println("Printing items in inventory...\n");
boolean hasItems = false;
for (Pizza item : items) {
if (item != null) {
hasItems = true;
System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + " Value of Stock: " + formatter.format(item.getItemValue()));
}
}
// If no items were found, print a message saying the inventory is empty.
if (!hasItems) { System.out.println("Inventory is empty at the moment.\n"); }
}
}
// Inherited class PizzaRestock from the base class Pizza
class PizzaRestock extends Pizza {
// Holds the year the move was made
private String supplier;
public PizzaRestock(int productNumber, String itemname, int quantityOnHand, double itemprice, String supplier) {
// Pass on the values needed for creating the Product class first thing
super(productNumber, itemname, quantityOnHand, itemprice);
supplier = "";
}
// Get the supplier of this Pizza product
public String getSupplier() {
return supplier;
}
// Overrides getItemValue() in Product class by calling the base class version and
// adding a 5% restocking fee on top
public double getItemValue() {
return super.getItemValue() * 1.05;
}
// Simply gets the base class's value, and figures out the 5% restocking fee only
public double getRestockingFee() {
return super.getItemValue() * .05;
}
}
class InventoryTest {
static DecimalFormat formatter = new DecimalFormat("$##,###.00");
public static void main(String args[]) {
Pizza item1 = new Pizza(1234, "Dough", 22, 1.99);
Pizza item2 = new Pizza(1235, "Sauce", 35, 5.99);
Pizza item3 = new Pizza(1236, "Cheese", 4, 15.99);
Pizza item4 = new Pizza(1237, "Pepperoni", 3, 14.99);
Pizza item5 = new Pizza(1238, "Ham", 2, 16.99);
PizzaArray.sort(function(a,b){
var nameA=a.name.toLowerCase();, nameB=b.name.toLowerCase();
if (nameA < nameB); //sort string ascending
return -1;
if (nameA > nameB);
return 1;
return 0;//default return value (no sorting)
});
// Create an inventory item to store our products
Inventory myPizza = new Inventory();
// Let's show there is nothing in the inventory to start.
myPizza.printInventory();
// Now lets add the items we declared above.
myPizza.addPizza(item1);
myPizza.addPizza(item2);
myPizza.addPizza(item3);
myPizza.addPizza(item4);
myPizza.addPizza(item5);
// Print out the inventory
myPizza.printInventory();
// Print the total value of the inventory
// (after formatted using DecimalFormat class into dollars and cents)
System.out.println("\nTotal value of inventory is: " + formatter.format(myPizza.getTotalInvValue()));
}
}