Repository navigation
Первая сдача работы #1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| 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("[а-яА-Я]+")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) + "."); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Рекомендация: вывод лучше делать через аргументы, а не через конкатенацию (оператор +) |
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| 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(); | ||
|
|
||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Рекомендация: хорошей практикой является сделать scanner.close() после того, как он больше не используется