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
106 changes: 103 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,106 @@
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);
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) {
String prodName = "";
if (myBusket.productList.size() == 0){

System.out.println("Введите название блюда");
prodName = scanner.next();
}
else {
System.out.println("Введите название блюда, если хотите добавить товар или 'завершить', если товаров больше нет.");

prodName = scanner.next();
if (prodName.equalsIgnoreCase("Завершить")) {
break;
}
}

// System.out.println("Введите название блюда.");
// String prodName = scanner.next();
// if (prodName.equalsIgnoreCase("Завершить")) {
// break;
// }
if (prodName.matches("[а-яА-Я]+")) {
while (true) {
try {
System.out.println("Введите стоимость блюда в формате рубли.копейки.");
Double prodPrice = scanner.nextDouble();
Product a = new Product(prodName, prodPrice);
myBusket.addToBusket(a);
System.out.println("Блюдо успешно добавлено в корзину!");

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) + ".");
}
}




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;
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();

}
}
}