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

public class Calculator {
double sum = 0;
String name = "";
int quantity;
Scanner scanner = new Scanner(System.in);
public int quantityOfGuest(){
System.out.println("Введите количество людей для разделения счета: ");
while (true) {
if (scanner.hasNextInt()) {
quantity = scanner.nextInt();
if (quantity<=1){
System.out.println("Введено отрицательное значение или 1.Повторите ввод.");
}else {
return quantity;
}
} else {
System.out.println("Введено не корректное значение.Повторите ввод.");
}
scanner.nextLine();
}
}
public void cheque(String name, double cost) {
if (cost < 1) {
System.out.println("Товар не добавлен.\nТовар не может быть бесплатным.Повторите ввод.");
} else {
sum += cost;
this.name = this.name + name + "\n";
System.out.println("Товар успешно добавлен.");
}
}
public void finalAccount(int quantity){
System.out.println("Добавленные товары: \n" + name );
sum = sum / quantity;
System.out.println("Каждый человек должен : \n" + String.format("%.2f",sum) + price());
}
public String price(){
if ((int)(sum % 100 / 10) == 1){
return " рублей.";
}
if ((int) (sum % 10) == 1){
return " рубль.";
} else if ((int) (sum % 10) >= 2 && (int) (sum % 10) <= 4){
return " рубля.";
} else {
return " рублей.";
}
}
}
28 changes: 26 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
Calculator calculator = new Calculator();
int quantity ;
quantity = calculator.quantityOfGuest();

while (true) {
System.out.println("Введите название товара.");
String name = scanner.nextLine();
System.out.println("Введите цену товара в формате: \"рубли,копейки\" [10,45]");
if (scanner.hasNextDouble()) {
double cost = scanner.nextDouble();
calculator.cheque(name, cost);
System.out.println("Хотите добавить еще товар? \n" +
"(Если да,поставте любой символ ,если нет,напишите \"Завершить\". )");
} else {
System.out.println("Некорректная цена.Повторите ввод.");
}
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")) {
calculator.finalAccount(quantity);
break;
}
scanner.nextLine();
}
}
}