Results 1 to 1 of 1
- 02-26-2009, 03:43 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
Need help with proofreading a program
/Inventory.java
//Program that displays the value of a product in a office supplies.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Locale;
import java.text.NumberFormat;
// Class that represents a product in an inventory
class Product implements Comparable
{
private double itemnum; // class variable that stores Item number
private double prodprice; // class variable that stores tprice of the product
private double numinstock; // class variable that stores the number of product in stock
private String prodname; //class variable for the name of the product
public Product() // Constructor for the Item
{
itemnum = 0.0;
prodprice = 0.0;
numinstock = 0.0;
}
public void setItemNum(double num) // Method to set the Item number
{
itemnum = num;
}
public double getitemnumber() // Method to get the Item number
{
return itemnum;
}
public void setprodprice(double prod) // Method to set the Price of the product
{
prodprice = prod;
}
public double getprodprice() // Method to get the Price of the product
{
return prodprice;
}
public void setnuminstock(double numstock) // Method to set the Price of the product
{
numinstock = numstock;
}
public double getnuminstock() // Method to get the Price of the product
{
return numinstock;
}
public void setprodname(String name) // Method to set the name of the product
{
prodname = name;
}
public String getprodname() // Method to get the name of the product
{
return prodname;
}
public double calculateValue() // Method to calculate the weekly pay
{
return prodprice * numinstock;
}
// the compareTo method is used to implement the Comparable interface. This enables us to sort a list of Products using Arrays.sort()
// this method returns -1, 0, or 1 depending on if the compared to object should appear before, the same, or after the current item
public int compareTo (Object o)
{
Product p = (Product) o;
return prodname.compareTo(p.getprodname());
}
// returns a string representation of the product
public String toString()
{
return "Product Name : " + getprodname() + "\n"
+ "Product Number : " + getitemnumber() + "\n"
+ "Product Price : " + NumberFormat.getCurrencyInstance(Locale.US).format (getprodprice()) + "\n"
+ "Number in Stock : " + getnuminstock() + "\n"
+ "Value : " + NumberFormat.getCurrencyInstance(Locale.US).format (calculateValue());
}
}//end class Product
// Class that represents a collection of Products
class Inventory {
// place to store all the Products in the inventory
Product[] inventory;
// create an initially empty inventory capable of holding different Products
public Inventory( )
{
inventory = new Product[0];
}
// add the Products to the end of the inventory
public void addProduct(Product new_Product)
{
// create a new array of Products that can hold one more than the current array of Products
Product[] new_inventory = new Product[this.getSize() + 1];
//copy all the old ones to the front of the new array
for (int i = 0; i < this.getSize(); i++) {
new_inventory[i] = inventory[i];
}
// add the new Product to the end of the new array
new_inventory[this.getSize()] = new_Product;
// replace the old inventory with the new one
inventory = new_inventory;
}
// return the Products at the location index in the inventory
public Product getProduct(int index)
{
return inventory[index];
}
// return the number of Products in the inventory
public int getSize()
{
return inventory.length;
}
// run through all the Products in the inventory, and add up their value.
public double getTotalValueOfAllInventory()
{
double tot = 0.00;
for(int i = 0; i < getSize(); i++)
{
tot += inventory[i].calculateValue();
}
return tot;
}
// sort the Products in the inventory by movie title
public void sortInventory()
{
Arrays.sort(inventory, 0, getSize());
}
}
// Program that displays the value of a product in inventory.
public class InventoryPart2
{
public static void main( String args[] ) throws IOException
{
// create Scanner to obtain input from command window
BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );
//Instantiate an Inventory object
Inventory myInventory = new Inventory();
//Print out a screen title
System.out.println("Inventory Program\n\n");
// we're going to keep asking for user input until the user enters stop for an item name
while(true) {
//Instantiate a new Product object each time we could get input from the user
Product myItem = new Product();
System.out.println( "Enter the name of product or the word stop: " ); // prompt for input
myItem.setprodname(input.readLine());// read item name or stop
// if the user entered stop for the item name, then quit the loop
if ( myItem.getprodname().equalsIgnoreCase("stop") ) {
break;
}
//Enter Item number
System.out.print( "Enter the Item number: " ); // prompt for input
double d = Double.valueOf(input.readLine()).doubleValue();
myItem.setItemNum(d);// read item number
// while loop to repeat the entry of the item number until a positive value is entered
while (myItem.getitemnumber()<=0.0)
{
System.out.println("Wrong Entry");
System.out.println("Please enter a positive Item Number:");
myItem.setItemNum(Double.valueOf(input.readLine()) .doubleValue()); // read Item number
}
// Enter Number of Items in Stock
System.out.print("Enter number of Items in Stock:");
myItem.setnuminstock(Double.valueOf(input.readLine ()).doubleValue()); // read Number of Items in Stock
// while loop to repeat the entry of Items in stock until a positive value is entered
while (myItem.getnuminstock()<=0.0)
{
System.out.println("Wrong Entry");
System.out.println("Please enter a positive Number in Stock:");
myItem.setnuminstock(Double.valueOf(input.readLine ()).doubleValue()); // read Item number
}
// Enter price of the Item
System.out.print( "Enter the price of the item: " ); // prompt for input
myItem.setprodprice(Double.valueOf(input.readLine( )).doubleValue()); // Read price of the product
// while loop to repeat the entry of Items in stock until a positive value is entered
while (myItem.getprodprice()<=0.0)
{
System.out.println("Wrong Entry");
System.out.println("Please enter a positive Item Price:");
myItem.setprodprice(Double.valueOf(input.readLine( )).doubleValue()); // read Item Price
}
// add the newly created object to the existing inventory
myInventory.addProduct(myItem);
} // end while
// Now that all the products have been added to the inventory object myInventory, sort them by their name
myInventory.sortInventory();
// Display the inventory of Products on the screen, one Product at a time
for (int i = 0; i < myInventory.getSize(); i++) {
System.out.println(myInventory.getProduct(i)); // returns the product at location i and then calls toString() to get the report about the product
System.out.println();
}
// Display the total value of the inventory on the screen
System.out.println("The Total Value of the inventory is: " + NumberFormat.getCurrencyInstance(Locale.US).format (myInventory.getTotalValueOfAllInventory()));
} // end method main
} // end class InventoryPart2
Similar Threads
-
Proofreading this small Java program
By almina in forum New To JavaReplies: 5Last Post: 10-23-2009, 07:42 AM -
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Money Conversion - Proofreading pls?
By javanewbie in forum New To JavaReplies: 5Last Post: 08-17-2008, 01:40 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks