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
59 changes: 59 additions & 0 deletions src/main/java/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import static java.lang.Float.parseFloat;

import java.util.Scanner;
public class Check {
Input input = new Input();
StringBuilder check = new StringBuilder();
Scanner scanner = new Scanner(System.in);
Format format = new Format();

public float getItem() {
boolean point = true;
float sum = 0.00f;
String item;
String price;
System.out.println("Если хотите завершить, веведите 'Завершить'.");
while (point){// Цикл на ввод товара и подсчет суммы чека
System.out.println("Введите наименование товара");
item = scanner.nextLine();
if (input.StopOrNot(item))//проверка на завершение ввода
break;
System.out.println("Введите цену товара");
price = scanner.nextLine();

if (getPrice(price) == 0.0f)//проверка на правильность ввода цены
System.out.println("Введите товар, стоимость которого больше нуля");
else {
sum += getPrice(price);//сумма чека

check.append("\n" + item + " " + getPrice(price) +" "+ format.ruble(getPrice(price)));
}
}
System.out.println("Добавленные товары:" + check);
return sum;
}
public float getPrice(String item) {
String strPrice;

if (item.replaceAll("[^-]","").equals("-")) {
return 0.0f;//отрицательная стоимость
}

if (!(item.replaceAll("[^a-zA-Zа-яА-Я]", "").equals(""))) {
return 0.0f; //текст в цене
}

strPrice = item.replaceAll(",",".");//Замена ',' на '.';

strPrice = strPrice.replaceAll("[^0-9?!\\.]","");//Выбор только стоимость

if (strPrice.equals("")) return 0.0f;// нет цены
else{
float price = parseFloat(strPrice);//перевод строки в число
if (price > 0.009f)
return price;
else return 0.0f;
}
}

}
26 changes: 26 additions & 0 deletions src/main/java/Format.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import static java.lang.Math.floor;

public class Format {
public float money(int people, float sum){
float result = sum / Float.parseFloat(String.valueOf(people));
return result;
}
public String ruble(float floatPrice){

int price = (int) (floatPrice * 100);
price = price /100 % 100;
if (price > 20)
price %= 10;
else if (price > 5) return "рублей";
else price %= 10;

if (price == 1)return "рубль";
else if (price < 5 && price > 0) return "рубля";
else return "рублей";
}
public String endString(float cash) {
String endLine = String.format("Каждый должен оплатить %.2f %s", cash, ruble(cash));
return endLine;
}

}
24 changes: 24 additions & 0 deletions src/main/java/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Scanner;

public class Input {
Scanner scanner = new Scanner(System.in);
public int getIntInput(String message){
Scanner scanner = new Scanner(System.in);
System.out.println(message);
int people = testPeople(scanner.nextLine());
return people;
}


public int testPeople(String str) {
if (str != null && str.matches("[-+]?\\d*\\d+") &&(Integer.parseInt(str) > 1))
return Integer.parseInt(str);
else return getIntInput("Введите целое число большее единицы:");
}

public boolean StopOrNot(String message) {
if (message.equalsIgnoreCase("Завершить"))
return true;
return false;
}
}
13 changes: 12 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@

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


Input input = new Input();
Check check = new Check();
Format format = new Format();

int people = input.getIntInput("На сколько человек нужно разделить счет?");
float sum = check.getItem();
float resultPrice = format.money(people, sum);
String resultLine = format.endString(resultPrice);
System.out.println(resultLine);
}

}