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
8 changes: 8 additions & 0 deletions src/main/java/Goods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Goods {
String name;
double price;
Goods (String name, double price) {
this.name = name;
this.price = price;
}
}
78 changes: 75 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,80 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
int count=0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

По умолчанию int инициализируется 0, так что здесь инициализировать не обязательно

Goods purchases = new Goods ("",0.0);
System.out.println ("For how many people do you need to split the bill?");
count = countOfPeople();
System.out.println (count);
Scanner scanner = new Scanner(System.in);
boolean trigger = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Это инициализированное значение не используется (в while будет проверяться то, что будет получено от !chek.equalsIgnoreCase("complete");), так что можно не инициализировать


do {
Goods product = new Goods("", 0.0);
System.out.println("Enter good`s name");
product.name = scanner.nextLine();
System.out.println("Enter good`s price in format ruble.penny. For example: 0.00");
while (true) {
if (scanner.hasNextDouble()) {
product.price = scanner.nextDouble();
if (product.price > 0) {
break;
} else {
System.out.println("Wrong format, pleas try again");
scanner.nextLine();
}
} else {
System.out.println("Wrong format, pleas try again");
scanner.nextLine();
}
}
purchases = Summoning.Calculate(product, purchases);
System.out.println("Do you want to add next product? If no, write \"complete\", else any phrase.");
scanner.nextLine();
String chek = scanner.nextLine();
trigger = !chek.equalsIgnoreCase("complete");

} while (trigger);

System.out.println("Purchased products:\n"+purchases.name);
double bill = purchases.price/count;
String finalPhrase1 = "%.2f рубль";
String finalPhrase2 = "%.2f рубля";
String finalPhrase3 = "%.2f рублей";
int ostatok = (int) (Math.floor(bill)%10.0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Хорошо, что используешь переменные для подсчета только 1 раз
Вместо 10.0 можно использовать просто 10

if ((bill <=19 && bill>=11) || (ostatok>=5 && ostatok<=9)|| ostatok==0){
System.out.println(String.format(finalPhrase3, bill));
} else{
if (ostatok == 1) {
System.out.println(String.format(finalPhrase1, bill));
} else{
System.out.println(String.format(finalPhrase2, bill));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Код System.out.println(String.format(finalPhrase2, bill)); повторяется несколько раз, лучше его вынести за if-else, а в нем определять только в переменной, каким будет слово рубль


}

}


}


public static int countOfPeople (){
Scanner scanner = new Scanner(System.in);
while (true){
if (scanner.hasNextInt()) {
int x = scanner.nextInt();
if (x > 1) {
return x;
} else {
System.out.println("Invalid quantity entered, please try again.");
}
} else{
System.out.println("Invalid quantity entered, please try again.");
scanner.nextLine();
}
}
}

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

public class Summoning {
public static Goods Calculate (Goods fullList, Goods product) {
Goods list = fullList;
list.name = list.name +'\n' + product.name;
list.price = list.price+ product.price;
return list;

}
}