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