Results 1 to 9 of 9
  1. #1
    VegasLarry is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Accessing sub classes

    You are all probably tired of seeing this... But I am stuck.

    This is what I need to do;
    "Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product."

    I have three files, Office Supplies (main), Product, and extensions.
    If I put my restock fee code into Product, I can access it and use it.
    When I try to access it from Products subclass extensions...
    I also have the GUI done, so not worried about that.
    The added information I should be able to understand after I get restock down pat.
    Last edited by VegasLarry; 08-29-2012 at 05:09 AM. Reason: Delete old code!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Accessing sub classes

    Quote Originally Posted by VegasLarry View Post
    You are all probably tired of seeing this... But I am stuck.
    You show us quite a bit of code, some requirements and a vague statement of being "stuck". Hopefully I'm wrong, but my experience here is that you aren't going to get a lot of help unless you ask a specific question. Please tell us exactly where you're stuck including all the gory details. I'm afraid if you don't do this, this thread will die a quiet death.

  3. #3
    VegasLarry is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Accessing sub classes

    OK, I am sorry, I thought I was clearer.

    I need to get the restocking fee from the subclass of Product, extensions to my main program Office Supplies.
    If I have it in Product, I can get it to fill in my text field, restockField.
    I do not understand how to get from a to b.
    I am not very good at explaining...

    The restocking fee needs to be calculated in the subclass extensions.
    Which it does not (?) as I have used System.out.println statements to see what happened.

    Shouldn't this (in the subclass extensions) return the fee?
    Java Code:
        public double getRestock() {
            restock = .05 * getProductInStock() * getProductEachPrice();
            System.out.println(restock); // test print to console
            return restock;
        }
    The above appears to do nothing by the way...

    Doing this;
    Java Code:
    	restockField.setValue(Pencil[i].getRestock());
    to get the fee from Office Supplies (the main prg)?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Accessing sub classes

    Quote Originally Posted by VegasLarry View Post
    "Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product."
    It tells you to create a method to calculate the restocking fee. Does it tell you to give this new class a restock field? You've given it a restock field -- but does setting this field in the constructor or in the setRestock(...) method have any effect? In other words, do you use the values that you've set for this field to do any calculations anywhere? Should it in fact have a restock field if you really don't do anything with it?

    Next does your current code even compile? If not are you seeing any compilation errors? If so, perhaps you should post the errors and indicate which lines are causing the errors.

    Next, it appears that you've given the Product class a getRestock() method. Are you supposed to modify Product? Where do your instructions tell you to do this?

    Finally, please learn and abide by the Java naming rules. That means that all class names start with a capital letter and all variable names with a lower case letter. It is very hard to try to understand someone else's code, especially a newbie's code, that they really shouldn't make it any harder for us to understand. You should change this now please.

    Edit: what you likely should do is remove getRestock() from Product, have your new class, perhaps called RestockableProduct, have this method (and no restock field), and then use objects of the new class in your GUI main program.
    Last edited by Fubarable; 08-29-2012 at 04:44 AM.

  5. #5
    VegasLarry is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Accessing sub classes

    It tells you to create a method to calculate the restocking fee.
    Does it tell you to give this new class a restock field?
    Yes, in the GUI the restocking fee must be displayed.

    You've given it a restock field -- but does setting this field in the constructor or in the setRestock(...) method have any effect? In other words, do you use the values that you've set for this field to do any calculations anywhere?
    This is where the problem arises.
    If I use the restock method in the Product class it works fine.
    If I comment out the restock method in Product and attempt to use the restock method in Extensions
    I get an error in Eclipse that says "The method getRestock() is undefined for the type Product"
    If Extensions is a sub class of Product, shouldn't it work?
    Do I need some form of call in Product to use Extensions?

    Should it in fact have a restock field if you really don't do anything with it?

    Restock field is required in the GUI window.


    Next does your current code even compile? If not are you seeing any compilation errors? If so, perhaps you should post the errors and indicate which lines are causing the errors.

    Runs sweet, with the exception of trying to use the subclass method of getting the restock fee. Using the method in Product, no errors.

    Next, it appears that you've given the Product class a getRestock() method. Are you supposed to modify Product? Where do your instructions tell you to do this?
    No, I am not supposed to do that. It was a test to see if I was doing anything right, because it errored (see above) when trying to use it from the subclass.

    Fixed code;

    Office Supplies (Main)
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    import javax.swing.JButton;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class OfficeSupplies {
    	static double tv = 0.00;
    	static int i = 0;
    	static DecimalFormat money = new DecimalFormat("$0.00");
    	static double restock;
    
    	public static void main(String[] args) {
    		// Insert of Inventory code
    
    		// Three array elements
    		final Product[] Pencil = new Product[3];
    
    		// Set array element Zero
    		Pencil[0] = new Product("Pencil, Number Two", 123456, 50,
    				1.50, 0.0, 0.0);
    
    		// Set array element One
    		Pencil[1] = new Product("Pen, Bic", 678904, 100, 2.50,
    				0.0, 0.0);
    
    		// Set array element Two
    		Pencil[2] = new Product("Pen, Waterman", 546987, 5,
    				450.00, 0.0, 0.0);
    		int i2 = Pencil.length;
    		// End of insert
    		JFrame frame = new JFrame("Office Supplies, Inc. Inventory");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		// No Layout Manager
    		panel.setLayout(null); // We are using absolute positioning.
    
    		// Define Buttons and Naming them
    		JButton nextButton = new JButton("Next");
    		JButton previousButton = new JButton("Previous");
    		JButton lastButton = new JButton("Last");
    
    		// Define Labels and assign display Names
    		JLabel nameLabel = new JLabel("Product");
    		JLabel skuLabel = new JLabel("SKU");
    		JLabel unitsLabel = new JLabel("Units in Stock");
    		JLabel priceLabel = new JLabel("Price per Unit");
    		JLabel stockvalueLabel = new JLabel("Value of Stock");
    		JLabel storevalueLabel = new JLabel("Total Store Inventory Value");
    		JLabel addinfoLabel = new JLabel("Additional Info");
    		JLabel restockLabel = new JLabel("Product Restocking Fee @ 5%");
    
    		// Define Text fields and declaring their length
    		final JTextField nameField = new JTextField(100);
    		final JFormattedTextField skuField = new JFormattedTextField(
    				DecimalFormat.getIntegerInstance());
    		final JFormattedTextField unitsField = new JFormattedTextField(
    				NumberFormat.getIntegerInstance());
    		final JFormattedTextField priceField = new JFormattedTextField(
    				DecimalFormat.getCurrencyInstance());
    		final JFormattedTextField stockvalueField = new JFormattedTextField(
    				DecimalFormat.getCurrencyInstance());
    		final JFormattedTextField storevalueField = new JFormattedTextField(
    				DecimalFormat.getCurrencyInstance());
    		final JFormattedTextField addinfoField = new JFormattedTextField(100);
    		final JFormattedTextField restockField = new JFormattedTextField(
    				DecimalFormat.getCurrencyInstance());
    
    		// Position values are x (from left), y (from top), width, height
    		// Set Buttons Positions
    		nextButton.setBounds(50, 40, 85, 30);
    		previousButton.setBounds(348, 40, 110, 30);
    		lastButton.setBounds(644, 40, 85, 30);
    
    		// Set Labels Positions
    		nameLabel.setBounds(50, 73, 184, 28);
    		skuLabel.setBounds(50, 173, 184, 28);
    		unitsLabel.setBounds(50, 273, 184, 28);
    		priceLabel.setBounds(50, 373, 184, 28);
    		stockvalueLabel.setBounds(50, 473, 184, 28);
    		storevalueLabel.setBounds(495, 173, 184, 28);
    		addinfoLabel.setBounds(495, 273, 184, 28);
    		restockLabel.setBounds(495, 373, 184, 28);
    
    		// Set Text fields Positions
    		nameField.setBounds(50, 105, 300, 40);
    		skuField.setBounds(50, 205, 300, 40);
    		unitsField.setBounds(50, 305, 300, 40);
    		priceField.setBounds(50, 405, 300, 40);
    		stockvalueField.setBounds(50, 505, 300, 40);
    		storevalueField.setBounds(495, 205, 250, 40);
    		addinfoField.setBounds(495, 305, 250, 40);
    		restockField.setBounds(495, 405, 250, 40);
    
    		// Add Buttons to Panel
    		panel.add(nextButton);
    		panel.add(previousButton);
    		panel.add(lastButton);
    
    		// Add Labels to Panel
    		panel.add(nameLabel);
    		panel.add(skuLabel);
    		panel.add(unitsLabel);
    		panel.add(priceLabel);
    		panel.add(stockvalueLabel);
    		panel.add(storevalueLabel);
    		panel.add(addinfoLabel);
    		panel.add(restockLabel);
    
    		// Add Text fields to Panel
    		panel.add(nameField);
    		panel.add(skuField);
    		panel.add(unitsField);
    		panel.add(priceField);
    		panel.add(stockvalueField);
    		panel.add(storevalueField);
    		panel.add(addinfoField);
    		panel.add(restockField);
    		frame.add(panel);
    		frame.setSize(800, 625);
    		frame.setVisible(true);
    
    		// Calculate Total Inventory Value Needs to be in Method!
    
    		for (i2 = 0; i2 < Pencil.length; i2++) {
    			tv = Pencil[i2].getProductValue() + tv;
    			Pencil[i2].setTotalValue(tv);
    		}
    
    				
    		// Let's set the initial array into the text fields!
    		i = 0;
    		nameField.setText(Pencil[i].getProductName());
    		skuField.setValue(Pencil[i].getProductSku());
    		unitsField.setValue(Pencil[i].getProductInStock());
    		priceField.setValue(Pencil[i].getProductEachPrice());
    		stockvalueField.setValue(Pencil[i].getProductValue());
    		storevalueField.setValue(Pencil[i2 - 1].getTotalValue());
    		addinfoField.setText("A");
    		 // restock = (Pencil[i].getProductEachPrice()) * .05; // needs to call from subclass
    		 restockField.setValue(Pencil[i].getRestock());
    		
    		
    
    		// Add action listener to nextButton
    		nextButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				// Execute when button is pressed
    				i++;
    				if (i >= (Pencil.length)) {
    					i = 0;
    				}
    				nameField.setText(Pencil[i].getProductName());
    				skuField.setValue(Pencil[i].getProductSku());
    				unitsField.setValue(Pencil[i].getProductInStock());
    				priceField.setValue(Pencil[i].getProductEachPrice());
    				stockvalueField.setValue(Pencil[i].getProductValue());
    				addinfoField.setText("A");
    				// restock = (Pencil[i].getProductEachPrice()) * .05; // needs to call from subclass
    				restockField.setValue(Pencil[i].getRestock());
    				
    			}
    		});
    		
    		// Add action listener to previousButton
    		previousButton.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    				// Execute when button is pressed
    				i--;
    				if (i < 0) {
    					i = (Pencil.length-1);
    				}
    				nameField.setText(Pencil[i].getProductName());
    				skuField.setValue(Pencil[i].getProductSku());
    				unitsField.setValue(Pencil[i].getProductInStock());
    				priceField.setValue(Pencil[i].getProductEachPrice());
    				stockvalueField.setValue(Pencil[i].getProductValue());
    				addinfoField.setText("A");
    				// restock = (Pencil[i].getProductEachPrice()) * .05; // needs to call from subclass
    				restockField.setValue(Pencil[i].getRestock());
    				
    				i = i--;
    			}
    		});
    		
    		// Add action listener to lastButton
    		lastButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				// Execute when button is pressed
    				i = (Pencil.length - 1);
    				nameField.setText(Pencil[i].getProductName());
    				skuField.setValue(Pencil[i].getProductSku());
    				unitsField.setValue(Pencil[i].getProductInStock());
    				priceField.setValue(Pencil[i].getProductEachPrice());
    				stockvalueField.setValue(Pencil[i].getProductValue());
    				addinfoField.setText("A");
    				// restock = (Pencil[i].getProductValue()) * .05; // needs to call from subclass
    				restockField.setValue(Pencil[i].getRestock());
    				
    			}
    		});
    	}
    
    
    
    }
    Product;
    Java Code:
    // Declare class
    
    public class Product {
    	// Declare and set default values
    	String productName = null; // name of product
    	int productSku = 0; // SKU for product
    	int productInStock = 0; // quantity of product in stock
    	double productEachPrice = 0.00; // unit price for product
    	double productValue = 0.00; // Value of Product in Stock
    	double totalValue = 0.00; // Value of All Products in Stock
    	double tv = 0.00; // Working variable for Total Value
    	double pv = 0.00; // Working variable for Total Products Value
    	double restock;
    	int i = 0; // Used as a counter...
    
    	// end declare class
    
    	// Constructors
    
    	public Product() {
    
    	}
    
    	public Product(String productName, int productSku, int productInStock,
    			double productEachPrice, double productValue, double totalValue) {
    		setProductName(productName);
    		setProductSku(productSku);
    		setProductInStock(productInStock);
    		setProductEachPrice(productEachPrice);
    		setProductValue(productValue);
    		setTotalValue(totalValue);
    	}
    
    	// End Constructors
    
    	// Start Methods
    
    	// Start Getters
    
    	public String getProductName() {
    		return productName;
    	}
    
    	public int getProductSku() {
    		return productSku;
    	}
    
    	public int getProductInStock() {
    		return productInStock;
    	}
    
    	public double getProductEachPrice() {
    		return productEachPrice;
    	}
    
    	public double getProductValue() {
    		productValue = productEachPrice * productInStock;
    		tv = tv + productValue;
    		return productValue;
    	}
    
    	public double getTotalValue() {
    
    		return totalValue;
    	}
    
    	// End Getters
    
    	// Start Setters
    
    	void setProductName(String productName) {
    		this.productName = productName;
    	}
    
    	void setProductSku(int productSku) {
    		this.productSku = productSku;
    	}
    
    	void setProductInStock(int productInStock) {
    		this.productInStock = productInStock;
    	}
    
    	void setProductEachPrice(double productEachPrice) {
    		this.productEachPrice = productEachPrice;
    	}
    
    	void setProductValue(double productValue) {
    		this.productValue = productValue;
    	}
    
    	void setTotalValue(double totalValue) {
    		this.totalValue = totalValue;
    
    	}
    
    	// End Setters
    
    	// End Methods
    
    	public double getRestock() {
    		restock = .05 * getProductInStock() * getProductEachPrice();
    		return restock;
    	}
    
    }
    Extentions
    Java Code:
    public class Extensions extends Product {
    
    	@SuppressWarnings("unused")
    	double restock;
    	String addInfo;
    
    	public Extensions(String productName, int productSku, int productInStock,
    			double productEachPrice, double productValue, double totalValue,
    			double restock, String addInfo) {
    
    		super(productName, productSku, productInStock, productEachPrice,
    				productValue, totalValue);
    		this.setRestock(restock);
    		this.setAddInfo(addInfo);
    	}
    
    	public void setRestock(double restock) {
    		this.restock = restock;
    	}
    
    	public double getRestock() {
    		restock = .05 * getProductInStock() * getProductEachPrice();
    		return restock;
    	}
    
    	public void setAddInfo(String addInfo) {
    		this.addInfo = addInfo;
    	}
    
    	public String getAddInfo(String addInfo) {
    		return addInfo;
    	}
    
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Accessing sub classes

    Quote Originally Posted by VegasLarry View Post
    It tells you to create a method to calculate the restocking fee.
    Does it tell you to give this new class a restock field?
    Yes, in the GUI the restocking fee must be displayed.
    You misunderstand me. Ignore the GUI for now. The question is, do your instructions tell you that the class that extends Product should have a restock field. This has nothing to do with the GUI and all to do with class construction. If you think that your instructions tell you to do this, please show me where in your instructions text.

    You've given it a restock field -- but does setting this field in the constructor or in the setRestock(...) method have any effect? In other words, do you use the values that you've set for this field to do any calculations anywhere?
    This is where the problem arises.
    If I use the restock method in the Product class it works fine.
    If I comment out the restock method in Product and attempt to use the restock method in Extensions
    I get an error in Eclipse that says "The method getRestock() is undefined for the type Product"
    If Extensions is a sub class of Product, shouldn't it work?
    Nope, only if you use *Extensions* objects in place of Products objects. The parent class should have no knowledge of its child classes including any new methods it may have.

    Consider posting your entire instructions, because based on what I'm seeing, I don't think that you should have a getRestock() even, that instead Extension should override an existing method in Product that "calculates the value of the inventory of a product", and that uses a restock multiplier when doing this calculation.

    Do I need some form of call in Product to use Extensions?
    No, definitely not.

    Should it in fact have a restock field if you really don't do anything with it?

    Restock field is required in the GUI window.
    Again, we're leaving the GUI out of this for now.

    Next does your current code even compile? If not are you seeing any compilation errors? If so, perhaps you should post the errors and indicate which lines are causing the errors.

    Runs sweet, with the exception of trying to use the subclass method of getting the restock fee. Using the method in Product, no errors.
    It compiles because you did a kludge -- you gave Products a method that it shouldn't have.

    Next, it appears that you've given the Product class a getRestock() method. Are you supposed to modify Product? Where do your instructions tell you to do this?
    No, I am not supposed to do that. It was a test to see if I was doing anything right, because it errored (see above) when trying to use it from the subclass.
    You shouldn't do this then.

  7. #7
    VegasLarry is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Accessing sub classes

    Before the GUI everything displayed in a DOS window or in the console.
    So it would not have been a filed, it would have been a print statement.
    Anyway, here is the complete assignment .

    CheckPoint
    Inventory Program Part 2
    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.


    CheckPoint
    Inventory Program Part 3

    Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

    Modify the output to display this additional feature you have chosen and the restocking fee.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Accessing sub classes

    Quote Originally Posted by VegasLarry View Post
    CheckPoint
    Inventory Program Part 3

    Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

    Modify the output to display this additional feature you have chosen and the restocking fee.
    So you are to create a class that is a more specific type of Product, such as a DVD class that has a field (for DVD it would be String movieTitle) with a constructor that can handle this, with getters and setters for that I imagine. It also will need to be able to calculate restocking fee and use that to alter the value returned by the current Product method that calculates the value of the inventory. I see no instructions telling you to give this class a restock field, and I strongly urge you to in fact not give this class this field. There's no need for the moment for this, unless you decide to make it some number other than 5% (you don't).

  9. #9
    VegasLarry is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Accessing sub classes

    Passing this class has become a mute point. It is a matter of learning for myself now.

    This is the assignment;
    Modify the Inventory Program to use a GUI.
    The GUI should display;
    the information one product at a time
    item number
    name of the product
    number of units in stock
    price of each unit
    value of the inventory of that product.

    In addition, the GUI should display
    value of the entire inventory
    the additional attribute
    the restocking fee.

    What I need to know is how to access the method 'restock' in Extensions and make it available to my main program.

    Does Product call Extensions in some manner to get the result?
    Do I have to tell it to do it?

Similar Threads

  1. Association Classes for these classes
    By kevinkhan in forum New To Java
    Replies: 9
    Last Post: 03-21-2012, 05:22 PM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  3. Use classes inside of other classes
    By BlankFile in forum New To Java
    Replies: 2
    Last Post: 02-06-2012, 01:51 PM
  4. Replies: 1
    Last Post: 09-09-2011, 06:57 PM
  5. Accessing objects between classes
    By bikashg in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 12:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •