public class Purchase
1:
2: import java.util.*;
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;
13: //Part of price, like the $1.99 in 2 for $1.99.
14: private int numberBought; //Total number being purchased.
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: Gets price and number being purchased from keyboard.
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: while ((groupCount <= 0) || (groupPrice <= 0))
66: {//Try again:
67: System.out.println(
68: "Both numbers must be positive. Try again.");
69: System.out.println("Enter price of item as two numbers.");
70: System.out.println(
71: "For example, 3 for $2.99 is entered as");
72: System.out.println("3 2.99");
73: System.out.println(
74: "Enter price of item as two numbers, now:");
75: groupCount = keyboard.nextInt( );
76: groupPrice = keyboard.nextDouble( );
77: }
78:
79: System.out.println("Enter number of items purchased:");
80: numberBought = keyboard.nextInt( );
81: while (numberBought <= 0)
82: {//Try again:
83: System.out.println(
84: "Number must be positive. Try again.");
85: System.out.println("Enter number of items purchased:");
86: numberBought = keyboard.nextInt( );
87: }
88: }
89:
90: /**
91: Outputs price and number being purchased to screen.
92: */
93: public void writeOutput( )
94: {
95: System.out.println(numberBought + " " + name);
96: System.out.println("at " + groupCount
97: + " for $" + groupPrice);
98: }
99:
100: public String getName( )
101: {
102: return name;
103: }
104:
105: public double getTotalCost( )
106: {
107: return ((groupPrice/groupCount)*numberBought);
108: }
109:
110: public double getUnitCost( )
111: {
112: return (groupPrice/groupCount);
113: }
114:
115: public int getNumberBought( )
116: {
117: return numberBought;
118: }
119: }