OOP2

Added Javadoc to all classes, per the requirement in the challenge (Most importantly, @author).

Author
Vngngdn
Date
Nov. 24, 2016, 2:53 p.m.
Hash
e58905458bfca974a57ba52df51b22014fa68ec8
Parent
7345d9a840ba28d7ad85ce5f886ee3256dd1280b
Modified files
Challenge 6/MainWindow.java
Challenge 6/ReservationController.java
Challenge 6/ReservationView.java
Challenge 6/Room.java
Challenge 6/RoomController.java
Challenge 6/SearchView.java

Challenge 6/MainWindow.java

8 additions and 0 deletions.

View changes Hide changes
+
1
 * Main window for the hostel program.
+
2
 * This class creates some sort of "main menu screen". It displays a concise set
+
3
 * of buttons, that allow the user to reach all parts of the program's GUI.
+
4
 * To be called as soon as the program is started (Essentially from the Main
+
5
 * class).
+
6
 * @author Maarten Vangeneugden - 1438256
+
7
 */
+
8
public class MainWindow {
1
9
2
10
	private ReservationController reservationController;
3
11
	private RoomController roomController;
4
12
	
5
13
	public MainWindow(ReservationController reservationController, RoomController roomController) {
6
14
		this.reservationController = reservationController;
7
15
		this.roomController = roomController;
8
16
9
17
		Window window = new Window("Main menu");
10
18
		window.createButton("New reservation", "", "addReservation", this);
11
19
		window.createButton("Search screen", "", "openSearchView", this);
12
20
	}
13
21
14
22
	public void openSearchView() {
15
23
		SearchView sv = new SearchView(this.reservationController, this.roomController);
16
24
	}
17
25
	
18
26
	public void addReservation() {
19
27
		Reservation reservation = new Reservation(this.reservationController, this.roomController);
20
28
		ReservationView rv = new ReservationView(reservation, this.reservationController, this.roomController);
21
29
22
30
	}
23
31
24
32
}
25
33

Challenge 6/ReservationController.java

10 additions and 0 deletions.

View changes Hide changes
1
1
import java.util.HashSet;
2
2
import java.util.Date;
3
3
4
4
public class ReservationController {
+
5
 * Controller class for the Reservations of this program.
+
6
 * Since this program handles a youth hostel, it's imperative that it holds a
+
7
 * number of Reservations.
+
8
 * 
+
9
 * ReservationController takes the responsibility of handling the addition,
+
10
 * cancelling, editing, ... of Reservations, and the related tasks inherent to
+
11
 * these responsibilities, such as reserving beds, generating IDs, ...
+
12
 * @author Maarten Vangeneugden - 1438256
+
13
 */
+
14
public class ReservationController {
5
15
6
16
	private Set<Reservation> reservations;
7
17
8
18
	public ReservationController() {
9
19
		this.reservations = new HashSet<>();
10
20
	}
11
21
12
22
	public void setReservations(Set<Reservation> reservations) {
13
23
		this.reservations = reservations;
14
24
	}
15
25
16
26
	public Set<Reservation> getReservations() {
17
27
		return reservations;
18
28
	}
19
29
20
30
	public int generateReservationID() {
21
31
		int ID = 0;
22
32
		while(true) {
23
33
			for(Reservation reservation: this.reservations) {
24
34
				if(reservation.getReservationID() == ID) {
25
35
					ID++;
26
36
					continue;
27
37
				}
28
38
			}
29
39
			break;
30
40
		}
31
41
		// Test:
32
42
		for(Reservation reservation: this.reservations) {
33
43
			assert reservation.getReservationID() != ID : "Duplicate ID generated!";
34
44
		}
35
45
		return ID;
36
46
	}
37
47
38
48
	/**
39
49
	 * Adds and confirms the reservation.
40
50
	 * By calling this method, the given reservation will be stored in the
41
51
	 * system, and the given room will be filled.
42
52
	 * @pre room must have enough empty beds
43
53
	 * @pre no parameter may be null
44
54
	 */
45
55
	public void addReservation(Reservation reservation, Room room) {
46
56
		// TODO: Add reservation to the set, and add the reserved days to the
47
57
		// beds in the given room.
48
58
	}
49
59
	public void cancelReservation(Reservation reservation) {
50
60
		// TODO: Remove from controller list; release beds, ...
51
61
	}
52
62
53
63
}
54
64

Challenge 6/ReservationView.java

1 addition and 0 deletions.

View changes Hide changes
1
1
import java.util.Set;
2
2
import java.util.HashSet;
3
3
import java.util.Date;
4
4
/**
5
5
 * Creates a view screen that allows interaction with the reservation.
6
6
 * This window will place the reservation details in editable fields, so its
7
7
 * state can be adapted.
8
8
 * It then also adds a couple of buttons; add/update/cancel, with their
9
9
 * respective actions. These will then be passed to the ReservationController.
10
10
 * It will not allow to add/update a reservation if dates overlap, i.e. beds
11
11
 * can't be reserved for that period.
12
12
 */
+
13
 */
13
14
public class ReservationView {
14
15
15
16
	private Reservation reservation;
16
17
	private Window window;
17
18
18
19
	private ReservationController rc;
19
20
	private RoomController roc;
20
21
21
22
	// GUI widgets:
22
23
	private JTextField nameField;
23
24
	private JSpinner amountPeopleField;
24
25
	private JTextField dateField;
25
26
	private JSpinner durationField;
26
27
	private JRadioButton[] typeField;
27
28
	private JCheckBox showerField;
28
29
	private JCheckBox bathField;
29
30
	private JCheckBox minibarField;
30
31
	private JCheckBox aircoField;
31
32
32
33
33
34
	public ReservationView(Reservation reservation, ReservationController rc, RoomController roc) {
34
35
		this.rc = rc;
35
36
		this.roc = roc;
36
37
37
38
		this.reservation = reservation;
38
39
		this.window = new Window("Reservation screen");
39
40
		this.addFields();
40
41
	}
41
42
42
43
	public void setReservation(Reservation reservation) {
43
44
		this.reservation = reservation;
44
45
	}
45
46
46
47
	public Reservation getReservation() {
47
48
		return reservation;
48
49
	}
49
50
50
51
	private void addFields() {
51
52
		this.window.createLabel("Group name");
52
53
		this.nameField = window.createTextField(this.reservation.getGroupName());
53
54
		this.window.createLabel("Amount of people");
54
55
		this.amountPeopleField = window.createSpinner(1, 20);
55
56
		// Formatting date for the date field:
56
57
		this.window.createLabel("date");
57
58
		Date date = this.reservation.getDate();
58
59
		String day = String.valueOf(date.getDate());
59
60
		String month = String.valueOf(date.getMonth()+1);
60
61
		String year = String.valueOf(date.getYear()+1900);
61
62
		this.dateField = window.createTextField(day +" "+ month +" "+ year);
62
63
		this.window.createLabel("Duration");
63
64
		this.durationField = window.createSpinner(1, 355);
64
65
		String[] types = new String[3];
65
66
		types[0] = "Male";
66
67
		types[1] = "Female";
67
68
		types[2] = "Mixed";
68
69
		this.typeField = this.window.createRadioButtons(types);
69
70
		this.showerField = this.window.createCheckbox("Shower");
70
71
		this.bathField = this.window.createCheckbox("Bath");
71
72
		this.minibarField = this.window.createCheckbox("Minibar");
72
73
		this.aircoField = this.window.createCheckbox("Airco");
73
74
74
75
75
76
		this.window.createButton("Add/Update reservation", "", "addReservation", this);
76
77
		this.window.createButton("Cancel reservation", "", "cancelReservation", this);
77
78
	}
78
79
79
80
	
80
81
81
82
	public void addReservation() {
82
83
		// Collect all data from the fields
83
84
		String name = this.nameField.getText();
84
85
		String date = this.dateField.getText();
85
86
		// Extracting date data:
86
87
		String[] dateParts = date.split(" ");
87
88
		int day = Integer.parseInt(dateParts[0]);
88
89
		int month = Integer.parseInt(dateParts[1]);
89
90
		int year = Integer.parseInt(dateParts[2]);
90
91
		Date actualDate = new Date(day, month-1, year-1900);
91
92
92
93
		int amount = (Integer)this.amountPeopleField.getValue();
93
94
		int duration = (Integer)this.durationField.getValue();
94
95
		String type = "";
95
96
		for(int i=0; i<this.typeField.length; i++) {
96
97
			if(this.typeField[i].isSelected()) {
97
98
				type = this.typeField[i].getText();
98
99
				break;
99
100
			}
100
101
		}
101
102
		Set<String> facilities = new HashSet<>();
102
103
		if(this.showerField.isSelected()) {
103
104
			facilities.add("Shower");
104
105
		}
105
106
		if(this.bathField.isSelected()) {
106
107
			facilities.add("Bath");
107
108
		}
108
109
		if(this.minibarField.isSelected()) {
109
110
			facilities.add("Minibar");
110
111
		}
111
112
		if(this.aircoField.isSelected()) {
112
113
			facilities.add("Airco");
113
114
		}
114
115
115
116
		Set<Room> possibleRooms = this.roc.getQualifiedRooms(actualDate, duration, type, facilities);
116
117
		if(possibleRooms.size() == 0) {
117
118
			boolean tryAgain = this.window.confirmDialog("No rooms met the requirements! Would you like to continue and change the parameters?");
118
119
			if(!tryAgain) {
119
120
				// TODO close window
120
121
			}
121
122
		}
122
123
		else {
123
124
			// Determine end date:
124
125
			long beginDate = actualDate.getTime();
125
126
			long endDate = beginDate + (duration * 24 * 60 * 60 * 1000);
126
127
			Date actualEndDate = new Date(endDate);
127
128
			Room pickedRoom = null;
128
129
			for(Room room: possibleRooms) {
129
130
				if(room.getEmptyBeds(actualDate, actualEndDate).size() < room.getBeds().size()) {
130
131
					// First, fill the rooms that are partially filled.
131
132
					pickedRoom = room;
132
133
					break;
133
134
				}
134
135
			}
135
136
			if(pickedRoom == null) { // If still no room, pick an empty room
136
137
				for(Room room: possibleRooms) {
137
138
					if(room.getEmptyBeds(actualDate, actualEndDate).size() >= amount) {
138
139
						pickedRoom = room;
139
140
						break;
140
141
					}
141
142
				}
142
143
			}
143
144
			assert pickedRoom != null;
144
145
			// TODO: Set reservation fields here!
145
146
			this.rc.addReservation(reservation, pickedRoom);
146
147
147
148
			// Confirm and show price:
148
149
			int price = this.reservation.getPrice();
149
150
			this.window.confirmDialog("Reservation confirmed! Price: " +String.valueOf(price));
150
151
		}
151
152
		
152
153
	}
153
154
	public void cancelReservation() {
154
155
		// TODO: Close the window. That's all.
155
156
	}
156
157
}
157
158

Challenge 6/Room.java

8 additions and 0 deletions.

View changes Hide changes
1
1
import java.util.HashSet;
2
2
import java.util.Date;
3
3
4
4
public class Room {
+
5
 * A room in a hostel.
+
6
 * Room represents just that: A room.
+
7
 * A room contains a set of Beds, facilities that can be used, etc.
+
8
 * It's highly decoupled: Apart from holding a set of Beds, the only members
+
9
 * types consist of those that are available in every OpenJDK implementation.
+
10
 * @author Maarten Vangeneugden - 1438256
+
11
 */
+
12
public class Room {
5
13
6
14
	private Set<Bed> beds;
7
15
	private String type;
8
16
	private Set<String> facilities;
9
17
10
18
	public Room(Set<Bed> beds, String type, Set<String> facilities) {
11
19
		this.beds = beds;
12
20
		this.type = type;
13
21
		this.facilities = facilities;
14
22
	}
15
23
16
24
	public void setBeds(Set<Bed> beds) {
17
25
		this.beds = beds;
18
26
	}
19
27
20
28
	public Set<Bed> getBeds() {
21
29
		return beds;
22
30
	}
23
31
24
32
	public void setType(String type) {
25
33
		this.type = type;
26
34
	}
27
35
28
36
	public String getType() {
29
37
		return type;
30
38
	}
31
39
32
40
	public void setFacilities(Set<String> facilities) {
33
41
		this.facilities = facilities;
34
42
	}
35
43
36
44
	public Set<String> getFacilities() {
37
45
		return facilities;
38
46
	}
39
47
40
48
	public Set<Bed> getEmptyBeds(Date begin, Date end) {
41
49
		Set<Bed> emptyBeds = new HashSet<>();
42
50
		for(Bed bed: this.beds) {
43
51
			if(bed.isFree(begin, end)) {
44
52
				emptyBeds.add(bed);
45
53
			}
46
54
		}
47
55
		return emptyBeds;
48
56
	}
49
57
50
58
}
51
59

Challenge 6/RoomController.java

4 additions and 0 deletions.

View changes Hide changes
1
1
import java.util.Date;
2
2
import java.util.HashSet;
3
3
4
4
/**
5
5
 * Holds all Rooms in the hostel.
6
6
 */
+
7
 * This class takes care of storing all those Rooms, and provides methods to
+
8
 * manage this, like removing Rooms, adding Rooms, ...
+
9
 * @author Maarten Vangeneugden - 1438256
+
10
 */
7
11
public class RoomController {
8
12
9
13
	private Set<Room> rooms;
10
14
11
15
	public RoomController() {
12
16
		this.rooms = new HashSet<>();
13
17
	}
14
18
15
19
	public void setRooms(Set<Room> rooms) {
16
20
		this.rooms = rooms;
17
21
	}
18
22
19
23
	public Set<Room> getRooms() {
20
24
		return rooms;
21
25
	}
22
26
23
27
	/**
24
28
	 * Returns all rooms that meet the given requirements.
25
29
	 * This method will search through all rooms, and check which rooms qualify.
26
30
	 * @return A set of all rooms that meet the requirements, or an empty set if
27
31
	 * none were found.
28
32
	 */
29
33
	public Set<Room> getQualifiedRooms(Date begin, int duration, String type, Set<String> facilities) {
30
34
		Set<Room> qualifiedRooms = new HashSet<>();
31
35
		long beginDate = begin.getTime();
32
36
		long endDate = beginDate + (duration * 24 * 60 * 60 * 1000);
33
37
		Date end = new Date(endDate);
34
38
35
39
		// TODO: Loop through all rooms that qualify, collect them, and return
36
40
		// that set.
37
41
		return qualifiedRooms;
38
42
	}
39
43
}
40
44

Challenge 6/SearchView.java

10 additions and 0 deletions.

View changes Hide changes
+
1
 * Class for creating a screen that allows querying actions.
+
2
 * This program offers the ability to search in the program for Reservations,
+
3
 * Room status, etc.
+
4
 * This class creates a GUI for user interaction.
+
5
 * When matches are found, it will display them. In case of a unique match, that
+
6
 * match is immediately displayed.
+
7
 * @see ReservationView
+
8
 * @author Maarten Vangeneugden - 1438256
+
9
 */
+
10
public class SearchView {
1
11
	private ReservationController rc;
2
12
	private RoomController roc;
3
13
	private Window window;
4
14
5
15
	public SearchView(ReservationController rc, RoomController roc) {
6
16
		this.rc = rc;
7
17
		this.roc = roc;
8
18
9
19
		this.window = new Window("Search screen");
10
20
		this.addFields();
11
21
	}
12
22
	
13
23
	private void addFields() {
14
24
		// TODO: Add fields to private members!
15
25
		this.window.createLabel("Reservation number:");
16
26
		this.window.createTextField("");
17
27
		this.window.createLabel("Group name:");
18
28
		this.window.createTextField("");
19
29
20
30
	}
21
31
}
22
32