-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStudent.java
executable file
·68 lines (58 loc) · 1.88 KB
/
Student.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
/*
Author: Senthuran Ambalavanar | 2015215 | w1608452
*/
import utils.LoggerProcess;
import utils.Utility;
import java.util.Random;
/**
* Represents a student who uses the printer to print documents
*/
public class Student extends Thread {
private Printer printer;
public Student(String name, ThreadGroup threadGroup, Printer printer) {
super(threadGroup, name);
this.printer = printer;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
String documentName = "doc_" + (i + 1);
Document document = new Document(
generateDocumentID(this.getName(), documentName),
documentName,
generateRandomPageCount());
Utility.log(
LoggerProcess.STUDENT,
"[" + this.getName() + "] Requested to print: " + document,
null);
printer.printDocument(document);
Utility.log(
LoggerProcess.STUDENT,
"Printer status. " + printer.toString(),
null);
try {
sleep(Utility.generateRandomDuration());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Generates document id with the given student name and the document name
*
* @param studentName Name of the student
* @param documentName Name of the document
* @return Document ID
*/
private String generateDocumentID(String studentName, String documentName) {
return studentName + "_" + documentName;
}
/**
* Generates a random page count, within the range of 1 to 20 (inclusive), for documents
*
* @return Random page count
*/
private int generateRandomPageCount() {
return new Random().nextInt(19) + 1;
}
}