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

public class Calculator {
double totalPrice=0;
String totalProduct = "" ;

public void priceEntry() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏫ Так как функция priceEntry используется только внутри класса Calculator, мы можем сделать ее private

System.out.println("Введите стоимость товара в формате \"xx,xx\" цифрами");

Scanner scanner = new Scanner(System.in);
while (true) {
if (scanner.hasNextDouble()) {
double inputPrice = scanner.nextDouble();
if (inputPrice<=0){
System.out.println("вы ввели некорректные данные");
scanner.nextLine();

} else{
System.out.println("Стоимость- "+ inputPrice);
totalPrice = totalPrice + inputPrice;
break;
}
} else {
System.out.println("Похоже вы ввели буквы или \".\",введите стоимость товара в формате \"xx,xx\" цифрами ");
scanner.nextLine();
}
}
}
public void productEntry(){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название продукта");
String nameOfProduct = scanner.nextLine();

while (true){
if(nameOfProduct.equalsIgnoreCase("завершить")){
System.out.println("операция завершена");
break;
}
else if(nameOfProduct.isEmpty()) {
System.out.println("вы ввели некоректные данные, попробуйте еще раз");
nameOfProduct = scanner.nextLine();
}
else {
System.out.println("вы выбрали "+ nameOfProduct);
totalProduct = totalProduct +"\n"+ nameOfProduct;
priceEntry();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏫ Так как такая конкатенция строк будет создавать новый объект String каждый раз, можно воспользоваться более оптимизированным способом формирования строки - с помощью StringBuilder, и добавлять строку с помощью метода append

System.out.println("Хотите добавить что-то еще? Если нет, введите \"завершить\" ?");
nameOfProduct = scanner.nextLine();
}
}
}

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

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Person person = new Person();
Calculator calculator = new Calculator();
person.numberOfPerson();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏫ Обычно функции, которые выполняют какое-либо действие принято называть глаголами. В твоем случае было бы удобнее назвать:

  • enterNumberOfPersons()
  • enterProducts()

calculator.productEntry();
System.out.println(String.format( "Добавленные товары: %s \nОбщая стоимость- %.2f",calculator.totalProduct,calculator.totalPrice));// System.out.println("Добавленные товары: "+ calculator.totalProduct + "\n Общая стоимость - "+ calculator.totalPrice);

double checkForEveryOne = calculator.totalPrice / person.valueOfPeople;
int sumCheck = (int) Math.floor(checkForEveryOne);
String rubles = "";
switch (sumCheck % 10){
case 0:
rubles = "рублей";
break;
case 1:
rubles = "рубль";
break;

case 2:
rubles = "рубля";
break;
case 3:
rubles = "рубля";
break;
case 4:
rubles = "рубля";
break;
case 5:
rubles = "рублей";
break;
case 6:
rubles = "рублей";
break;
case 7:
rubles = "рублей";
break;
case 8:
rubles = "рублей";
break;
case 9:
rubles = "рублей";
break;
}
switch (sumCheck % 100){
case 11:
rubles ="рублей";
case 12:
rubles ="рублей";
case 13:
rubles ="рублей";
case 14:
rubles ="рублей";
}

System.out.println("Каждый платит - " + String.format("%.2f %s",checkForEveryOne, rubles));


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

public class Person {
int valueOfPeople ;


public void numberOfPerson() {
Scanner scanner1 = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счёт?");
// количество человек

while (true) {

if (scanner1.hasNextInt()) {
valueOfPeople = scanner1.nextInt();

if (valueOfPeople == 1) {
System.out.println("Деление не требуется,за счёт платит " + valueOfPeople + " человек!");
break;

} else if (valueOfPeople <= 0 ) {
System.out.println("Введены некорректные данные, попробуйте еще раз");
scanner1.nextLine();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏫ Эти два условия можно было бы объединить под

if (valueOfPeople <= 0 || valueOfPeople > 20000) {...}


}else if (valueOfPeople > 20000 ) {
System.out.println("Введены некорректные данные,попробуйте еще раз");
scanner1.nextLine();

}
else {
System.out.println("Счёт будет поделен на " + valueOfPeople );
break;
}
}
else {
System.out.println("Некорректные данные, попробуйте еще раз");
scanner1.nextLine();

}
}
}
}