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

Dev #2

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
ValentinPside wants to merge 2 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
53 changes: 53 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,53 @@
import java.util.ArrayList;

public class Calculator {
public Calculator(int personCount, ArrayList<Dish> dishesList) {
calculate(personCount, dishesList);
}

private void calculate(int personCount, ArrayList<Dish> dishesList){
showDishList(dishesList);
totalPriceForPerson(personCount, dishesList);
}

private String getRightEnding(double price){
String ending = "";
String x = "рубль";
String y = "рубля";
String z = "рублей";
if(price%100 >= 11 && price%100 <= 14){
ending = z;
return ending;
}
if(price%10 == 1){
ending = x;
return ending;
}
if(price%10 == 2 || price%10 == 3 || price%10 == 4){
ending = y;
return ending;
}
return z;
}

private void showDishList(ArrayList<Dish> dishesList){
System.out.println("Добавленные товары:");
double price;
for(int i = 0; i < dishesList.size(); i++){
price = dishesList.get(i).getPrice();
String ending = getRightEnding(price);
System.out.println("Позиция: " + dishesList.get(i).getName() + " Цена: " + String.format("%.2f", dishesList.get(i).getPrice()) + " " + ending);
}
}

private void totalPriceForPerson(int personCount, ArrayList<Dish> dishesList){
Double totalPrice = 0.0;
for(int i = 0; i < dishesList.size(); i++){
totalPrice = totalPrice + dishesList.get(i).getPrice();
}
totalPrice = totalPrice / personCount;
double price = totalPrice;
String ending = getRightEnding(price);
System.out.println("Итоговая цена для каждой персоны: " + String.format("%.2f", totalPrice) + " " + ending);
}
}
57 changes: 57 additions & 0 deletions src/main/java/Dish.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Scanner;

public class Dish {

private Double price;
private String name;

public Dish() {
setName();
setPrice();
System.out.println("Позиция успешно добавлена!");
}

public Double getPrice() {
return price;
}

public String getName() {
return name;
}

public void setName() {
Scanner scanner = new Scanner(System.in);
Copy link

@gusar-off gusar-off Dec 29, 2022

Choose a reason for hiding this comment

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

Лучшее переиспользовать инстанс Scanner, а не создавать каждый раз новый.

while(true){
System.out.println("Введите название блюда.");
String line = scanner.nextLine();
if(line.trim().isEmpty()){
System.out.println("Ничего не введено. Попробуйте снова");
continue;
}
else{
name = line;
break;
}
}
}

public void setPrice() {
Scanner scanner;
while(true){
System.out.println("Укажите стоимость " + name);
scanner = new Scanner(System.in);;
if(!scanner.hasNextDouble()){
System.out.println("Нужно число без букв. Попробуйте снова");
continue;
}
price = scanner.nextDouble();
if(price <= 0.0){
System.out.println("Цена должна быть положительной. Попробуйте снова");
continue;
}
else{
break;
}
}
}
}
49 changes: 45 additions & 4 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
import java.util.ArrayList;
import java.util.Scanner;

// dev branch for Y.Practicum
public class Main {

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

ArrayList<Dish> dishesList = new ArrayList<>();

int personCount = getPersonCount();

Scanner scanner = new Scanner(System.in);
String line = "";
while(true){
dishesList.add(new Dish());
System.out.println("Для добавления нового товара введите что угодно. Для завершения введите 'завершить'");
line = scanner.nextLine();
if(line.equalsIgnoreCase("завершить")){
System.out.println("Составление списка успешно завершино");
break;
}
}
Calculator calculator = new Calculator(personCount, dishesList);

}
private static int getPersonCount(){
Scanner scanner;
while(true){
System.out.println("На скольких человек необходимо разделить счёт?");
scanner = new Scanner(System.in);
if(!scanner.hasNextInt()){
System.out.println("Нужно целое число без букв. Попробуйте снова");
continue;
}
int personCount = scanner.nextInt();
if(personCount < 2){
System.out.println("Число персон должно быть не меньше двух. Попробуйте снова");
continue;
}
else{
System.out.println("Счёт будет разделён на " + personCount + " персоны");
return personCount;
}
}
}
}
}

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