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
15 changes: 15 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Calculator {
String name;
double cost;
Calculator(String productName, double productCost){
name = productName;
cost = productCost;
}
void fullProductName(String productName){
name = name + "\n" + productName;
}

void fullProductCost(double productCost){
cost += productCost;
}
}
27 changes: 27 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class Formatter {
int format;
void format(double formCost){

format = (int) formCost;
if (format > 100){
format = format % 100;
}
if (format > 20) {
format = format % 10;
}
switch (format){
case 1:
System.out.print("рубль.");
break;
case 2:
case 3:
case 4:
System.out.print("рубля.");
break;
default:
System.out.print("рублей.");
break;
}

}
}
75 changes: 73 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,79 @@
// dev branch for Y.Practicum
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
Scanner scanner = new Scanner(System.in);
int countPeople = 0;
String name;
double cost = 0;
double finalCost;

while (countPeople < 2) {
System.out.println("На скольких людей разделить счет?");
if (scanner.hasNextInt()) {
countPeople = scanner.nextInt();
}else {
System.out.println("Ошибка! Введено некорректное количество людей");
scanner.next();
}
}

System.out.println("Укажите имя товара");
name = scanner.next();
System.out.println("Укажите цену товара");
while (true){
if (scanner.hasNextDouble()){
cost = scanner.nextDouble();
if (cost > 0){
break;
} else {
System.out.println("Ведено некоректное значение, введите стоимость заново.");
}
}else {
System.out.println("Ведено некоректное значение, введите стоимость заново.");
scanner.next();
}
}
Calculator product = new Calculator(name, cost);
System.out.println("Товар успешно добавлен!");
while (true){
System.out.println("Хотите ли вы добавить ещё один товар?");
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")){
break;
}else {
System.out.println("Укажите имя товара");
name = scanner.next();
product.fullProductName((name));
System.out.println("Укажите цену товара");
while (true){
if (scanner.hasNextDouble()){
cost = scanner.nextDouble();
if (cost > 0){
break;
} else {
System.out.println("Ведено некоректное значение, введите стоимость заново.");
}
}else {
System.out.println("Ведено некоректное значение, введите стоимость заново.");
scanner.next();
}
}
product.fullProductCost(cost);
System.out.println("Товар успешно добавлен!");
}

}
System.out.println("Добавленные товары:\n" + product.name);
String format ="Цена на человека %.2f ";
finalCost = product.cost/countPeople;
System.out.print(String.format(format, finalCost));
Formatter formatCost = new Formatter();
formatCost.format(finalCost);

// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");

}
}