-
Notifications
You must be signed in to change notification settings - Fork 0
Никита Филипенко, Когорта 12, Консольное приложение №1 #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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| StringBuilder productList = new StringBuilder("Добавленные товары:"); | ||
| double productListPrice; | ||
| String guess = ""; | ||
| double productPrice; | ||
|
|
||
| private void readProductName(Scanner scanner, Product product) { | ||
| System.out.println("Введите название товара:"); | ||
| product.name = scanner.nextLine(); | ||
| } | ||
|
|
||
| private void readProductPrice(Scanner scanner, Product product) { | ||
| System.out.println("Введите стоимость товара:"); | ||
| while (true) { | ||
| if (scanner.hasNextDouble()) { | ||
| productPrice = scanner.nextDouble(); | ||
| if (productPrice > 0) { | ||
| product.price = productPrice; | ||
| scanner.nextLine(); | ||
| break; | ||
| } else { | ||
| System.out.println("Цена не может быть отрицательной или нулевой, введите снова"); | ||
| } | ||
|
|
||
| } else { | ||
| System.out.println("Некорректные данные."); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void calculateTotalPrice(Product product) { | ||
| productListPrice += product.price; | ||
| } | ||
|
|
||
| void addNewProduct(Scanner scanner) { | ||
| while (!guess.equalsIgnoreCase("завершить")) { | ||
| Product product = new Product(); | ||
| readProductName(scanner, product); | ||
| readProductPrice(scanner, product); | ||
| productList.append("\n").append(product.name); | ||
| calculateTotalPrice(product); | ||
| System.out.println("Товар \"" + product.name + "\" успешно добавлен.\n" + | ||
| "Хотите добавить ещё товар?"); | ||
| guess = scanner.nextLine(); | ||
| } | ||
| } | ||
|
|
||
| void printProductList(int people) { | ||
| String roundedPrice = PriceFormat.roundPrice(productListPrice / people); | ||
| System.out.println(productList.toString()); | ||
| System.out.println("С каждого человека " + roundedPrice+ " " + PriceFormat.rubInflection(productListPrice / people)); | ||
|
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 |
|---|---|---|
| @@ -1,6 +1,13 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| PeopleAmount peopleAmount = new PeopleAmount(); | ||
| Calculator calculator = new Calculator(); | ||
| int people = peopleAmount.readPeopleAmount(scanner); | ||
| calculator.addNewProduct(scanner); | ||
| calculator.printProductList(people); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class PeopleAmount { | ||
|
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. Я бы назвал класс |
||
| int readPeopleAmount(Scanner scanner) { | ||
| int amount; | ||
| System.out.println("На скольких человек делим счёт?"); | ||
| while(true) { | ||
| if (scanner.hasNextInt()) { | ||
| amount = scanner.nextInt(); | ||
| if (amount <= 1) { | ||
| System.out.println("Количество человек должно быть больше одного."); | ||
| } else { | ||
| scanner.nextLine(); | ||
| return amount; | ||
| } | ||
| } else { | ||
| System.out.println("Количество человек болжно быть введено в виде числа."); | ||
| scanner.next(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class PriceFormat { | ||
| static String roundPrice(double price) { | ||
| return String.format("%.2f", price); | ||
| } | ||
|
|
||
| static String rubInflection(double price) { | ||
| if (price % 10 == 1 && price % 100 != 11) { | ||
| return "рубль"; | ||
| } else if (price % 10 >= 2 && price % 10 <= 4 && (price % 100 < 10 || price % 100 >= 20)) { | ||
| return "рубля"; | ||
| } else { | ||
| return "рублей"; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| public class Product { | ||
| String name; | ||
| double price; | ||
| } |
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.
Лучше добавить проверку параметра
people, чтобы случайно не поделить на 0, если в будушем эту функцию будет кто-то переиспользовать