forked from Jared-Adamson/CSE_Java_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab5.java
More file actions
162 lines (141 loc) · 5.25 KB
/
Lab5.java
File metadata and controls
162 lines (141 loc) · 5.25 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Author: Jared Adamson
// Title: Lab 5
import java.util.Scanner;
public class Lab5
{
public static void main(String[] args)
{
Course java;
Course math;
Course english;
Scanner scan = new Scanner(System.in);
final int ENROLL = 1, ADD_SEATS = 2, QUIT = 3;
int choice;
int courseChoice;
//Instantiate 3 objects of type course; one for Java with 100 seats, one for math with 50 seats, and
// one for english with 30 seats (make the course title whatever you like).
java = new Course ("Introduction to Java", 100);
math = new Course ("Intro to Adding Stuff", 50);
english = new Course ("English Reading", 30);
//Add 100 students to the java course, 45 students to the math course, and 10 students to the english course.
for(int i = 0; i < 100; i++)
{
if(java.enrollStudent() == false )
System.out.println("Sorry, this course is full. Please choose another course or add seats to the course.");
}
for(int j = 0; j < 45; j++)
{
if(math.enrollStudent() == false)
System.out.println("Sorry, this course is full. Please choose another course or add seats to the course.");
}
for(int k = 0; k < 10; k++)
{
if(english.enrollStudent() == false)
System.out.println("Sorry, this course is full. Please choose another course or add seats to the course.");
}
do
{
//Print the main menu and get the user's choice by calling the appropriate method
choice = mainMenu(scan);
switch(choice)
{
case ENROLL:
//Print the enroll student menu and get the user's choice by calling the appropriate method
do
{
courseChoice = enrollMenu(scan,java,math,english);
}while (courseChoice > 3 || courseChoice < 1);
switch(courseChoice)
{
//For each case, call the appropriate method for enrolling a student for the appropriate class
case 1:
if(!java.enrollStudent())
System.out.println("Sorry, this course is full. Please choose another course or add seats to the course.");
break;
case 2:
if(!math.enrollStudent())
System.out.println("Sorry, this course is full. Please choose another course or add seats to the course.");
break;
case 3:
if(!english.enrollStudent())
System.out.println("Sorry, this course is full. Please choose another course or add seats to the course.");
break;
default:
System.out.println("Invalid choice");
}
break;
case ADD_SEATS:
//Print the add seats menu and get the user's choice by calling the appropriate method
do
{
courseChoice = enrollMenu(scan,java,math,english);
}while (courseChoice > 3 || courseChoice < 1);
switch(courseChoice)
{
//For each case, call the appropriate method for adding 10 seats for the appropriate class
case 1:
java.addSeats(10);
break;
case 2:
math.addSeats(10);
break;
case 3:
english.addSeats(10);
break;
default:
System.out.println("Invalid choice");
}
break;
case QUIT:
System.out.println("Have a good day.");
break;
default:
System.out.println("Invalid choice");
}
}while(choice != QUIT);
}
/*
* This method prints the main menu, prompts the user for his choice,
* and returns the int choice.
* Params: scan - a Scanner object used to receive input from the user
* Return: int value of the user's choice
*/
public static int mainMenu(Scanner scan)
{
System.out.println("Please choose from the following options:");
System.out.println("1. Enroll in a course");
System.out.println("2. Increase a course's capacity");
System.out.println("3. Quit");
return scan.nextInt();
}
/*
* This method prints the menu for enrolling a student, prompts the user
* for his choice, and returns the int choice.
* Params: scan - a Scanner object used to receive input from the user
* Return: int value of the user's choice
*/
public static int enrollMenu(Scanner scan, Course java, Course math, Course english)
{
//Use the appropriate methods to display the course details
System.out.println("What course would you like to enroll in?");
System.out.println("1. " + java.toString());
System.out.println("2. " + math.toString());
System.out.println("3. " + english.toString());
return scan.nextInt();
}
/*
* This method prints the menu for adding seats, prompts the user for
* his choice, and returns the int choice.
* Params: scan - a Scanner object used to receive input from the user
* Return: int value of the user's choice
*/
public static int addSeatsMenu(Scanner scan, Course java, Course math, Course english)
{
//Use the appropriate methods to display the course details
System.out.println("For which course would you like to increase the capacity?");
System.out.println("1. " + java.toString());
System.out.println("2. " + math.toString());
System.out.println("3. " + english.toString());
return scan.nextInt();
}
}