Repository navigation
Домашнее задание, спринт 2 #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
Xaxaido
wants to merge
23
commits into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1cac854
Домашнее задание, спринт 2
Xaxaido d9372de
Домашнее задание, спринт 2
Xaxaido a482d5d
Домашнее задание, спринт 2
Xaxaido 2f9cc82
Домашнее задание, спринт 2
Xaxaido bc6b75a
Домашнее задание, спринт 2
Xaxaido bb15c62
Домашнее задание, спринт 2
Xaxaido 541d0ac
Домашнее задание, спринт 2
Xaxaido b3db92c
Домашнее задание, спринт 2
Xaxaido 6c87bb3
Домашнее задание, спринт 2
Xaxaido 5ff59f0
Домашнее задание, спринт 2
Xaxaido 5b76db0
Домашнее задание, спринт 2
Xaxaido 9f14240
Домашнее задание, спринт 2
Xaxaido c4022a4
Домашнее задание, спринт 2
Xaxaido 94dd026
Домашнее задание, спринт 2
Xaxaido a0e688f
Домашнее задание, спринт 2
Xaxaido b254edb
Домашнее задание, спринт 2
Xaxaido 4798156
Домашнее задание, спринт 2
Xaxaido 9da675a
Домашнее задание, спринт 2
Xaxaido 64c6f89
Домашнее задание, спринт 2
Xaxaido c467b2e
Домашнее задание, спринт 2
Xaxaido c48b71b
Домашнее задание, спринт 2
Xaxaido 215640f
Домашнее задание, спринт 2
Xaxaido a666803
Домашнее задание, спринт 2
Xaxaido 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
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,67 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public final class Calc { | ||
|
|
||
| private final int personsCount; | ||
| private double total; | ||
| private final ArrayList<Product> order; | ||
| final static String[] valueToFormat = {"рубль", "рубля", "рублей"}; | ||
| private int maxLength = 0; | ||
|
|
||
| public static Calc getInstance(final MyScanner scanner) { | ||
|
|
||
| Calc calc = null; | ||
|
|
||
| while (calc == null) { | ||
|
|
||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
|
|
||
| int personsCount = scanner.nextInt(); | ||
| if (personsCount >= 2) { | ||
| calc = new Calc(personsCount, new ArrayList<Product>()); | ||
| } else { | ||
| MyScanner.errorMessage(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return calc; | ||
|
|
||
| } | ||
|
|
||
| public void addProduct(final String name, final double price) { | ||
|
|
||
| Product newProduct = new Product(name, price); | ||
| order.add(newProduct); | ||
| total += price; | ||
| maxLength = Math.max(maxLength, name.length()); | ||
|
|
||
| } | ||
|
|
||
| public void printOrder() { | ||
|
|
||
| System.out.println("Добавленные товары:"); | ||
|
|
||
| for (Product product : order) { | ||
| String name = product.getName(); | ||
| double price = product.getPrice(); | ||
| System.out.println(String.format(name + ".".repeat(maxLength - name.length() + 5) + "%.2f" + | ||
| Formatter.formatValue(price, valueToFormat), price)); | ||
| } | ||
|
|
||
| double eachToPay = total / personsCount; | ||
| String total = String.format("Сумма к оплате для каждого человека: %.2f %s", | ||
| eachToPay, Formatter.formatValue(eachToPay, valueToFormat)); | ||
| System.out.println("_".repeat(total.length())); | ||
| System.out.println(total); | ||
|
|
||
| } | ||
|
|
||
| Calc(int personsCount, ArrayList<Product> order) { | ||
|
|
||
| this.personsCount = personsCount; | ||
| this.order = order; | ||
|
|
||
| } | ||
|
|
||
| } |
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,21 @@ | ||
| public final class Formatter { | ||
|
|
||
| public static String formatValue(final double value, final String[] valueNameToFormat) { | ||
|
|
||
| int roundedValue = (int) Math.floor(value); | ||
| int preLastNumber = roundedValue % 100 / 10; | ||
| int lastNumber = roundedValue % 10; | ||
|
|
||
| if (preLastNumber == 1) { | ||
| return " " + valueNameToFormat[2]; | ||
| } | ||
|
|
||
| return switch (lastNumber) { | ||
| case 1 -> " " + valueNameToFormat[0]; | ||
| case 2, 3, 4 -> " " + valueNameToFormat[1]; | ||
| default -> " " + valueNameToFormat[2]; | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| } |
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,50 @@ | ||
|
|
||
| public class Main { | ||
|
|
||
| private final static String STOP_WORD = "Завершить"; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| MyScanner scanner = new MyScanner(System.in); | ||
| String productName; | ||
| double price; | ||
| boolean isStop; | ||
| Calc calc = Calc.getInstance(scanner); | ||
|
|
||
| do { | ||
|
|
||
| System.out.println("Введите название товара"); | ||
| productName = scanner.next(); | ||
|
|
||
| while (true) { | ||
|
|
||
| System.out.println("Введите стоимость товара"); | ||
| price = scanner.nextDouble(); | ||
|
|
||
| if (price < 0) { | ||
| MyScanner.errorMessage(); | ||
| } else { | ||
| isStop = newProductOrStop(scanner, calc, productName, price); | ||
| break; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } while (!isStop); | ||
|
|
||
| calc.printOrder(); | ||
| scanner.close(); | ||
|
|
||
| } | ||
|
|
||
| private static boolean newProductOrStop(final MyScanner scanner, final Calc calc, final String productName, final double price) { | ||
|
|
||
| calc.addProduct(productName, price); | ||
| System.out.println(String.format("Товар '%s' стоимостью %.2f %s успешно добавлен", | ||
| productName, price, Formatter.formatValue(price, Calc.valueToFormat))); | ||
| System.out.println("Хотите добавить ещё товар?\nДля продолжения введите любой символ\nДля отображения счёта введите '" + STOP_WORD + "'"); | ||
|
|
||
| return scanner.next().equalsIgnoreCase(STOP_WORD); | ||
|
|
||
| } | ||
|
|
||
| } | ||
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 @@ | ||
| import java.io.InputStream; | ||
| import java.util.Scanner; | ||
|
|
||
| public final class MyScanner { | ||
|
|
||
| private final Scanner scanner; | ||
|
|
||
| public String next() { return scanner.next(); } | ||
|
|
||
| public int nextInt() { return (int) nextDouble(); } | ||
|
|
||
| public double nextDouble() { | ||
| return readValue(scanner.next()); | ||
| } | ||
|
|
||
| private double readValue(final String value) { | ||
|
|
||
| double newValue; | ||
|
|
||
| try { | ||
| newValue = Double.parseDouble(value); | ||
| } catch(NumberFormatException e) { | ||
| newValue = -1; | ||
| } | ||
|
|
||
| return newValue; | ||
|
|
||
| } | ||
|
|
||
| public void close() { | ||
| scanner.close(); | ||
| } | ||
|
|
||
| public static void errorMessage() { System.out.println("Недопустимый ввод, попробуйте ещё раз"); } | ||
|
|
||
| public MyScanner(InputStream in) { | ||
| this.scanner = new Scanner(in); | ||
| } | ||
|
|
||
| } |
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,17 @@ | ||
| public final class Product { | ||
|
|
||
| private final String name; | ||
| private final double price; | ||
|
|
||
| public String getName() { return this.name; } | ||
|
|
||
| public double getPrice() { return this.price; } | ||
|
|
||
| Product(String name, double price) { | ||
|
|
||
| this.name = name; | ||
| this.price = 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.
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.
Слишком длинный метод. Его сложно поддерживать и лучше разбить на логические составляющие