OOP2

SearchView.java

1
import javax.swing.*;
2
import java.util.ArrayList;
3
import java.util.Date;
4
5
/**
6
 * Class for creating a screen that allows querying actions.
7
 * This program offers the ability to search in the program for Reservations,
8
 * Room status, etc.
9
 * This class creates a GUI for user interaction.
10
 * When matches are found, it will display them. In case of a unique match, that
11
 * match is immediately displayed.
12
 * @see ReservationView
13
 * @see WalkView
14
 * @author Maarten Vangeneugden - 1438256
15
 */
16
public class SearchView {
17
	private ReservationController rc;
18
	private RoomController roc;
19
	private Window window;
20
21
	private JTextField groupNameField;
22
	private JTextField reservationIDField;
23
	private JTextField bedDateField;
24
	private JTextField breakfastDayField;
25
26
	public SearchView(ReservationController rc, RoomController roc) {
27
		this.rc = rc;
28
		this.roc = roc;
29
30
		this.window = new Window("Search screen");
31
		this.addFields();
32
	}
33
	
34
	private void addFields() {
35
		// Reservations querying
36
		this.window.createLabel("Search reservations:");
37
		this.groupNameField = this.window.createTextField("Search by group name");
38
		this.reservationIDField = this.window.createTextField("Search by reservation ID");
39
		this.window.createButton("Search reservation", "", "queryReservations", this);
40
		// Reserved beds querying
41
		this.window.createLabel("Search reserved beds on given day:");
42
		this.bedDateField = this.window.createTextField(new Date().toGMTString());
43
		this.window.createButton("Get reserved beds", "", "queryBeds", this);
44
		// Breakfast querying
45
		this.window.createLabel("Find amount of breakfasts:");
46
		this.breakfastDayField = this.window.createTextField(new Date().toGMTString());
47
		this.window.createButton("Get breakfasts on given day", "", "queryBreakfasts", this);
48
	}
49
50
	public void queryReservations() {
51
		ArrayList<Reservation> foundReservations = new ArrayList<>();
52
		String nameQuery = this.groupNameField.getText();
53
		String IDQuery = this.reservationIDField.getText();
54
		for(Reservation activeReservation : this.rc.getReservations()) {
55
			if(activeReservation.getGroupName().contains(nameQuery) &&
56
				String.valueOf(activeReservation.getReservationID()).contains(IDQuery)) {
57
				foundReservations.add(activeReservation);
58
				}
59
		}
60
		// After collecting all results, offer the user the choice about which
61
		// one to take:
62
		String[] reservationNames = new String[foundReservations.size()];
63
		for(int i=0; i<foundReservations.size(); i++) {
64
			String text = foundReservations.get(i).getGroupName() +" ("+
65
				String.valueOf(foundReservations.get(i).getReservationID()) +")";
66
			reservationNames[i] = text;
67
		}
68
		int choice = this.window.choiceDialog("Multiple results were found. Specify which one you want to view:", reservationNames);
69
		if(choice == -1) { // No result found
70
			this.window.messageDialog("No reservations matched the given details.");
71
		}
72
		else {
73
			new ReservationView(foundReservations.get(choice), this.rc, this.roc);
74
		}
75
	}
76
77
	/**
78
	 * Search and display the reservation of Beds in Rooms.
79
	 * This method takes the given begin and end dates from the GUI, and lists
80
	 * for each Room, how many Beds are reserved, and by whom.
81
	 */
82
	public void queryBeds() {
83
		// The amount of reserved Beds is determined by the Reservations.
84
		// If a Reservation (partially) overlaps with the given time, then this
85
		// Bed is reserved in the given period.
86
		final Date date = new Date(this.bedDateField.getText());
87
		// endDate is required as it has to be strictly later than date.
88
		final Date endDate = new Date(date.getTime() + 1);
89
		String result = "";
90
91
		for(Room room: this.roc.getRooms()) {
92
			// XXX: The Java Object class provides a "hashcode()" method, to
93
			// retrieve an object's hashcode. I'm using that to identify the
94
			// different rooms.
95
			int usedBeds = room.getBeds().size() - room.getEmptyBeds(date, endDate).size();
96
			result = result + "Room "+ String.valueOf(room.hashCode()) +": "+ String.valueOf(usedBeds) +" reserved beds.\n";
97
		}
98
		this.window.messageDialog(result);
99
		/*for(Reservation reservation: this.rc.getReservations()) {
100
			if(!
101
				(reservation.getBeginDate().before(beginDate) && reservation.getEndDate().before(beginDate)) ||
102
				(reservation.getBeginDate().after(endDate) && reservation.getEndDate().after(endDate)))
103
			{ // This block is only reached if the valid options didn't match.
104
				String name = reservation.getGroupName();
105
				String beds = String.valueOf(reservation.getReservedBeds().size());
106
				String begin = reservation.getBeginDate().toGMTString();
107
				String end = reservation.getEndDate().toGMTString();
108
				result = result +
109
					name +" reserved "+ beds +" between "+ begin +" and "+ end +".\n";
110
			}
111
		}*/
112
	}
113
114
	/**
115
	 * Queries and displays breakfast reservations.
116
	 * After the user has entered a day in the corresponding field, this method
117
	 * can be called to search for breakfast reservations on that day.
118
	 * Breakfasts are ordered by Reservation, accompagnied by the amount of
119
	 * people in that Reservation.
120
	 * // TODO finish this javadoc
121
	 */
122
	public void queryBreakfasts() {
123
		Date date = new Date(this.breakfastDayField.getText());
124
		// Breakfast is only served at 9 o'clock, so set that time for easy
125
		// reference during querying:
126
		date.setHours(8);
127
		date.setMinutes(0);
128
		date.setSeconds(0);
129
		String response = "";
130
		for(Reservation reservation: this.rc.getReservations()) {
131
			if(reservation.getBreakfastDays().contains(date)) {
132
				String name = reservation.getGroupName();
133
				String amount = String.valueOf(reservation.getPeople());
134
				response = response +
135
					"Reservation "+ name +" ordered breakfast for "+ amount +" people.\n";
136
			}
137
		}
138
		if(!response.isEmpty()) {
139
			this.window.messageDialog(response);
140
		}
141
		else {
142
			this.window.messageDialog("There are no breakfasts ordered for this day.");
143
		}
144
	}
145
}
146