Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Homework 1 #1

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

Open
h0mepunk wants to merge 4 commits into master
base: master
Choose a base branch
Loading
from homework-1
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
26 changes: 26 additions & 0 deletions src/main/java/Car.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Car {
private Integer speed;
private String name;

public Car(Integer speed, String name) {
this.setSpeed(speed);
this.setName(name);
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSpeed() {
return speed;
}

public void setSpeed(Integer speed) {
this.speed = speed;
}
}
5 changes: 2 additions & 3 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
public class Main {
static Race race = new Race();

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
race.contestStart();
}
}
117 changes: 117 additions & 0 deletions src/main/java/Race.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;

public class Race {
Car winner;

Scanner scanner = new Scanner(System.in);

ArrayList<Car> cars = new ArrayList<>();

public void calculateWinner(ArrayList<Car> cars) {
winner = cars.get(0);
for(Car car: cars) {
if (car.getSpeed() >= winner.getSpeed()) {
winner = car;
}
}
}

public void contestStart() {

System.out.println("Введите команду ");

while (true) {
printMenu();
int command = scanner.nextInt();
if (command == 1) {
carInput();
} else if (command == 2) {
calculateWinner(cars);
System.out.println("Побеждает тачка:" + winner.getName() + " со скоростью " + winner.getSpeed());
} else if (command == 0) {
System.out.println("Выход");
break;
} else {
System.out.println("Извините, такой команды пока нет");
}
}
}

public void printMenu() {
System.out.println("Что вы хотите сделать? ");
System.out.println("1 - Добавить тачку");
System.out.println("2 - Узнать победителя гонки");
System.out.println("0 - Выход");
}

public void carInput() {
while (true) {
Integer speed = inputSpeed();
String name = inputName();
Car car = new Car(speed, name);
cars.add(car);
System.out.println("Тачка успешно создана!");
break;
}
}

private String inputName() {
System.out.println("Введите название тачилы");
return scanner.nextLine();
}

private Integer inputSpeed() {
Integer speed;
while(true) {
speed = scanSpeed();
if (checkSpeed(speed)) {
break;
} else {
speed = inputSpeed();
break;
}
}
return speed;
}

private Boolean checkSpeed(Integer speed) {
Boolean isCorrect;
if (speed <= 0) {
System.out.println(
"Вы ввели отрицательную скорость.Введите скорость > 0 но =< 250"
);
isCorrect = false;
} else if (speed > 250) {
System.out.println(
"Вы ввели слишком большую скорость. Введите скорость > 0 но =< 250"
);
isCorrect = false;
} else {
isCorrect = true;
}
return isCorrect;
}

private Integer scanSpeed() {
Integer speed;
System.out.println("Введите скорость > 0 но =< 250");
while (true) {
try {
speed = scanner.nextInt();
scanner.nextLine(); // очищаем буфер
if (speed > 0 && speed <= 250) {
break; // выход из цикла при корректном вводе
} else {
System.out.println("Введите скорость в пределах > 0 но =< 250");
}
} catch (InputMismatchException e) {
System.out.println("Вы ввели неверные данные. Введите скорость > 0 но =< 250 как целое число");
scanner.nextLine(); // очищаем буфер
}
}
return speed;
}
}

AltStyle によって変換されたページ (->オリジナル) /