Skip to content
Merged
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
93 changes: 90 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,93 @@
import java.util.Scanner;
import mypackage.Product;
import mypackage.ProductBusket;




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

public static String rubleDeclination(double numberExample) {
int rouble = (int) Math.floor(numberExample);

if (rouble >= 5 && rouble <= 20)
{
return "рублей";
}
switch (rouble % 10)
{
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}
public static void main(String[] args) {
System.out.println(Main.class.getPackage().getName());
int numberOfGuests = 0;

Scanner scanner = new Scanner(System.in);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Рекомендация: хорошей практикой является сделать scanner.close() после того, как он больше не используется

while (true) {

try {
System.out.println("Введите количество гостей.");
numberOfGuests = scanner.nextInt();
if (numberOfGuests < 1)
System.out.println("Ошибка. Введите корректное количество гостей заново.");
else
break;
} catch (Exception e) {
String wrongInput = scanner.next();
System.out.println("Ошибка. Введите корректное количество гостей заново.");

}
}

ProductBusket myBusket = new ProductBusket();

while (true) {

System.out.println("Введите название блюда.");
String prodName = scanner.next();
if (prodName.equalsIgnoreCase("Завершить"))
{
break;
}
if (prodName.matches("[а-яА-Я]+")) {

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(true) {
try {
System.out.println("Введите стоимость блюда в формате рубли.копейки.");
Double prodPrice = scanner.nextDouble();
Product a = new Product(prodName, prodPrice);
myBusket.addToBusket(a);



break;


} catch (Exception e) {
String WrongName = scanner.next();
System.out.println("Ошибка. Неверно введена стоимость блюда. Введите снова!");
continue;
}
}
}
else {
System.out.println("Неккоректно введено название блюда введите снова.");
}
}
myBusket.displayProducts();
double result = ProductBusket.finalPrice / numberOfGuests;
double finalResult = Math.round(result * 100.0) / 100.0;
System.out.println( "Каждый гость заплатит: " + finalResult + " " + rubleDeclination(finalResult) + ".");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Рекомендация: вывод лучше делать через аргументы, а не через конкатенацию (оператор +)

}
}




18 changes: 18 additions & 0 deletions src/main/java/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mypackage;
public class Product {

// Fields (instance variables)
public String name;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Рекомендация: если не планируется менять значение полей извне, то поля лучше сделать приватными

public double price;

// Constructor
public Product(String name, double price) {
this.name = name;
this.price = price;
}

// Methods
public void displayInfo() {
System.out.println(name + " " + price);
}
}
30 changes: 30 additions & 0 deletions src/main/java/ProductBusket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mypackage;
import mypackage.Product;
import java.util.ArrayList;
import java.util.List;


public class ProductBusket {

public static double finalPrice;
public List<Product> productList;

public ProductBusket() {
this.finalPrice = 0;
this.productList = new ArrayList<>();
}
public void addToBusket(Product productExample){
productList.add(productExample);
finalPrice = finalPrice + productExample.price;

}

public void displayProducts() {
String message = "Добавленные товары: ";
System.out.println(message);
for (Product element : this.productList) {
element.displayInfo();

}
}
}