By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,336 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,336 IT Pros & Developers. It's quick & easy.

Java Inventory part 4 problem

P: 2
Hello all, I'm new to this forum and Java and having a alot of problems understanding this language. I am working on an invetory program part 4. The assignment requirements are listed but the reading has very little in it that helps can someone take a look at this code and point me in the right direction please. Thanks for any help that is offered.

//Modify the Inventory Program to use a GUI. The GUI should display the
//informationone 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 GUI should display the value of
//the entire inventory, the additional attribute,and the restocking fee.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; // program uses class Scanner
  2.  
  3. public class Invprogram4
  4. {   
  5.    public static void main(String args [])// main method begins application
  6.      {
  7.      Inventory theinventory = new Inventory();
  8.      theinventory.printitem(1);
  9.      theinventory.printitem(2);
  10.      theinventory.printitem(3);
  11.      theinventory.printitem(4);
  12.      theinventory.printitem(5);
  13.      theinventory.printitem(6);
  14.  
  15.      } //End main Method
  16.  
  17.  
  18. }  //End class Invprogram4
  19.  
  20. class Inventory
  21. {
  22.    Stock[] stockarray = new Stock[10];//declares and sets up array
  23.  
  24.    public Inventory()
  25.      {
  26.      stockarray[1] = new Stock(1, "Hand Forged Forks", 5, 2.00);
  27.      stockarray[2] = new Stock(2, "Hand Forged Spoons", 5, 2.00);
  28.      stockarray[3] = new Stock(3, "Hand Forged Knives", 5, 3.00);
  29.      stockarray[4] = new Stock(4, "Hand Forged Hinged Sporks", 10, 5.00);
  30.      stockarray[5] = new Stock(5, "Throwing Knife Blanks", 5, 15.00);
  31.      stockarray[6] = new Forkprongs(6, "Forkprongs", 5, 10.00, 2);
  32.      }//close constructor
  33.  
  34.    public void printitem(int i)
  35.      {    
  36.      System.out.println("The Item number is " + stockarray[i].getproductUPC());
  37.      System.out.println("The Product Name is " + stockarray[i].getproductName());
  38.      System.out.println("The number of units in stock is " +     stockarray[i].getproductQuantity());
  39.      System.out.println("The price per piece is " + stockarray[i].getproductPrice());
  40.      System.out.println("The value of the inventory is " + stockarray[i].getStockValue());    
  41.      }//close printitem method
  42.  
  43.  
  44.    public void getTotalValue()  // total value getStockValue
  45.      {
  46.      int total = 0;
  47.  
  48.      for ( int i=1; i<6; i++ ) total += stockarray[i].getStockValue();
  49.      System.out.printf("Total of stockarray elements %d\n", total);
  50.      }
  51.  
  52.  
  53. }//close class Inventory
  54.  
  55. class Stock
  56.  
  57. {
  58.  
  59.      private double productUPC; // Item number or Universial Product Code
  60.      private String productName;//name of product
  61.      private double productQuantity;//number of pieces in stock
  62.      private double productPrice; //price per piece
  63.      private double productValue; //price of all pieces in a UPC
  64.  
  65.    public Stock(double upc, String name, double quantity, double price)
  66.      {
  67.      //assign variables 
  68.      productUPC = upc;
  69.      productName = name;
  70.      productQuantity = quantity;
  71.      productPrice = price;
  72.      }  //end constructor
  73.  
  74. //collection of set and get methods
  75.  
  76.    public void setproductUPC(double upc) 
  77.      { productUPC = upc; }
  78.  
  79.    public double getproductUPC() 
  80.      { return productUPC; }
  81.  
  82.    public void setproductName(String name) 
  83.      { productName = name; }
  84.  
  85.    public String getproductName() 
  86.      { return productName; }
  87.  
  88.    public void setproductQuantity(double quantity) 
  89.      { productQuantity = quantity; }
  90.  
  91.    public double getproductQuantity() 
  92.      { return productQuantity; }
  93.  
  94.    public void setproductPrice(double price) 
  95.      { productPrice = price; }
  96.  
  97.    public double getproductPrice() 
  98.      { return productPrice; }
  99.  
  100.    public double getStockValue()
  101.      { return this.getproductPrice() * productQuantity; }
  102.  
  103.  
  104. } //end class Stock
  105.  
  106. // This class defines types of forks named forkprongs it is a subclass of forks.  
  107. //It is a stock, but has the extra feature of having prongs as a type and adds a 
  108. //5% restocking fee.
  109.  
  110.    class Forkprongs extends Stock 
  111.      { // starts subclass 
  112.    private int productNumProngs;
  113.  
  114.    public Forkprongs(double UPC, String Name, double Quantity, double Price, 
  115.      int NumProngs)
  116.      {
  117.      super(UPC, Name, Quantity, Price);
  118.      productNumProngs = NumProngs;
  119.      }
  120.  
  121.    public double getproductPrice() 
  122.      {
  123.      double productPrice = super.getproductPrice();
  124.      return (productPrice * 1.05);
  125.      }
  126.  
  127.  
  128. // set and get method for subclass
  129.    public void setproductNumProngs(int NumProngs) 
  130.      { productNumProngs = NumProngs; }
  131.  
  132.    public int getproductNumProngs() 
  133.      { return productNumProngs; }
  134.  
  135. }// end class Forkprongs
  136.  
Jul 5 '08 #1
Share this Question
Share on Google+
2 Replies

P: 2
OK, I just came back to this and discovered my spelling was horrific. I apologize.
Jul 5 '08 #2

Expert 10K+
P: 11,448
Your current classes have set up a 'model' of your stock and a bit of a 'view'
that displays the entire inventory (btw, arrays start their index values at 0, not
at the value 1). Your last assignment wants you to replace your 'view' by a GUI
based view, not just the console.

For starters have a look at the JFrame class, you'll definitely need it and have
a look at the JTable class; it can display the 'records' of your inventory. There
isn't much of a 'controller' in your program because you don't want to, or have to
do much with your inventory, you just have to display it.

kind regards,

Jos
Jul 6 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.