-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPurchase.java
More file actions
88 lines (86 loc) · 2.4 KB
/
Copy pathPurchase.java
File metadata and controls
88 lines (86 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.Scanner;
public class Purchase {
private String name;
private int groupCount;
private double groupPrice;
private int numberBought;
public void setName(String newName)
{
name = newName;
}
public void setPrice(int count, double costForCount)
{
if((count <= 0) || (costForCount <= 0))
{
System.out.print("Error: Bad parameter in set.Price.");
System.exit(0);
}
else{
groupCount = count;
groupPrice = costForCount;
}
}
public void setNumberBought(int number)
{
if (number <=0)
{
System.out.println("Error: Bad parameter in setNumberBough");
System.exit(0);
}
else
numberBought=number;
}
public void readInput()
{
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter name of the item you are purchasing: ");
name=keyboard.nextLine();
System.out.println("Enter price of item as two numbers.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3 2.99");
System.out.println("Enter price of item as two numbers, now: ");
groupCount=keyboard.nextInt();
groupPrice=keyboard.nextDouble();
while ((groupCount <= 0) || (groupPrice <= 0))
{
System.out.println("Both Number must be positive. Try again.");
System.out.println("Enter price of item as two numbers.");
System.out.println("For example, 3 for $2.99 is entered as");
System.out.println("3 2.99");
System.out.println("Enter price of item as two numbers, now: ");
groupCount=keyboard.nextInt();
groupPrice=keyboard.nextDouble();
}
System.out.println("Enter number of items purchased:");
numberBought = keyboard.nextInt();
while (numberBought <= 0)
{
System.out.println("Number must be positive. Try again.");
System.out.println("Enter number of items purchased:");
numberBought = keyboard.nextInt();
}
keyboard.close();
}
public void writeOutput()
{
System.out.println(numberBought + " "+name);
System.out.println("at " + groupCount+ " for $"+ groupPrice);
}
public String getName()
{
return name;
}
public double getTotolCost()
{
return
(groupPrice/groupCount)*numberBought;
}
public double getUnitCost()
{
return groupPrice/groupCount;
}
public int getNumberBought()
{
return numberBought;
}
}