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

homework #1 #296

Open
wants to merge 1 commit into
base: dev
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
21 changes: 21 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Car {
private String name;
private int speed;

public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}

public String carName() {
return name;
}

public int carSpeed() {
return speed;
}

public int distance() {
return 24 * speed;
}
}
31 changes: 30 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
Race race = new Race();

for (int i = 1; i <= 3; i++) {
System.out.print("Введите название машины #" + i + ": ");
String name = scanner.nextLine();

int speed = 0;
while (true) {
try {
System.out.print("Введите скорость машины #" + i + ": ");
speed = Integer.parseInt(scanner.nextLine());

if (speed > 0 && speed <= 250) {
break;
} else {
System.out.println("Неправильная скорость");
}
} catch (NumberFormatException e) {
System.out.println("Неправильная скорость");
}
}
Car car = new Car(name, speed);
race.addCar(car);
}
String leader = race.whoLeader();
System.out.println("Самая быстрая машина: " + leader);

scanner.close();
}
}
26 changes: 26 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.ArrayList;
import java.util.List;

public class Race {
private List<Car> cars;

public Race() {
this.cars = new ArrayList<>();
}

public void addCar(Car car) {
cars.add(car);
}

public String whoLeader() {

Car leader = cars.get(0);
for (Car car : cars) {
if (car.distance() > leader.distance()) {
leader = car;
}
}

return leader.carName();
}
}