Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Дз #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Trofiproll wants to merge 7 commits into main
base: main
Choose a base branch
Loading
from dev
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
73 changes: 73 additions & 0 deletions src/main/java/Calculator.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {
private int guestsNumber;
private double totalSum = 0;
private ArrayList<String> items;
private ArrayList<Double> prices;



Calculator(int guestsNumber){
this.guestsNumber = guestsNumber;
items = new ArrayList<>();
prices = new ArrayList<>();
String persons;
if(guestsNumber >= 11 && guestsNumber <= 14) persons = "человек";
else persons = switch (guestsNumber%10){
case 1,2,3,4 -> "человека";
default -> "человек";
};
System.out.printf("Счёт на %d %s создан!\n" +
"Добавляйте товары в формате \"<Товар> <стоимость>\".\n" +
"Название товара может состоять из нескольких слов.\n" +
"Для завершения добавления введите \"Завершить\".\n\n", guestsNumber, persons);
}

void addProducts(){
Scanner in = new Scanner(System.in);
String inputItem = "";
double inputPrice;
while(true){
while(in.hasNext() && !in.hasNextDouble()){
if(inputItem.isEmpty()) inputItem += in.next();
else inputItem += ' ' + in.next();
if(inputItem.equalsIgnoreCase("завершить")){
System.out.println("Добавление товаров завершено");
return;
}
}
if(in.hasNextDouble()){
if(!inputItem.isEmpty()){
inputPrice = in.nextDouble();
items.add(inputItem);
prices.add(inputPrice);
totalSum += inputPrice;
System.out.printf("Товар '%s' по цене %.2f успешно добавлен\n", inputItem, inputPrice);
inputItem = "";
}
else in.next();
}
}
}

void calcutate(){
System.out.println("\nДобавленные товары:");
for(int i = 0; i < items.size(); i++){
System.out.printf("%s %.2f\n", items.get(i), prices.get(i));
}
System.out.printf("\nВсего на %.2f %s.\n", totalSum, rouble(totalSum));
double priceForEach = totalSum / guestsNumber;
System.out.printf("\nС каждого %.2f %s.\n", priceForEach, rouble(priceForEach));
}

private String rouble(double price){
if(price > 10 && price < 15) return "рублей";
else return switch ((int)price % 10){
case 1 -> "рубль";
case 2, 3, 4 -> "рубля";
default -> "рублей";
};
}
}
25 changes: 24 additions & 1 deletion src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import java.util.Locale;
import java.util.Scanner;

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

Calculator bill = new Calculator(parseGuestNumber());
bill.addProducts();
bill.calcutate();
}

private static int parseGuestNumber(){
Scanner in = new Scanner(System.in);
System.out.println("На сколько человек нужно разделить счёт?");
int guestsNumber;
while(true) {
if (in.hasNextInt()) {
guestsNumber = in.nextInt();
if(guestsNumber > 1) break;
else System.out.println("Введено число меньше 2. Повторите попытку");
} else if (in.hasNext()) {
System.out.println("Некорректный ввод. Повторите попытку");
in.next();
}
}
return guestsNumber;
}
}

AltStyle によって変換されたページ (->オリジナル) /