Ronitrocket
Ronitrocket

Reputation: 1

How can I access a child method in an ArrayList of parent type?

I am working on a problem wherein I need to create a virtual Dessert Shoppe with Java classes. I need to print a sort of receipt that is formatted in a certain way, and I have a class Sundae. I also have an ArrayList of type DessertItem (superclass to Ice Cream, which is superclass to Sundae). I want to access a method in Sundae from this ArrayList, but it won't let me as the ArrayList type is of DessertItem.

Here is the DessertItem class:

public abstract class DessertItem {
  
  protected String name;
  
  public DessertItem() {
    this("");
  }

  public DessertItem(String name) {
    if (name.length() <= DessertShoppe.MAX_ITEM_NAME_SIZE)
      this.name = name;
    else 
      this.name = name.substring(0,DessertShoppe.MAX_ITEM_NAME_SIZE);
  }

  public final String getName() {
    return name;
  }

  public abstract int getCost();
}

The Checkout class (which has the receipt):

import java.util.ArrayList;

public class Checkout {
    
    ArrayList<DessertItem> dessertItems = new ArrayList<DessertItem>();

    public int cost;

    public void clear() {
        dessertItems.clear();
    }

    public void enterItem(DessertItem item) {
        dessertItems.add(item);
    }

    public int numberOfItems() {
        return dessertItems.size();
    }

    @Override
    public String toString() {
        String reciept = "      " + DessertShoppe.STORE_NAME + "\n" +
                        "      --------------------\n" +
                        " \n";
        cost = 0;

        for (int i = 0; i < dessertItems.size(); i++) {
            if (dessertItems.get(i) instanceof IceCream) {
                reciept = reciept + " " + dessertItems.get(i).getName() + "       " + Integer.toString(dessertItems.get(i).getCost()) + "\n";
            } else if (dessertItems.get(i) instanceof Sundae) {
                reciept = reciept + (" Unfinished\n");
//              string.concat(" " + dessertItems.get(i).getName()).concat("       " + Integer.toString(dessertItems.get(i).getCost())).concat("\n");
            } else if (dessertItems.get(i) instanceof Candy) {
//              string.concat(" " + dessertItems.get(i).getName()).concat("       " + Integer.toString(dessertItems.get(i).getCost())).concat("\n");
            } else if (dessertItems.get(i) instanceof Cookie) {
                reciept = reciept + (" Unfinished\n");
//              string.concat(" " + dessertItems.get(i).getName()).concat("       " + Integer.toString(dessertItems.get(i).getCost())).concat("\n");
            }

            cost += dessertItems.get(i).getCost();
        }

        double taxRate = DessertShoppe.TAX_RATE;
        double tax = taxRate / 100;

        reciept.concat(" \n");
        reciept.concat(" Tax                         " + tax + "\n");
        reciept.concat(" Total Cost                " + (cost * (1 + tax)) + "\n");
        
        return reciept;
    }

    public int totalCost() {
        cost = 0;

        for (int i = 0; i < dessertItems.size(); i++) {
            cost += dessertItems.get(i).getCost();
        }

        return cost;
    }

    public int totalTax() {
        cost = 0;

        for (int i = 0; i < dessertItems.size(); i++) {
            cost += dessertItems.get(i).getCost();
        }
        double taxRate = DessertShoppe.TAX_RATE;
        double tax = taxRate / 100;

        return (int) Math.round(cost * tax);
    }
}

The Sundae class:

public class Sundae extends IceCream {

    public String toppingName;
    public int cost;
    public int toppingCost;

    public Sundae() {
        this("", 0, "", 0);
    }

    public Sundae(String newName, int newCost, String newToppingName, int newToppingCost) {
        if (newName.length() <= DessertShoppe.MAX_ITEM_NAME_SIZE)
            this.name = newName;
        else
            this.name = newName.substring(0,DessertShoppe.MAX_ITEM_NAME_SIZE);
       
        if (newToppingName.length() <= DessertShoppe.MAX_ITEM_NAME_SIZE)
            this.toppingName = newToppingName;
        else
            this.toppingName = newToppingName.substring(0,DessertShoppe.MAX_ITEM_NAME_SIZE);

        this.cost = newCost;
        this.toppingCost = newToppingCost;
    }

    @Override
    public int getCost() {
        return cost + toppingCost;
    }

    public String getToppingName() {
        return toppingName;
    }
}

Upvotes: 0

Views: 2440

Answers (1)

GutZuFusss
GutZuFusss

Reputation: 140

Have you tried casting to Sundae?

((Sundae)(dessertItems.get(i))).getName()

Something along these lines. This should work althought I have only tried it in the opposite direction: Casting a child to a parent.

You can read more about this at the polymorphism documentation by Oracle themselves.

Good luck with you project!

Upvotes: 2

Related Questions