Thanks
public class testInventory {
public static void main(String args[]){
// Create an inventory object
Inventory inv = new Inventory();
// Create some products to put into inventory
DVD movie = new DVD(1,"Cars", 10, 23.99,2007);
DVD movie = new DVD(2,"American Gangster", 4, 11.99,2008);
DVD movie = new DVD(3, "Gladiator DVD", 3, 15.99, 2000);
// Add the products to the inventory object using addProduct
inv.addProduct(product1);
inv.addProduct(product2);
inv.addProduct(movie);
// Now you could display the inventory total
System.out.println("Total inventory value is: " + inv.getTotalInvValue());
}
}
import java.text.DecimalFormat;
public class Inventory {
int inventorySize = 30;
private Product items[] = new Product [inventorySize];
DecimalFormat formatter = new DecimalFormat ("$##,###.00");
// Adds a product to the array of products.
public void addProduct(Product item) {
for (int i =0; i < inventorySize; i++) {
if (items[i] == null) {
items[i] = item;
return;
}
}
}
public double getTotalInvValue() {
double sumOfInventory = 0.0;
for (Product item : items) {
if (item != null) {
sumOfInventory += item.getItemValue();
}
}
return sumOfInventory;
}
public void printInventory() {
System.out.println("Printing items in inventory...\n");
boolean hasItems = false;
for (Product 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"); }
}
}
public class Product{
//Private variables
private String name;
private int quantity;
private double price;
private int productid = 0;
public Product() {
this(0,"American Gangster",4,19.99);
}
public Product(int productID, String itemname, int quantityOnHand,double itemprice) {
productid = productID;
setName(itemname);
setQuantityOnHand(quantityOnHand);
setPrice(itemprice);
}
public void setName(String itemname) {
name = itemname;
}
public void setPrice(double itemPrice) {
if (itemPrice > 0.00) {
price =itemPrice;
}
else { price = 0.00;}
}
public void setQuantityOnHand(int theQuantity) {
quantity = theQuantity;
}
public String getName() {
return name;
}
public int getQuantityOnHand() {
return quantity;
}
public double getPrice() {
return price;
}
public double getItemValue() {
return (price * (double)quantity);
}
public String toString() {
return name + "-" + price;
}
}
// Inherited class DVD from the base class Product
public class DVD extends Product {
// Holds the year of movie
private int movieyear;
public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) {
super(productId, itemname,quantityOnHand, itemprice);
movieyear = year;
}
public void setYear(int year) {
movieyear = year;
}
//Get the year of this DVD
public int getYear() {
return movieyear;
}
// adding a 5% restocking fee
public double getItemValue() {
return super.getItemValue() *1.05;
}
public double getRestockinkingFee() {
return super.getItemValue() * .05;
}
}

New Topic/Question
Reply




MultiQuote





|