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

1 #279

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

1 #279

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
17 changes: 17 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Car {
private final String name;
private final int speed;

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

public String gName(){
return name;
}

public int gSpeed(){
return speed;
}
}
39 changes: 34 additions & 5 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@

import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Добро пожаловать в самую реалистичную гонку во вселенной");

List<Car> cars = new ArrayList<>();
for (int i = 0; i < 3; i++){
System.out.println("Укажи название автомобиля " + (i+1) + " ");
String name = scanner.nextLine().trim();

System.out.println("Укажи скорость автомобиля " + (i+1) + " " );
int speed;
do {
try {
speed = Integer.parseInt(scanner.nextLine().trim());
if (speed >= 0 && speed <= 250) {
break;
} else {
System.out.println("Неправильно указал скорость. Введи число от 1 до 250");
}
} catch (NumberFormatException e) {
System.out.println("Неправильно написал. Нужно указать число от 1 до 250");
}
} while (true);

cars.add(new Car(name, speed));
}
Race race = new Race(cars);
race.printLeadInfo();
}

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

public class Race {
private List<Car> cars;

public Race(List<Car> cars) {
this.cars = cars;
}

public Car fWin() {
double mDistant = 0;
Car win = null;

for (Car car : cars) {
double distan = calcDistanc(car);
if (distan > mDistant){
mDistant = distan;
win = car;
}
}

return win;
}

private double calcDistanc(Car car) {
return car.gSpeed() * 24;
}
public void printLeadInfo() {
Car win = fWin();
System.out.printf("Самая быстрая машина: %s, прошла %,.2f км за 24 часа.\n", win.gName(), calcDistanc(win));
}
}