-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupervisor.java
98 lines (88 loc) · 3.09 KB
/
Supervisor.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
package com.B5015845.CSC8002.Coursework;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* The Supervisor class acts as a factory to create unique instances of supervisors that have been read in from a file containing all
* the supervisors available. It stores and retrieves information about supervisors.
* Supervisor format is firstname + " " + second name e.g. "Albus Dumbledore"
* @author Arianna Esposito
*/
public final class Supervisor {
private final Name name;
private static final Map<String, Supervisor> supervisors = new HashMap<String, Supervisor>();
/**
* Private constructor to ensure that supervisors are only created from the file containing supervisors with getInstance().
* This file can be modified to add or remove supervisors as necessary.
* @param name of supervisor
*/
private Supervisor(Name name) //immutable supervisor
{
this.name = new Name(name.getFirstName(), name.getLastName());
}
/**
* This method acts as a factory to create unique instances of supervisors. It reads data from the file. It checks uniqueness by
* adding supervisors to a hashmap, which does not store duplicates.
* This code is adapted from the CSC8002 lecture slides on object factories.
* @throws FileNotFoundException
*/
private static void readSupervisorNames() throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("Supervisors"));
Supervisor s;
while (inFile.hasNextLine())
{
String supervisor = inFile.nextLine();
String [] splitSupervisor = supervisor.split(" ");
Name n = new Name(splitSupervisor[0], splitSupervisor[1]);
s = new Supervisor(n);
supervisors.put(n.toString(), s);
}
inFile.close();
}
/**
* This method returns supervisors from the supervisor hashmap based on the user's input of the name. If the name is non-
* existent, an error message is passed to the user.
* @param name
* @return selected supervisor
* @throws FileNotFoundException
* @throws IllegalArgumentException
*/
public static Supervisor getInstance(String name) throws FileNotFoundException, IllegalArgumentException
{
readSupervisorNames();
if (supervisors.containsKey(name))
{
return supervisors.get(name);
}
else throw new IllegalArgumentException("This supervisor does not exist");
}
/**
* Retrieves the supervisor name
* @return supervisor name
*/
public Name getName()
{
return new Name (name.getFirstName(), name.getLastName());
}
/**
* @Override
* @return the string representation of a supervisor
*/
@Override
public String toString() {
return name.getFirstName() + " " + name.getLastName();
}
/**
* Retrieves the list of supervisors available from the file "Supervisors".
* @return list of supervisors, read from file
* @throws FileNotFoundException
*/
public static Map<String, Supervisor> getListOfSupervisors() throws FileNotFoundException
{
readSupervisorNames();
return supervisors;
}
}