-
Notifications
You must be signed in to change notification settings - Fork 17
/
InternetServiceProviderPart2.java
134 lines (109 loc) · 2.91 KB
/
InternetServiceProviderPart2.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Modify the program you wrote for Programming Challenge 13 so it also calculates and displays the amount of money
* Package A customers would save if they purchased Packages B or C, and the amount of money Package B customers
* would save if they purchased Package C. If there would be no savings, no message should be printed.
*/
import java.util.Scanner;
public class InternetServiceProviderPart2 {
// hours is static because passing it between all of the functions isn't needed for a small program, such as this.
static double hours;
public static void main (String args[])
{
Scanner userInput = new Scanner (System.in);
while (true)
{
// Calls printMonthlyBill with the appropriate functions as parameters, which executes them
printMonthlyBill(calculateBill(getHours(userInput), menu(userInput)));
}
}
// Gets hours
public static double getHours(Scanner userInput)
{
System.out.print("Please enter the hours used: ");
hours = userInput.nextDouble();
return hours;
}
// Displays main menu and gets user's package
public static int menu (Scanner userInput)
{
int packageChoice;
System.out.println("\n[1] Package A");
System.out.println("[2] Package B");
System.out.println("[3] Package C");
System.out.print("Please select your package: ");
packageChoice = userInput.nextInt();
return packageChoice;
}
// Calculates the bill
public static double calculateBill(double hours, int packageChoice)
{
switch (packageChoice)
{
case 1:
return getPackageA(hours);
case 2:
return getPackageB(hours);
case 3:
return getPackageC();
default:
System.out.println("\nInvalid input!");
return 0;
}
}
// Prints bill and savings
public static void printMonthlyBill(double bill)
{
if (bill != 0)
{
System.out.printf("\nYour monthly bill is $%.2f\n", bill);
getSavings(bill);
System.out.printf("\n\n");
}
else
System.out.println("\n");
}
// Gets bill if user has Package A
public static double getPackageA(double hours)
{
if (hours < 10)
{
return 9.95;
}
else
{
return (hours - 10)*2 + 9.95;
}
}
// Gets bill if user has Package B
public static double getPackageB(double hours)
{
if (hours < 20)
{
return 13.95;
}
else
{
return (hours - 20) + 13.95;
}
}
// Gets bill if user has Package C
public static double getPackageC()
{
return 19.95;
}
// Checks if savings are applicable and displays potential savings to the user
public static void getSavings(double bill)
{
if(bill > getPackageA(hours))
{
System.out.printf("If you had Package A, you'd save $%.2f\n", bill - getPackageA(hours));
}
if(bill > getPackageB(hours))
{
System.out.printf("If you had Package B, you'd save $%.2f\n", bill - getPackageB(hours));
}
if(bill > getPackageC())
{
System.out.printf("If you had Package C, you'd save $%.2f\n", bill - getPackageC());
}
}
}