Я работаю над проблемой, в которой мне нужно создать виртуальный Dessert Shoppe с классами Java.Мне нужно распечатать квитанцию определенного формата, и у меня есть класс Sundae.У меня также есть ArrayList типа DessertItem (суперкласс для Ice Cream, который является суперклассом для Sundae).Я хочу получить доступ к методу в Sundae из этого массива, но он не позволит мне, так как тип массива имеет DessertItem.Я не могу изменить тип ArrayList, поскольку проблема запрещает это.Я также не могу редактировать класс DessertItem, поскольку проблема запрещает это.Я пробовал много вещей, таких как создание объекта Sundae и приведение элемента DessertItem в массиве к списку Sundae и назначение его объекту.Я полагаю, это потому, что когда объект sundae передается в методе enterItem класса Checkout, он передается как объект IceCream.
(я помещаю код в pastebin, потому что он не будет правильно форматироваться)
Вот класс DessertItem:
// DessertItem.java - Dessert Item abstract superclass
/**
* Abstract superclass for Dessert Item hierarchy
*/
public abstract class DessertItem {
protected String name;
/**
* Null constructor for DessertItem class
*/
public DessertItem() {
this("");
}
/**
* Initializes DessertItem data
*/
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);
}
/**
* Returns name of DessertItem
* @return name of DessertItem
*/
public final String getName() {
return name;
}
/**
* Returns cost of DessertItem
* @return cost of DessertItem
*/
public abstract int getCost();
}
Класс Checkout (в котором есть квитанция):
import java.util.ArrayList;
public class Checkout {
ArrayList<DessertItem> dessertItems = new ArrayList<DessertItem>();
public int cost;
public Checkout() {
// TODO Auto-generated constructor stub
}
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);
}
}
Класс Sundae:
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;
}
}
Пожалуйста, дайтемне указатель, как я полностью потерян.