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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 86 additions & 4 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,90 @@
public class Main {
import java.util.Scanner;

public class Main {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
System.out.println("Делитель счёта v.7. Привет!");
Calculate cal = new Calculate();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можно сделать метод и переменные Calculate static, чтобы не приходилось создавать экземпляр класса и можно было просто вызывать методы класса напрямую

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Не получилось. Не совсем понял.

cal.inputPersons();
cal.inputItems();
cal.payPeople();
}
static class Calculate{
double price=0;
double total=0;
int persons=0;
String items= "";
int payPerPerson;

String endRuble;


public void inputPersons(){
while(true){
System.out.println("Введите количество человек:");
if (sc.hasNextInt()) {
persons = sc.nextInt();
}if (persons>=2){
break;
}else{
System.out.println("Некорректный ввод. Попробуйте ещё раз.");
sc.nextLine();
}
}
}


public void inputItems(){
while(true){
System.out.println("Введите название товара:");
String item=sc.next()+sc.nextLine();
System.out.println("Введите цену товара:");


if (sc.hasNextDouble()) {
price = sc.nextDouble();
}if (price>2){
total+=price;
items+=item+"\n";
}else{
System.out.println("Неверный формат ввода!");
sc.nextLine();
}
System.out.println("Товар " + item + " добавлен. Добавим ещё? Да/Нет?");
String getMore=sc.next();
if (getMore.equalsIgnoreCase("нет")) {
break;
}else{
}
}
}

public void payPeople(){

System.out.println("Добавленные товары: \n"+items);
payPerPerson=(int)total/persons;
double pppWithoutRemains=(double) payPerPerson; // по другому я просто не знаю как игнорировать копейки при объявлении окончания рубля, поэтому пришлось убрать их вовсе,
endRubles();//другого способа как их заигнорить я не нашёл, так что не судите строго.
System.out.println("Каждый из " + persons + " человек заплатит по " + String.format("%.2f",pppWithoutRemains)+ " " +endRuble+ ".");

}

public void endRubles() {


if (payPerPerson % 10 == 1) {
endRuble = "рубль";
} else if (payPerPerson % 10 == 2 || payPerPerson % 10 == 3 || payPerPerson % 10 == 4) { // копейки всё портят.
endRuble = "рубля";
} else {
endRuble = "рублей";

}

}


}

}