This commit is contained in:
xivihwa 2023-05-22 14:10:26 +03:00
parent a9dac38d8c
commit 9eab1efbc2
12 changed files with 192 additions and 0 deletions

BIN
lab6/lab6.pdf Normal file

Binary file not shown.

8
lab6/untitled/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/untitled.iml" filepath="$PROJECT_DIR$/untitled.iml" />
</modules>
</component>
</project>

Binary file not shown.

159
lab6/untitled/src/Main.java Normal file
View File

@ -0,0 +1,159 @@
import java.util.Arrays;
import java.util.Comparator;
/**
* Клас, що описує базовий рухомий склад залізничного транспорту.
*/
class RollingStock {
private int number;
public RollingStock(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
/**
* Клас, що описує пасажирський вагон.
*/
class PassengerCarriage extends RollingStock {
private int passengerCount;
private int comfortLevel;
public PassengerCarriage(int number, int passengerCount, int comfortLevel) {
super(number);
this.passengerCount = passengerCount;
this.comfortLevel = comfortLevel;
}
public int getPassengerCount() {
return passengerCount;
}
public int getComfortLevel() {
return comfortLevel;
}
}
/**
* Клас, що описує вагон для багажу.
*/
class BaggageCarriage extends RollingStock {
private int baggageCount;
public BaggageCarriage(int number, int baggageCount) {
super(number);
this.baggageCount = baggageCount;
}
public int getBaggageCount() {
return baggageCount;
}
}
/**
* Клас, що описує пасажирський потяг.
*/
class PassengerTrain {
private RollingStock[] rollingStock;
public PassengerTrain(RollingStock[] rollingStock) {
this.rollingStock = rollingStock;
}
public int getTotalPassengerCount() {
int totalPassengerCount = 0;
for (RollingStock stock : rollingStock) {
if (stock instanceof PassengerCarriage) {
PassengerCarriage carriage = (PassengerCarriage) stock;
totalPassengerCount += carriage.getPassengerCount();
}
}
return totalPassengerCount;
}
public int getTotalBaggageCount() {
int totalBaggageCount = 0;
for (RollingStock stock : rollingStock) {
if (stock instanceof BaggageCarriage) {
BaggageCarriage carriage = (BaggageCarriage) stock;
totalBaggageCount += carriage.getBaggageCount();
}
}
return totalBaggageCount;
}
public void sortByComfortLevel() {
Arrays.sort(rollingStock, new Comparator<RollingStock>() {
@Override
public int compare(RollingStock o1, RollingStock o2) {
if (o1 instanceof PassengerCarriage && o2 instanceof PassengerCarriage) {
PassengerCarriage carriage1 = (PassengerCarriage) o1;
PassengerCarriage carriage2 = (PassengerCarriage) o2;
return carriage1.getComfortLevel() - carriage2.getComfortLevel();
}
return 0;
}
});
}
public RollingStock findCarriageByPassengerCountRange(int minPassengerCount, int maxPassengerCount) {
for (RollingStock stock : rollingStock) {
if (stock instanceof PassengerCarriage) {
PassengerCarriage carriage = (PassengerCarriage) stock;
if (carriage.getPassengerCount() >= minPassengerCount && carriage.getPassengerCount() <= maxPassengerCount) {
return carriage;
}
}
}
return null;
}
}
/**
* Головний клас програми.
*/
public class Main {
public static void main(String[] args) {
// Створення вагонів потягу
RollingStock[] rollingStock = new RollingStock[5];
rollingStock[0] = new PassengerCarriage(1, 40, 3);
rollingStock[1] = new PassengerCarriage(2, 30, 2);
rollingStock[2] = new BaggageCarriage(3, 50);
rollingStock[3] = new PassengerCarriage(4, 20, 1);
rollingStock[4] = new PassengerCarriage(5, 35, 2);
// Створення пасажирського потягу
PassengerTrain train = new PassengerTrain(rollingStock);
// Розрахунок загальної кількості пасажирів та багажу в потязі
int totalPassengerCount = train.getTotalPassengerCount();
int totalBaggageCount = train.getTotalBaggageCount();
System.out.println("Total passenger count: " + totalPassengerCount);
System.out.println("Total baggage count: " + totalBaggageCount);
// Сортування вагонів за рівнем комфортності
train.sortByComfortLevel();
System.out.println("Sorted carriages by comfort level:");
for (RollingStock stock : rollingStock) {
if (stock instanceof PassengerCarriage) {
PassengerCarriage carriage = (PassengerCarriage) stock;
System.out.println("Carriage " + carriage.getNumber() + ", Comfort Level: " + carriage.getComfortLevel());
}
}
// Знаходження вагона з пасажирськими місцями в заданому діапазоні кількості пасажирів
int minPassengerCount = 25;
int maxPassengerCount = 40;
RollingStock foundCarriage = train.findCarriageByPassengerCountRange(minPassengerCount, maxPassengerCount);
if (foundCarriage != null) {
System.out.println("Found carriage with passenger count in range (" + minPassengerCount + "-" + maxPassengerCount + "): " + foundCarriage.getNumber());
} else {
System.out.println("No carriage found with passenger count in range (" + minPassengerCount + "-" + maxPassengerCount + ")");
}
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>