forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Первая попытка сдачи задания на проверку #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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
src/main/java/AddFormat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| public class AddFormat { | ||
| public static String format(int num, String s1, String s2, String s3, boolean print_num){ | ||
|
|
||
| int count = num%100; | ||
|
|
||
| String result; | ||
|
|
||
| if (count >= 5 && count <= 20) { | ||
| result = s3; | ||
| } else { | ||
| count = count % 10; | ||
| if (count == 1) { | ||
| result = s1; | ||
| } else if (count >= 2 && count <= 4) { | ||
| result = s2; | ||
| } else { | ||
| result = s3; | ||
| } | ||
| } | ||
|
|
||
| if(print_num){ | ||
| result = String.format("%d %s", num, result); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public static String rub(double price){ | ||
|
|
||
| int num = (int)Math.floor(price); | ||
|
|
||
| String result = format(num, "рубль", "рубля", "рублей", false); | ||
|
|
||
| return String.format("%.2f %s", price, result); | ||
| } | ||
|
|
||
| public static String rub(float price){ | ||
| return rub((double)price); | ||
| } | ||
| } |
57 changes: 56 additions & 1 deletion
src/main/java/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,61 @@ | ||
|
|
||
| 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.US); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
|
|
||
| int persons; | ||
|
|
||
| while (true){ | ||
| try{ | ||
| persons = scanner.nextInt(); | ||
|
|
||
| if(!scanner.nextLine().trim().equals("")){ | ||
| //100 раз! Уже 100 раз я повторял, что хочу поделить счет на 5 человек | ||
| System.out.println("Введите просто число без дополнительных знаков:"); | ||
| continue; | ||
| } | ||
|
|
||
| if(persons>1){ | ||
| break; | ||
| } | ||
|
|
||
| System.out.println("Введите число человек больше 1:"); | ||
|
|
||
| }catch (Exception e) { | ||
| //System.out.println(e); | ||
| //scanner = new Scanner(System.in); | ||
| if(scanner.hasNextLine()) scanner.nextLine(); | ||
| System.out.println("Введите число, а не строку:"); | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Будем делить счет на "+AddFormat.format(persons, "человека", "человека","человек", true)); | ||
|
|
||
| MyCalc calc = new MyCalc(persons); | ||
|
|
||
| System.out.println("Теперь давайте добавим товары в счет"); | ||
|
|
||
| while (true){ | ||
|
|
||
| calc.addProduct(); | ||
|
|
||
| System.out.println("Хотите добавить ещё один товар? [да/завершить]"); | ||
|
|
||
| String str = scanner.nextLine(); | ||
|
|
||
| if(str.trim().equalsIgnoreCase("завершить")){ | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| calc.calculateAndPrint(); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
src/main/java/MyCalc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| public class MyCalc { | ||
| private int persons; | ||
| public String products = ""; | ||
| public double total_price = 0; | ||
|
|
||
| private int total = 0; | ||
| MyCalc(int persons){ | ||
| this.persons = persons; | ||
| } | ||
|
|
||
| public void addProduct(){ | ||
|
|
||
| ProductItem item = new ProductItem(); | ||
|
|
||
| item.askProductData(); | ||
|
|
||
| System.out.println("Добавлен товар " + item.name + " стоимостью "+item.getPrice()); | ||
|
|
||
| products += item.getNameAndPrice() + "\n"; | ||
|
|
||
| this.total_price += item.price; | ||
| this.total++; | ||
| } | ||
|
|
||
| public void calculateAndPrint(){ | ||
| System.out.println("\n"); | ||
| System.out.println("Заказанные товары:"); | ||
| System.out.println(products.trim()); | ||
|
|
||
| //продавец и копейки не простит, так что если поровну не поделится будут чаевые | ||
| double price_per_person = Math.ceil(100*total_price/persons)/100; | ||
|
|
||
| System.out.println("Итого "+AddFormat.format(total, "товар", "товара","товаров", true)+" на сумму "+AddFormat.rub(total_price)); | ||
| System.out.println("Делим счет на "+ AddFormat.format(persons, "человека", "человек","человек", true)); | ||
| System.out.println("Каждый должен заплатить по "+ AddFormat.rub(price_per_person)); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
src/main/java/ProductItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class ProductItem { | ||
|
|
||
| public String name; | ||
| public double price; | ||
|
|
||
| public void askProductData(){ | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| while (true){ | ||
| System.out.println("Введите название товара:"); | ||
|
|
||
| name = scanner.nextLine(); | ||
|
|
||
| if(!name.trim().equals("")){ | ||
| break; | ||
| } | ||
|
|
||
| System.out.println("Ошибка! Название не должно быть пустой строкой"); | ||
| } | ||
|
|
||
| while(true) { | ||
|
|
||
| System.out.println("Введите стоимость товара:"); | ||
|
|
||
| try { | ||
|
|
||
| price = scanner.nextDouble(); | ||
|
|
||
| if(!scanner.nextLine().trim().equals("")){ | ||
| System.out.println("Введите только число в формате 0.00 без дополнительных знаков"); | ||
| continue; | ||
| } | ||
|
|
||
| if (price > 0) { | ||
| break; | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
|
|
||
| if(scanner.hasNextLine()) scanner.nextLine(); | ||
| //scanner = new Scanner(System.in); | ||
|
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. закомментированный код нужно удалять перед созданием РR, или подписывать для каких целей его оставил(но это очень редко делается). Это мусор лучше код от него очищать) |
||
| } | ||
|
|
||
| System.out.println("Введите число в формате 0.00"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public String getNameAndPrice(){ | ||
| return name + " " + AddFormat.rub(price); | ||
| } | ||
|
|
||
| public String getPrice(){ | ||
| return AddFormat.rub(price); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.