/
FCITregistrationLab.java
133 lines (122 loc) · 4.64 KB
/
FCITregistrationLab.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
import java.io.*;
import java.util.Scanner;
public class FCITregistrationLab {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("src/input.txt"));
int noOfLaptops = in.nextInt();
PrintWriter out = new PrintWriter("src/output.txt", "UTF-8");
FCITlaptopStack laptopStack = new FCITlaptopStack(noOfLaptops);
addLaptops(noOfLaptops, in, laptopStack);
routine(in, laptopStack, out);
in.close();
out.close();
}
public static void addLaptops(int noOfLaptops, Scanner in,
FCITlaptopStack laptopStack) {
for (int i = 0; i < noOfLaptops; i++) {
laptopStack.push(in.nextInt());
}
}
public static void routine(Scanner in, FCITlaptopStack laptopStack,
PrintWriter out) {
int noOfDays = in.nextInt();
for (int i = 1; i <= noOfDays; i++) {
out.printf("********** \nDay %d:\n**********\n",i);
performDayRoutine(in, laptopStack, i, out);
}
}
public static void performDayRoutine(Scanner in,
FCITlaptopStack laptopStack, int i, PrintWriter out) {
int studentsCount = in.nextInt();
FCITqueue outsideQueue = addStudentsToOutsideLine(in, studentsCount);
int startTime = getStartTime(outsideQueue);
int endTime = getEndTime(outsideQueue)>=299? 299:getEndTime(outsideQueue);
FCITqueue laptopQueue = new FCITqueue();
FCITqueue regQueue = new FCITqueue();
FCITqueue lapReturnQueue = new FCITqueue();
int laptopIssueTime = 0;
int laptopReturnTime =0;
for (; startTime <= endTime; startTime++) {
if (!(outsideQueue.isEmpty())) {
while (!(outsideQueue.isEmpty())
&& outsideQueue.getfront().getEnterTime() <= startTime) {
printTimeStamp(startTime, out);
out.println(outsideQueue.getfront().getFirstName()
+ " "
+ outsideQueue.getfront().getLastName()
+ " has arrived at the Registration Lab and entered the Laptop Check-out Line.");
laptopQueue.enqueue(outsideQueue.dequeue());
}
}
if(laptopIssueTime!=0){
if (laptopQueue.getfront().getLaptopSerialNumber() == 0) {
laptopQueue.getfront().setLaptopSerialNumber(laptopStack.pop());
lapReturnQueue.enqueue(laptopQueue.dequeue());
printTimeStamp(startTime, out);
out.println(lapReturnQueue.getBack().getFirstName() + " "
+ lapReturnQueue.getBack().getLastName()
+ " has checked-out laptop # "
+ lapReturnQueue.getBack().getLaptopSerialNumber());
laptopIssueTime = 0;
}else{
//laptopQueue.get
}
}else if(!(laptopQueue.isEmpty())) laptopIssueTime++;
registration(startTime,lapReturnQueue,laptopStack,out);
}
}
private static void registration(int startTime, FCITqueue lapReturnQueue,
FCITlaptopStack laptopStack, PrintWriter out) {
FCITstudent curr = lapReturnQueue.getfront();
while(curr!=null){
curr.setTimeForReg(curr.getTimeForReg()+1);
if(curr.getTimeForReg()==6){
printTimeStamp(startTime, out);
out.println(curr.getFirstName()+" "+curr.getLastName()+" finished registering and entered the Laptop Return Line.");
}else if(curr.getTimeForReg()==7){
printReportMessage(startTime, curr, out);
laptopStack.push(lapReturnQueue.getfront().getLaptopSerialNumber());
lapReturnQueue.dequeue();
}
curr = curr.getNext();
}
}
private static int getEndTime(FCITqueue outsideQueue) {
// TODO Auto-generated method stub
return outsideQueue.getBack().getEnterTime();
}
private static int getStartTime(FCITqueue outsideQueue) {
// TODO Auto-generated method stub
return outsideQueue.getfront().getEnterTime();
}
private static void printReportMessage(int startTime, FCITstudent first,
PrintWriter out) {
printTimeStamp(startTime, out);
out.println(first.getFirstName() + " " + first.getLastName()
+ " successfully registered and returned laptop # " + first.getLaptopSerialNumber());
}
private static void printTimeStamp(int startTime, PrintWriter out) {
out.printf("%d:%02d PM: ",
(startTime / 60 == 0 ? 12 : startTime / 60), (startTime % 60));
}
public static FCITqueue addStudentsToOutsideLine(Scanner in,
int studentsCount) {
FCITqueue outsideQueue = new FCITqueue();
for (int i = 1; i <= studentsCount; i++) {
int enterTime = in.nextInt();
String lastName = in.next();
String firstName = in.next();
int id = in.nextInt();
int numClasses = in.nextInt();
//FCITcourse[] courses = new FCITcourse[numClasses];
in.nextLine();
for (int k = 0; k <numClasses; k++) {
in.nextLine();
}
FCITstudent student = new FCITstudent(enterTime, firstName,
lastName, id, numClasses);
outsideQueue.enqueue(student);
}
return outsideQueue;
}
}