-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtollbooth.java
56 lines (46 loc) · 1.47 KB
/
tollbooth.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
import java.util.Scanner;
class tollbooth {
private int totalCars;
private double totalCash;
// Constructor to initialize values
public tollbooth() {
totalCars = 0;
totalCash = 0.0;
}
// Method to count a paying car
public void payingCar() {
totalCars++;
totalCash += 0.50;
}
// Method to count a non-paying car
public void nopayCar() {
totalCars++;
}
// Method to display totals
public void display() {
System.out.println("Total Cars: " + totalCars);
System.out.println("Total Cash: $" + totalCash);
}
// Main Method
public static void main(String[] args) {
tollbooth booth = new tollbooth();
Scanner scanner = new Scanner(System.in);
char choice;
System.out.println("Press 'P' for a paying car, 'N' for a non-paying car, 'E' to exit.");
while (true) {
choice = scanner.next().charAt(0);
if (choice == 'P' || choice == 'p') {
booth.payingCar();
} else if (choice == 'N' || choice == 'n') {
booth.nopayCar();
} else if (choice == 'E' || choice == 'e') {
booth.display();
System.out.println("Exiting program.");
break;
} else {
System.out.println("Invalid input. Please enter 'P', 'N', or 'E'.");
}
}
scanner.close();
}
}