Skip to content
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
57 changes: 57 additions & 0 deletions src/main/java/Actions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Scanner;

public class Actions {
public static double GetProductPrice(Scanner scan) {
while (true) {
System.out.println("Введите цену товара!");
if (scan.hasNextDouble()) {
double price = scan.nextDouble();
if(price <= 0)
{
System.out.println("Ошибка! Ведите цену больше 0!");
}
else
{
return price;
}
} else {
scan.nextLine();
System.out.println("Ошибка! Введите дробное число!");
}
}
}

public static int GetNumber(Scanner scan) {
while (true) {
if (scan.hasNextInt()) {
return scan.nextInt();
} else {
scan.nextLine();
System.out.println("Ошибка! Введите число!");
}
}
}

public static String GetProducts(String name, String allNames) {
allNames = allNames + name + ".\n";
return allNames;
}

public static double GetPrices(double price, double allPrice) {
allPrice += price;
return allPrice;
}

public static void ShowResult(double price) {
String result = "Цена для каждого: \"%.2f %s\"";
if (Math.floor(price) > 11 && Math.floor(price) < 15) {
System.out.println(String.format(result, price, "рублей"));
} else if (Math.floor(price) % 10 == 1) {
System.out.println(String.format(result, price, "рубль"));
} else if (Math.floor(price) % 10 > 1 && Math.floor(price) % 10 < 5) {
System.out.println(String.format(result, price, "рубля"));
} else {
System.out.println(String.format(result, price, "рублей"));
}
}
}
37 changes: 35 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
//Уважаемый ревьюрер, я забыл при прошлом отправлении сделать коммит, прошу прощения.
while (true) {
System.out.println("На сколько человек хотите разделить покупку?");
Scanner scan = new Scanner(System.in);

int n = Actions.GetNumber(scan);

if (n == 1) {
System.out.println("Ощибка! Введите значение >1!");
} else if (n < 1) {
System.out.println("Ощибка! Некорректное число! Введите значение >1!");
} else {
double prices = 0.0;
String products = "";

while (true) {
System.out.println("Введите название товара или \"Завершить!\"");
scan.nextLine();
String name = scan.nextLine();
if (name.equalsIgnoreCase("Завершить")) {
System.out.println("Ваши товары: \n" + products);
Actions.ShowResult(prices / n);
scan.close();
break;
} else {
double price = Actions.GetProductPrice(scan);
prices = Actions.GetPrices(price, prices);
products = Actions.GetProducts(name, products);
}
}
break;
}
}
}
}
}