Source of Purchase.java


  1: //Purchase.java
  2: 
  3: import java.util.Scanner;
  4: 
  5: /**
  6:  * Class for the purchase of one kind of item, such as 3 oranges.
  7:  * Prices are set supermarket style, such as 5 for $1.25.
  8: */
  9: public class Purchase
 10: {
 11:     private String name;
 12:     private int groupCount;   //Part of price, like the 2 in 2 for $1.99.
 13:     private double groupPrice;//Part of price, like the $1.99 in 2 for $1.99
 14:     private int numberBought; //Number of items bought.
 15: 
 16:     public void setName
 17:     (
 18:         String newName
 19:     )
 20:     {
 21:         name = newName;
 22:     }
 23: 
 24:     /**
 25:      * Sets price to count pieces for $costForCount.
 26:      * For example, 2 for $1.99.
 27:     */
 28:     public void setPrice
 29:     (
 30:         int count,
 31:         double costForCount
 32:     )
 33:     {
 34:         if ((count <= 0) || (costForCount <= 0))
 35:         {
 36:             System.out.println("Error: Bad parameter in setPrice.");
 37:             System.exit(0);
 38:         }
 39:         else
 40:         {
 41:             groupCount = count;
 42:             groupPrice = costForCount;
 43:         }
 44:     }
 45: 
 46:     public void setNumberBought
 47:     (
 48:         int number
 49:     )
 50:     {
 51:         if (number <= 0)
 52:         {
 53:             System.out.println("Error: Bad parameter in setNumberBought.");
 54:             System.exit(0);
 55:         }
 56:         else
 57:             numberBought = number;
 58:     }
 59: 
 60:     /**
 61:      * Reads from keyboard the price and number of a purchase.
 62:      */
 63:     public void readInput()
 64:     {
 65:         Scanner keyboard = new Scanner(System.in);
 66:         System.out.println("Enter name of item you are purchasing:");
 67:         name = keyboard.nextLine();
 68: 
 69:         System.out.println("Enter price of item as two numbers.");
 70:         System.out.println("For example, 3 for $2.99 is entered as");
 71:         System.out.println("3 2.99");
 72:         System.out.println("Enter price of item as two numbers, now:");
 73:         groupCount = keyboard.nextInt();
 74:         groupPrice = keyboard.nextDouble();
 75: 
 76:         while ((groupCount <= 0) || (groupPrice <= 0))
 77:         { //Try again:
 78:             System.out.println("Both numbers must be positive. Try again.");
 79:             System.out.println("Enter price of item as two numbers.");
 80:             System.out.println("For example, 3 for $2.99 is entered as");
 81:             System.out.println("3 2.99");
 82:             System.out.println("Enter price of item as two numbers, now:");
 83:             groupCount = keyboard.nextInt();
 84:             groupPrice = keyboard.nextDouble();
 85:         }
 86: 
 87:         System.out.println("Enter number of items purchased:");
 88:         numberBought = keyboard.nextInt();
 89: 
 90:         while (numberBought <= 0)
 91:         { //Try again:
 92:             System.out.println("Number must be positive. Try again.");
 93:             System.out.println("Enter number of items purchased:");
 94:             numberBought = keyboard.nextInt();
 95:         }
 96:     }
 97: 
 98:     /**
 99:      * Displays price and number being purchased.
100:      */
101:     public void writeOutput()
102:     {
103:         System.out.println(numberBought + " " + name);
104:         System.out.println("at " + groupCount +
105:                            " for $" + groupPrice);
106:     }
107: 
108:     public String getName()
109:     {
110:         return name;
111:     }
112: 
113:     public double getTotalCost()
114:     {
115:         return (groupPrice / groupCount) * numberBought;
116:     }
117: 
118:     public double getUnitCost()
119:     {
120:         return groupPrice / groupCount;
121:     }
122: 
123:     public int getNumberBought()
124:     {
125:         return numberBought;
126:     }
127: }