Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson4 #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/com/javafordev/lesson4/task1/Airline.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.util.Arrays;

import static com.javafordev.lesson4.task1.Validator.validateInputParameterForString;
import static com.javafordev.lesson4.task1.Validator.validateStringParameterForNumber;
import static com.javafordev.lesson4.task1.Validator.*;

/**
* 1)Создать класс Airline:
Expand Down Expand Up @@ -53,38 +52,38 @@ public String[] getDaysOfWeek() {
}

public void setDestination(String destination) {
this.destination = validateInputParameterForString("destination", destination);
this.destination = validateDestination(destination);
}

public void setNumberOfFlight(int numberOfFlight) {
this.numberOfFlight = validateStringParameterForNumber("numberOfFlight", numberOfFlight);
this.numberOfFlight = validateNumberOfFlight(numberOfFlight);
}

public void setPlaneType(char planeType) {
this.planeType = planeType;
}

public void setDepartureTimeInHours(int departureTimeInHours) {
this.departureTimeInHours = validateStringParameterForNumber("departureTimeInHours", departureTimeInHours);
this.departureTimeInHours = validateDepartureTimeInHours(departureTimeInHours);
}

public void setDepartureTimeInMinutes(int departureTimeInMinutes) {
this.departureTimeInMinutes = validateStringParameterForNumber("departureTimeInMinutes", departureTimeInMinutes);
this.departureTimeInMinutes = validateDepartureTimeInMinutes(departureTimeInMinutes);
}

public void setDaysOfWeek(String... daysOfWeek) {
public void setDaysOfWeek(String[] daysOfWeek) {
this.daysOfWeek = new String[daysOfWeek.length];
for (int i = 0; i < daysOfWeek.length; i++) {
this.daysOfWeek[i] = daysOfWeek[i];
}
}

public Airline(String destination, int numberOfFlight, char planeType, int departureTimeInHours, int departureTimeInMinutes, String... daysOfWeek) {
public Airline(String destination, int numberOfFlight, char planeType, int departureTimeInHours, int departureTimeInMinutes, String[] daysOfWeek) {

this.destination = validateInputParameterForString("destination", destination);
this.numberOfFlight = validateStringParameterForNumber("numberOfFlight", numberOfFlight);
this.departureTimeInHours = validateStringParameterForNumber("departureTimeInHours", departureTimeInHours);
this.departureTimeInMinutes = validateStringParameterForNumber("departureTimeInMinutes", departureTimeInMinutes);
this.destination = validateDestination(destination);
this.numberOfFlight = validateNumberOfFlight(numberOfFlight);
this.departureTimeInHours = validateDepartureTimeInHours(departureTimeInHours);
this.departureTimeInMinutes = validateDepartureTimeInMinutes(departureTimeInMinutes);
this.planeType = planeType;
this.daysOfWeek = new String[daysOfWeek.length];
for (int i = 0; i < daysOfWeek.length; i++) {
Expand Down
43 changes: 21 additions & 22 deletions src/com/javafordev/lesson4/task1/AirlineRunner.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.javafordev.lesson4.task1;

import java.util.Arrays;

import static com.javafordev.lesson4.task1.AirlineService.printArrayOfAirlines;


public class AirlineRunner {
/**
* Тестовые значения:
Expand All @@ -22,38 +17,42 @@ public class AirlineRunner {
public static void main(String[] args) {

int numberOfAirlines = 5;
String firstFilterName = "destination";
String firstFilterValue = "Warsaw";
String secondFilterName = "dayOfWeek";
String secondFilterValue = "Thu";
String firstFilterValue = "Tokyo";
String secondFilterValue = "Fri";
int departureTimeInHoursToFilter = 5;
int departureTimeInMinutesToFilter = 22;

//инициализация массива объектов типа Airline
Airline[] airlines = new Airline[numberOfAirlines];

airlines[0] = new Airline("Братислава", 1675, 'A', 22, 10, "Sun", "Mon");
airlines[1] = new Airline("Warsaw", 1540, 'B', 9, 3, "Thu");
airlines[2] = new Airline("Ottawa", 1004, 'A', 7, 37, "Mon", "Fri");
airlines[3] = new Airline("Tokyo", 936, 'C', 16, 21, "Mon", "Tue", "Fri", "Sat");
airlines[4] = new Airline("Minsk", 2255, 'B', 4, -21, "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
String[] daysOfWeek0 = {"Sun", "Mon"};
String[] daysOfWeek1 = {"Thu"};
String[] daysOfWeek2 = {"Mon", "Fri"};
String[] daysOfWeek3 = {"Mon", "Tue", "Fri", "Sat"};
String[] daysOfWeek4 = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
airlines[0] = new Airline("Rome", 1675, 'A', 22, 10, daysOfWeek0);
airlines[1] = new Airline("Warsaw", 1540, 'B', 9, 3, daysOfWeek1);
airlines[2] = new Airline("Ottawa", 1004, 'A', 7, 37, daysOfWeek2);
airlines[3] = new Airline("Tokyo", 936, 'C', 16, 21, daysOfWeek3);
airlines[4] = new Airline("Minsk", 2255, 'B', 4, 22, daysOfWeek4);

AirlineService airlineService = new AirlineService(airlines);

System.out.println("Вывод элементов массива отфильтрованного по destination");
Airline[] airlinesFilteredByDestination = airlineService.findAirlinesByFilter(firstFilterName, firstFilterValue);
printArrayOfAirlines(airlinesFilteredByDestination);
Airline[] airlinesFilteredByDestination = airlineService.findAllByDestination(firstFilterValue);
AirlineService airlinesFilteredByDestinationService = new AirlineService(airlinesFilteredByDestination);
airlinesFilteredByDestinationService.printList();
System.out.println();

System.out.println("Вывод элементов массива отфильтрованного по dayOfWeek");
Airline[] airlinesFilteredByDayOfWeek = airlineService.findAirlinesByFilter(secondFilterName, secondFilterValue);
printArrayOfAirlines(airlinesFilteredByDayOfWeek);
Airline[] airlinesFilteredByDayOfWeek = airlineService.findAllByDayOfWeek(secondFilterValue);
AirlineService airlinesFilteredByDayOfWeekService = new AirlineService(airlinesFilteredByDayOfWeek);
airlinesFilteredByDayOfWeekService.printList();
System.out.println();

AirlineService airlineServiceFilteredByDayOfWeek = new AirlineService(airlinesFilteredByDayOfWeek);

System.out.println("Вывод элементов массива отфильтрованного по dayOfWeek и departureTime");
Airline[] airlinesFilteredByDayOfWeekAndDepartureTime = airlineServiceFilteredByDayOfWeek.findAirlinesByDepartureTime(departureTimeInHoursToFilter, departureTimeInMinutesToFilter);
printArrayOfAirlines(airlinesFilteredByDayOfWeekAndDepartureTime);
Airline[] airlinesFilteredByDayOfWeekAndDepartureTime = airlinesFilteredByDayOfWeekService.findAllByDepartureTime(departureTimeInHoursToFilter, departureTimeInMinutesToFilter);
AirlineService airlinesFilteredByDayOfWeekAndDepartureTimeService = new AirlineService(airlinesFilteredByDayOfWeekAndDepartureTime);
airlinesFilteredByDayOfWeekAndDepartureTimeService.printList();
}
}
81 changes: 32 additions & 49 deletions src/com/javafordev/lesson4/task1/AirlineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,37 @@ public String toString() {
'}';
}

//метод для поиска объектов из массива airlines по фильтру - 1) destination, 2)dayOfWeek, 3) можно добавить другои - planeType, numberOfFlight
public Airline[] findAirlinesByFilter(String filterName, String filterValue) {
public Airline[] findAllByDestination(String destination) {
Airline[] filteredAirlines = new Airline[this.airlines.length];
switch (filterName) {
case "destination":
int i = 0;
for (Airline element : this.airlines) {
if (element.getDestination().equals(filterValue)) {
filteredAirlines[i] = element;
i++;
}
}
break;
case "dayOfWeek":
int j = 0;
for (Airline element : this.airlines) {
for (int k = 0; k < element.getDaysOfWeek().length; k++) {
if (element.getDaysOfWeek()[k].equals(filterValue)) {
filteredAirlines[j] = element;
j++;
}
}
int i = 0;
for (Airline element : this.airlines) {
if (element.getDestination().equals(destination)) {
filteredAirlines[i] = element;
i++;
}
}

Airline[] fitleredAirlinesWithoutNullElements = ArrayUtils.removeNullElementsFromAirlines(filteredAirlines);

if (fitleredAirlinesWithoutNullElements.length == 0) {
System.out.println("Не удалось наити объект массива airlines, которыи соответствует фильтру");
}
return fitleredAirlinesWithoutNullElements;
}

public Airline[] findAllByDayOfWeek(String dayOfWeek) {
Airline[] filteredAirlines = new Airline[this.airlines.length];
int j = 0;
for (Airline element : this.airlines) {
for (int k = 0; k < element.getDaysOfWeek().length; k++) {
if (element.getDaysOfWeek()[k].equals(dayOfWeek)) {
filteredAirlines[j] = element;
j++;
}
break;
default:
System.out.println("Невозможно выполнить фильтрация по заданному параметру!");
}
}

Airline[] fitleredAirlinesWithoutNullElements = removeNullElementsFromAirlines(filteredAirlines);
Airline[] fitleredAirlinesWithoutNullElements = ArrayUtils.removeNullElementsFromAirlines(filteredAirlines);

if (fitleredAirlinesWithoutNullElements.length == 0) {
System.out.println("Не удалось наити объект массива airlines, которыи соответствует фильтру");
Expand All @@ -64,7 +66,7 @@ public Airline[] findAirlinesByFilter(String filterName, String filterValue) {
}

//метод для поиска объектов из массива airlines по фильтру departureTime
public Airline[] findAirlinesByDepartureTime(int departureTimeInHours, int departureTimeInMinutes) {
public Airline[] findAllByDepartureTime(int departureTimeInHours, int departureTimeInMinutes) {
Airline[] filteredAirlines = new Airline[this.airlines.length];
int timeInMinutesToFilter = departureTimeInHours * 60 + departureTimeInMinutes;
int i = 0;
Expand All @@ -75,36 +77,17 @@ public Airline[] findAirlinesByDepartureTime(int departureTimeInHours, int depar
i++;
}
}
Airline[] fitleredAirlinesWithoutNullElements = removeNullElementsFromAirlines(filteredAirlines);
Airline[] fitleredAirlinesWithoutNullElements = ArrayUtils.removeNullElementsFromAirlines(filteredAirlines);

if (fitleredAirlinesWithoutNullElements.length == 0) {
System.out.println("Не удалось наити объект массива airlines, которыи соответствует фильтру");
}
return fitleredAirlinesWithoutNullElements;
}

public static Airline[] removeNullElementsFromAirlines(Airline[] airlines) {
int countOfNotNullElements = 0;
for (Airline element : airlines) {
if (element != null) {
countOfNotNullElements++;
}
}

Airline[] fitleredAirlinesWithoutNullElements = new Airline[countOfNotNullElements];
int i = 0;
for (Airline element : airlines) {
if (element != null) {
fitleredAirlinesWithoutNullElements[i] = element;
i++;
}
}
return fitleredAirlinesWithoutNullElements;
}

public static void printArrayOfAirlines(Airline[] airlines) {
for (Airline element : airlines) {
System.out.println(element.toString());
public void printList() {
for (Airline element : this.airlines) {
System.out.println(element);
}
}
}
23 changes: 23 additions & 0 deletions src/com/javafordev/lesson4/task1/ArrayUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.javafordev.lesson4.task1;

public class ArrayUtils {

public static Airline[] removeNullElementsFromAirlines(Airline[] airlines) {
int countOfNotNullElements = 0;
for (Airline element : airlines) {
if (element != null) {
countOfNotNullElements++;
}
}

Airline[] fitleredAirlinesWithoutNullElements = new Airline[countOfNotNullElements];
int i = 0;
for (Airline element : airlines) {
if (element != null) {
fitleredAirlinesWithoutNullElements[i] = element;
i++;
}
}
return fitleredAirlinesWithoutNullElements;
}
}
56 changes: 29 additions & 27 deletions src/com/javafordev/lesson4/task1/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@

public class Validator {

public static String validateInputParameterForString(String parameterName, String parameterValue) {
public static String validateDestination(String destination) {

if (parameterName.equals("destination") && parameterValue.matches("[A-Za-z]+")) {
return parameterValue;
if (destination.matches("[A-Za-z]+")) {
return destination;
} else {
System.out.println("Для параметра destination задано пустое значение или строка состоит не из латинских символов.");
return " ";
}
}

public static int validateStringParameterForNumber(String parameterName, int parameterValue) {

switch (parameterName) {
case "numberOfFlight":
if (parameterValue <= 0) {
System.out.println("Для параметра numberOfFlight задано отрицательное или нулевое значение. "
+ "Допустимые значения - целые положительные числа");
}
break;
case "departureTimeInHours":
if (parameterValue < 0 || parameterValue > 23) {
System.out.println("Для параметра departureTimeInHours задано недопустимое значение. "
+ "Допустимые значения - целые положительные числа от 0 до 23");
}
break;
case "departureTimeInMinutes":
if (parameterValue < 0 || parameterValue > 59) {
System.out.println("Для параметра departureTimeInMinutes задано недопустимое значение. "
+ "Допустимые значения - целые положительные числа от 0 до 59");
}
break;
default:
System.out.println("Задано некорректное имя входного параметра!");
public static int validateNumberOfFlight(int numberOfFlight) {
if (numberOfFlight <= 0) {
System.out.println("Для параметра numberOfFlight задано отрицательное или нулевое значение. "
+ "Допустимые значения - целые положительные числа");
}
return parameterValue;
return numberOfFlight;

}

public static int validateDepartureTimeInHours(int departureTimeInHours) {
if (departureTimeInHours < 0 || departureTimeInHours > 23) {
System.out.println("Для параметра departureTimeInHours задано недопустимое значение. "
+ "Допустимые значения - целые положительные числа от 0 до 23");
}
return departureTimeInHours;

}

public static int validateDepartureTimeInMinutes(int departureTimeInMinutes) {
if (departureTimeInMinutes < 0 || departureTimeInMinutes > 59) {
System.out.println("Для параметра departureTimeInMinutes задано недопустимое значение. "
+ "Допустимые значения - целые положительные числа от 0 до 59");
}
return departureTimeInMinutes;

}

public static Airline[] validateInputParameterForArray(Airline[] arrayName) {
for (Airline element : arrayName) {
Expand All @@ -49,5 +48,8 @@ public static Airline[] validateInputParameterForArray(Airline[] arrayName) {
}
return arrayName;
}



}

Loading