/
Program.cs
299 lines (251 loc) · 10.6 KB
/
Program.cs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.IO;
using System.Threading;
namespace Programming_Assignment_Final
{
class Program
{
public struct Prize
{
public string prizeName;
public int quantity;
public int lowerBound;
public int upperBound;
}
public struct Student
{
public String firstName;
public String lastName;
public String phoneNumber;
}
public static void WriteToFile(Student[] students)
{
int count = 0;
StreamWriter sw = new StreamWriter("../class.txt");
for (int i = 0; i < students.Length; i++)
{
sw.WriteLine(students[i].firstName);
sw.WriteLine(students[i].lastName);
sw.WriteLine(students[i].phoneNumber);
count++;
}
sw.Close();
}
public static void Customers(Student[] students) // Displays Students and Orders it by last names alphabetically
{
Console.Clear();
Student[] alphaStudents = students;
Array.Sort<Student>(alphaStudents, (student1, student2) => student1.lastName.CompareTo(student2.lastName));
foreach(Student s in alphaStudents)
{
Console.WriteLine(s.firstName + " " + s.lastName + " " + s.phoneNumber);
}
Console.ReadLine();
}
public static Student[] Newclass(Student[] students)
{
int count = 0;
students = new Student[21]; /*class.txt has 21 sets of firstname, lastname and phone numbers*/
String tempName = "";
String tempLast = "";
String tempPhone;
StreamReader sr = new StreamReader("../class.txt"); /*fix file location for the class.txt file*/
while (count < 21)
{
tempName = sr.ReadLine();
tempLast = sr.ReadLine();
tempPhone = sr.ReadLine();
students[count].firstName = tempName;
students[count].lastName = tempLast;
students[count].phoneNumber = tempPhone;
count++;
}
sr.Close();
return students;
}
public static Prize[] GeneratePrizeList(Prize[] prizes)
{
int count = 0;
prizes = new Prize[14];
String tempPrizeName = "";
int tempQuantity = 0;
int tempLowerBound = 0;
int tempUpperBound = 0;
StreamReader sr = new StreamReader("../prizes.txt");
while (count < 14)
{
tempPrizeName = sr.ReadLine();
tempQuantity = Convert.ToInt32(sr.ReadLine());
tempLowerBound = tempUpperBound + 1;
tempUpperBound = Convert.ToInt32(sr.ReadLine());
prizes[count].prizeName = tempPrizeName;
prizes[count].quantity = tempQuantity;
prizes[count].lowerBound = tempLowerBound;
prizes[count].upperBound = tempUpperBound;
count++;
}
sr.Close();
return prizes;
}
public static void Phonenumber(Student[] students) // Lets a user enter the first and last name of the individual whos number they want to change
{
Console.Clear();
Console.WriteLine("Enter the first and last name of the customer you want to update the Phone Number of");
String temp = Console.ReadLine();
String temp2 = "";
bool found = false;
for(int i = 0; i < 21; i++)
{
temp2 = students[i].firstName + " " + students[i].lastName;
if(temp == temp2)
{
Console.WriteLine("Enter " + temp + "'s new phone number");
String num = Console.ReadLine();
students[i].phoneNumber = num;
found = true;
}
}
if(found)
{
Console.WriteLine("Phone number successfully updated");
}
else
{
Console.WriteLine("No customer by that name found.");
}
Console.ReadLine();
WriteToFile(students);
}
public static int[] Sellingtickets(Student[] students) //Picks 10 random customers from the students array and lists them
{
Random rand = new Random();
int[] ticketPurchasers = new int[10];
bool flag = false;
for (int i = 0; i < 10; i++)
{
int r = rand.Next(1, 22);
for(int i2 = 0; i2 < 10; i2++) //loop to check if number already used
{
if(ticketPurchasers[i2] == r) //checks if a number has already been used
{
flag = true; // marks that a number has already been used
}
}
if(flag)
{
i--; //resets the random number generator for this instance in the loop
flag = false; // resets the flag
}
else
{
ticketPurchasers[i] = r; //adds the number generated into the array if already not in use
}
}
Console.Clear();
Console.WriteLine("These are the people who will buy tickets\n");
foreach(int i in ticketPurchasers)
{
Console.WriteLine(students[i-1].firstName + " " + students[i-1].lastName);
}
Console.ReadLine();
return ticketPurchasers;
}
public static void Raffle(Student[] students, int[] ticketPurchasers, Prize[] prizes) // activity diagram for this one
{
int roll = 0;
Random rand = new Random();
foreach(int count in ticketPurchasers)
{
int count2 = 0;
Console.WriteLine(students[count-1].firstName + " " + students[count-1].lastName + "'s prizes are:");
while (count2 < 4)
{
roll = rand.Next(0, 3616001);
for(int i = 0; i < 14; i++)
{
if (roll >= prizes[i].lowerBound)
{
if (roll <= prizes[i].upperBound)
{
if (prizes[i].quantity > 0)
{
Console.WriteLine("Prize " + (count2+1) + ": " + prizes[i].prizeName);
prizes[i].quantity = prizes[i].quantity - 1;
}
else
{
Console.WriteLine("Prize " + (count2+1) + ": " + "better luck next time");
}
}
}
}
count2++;
}
Console.ReadLine();
}
}
public static void Menusystem(Student[] students, Prize[] prizes) //The main menu using a switch
{
int[] ticketPurchasers = null;
int taskNumber;
if(students == null)
{
students = Newclass(students);
}
if(prizes == null)
{
prizes = GeneratePrizeList(prizes);
}
do
{
Console.WriteLine("Welcome to the main menu! ");
Console.WriteLine();
Console.WriteLine("What would you like to do? :");
Console.WriteLine();
Console.WriteLine("To list all potential customers enter '1' ");
Console.WriteLine("To locate a person and update their phone number enter '2' ");
Console.WriteLine("To sell tickets to ten different inviduals enter '3' ");
Console.WriteLine("To generate prizes for the ten individuals enter '4' (MUST HAVE ALREADY DONE OPTION 3)");
Console.WriteLine("Otherwise to exit to the main menu please enter '0'");
taskNumber = Convert.ToInt32(Console.ReadLine());
switch (taskNumber)
{
case 0:
Menusystem(students, prizes);
break;
case 1:
Customers(students);
break;
case 2:
Phonenumber(students);
break;
case 3:
ticketPurchasers = Sellingtickets(students);
break;
case 4:
Raffle(students, ticketPurchasers, prizes);
break;
default:
Menusystem(students, prizes);
break;
}
Console.Clear();
} while (taskNumber != 0);
Console.Clear();
}
public static void Main(string[] args)
{
Student[] students = null;
Prize[] prizes = null;
Menusystem(students, prizes);
}
}
}
/*Menu Program - Minimum requirements allows the user to do the following:
* 1. List all the potential customers. Sort the data by last name. (class.txt file in programming assignment final > bin > debug)
* 2. Locate a person in the list and update their phone number. Save these changes back to the file.
* 3. You have to sell ten tickets to ten different individuals.
* Randomly generate 10 numbers; these numbers must range from 1 to the number of people in the array (21).
* You can not have duplicate numbers being drawn. Find and list the people who will buy your tickets.
* 4. Loop through your ten ticket holders showing their Loop 4 results.
*/