You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

package hs.fulda.de.ci.exam.project;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Person {
private String name;
private Address address;
private String email;
private String phone;
private FlightRepository flightRepository;
private ItineraryRepository itineraryRepository;
public Person(String name, Address address, String email, String phone) {
this.name = name;
this.address = address;
this.email = email;
this.phone = phone;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public void validateName() {
if(this.name.isBlank())
throw new RuntimeException(("Name Cannot be null or empty"));
}
public void validateAddress(){
if(this.address.equals(null))
throw new RuntimeException(("Address Cannot be null"));
}
public void validateEmailAddress(){
Pattern pattern = Pattern.compile("^(.+)@(.+)$");
Matcher matcher = pattern.matcher(this.email);
if(email.isBlank()){
throw new RuntimeException("Email cannot be blank");
}
if(!matcher.matches()){
throw new RuntimeException("Email address is not Valid");
}
}
public void validatePhoneNumber(){
if(this.phone.isBlank()) {
throw new RuntimeException("Phone Number Cannot be null or empty");
}
if(this.phone.length()>13) {
throw new RuntimeException("Phone Number is too long");
}
if(this.phone.length()<9) {
throw new RuntimeException("Phone Number is too short");
}
if(this.phone.matches("\\d")) {
throw new RuntimeException("Phone Number Contain only digits");
}
if(!this.phone.startsWith("0")) {
throw new RuntimeException("Phone Number should start with 0");
}
}
public String searchFlights(String flightNumber){
String flightDetails = flightRepository.findFlightByFlightNumber(flightNumber);
if(flightDetails.isBlank()){
throw new RuntimeException("Flight does not exist.");
}
return flightDetails;
}
public ArrayList<String> searchAllFlightsByCriteria(Airport departure, Airport arrival){
ArrayList<String> flights = flightRepository.findbyDepartureArrival(departure, arrival);
if(flights.isEmpty()){
throw new RuntimeException("Flights do not exist.");
}
return flights;
}
public ArrayList<Itinerary> getItineraries() {
return itineraryRepository.findAll();
}
}