Fixed the small bug in the Name validation and also improved the error messages when adding a new Flight path and inputting an incorrect ICAO code.
This commit is contained in:
@@ -37,8 +37,14 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>hamcrest-core</artifactId>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -132,6 +132,14 @@ public class EntryParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public FlightPoint parsePoint(String name, String type, String altitude, String latitude, String longitude) throws DataException{
|
public FlightPoint parsePoint(String name, String type, String altitude, String latitude, String longitude) throws DataException{
|
||||||
|
//(airport) name (first and last point)
|
||||||
|
if (!isLetter(name)) {
|
||||||
|
throw new DataException("Airport ICAO code must contain only letters");
|
||||||
|
}
|
||||||
|
if (name.length() != 4) {
|
||||||
|
throw new DataException("Aiport ICAO code must be of length four");
|
||||||
|
}
|
||||||
|
//type
|
||||||
type = type.toUpperCase();
|
type = type.toUpperCase();
|
||||||
if (!type.equals("APT") && !type.equals("VOR") && !type.equals("FIX") && !type.equals("NDB") && !type.equals("LATLON")){
|
if (!type.equals("APT") && !type.equals("VOR") && !type.equals("FIX") && !type.equals("NDB") && !type.equals("LATLON")){
|
||||||
throw new DataException("Type of flight must be either APT, VOR, FIX, NDB or LATLON");
|
throw new DataException("Type of flight must be either APT, VOR, FIX, NDB or LATLON");
|
||||||
@@ -167,4 +175,31 @@ public class EntryParser {
|
|||||||
return parseSuccess;
|
return parseSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws an exception if the point name is not a valid AIRPORT ICAO code.
|
||||||
|
* @param name
|
||||||
|
* @throws DataException
|
||||||
|
*/
|
||||||
|
public void parsePointName(String name)throws DataException{
|
||||||
|
if (!isLetter(name)){
|
||||||
|
throw new DataException("Airport ICAO code must contain only letters!");
|
||||||
|
}
|
||||||
|
if (name.length() != 4) {
|
||||||
|
throw new DataException("Aiport ICAO code must be of length four!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Cycles through a string to make sure all the characters are letters
|
||||||
|
*/
|
||||||
|
private static boolean isLetter(String string){
|
||||||
|
char[] chars = string.toCharArray();
|
||||||
|
|
||||||
|
for (char element : chars) {
|
||||||
|
if(!Character.isLetter(element)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package seng202.group9.GUI;
|
package seng202.group9.GUI;
|
||||||
|
|
||||||
|
import seng202.group9.Controller.DataException;
|
||||||
|
import seng202.group9.Controller.EntryParser;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
@@ -29,10 +32,22 @@ public class NewPathPopUp {
|
|||||||
if (result == JOptionPane.OK_OPTION) {
|
if (result == JOptionPane.OK_OPTION) {
|
||||||
sourceAirport = field1.getText().toUpperCase();
|
sourceAirport = field1.getText().toUpperCase();
|
||||||
destinationAirport = field2.getText().toUpperCase();
|
destinationAirport = field2.getText().toUpperCase();
|
||||||
if (validate(sourceAirport) != true && validate(destinationAirport) != true){
|
try{
|
||||||
|
EntryParser parser = new EntryParser();
|
||||||
|
parser.parsePointName(sourceAirport);
|
||||||
|
}catch (DataException e){
|
||||||
sourceAirport = null;
|
sourceAirport = null;
|
||||||
destinationAirport = null;
|
destinationAirport = null;
|
||||||
JOptionPane.showMessageDialog(null, "Enter a vaild ICAO Code!");
|
JOptionPane.showMessageDialog(null, "Source " + e.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
EntryParser parser = new EntryParser();
|
||||||
|
parser.parsePointName(destinationAirport);
|
||||||
|
}catch (DataException e){
|
||||||
|
sourceAirport = null;
|
||||||
|
destinationAirport = null;
|
||||||
|
JOptionPane.showMessageDialog(null, "Destination " + e.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -41,30 +56,6 @@ public class NewPathPopUp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks the users entered string to make sure it is a 4 letter valid code.
|
|
||||||
private static boolean validate(String airportCode){
|
|
||||||
if(airportCode == ""){
|
|
||||||
return false;
|
|
||||||
} else if(airportCode.length() != 4){
|
|
||||||
return false;
|
|
||||||
} else if(!isLetter(airportCode)){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used by the validate() method to cycle through the string looking for non letter characters.
|
|
||||||
private static boolean isLetter(String airportCode){
|
|
||||||
char[] chars = airportCode.toCharArray();
|
|
||||||
|
|
||||||
for (char element : chars) {
|
|
||||||
if(!Character.isLetter(element)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSourceAirport() {
|
public String getSourceAirport() {
|
||||||
return sourceAirport;
|
return sourceAirport;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user