OOP2

WalkView.java

1
import javax.swing.*;
2
3
import java.util.Set;
4
import java.util.HashSet;
5
import java.util.Date;
6
7
/**
8
 * GUI class that handles the GUI part of walks.
9
 * @author Maarten Vangeneugden - 1438256
10
 */
11
public class WalkView {
12
	private JTextField groupNameField;
13
	private JComboBox<String> possibleWalksField;
14
	private JTextField dateField;
15
	private JSpinner peopleField;
16
17
	private WalkReservation walkReservation;
18
	private ReservationController rc;
19
	private WalkController wc;
20
	private Window window;
21
22
	public WalkView(WalkReservation walkReservation, ReservationController rc, WalkController wc) {
23
		if(walkReservation == null || rc == null || wc == null) {
24
			throw new NullPointerException("One or more of the given parameters is a null pointer.");
25
		}
26
		// Contract validated
27
		this.walkReservation = walkReservation;
28
		this.rc = rc;
29
		this.wc = wc;
30
31
		this.window = new Window("Walk reservation screen");
32
		this.addFields();
33
	}
34
35
	private void addFields() {
36
		this.groupNameField = this.window.createTextField(this.walkReservation.getGroupName());
37
38
		// Getting list of all walks
39
		Set<String> walkNames = new HashSet<>();
40
		for(Walk walk: this.wc.getWalks()) {
41
			System.out.println("YAY A WALKK");
42
			walkNames.add(walk.getName());
43
		}
44
		this.possibleWalksField = this.window.addComboBox(walkNames.toArray(new String[1]));
45
		this.dateField = this.window.createTextField(walkReservation.getBeginDate().toGMTString());
46
		this.peopleField = this.window.createSpinner(1, 10, this.walkReservation.getPeople(), 1);
47
48
		this.window.createButton("Add/Update walk reservation", "", "addWalkReservation", this);
49
		this.window.createButton("Remove walk reservation", "", "removeWalkReservation", this);
50
	}
51
52
53
	public void setWalkReservation(WalkReservation walkReservation) {
54
		this.walkReservation = walkReservation;
55
	}
56
57
	public WalkReservation getWalkReservation() {
58
		return walkReservation;
59
	}
60
61
	public void setRc(ReservationController rc) {
62
		this.rc = rc;
63
	}
64
65
	public ReservationController getRc() {
66
		return rc;
67
	}
68
69
	public void setWc(WalkController wc) {
70
		this.wc = wc;
71
	}
72
73
	public WalkController getWc() {
74
		return wc;
75
	}
76
77
	public void addWalkReservation() {
78
		// First, we collect the relevant data
79
		final String groupName = this.groupNameField.getText();
80
		final Date date = new Date(this.dateField.getText());
81
		final int people = (Integer)this.peopleField.getValue();
82
		final String stringWalk = (String)this.possibleWalksField.getSelectedItem();
83
		final Walk walk = this.wc.getWalkFromName(stringWalk);
84
		// Now create a WalkReservation with this data, and send that to the
85
		// Controller.
86
		this.walkReservation.setGroupName(groupName);
87
		this.walkReservation.setBeginDate(date);
88
		this.walkReservation.setPeople(people);
89
		this.walkReservation.setWalk(walk);
90
		// Add new/updated Walk reservation to the system
91
		if(!this.wc.getWalkReservations().contains(this.walkReservation)) {
92
			this.wc.addWalkReservation(this.walkReservation);
93
		}
94
		// Display confirmation screen to user
95
		int price = this.walkReservation.getPrice(this.rc);
96
		this.window.messageDialog("Guided walk reserved! Price: "+ price);
97
		this.window.close();
98
	}
99
	
100
	public void removeWalkReservation() {
101
		if(this.wc.getWalkReservations().contains(this.walkReservation)) {
102
			this.wc.cancelWalkReservation(this.walkReservation);
103
		}
104
		this.window.close();
105
	}
106
}
107