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
20 changes: 20 additions & 0 deletions src/main/java/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Check {
private String checkResult;
private double totalPrice;
private int personCnt;
Check(){
this.checkResult = "Добавленные товары:\n";
}
public void setPersonCnt(int personCnt){
this.personCnt = personCnt;
}
public void addProduct(String producName, double productPrice) {
this.checkResult = this.checkResult + String.format("%s %s\n", producName, Formater.format(productPrice));
this.totalPrice += productPrice;
System.out.println(String.format("Товар успешно добавлен, всего добавлено товаров на сумму %s.", Formater.format(this.totalPrice)));
}
public void printCheck(){
System.out.println(this.checkResult);
System.out.println(String.format("Cтоимость всех товаров %s, каждый человек должен заплатить %s.", Formater.format(this.totalPrice), Formater.format(this.totalPrice/this.personCnt)));
}
}
24 changes: 24 additions & 0 deletions src/main/java/Formater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Formater {
public static String format(double number){
String result;
int num = (int)Math.floor(number);
num = Math.abs(num);
num = num % 100;
if (num > 19) {
num = num % 10;
}
switch (num){
case 1:
result = "рубль";
break;
case 2:
case 3:
case 4:
result = "рубля";
break;
default:
result = "рублей";
}
return String.format("%.2f %s", number, result);
}
}
61 changes: 61 additions & 0 deletions src/main/java/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.Scanner;

public class Input {

public static void inputPersonCnt(Check check){

System.out.println("На сколько человек разделить счет?");
int cnt;
Scanner scan = new Scanner(System.in);
while (true) {

if (scan.hasNextInt()) {
cnt = scan.nextInt();
if (cnt > 1){
check.setPersonCnt(cnt);
break;
} else {
System.out.println("Введено некоректное значение, повторите ввод:");
scan.nextLine();
}
} else {
System.out.println("Введено некоректное значение, повторите ввод:");
scan.nextLine();
}

}
}

public static void inputCheck(Check check){
Scanner scan = new Scanner(System.in).useDelimiter("\n");

while (true){
double price;
System.out.println("Введите название товара:");
String productName = scan.next();
System.out.println("Введите стоимость товара:");
scan.nextLine();
while(true){
if (scan.hasNextDouble()) {
price = scan.nextDouble();
if (price >= 0){
check.addProduct(productName,price);
break;
} else {
System.out.println("Введено некоректное значение, повторите ввод:");
scan.nextLine();
}
} else {
System.out.println("Введено некоректное значение, повторите ввод:");
scan.nextLine();
}
}
System.out.println("Добавить еще один товар?");
String answer = scan.next();
if(answer.equalsIgnoreCase("завершить")){
break;
}
}
}

}
6 changes: 5 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

Check check = new Check();
Input.inputPersonCnt(check);
Input.inputCheck(check);
check.printCheck();
}
}