/home/wpollock1/public_html/Java/GiftBasket.java
// GiftBasket.java - A demonstration of interfaces. This program could be the basis
// of a simplified "point of sale" (POS) system: Imagine each item is scanned
// when added to the gift basket, and the scanner reads a bar-code that identifies
// the item. The program might then print the items description and price on the
// receipt, update the inventory records, etc. The Sellable interface defines the
// guarantee that any Sellable item can have its description printed and its cost
// calculated.
//
// Classes Fruit and Bouquet are unrealistic here to keep the example simpler.
// This slightly more complex example correctly uses abstract classes to define
// the generalization of fruits and flowers. Also notice the constructor details
// in class Bouquet: "type" has a reasonable default value so no constructor argument
// is needed, but may be supplied. Overloaded constructors and methods are a common
// way to deal with optional arguments.
//
// Written 2002 by Wayne Pollock, Tampa FL USA. All Rights Reserved.
import java.text.NumberFormat;
interface Sellable
{
float cost();
void describe ();
}
abstract class Fruit
{
String color;
float weight;
public Fruit ( String color, float weight )
{ this.color = color;
this.weight = weight;
}
public void describe ()
{
System.out.println( "I am " + color +
" and I weigh " + weight + " pounds." );
}
}
class Mango extends Fruit implements Sellable
{
public Mango ( String color, float weight )
{ super( color, weight ); }
public void describe ()
{ System.out.println( "A juicy " + color + " " + weight + " lb. mango." ); }
public float cost ()
{ return 0.88f * weight; }
}
class Banana extends Fruit implements Sellable
{
public Banana ( String color, float weight )
{ super( color, weight ); }
public void describe ()
{ System.out.println( "A " + color + " " + weight + " lb. bunch of bananas." ); }
public float cost ()
{ return 0.88f * weight; }
}
class Orange extends Fruit
{
public Orange ( String color, float weight )
{ super( color, weight ); }
public void describe ()
{ System.out.println( "A ripe " + color + " " + weight + " lb. Orange." ); }
public float cost ()
{ return 0.39f * weight; }
}
class Marmalade implements Sellable
{
String type;
public Marmalade ( String type )
{ this.type = type; }
public void describe ()
{ System.out.println( "A tasty jar of " + type + " marmalade." ); }
public float cost ()
{ return 1.99f; }
}
abstract class Bouquet implements Sellable
{
int numOfFlowers;
float priceEach;
String type;
public Bouquet ( int numOfFlowers, float priceEach )
{ this( numOfFlowers, priceEach, "flowers" ); }
public Bouquet ( int numOfFlowers, float priceEach, String type )
{ this.numOfFlowers = numOfFlowers;
this.priceEach = priceEach;
this.type = type;
}
public void describe ()
{ System.out.println( "A fragrent bouquet of " + numOfFlowers + " " + type + "." );
}
abstract public float cost ();
}
class RoseBouquet extends Bouquet
{
public RoseBouquet ( int numOfFlowers )
{ super( numOfFlowers, 1.25f );
type = "Roses";
}
public float cost ()
{ float price = numOfFlowers * priceEach;
if ( numOfFlowers > 12 )
return 0.90f * price; // a 10% discount
else
return price;
}
}
public class GiftBasket
{
public static void main ( String [] args )
{
int numItems = 0;
Sellable [] giftBasket = new Sellable[10]; // An array of 10 "Sellable" items.
giftBasket[numItems++] = new Mango( "red", .25f );
giftBasket[numItems++] = new Banana( "yellow", 2.15f );
// giftBasket[numItems++] = new Orange( "orange", 0.12f ); // This won't work!
giftBasket[numItems++] = new Marmalade( "Lemon" );
giftBasket[numItems++] = new Mango( "red", .18f );
giftBasket[numItems++] = new RoseBouquet( 6 );
float totalCost = 0;
System.out.println( "\nYour gift basket contains the following items:\n" );
for ( int i = 0; i < numItems; ++i )
{
giftBasket[i].describe();
totalCost += giftBasket[i].cost();
}
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println( "\nThe total cost is " + nf.format( totalCost ) + "." );
}
}