Your code looks pretty good to me.
Every object you created is an Object and GreetingCard object and maybe one or more extended type of GreetingCard.
You could make an array of Objects, with each object a completely different type of Object:
Object[] cards = {
new GreetingCard("Recipient", "Sender"),
new BirthdayCard("Recipient", "Sender", 18),
new WeddingCard("Bride", "Groom", "Sender"),
new YouthBirthday("Recipient", "Sender", 8),
new AdultBirthday("Recipient", "Sender", 30),
};
and then iterate as follows:
for (int i = 0; i < cards.length; i++){
GreetingCard nextCard = (GreetingCard)cards[i];
System.out.println(nextCard.showCard());
}
You could iterate through your cards and write out a message about each card type:
System.out.println("My " + cards.length + " cards include the following types:");
for (int i = 0; i < cards.length; i++){
GreetingCard nextCard = (GreetingCard)cards[i];
if (nextCard instanceof GreetingCard) System.out.println("Card number " + (i + 1) + " is a Greeting Card");
if (nextCard instanceof BirthdayCard) System.out.println("Card number " + (i + 1) + " is a Birthday Card");
if (nextCard instanceof WeddingCard) System.out.println("Card number " + (i + 1) + " is a Wedding Card");
if (nextCard instanceof AdultBirthday) System.out.println("Card number " + (i + 1) + " is a Adult Birthday Card");
if (nextCard instanceof YouthBirthday) System.out.println("Card number " + (i + 1) + " is a Youth Birthday Card");
}
The whole code:
class MyCards{
public static void main(String[] args){
Object[] cards = {
new GreetingCard("Recipient", "Sender"),
new BirthdayCard("Recipient", "Sender", 18),
new WeddingCard("Bride", "Groom", "Sender"),
new YouthBirthday("Recipient", "Sender", 8),
new AdultBirthday("Recipient", "Sender", 30),
};
for (int i = 0; i < cards.length; i++){
GreetingCard nextCard = (GreetingCard)cards[i];
System.out.println(nextCard.showCard());
}
System.out.println("My " + cards.length + " cards include the following types:");
for (int i = 0; i < cards.length; i++){
GreetingCard nextCard = (GreetingCard)cards[i];
if (nextCard instanceof GreetingCard) System.out.println("Card number " + (i + 1) + " is a Greeting Card");
if (nextCard instanceof BirthdayCard) System.out.println("Card number " + (i + 1) + " is a Birthday Card");
if (nextCard instanceof WeddingCard) System.out.println("Card number " + (i + 1) + " is a Wedding Card");
if (nextCard instanceof AdultBirthday) System.out.println("Card number " + (i + 1) + " is a Adult Birthday Card");
if (nextCard instanceof YouthBirthday) System.out.println("Card number " + (i + 1) + " is a Youth Birthday Card");
}
}
}