Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How can I change the values in an object instantiated from a class formed from an abs

    How can I change the values in an object instantiated from a class formed from an abstract class?
    In this program I want to decrease the amount of stock from each flower count by using a function called subRoseStock() that takes in a variable called amtRosesInStock and subtracts the order value the customer gives from it. Here is the code:

    [HTML]//Flower Class
    public abstract class Flower{
    String name;
    String color;
    int orderAmt;
    double price;



    Flower(){

    name="Unknown name";
    color="Unknown color";
    orderAmt=0;
    price=0.00;

    }

    Flower(String name, String color, int orderAmt){
    this.name=name;
    this.color=color;
    this.orderAmt=orderAmt;
    }

    public String getFlowerName(){
    return name;
    }
    public double getFlowerPrice(){
    return price;
    }

    public String getFlowerColor(){
    return color;
    }
    public int getorderAmt() {
    return orderAmt;
    }



    }
    //Rose class

    public class Rose extends Flower {

    public Rose(){
    name="Rose";
    price=1.50;
    color="Red";
    orderAmt=0;

    }



    public Rose(String name, String color, int amtinstock) {
    super(name, color, amtinstock);

    }


    public String getFlowerName() {
    return name;

    }


    public double getFlowerPrice() {

    return price;
    }


    public String getFlowerColor() {

    return color;
    }

    public int getorderAmt() {
    return orderAmt;
    }



    }
    //FlowerInventory

    public class FlowerInventory {
    int amtRosesInStock;
    int amtGeraInStock;
    int amtOrchidInStock;

    public int getRoseStock(){
    return amtRosesInStock;
    }
    public int getGeraInStock(){
    return amtGeraInStock;
    }
    public int getOchidInStock(){
    return amtOrchidInStock;
    }

    public int restockRoses(){
    amtGeraInStock=100;
    return amtGeraInStock;
    }
    public int restockGeranium(){
    amtRosesInStock=100;
    return amtGeraInStock;
    }
    public int restockOrchid(){
    amtOrchidInStock=100;
    return amtOrchidInStock;
    }

    /*public void subRoseStock(int amtRosesInStock){
    amtRosesInStock=amtRosesInStock-Rose.getOrderAmt();



    }*/
    }[/HTML]

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There's too much thought here.
    Abstract class Flower does all the heavy work for you. All you need to do is simply write a single method in the concrete: getFlowerName, and only if you want to keep the flower abstract. Beyond this, just the constructors are required. You can choose to either name it or class it. Then make dynamic binding do all the work. Here's an example, I choose to use a Hashtable<Class<? extends Flower>, Integer> for my inventory type, mainly because I'm lazy. I would personally handle stock differently to track each flower including its colour.
    Code:
    import java.util.Hashtable;
    
    abstract class Flower
    {
    	protected String color;
    	protected int orderAmt;
    	protected double price;
    
    	public Flower()
    	{
    		this("Unknown Colour", 0, 0.0);
    	}
    
    	public Flower(String color, int orderAmt, double price)
    	{
    		this.color = color;
    		this.orderAmt = orderAmt;
    		this.price = price;
    	}
    
    	public abstract String getFlowerName();
    
    	public double getFlowerPrice()
    	{
    		return price;
    	}
    
    	public String getFlowerColor()
    	{
    		return color;
    	}
    
    	public int getorderAmt()
    	{
    		return orderAmt;
    	}
    }
    
    class Rose extends Flower
    {
    	public Rose()
    	{
    		this("Red", 0, 1.5);
    	}
    
    	public Rose(String s, int i, double d)
    	{
    		super(s, i, d);
    	}
    
    	public String getFlowerName()
    	{
    		return "Rose";
    	}
    }
    
    class Orchid extends Flower
    {
    	public Orchid()
    	{
    		this("White", 0, 1.75);
    	}
    
    	public Orchid(String s, int i, double d)
    	{
    		super(s, i, d);
    	}
    
    	public String getFlowerName()
    	{
    		return "Orchid";
    	}
    }
    
    public class FlowerInventory
    {
    	private Hashtable<Class<? extends Flower>, Integer> inStock;
    	
    	public FlowerInventory()
    	{
    		this.inStock = new Hashtable<Class<? extends Flower>, Integer>();
    	}
    	
    	public void stockFlower(Class<? extends Flower> f, int iAmount)
    	{
    		inStock.put(f, iAmount);
    	}
    	
    	public void subFlowerStock(Flower f)
    	{
    		inStock.put(f.getClass(), inStock.get(f.getClass()) - f.getorderAmt());
    	}
    
    	public int getFlowerInStock(Class<? extends Flower> c)
    	{
    		return inStock.get(c);
    	}
    	
    	public static void main(String... argv)
    	{
    		FlowerInventory fi = new FlowerInventory();
    		fi.stockFlower(Rose.class, 100);
    		fi.stockFlower(Orchid.class, 100);
    
    		Flower[] toBuy = new Flower[2];
    		toBuy[0] = new Rose("Red", 12, 1.25);
    		toBuy[1] = new Orchid("White", 8, 1.34);
    		for (Flower f : toBuy)
    		{
    			System.out.printf("Order for %d %s %s\n", f.getorderAmt(),
    					f.getFlowerColor(), f.getFlowerName());
    			fi.subFlowerStock(f);
    		}
    
    		System.out.println("Stock remaining for roses: " + fi.getFlowerInStock(Rose.class));
    	}
    }

  • Users who have thanked Fou-Lu for this post:

    babe20042004 (12-15-2011)

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your reply and your help. Time to add more features to my code


  •  

    Posting Permissions

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