GiftBasket.java

Download GiftBasket.java

  1: // GiftBasket.java - A demonstration of interfaces.  This program could be the basis
  2: // of a simplified "point of sale" (POS) system:  Imagine each item is scanned
  3: // when added to the gift basket, and the scanner reads a bar-code that identifies
  4: // the item.  The program might then print the items description and price on the
  5: // receipt, update the inventory records, etc.  The Sellable interface defines the
  6: // guarantee that any Sellable item can have its description printed and its cost
  7: // calculated.
  8: //
  9: // Classes Fruit and Bouquet are unrealistic here to keep the example simpler.
 10: // This slightly more complex example correctly uses abstract classes to define
 11: // the generalization of fruits and flowers.  Also notice the constructor details
 12: // in class Bouquet: "type" has a reasonable default value so no constructor argument
 13: // is needed, but may be supplied.  Overloaded constructors and methods are a common
 14: // way to deal with optional arguments.
 15: //
 16: // Written 2002 by Wayne Pollock, Tampa FL USA.  All Rights Reserved.
 17: 
 18: import java.text.NumberFormat;
 19: 
 20: interface Sellable
 21: {
 22:    float cost();
 23:    void describe ();
 24: }
 25: 
 26: abstract class Fruit
 27: {
 28:    String color;
 29:    float weight;
 30: 
 31:    public Fruit ( String color, float weight )
 32:    {  this.color = color;
 33:       this.weight = weight;
 34:    }
 35: 
 36:    public void describe ()
 37:    {
 38:       System.out.println( "I am " + color +
 39:          " and I weigh " + weight + " pounds." );
 40:    }
 41: }
 42: 
 43: class Mango extends Fruit implements Sellable
 44: {
 45:    public Mango ( String color, float weight )
 46:    {  super( color, weight );  }
 47: 
 48:    public void describe ()
 49:    {  System.out.println( "A juicy " + color + " " + weight + " lb. mango." );  }
 50: 
 51:    public float cost ()
 52:    {  return 0.88f * weight;  }
 53: }
 54: 
 55: class Banana extends Fruit implements Sellable
 56: {
 57:    public Banana ( String color, float weight )
 58:    {  super( color, weight );  }
 59: 
 60:    public void describe ()
 61:    {  System.out.println( "A " + color + " " + weight + " lb. bunch of bananas." );  }
 62: 
 63:    public float cost ()
 64:    {  return 0.88f * weight;  }
 65: }
 66: 
 67: class Orange extends Fruit
 68: {
 69:    public Orange ( String color, float weight )
 70:    {  super( color, weight );  }
 71: 
 72:    public void describe ()
 73:    {  System.out.println( "A ripe " + color + " " + weight + " lb. Orange." );  }
 74: 
 75:    public float cost ()
 76:    {  return 0.39f * weight;  }
 77: }
 78: 
 79: class Marmalade implements Sellable
 80: {
 81:    String type;
 82: 
 83:    public Marmalade ( String type )
 84:    {  this.type = type;  }
 85: 
 86:    public void describe ()
 87:    {  System.out.println( "A tasty jar of " + type + " marmalade." );  }
 88: 
 89:    public float cost ()
 90:    {  return 1.99f;  }
 91: }
 92: 
 93: abstract class Bouquet implements Sellable
 94: {
 95:    int numOfFlowers;
 96:    float priceEach;
 97:    String type;
 98: 
 99:    public Bouquet ( int numOfFlowers, float priceEach )
100:    {  this( numOfFlowers, priceEach, "flowers" );  }
101: 
102:    public Bouquet ( int numOfFlowers, float priceEach, String type )
103:    {  this.numOfFlowers = numOfFlowers;
104:       this.priceEach = priceEach;
105:       this.type = type;
106:    }
107: 
108:    public void describe ()
109:    {  System.out.println( "A fragrent bouquet of " + numOfFlowers + " " + type + "." );
110:    }
111: 
112:    abstract public float cost ();
113: }
114: 
115: class RoseBouquet extends Bouquet
116: {
117:    public RoseBouquet ( int numOfFlowers )
118:    {  super( numOfFlowers, 1.25f );
119:       type = "Roses";
120:    }
121: 
122:    public float cost ()
123:    {  float price = numOfFlowers * priceEach;
124:       if ( numOfFlowers > 12 )
125:          return 0.90f * price;  // a 10% discount
126:       else
127:          return price;
128:    }
129: }
130: 
131: public class GiftBasket
132: {
133:    public static void main ( String [] args )
134:    {
135:       int numItems = 0;
136:       Sellable [] giftBasket = new Sellable[10];  // An array of 10 "Sellable" items.
137: 
138:       giftBasket[numItems++] = new Mango( "red", .25f );
139:       giftBasket[numItems++] = new Banana( "yellow", 2.15f );
140: //    giftBasket[numItems++] = new Orange( "orange", 0.12f );  // This won't work!
141:       giftBasket[numItems++] = new Marmalade( "Lemon" );
142:       giftBasket[numItems++] = new Mango( "red", .18f );
143:       giftBasket[numItems++] = new RoseBouquet( 6 );
144: 
145:       float totalCost = 0;
146:       System.out.println( "\nYour gift basket contains the following items:\n" );
147:       for ( int i = 0; i < numItems; ++i )
148:       {
149:          giftBasket[i].describe();
150:          totalCost += giftBasket[i].cost();
151:       }
152:       NumberFormat nf = NumberFormat.getCurrencyInstance();
153:       System.out.println( "\nThe total cost is " + nf.format( totalCost ) + "." );
154:    }
155: }