-
Notifications
You must be signed in to change notification settings - Fork 215
Sprint2 Project Work #88
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: master
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,8 @@ | ||
| public class Guest { | ||
|
|
||
| int guests; | ||
|
|
||
| Guest(int guests) { | ||
| this.guests = guests; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,91 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| static Scanner scanner = new Scanner(System.in); | ||
| static ArrayList<Product> products = new ArrayList<Product>(); | ||
| static Guest guests; | ||
| static ProdCalc prodCalc; | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| prodCalc = new ProdCalc(); | ||
| addGuests(); | ||
| addProd(); | ||
| runSum(); | ||
| runDivide(); | ||
| printResult(); | ||
| } | ||
|
|
||
| public static void addGuests() { | ||
| int person; | ||
| do { | ||
| System.out.println("Сколько вас человек?"); | ||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Введите корректное значение: "); | ||
| scanner.next(); | ||
| } | ||
| person = scanner.nextInt(); | ||
| } while (person <= 1); | ||
| guests = new Guest(person); | ||
| } | ||
|
|
||
| public static void addProd() { | ||
| String food; | ||
| double price; | ||
| while (true) { | ||
| System.out.println("Ведите название товара или 'Завершить'"); | ||
| food = scanner.next(); | ||
| if (food.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } else { | ||
| do { | ||
| System.out.println("Стоимость товара в формате 'рубли.копейки' (XX,XX)"); | ||
| while (!scanner.hasNextDouble()) { | ||
| System.out.println("Введите корректное значение!"); | ||
| scanner.next(); | ||
| } | ||
| price = scanner.nextDouble(); | ||
| } | ||
| while (price <= 0); | ||
| } | ||
| products.add(new Product(food, price)); | ||
| System.out.println("Добавлено"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static double runSum() { | ||
|
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. Возвращаемое значение метода не используется. Можно сделать метод void или использовать значение |
||
| for (int i = 0; i < products.size(); i++) { | ||
| prodCalc.sum(products.get(i).coast); | ||
| } | ||
| return prodCalc.summaryCoast; | ||
| } | ||
|
|
||
| public static double runDivide() { | ||
|
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. Возвращаемое значение метода не используется. Можно сделать метод void или использовать значение |
||
| return prodCalc.divide(guests.guests); | ||
| } | ||
|
|
||
| public static void printResult() { | ||
| String template = "Каждый должен заплатить %.2f %s"; | ||
| System.out.println("Добавленные товары: "); | ||
|
|
||
|
|
||
| for (int i = 0; i < products.size(); i++) { | ||
| System.out.println(products.get(i)); | ||
| } | ||
| System.out.println("Общая сумма: " + prodCalc.summaryCoast); | ||
| System.out.println(String.format(template, prodCalc.moneyForPerson, getRightWord(prodCalc.moneyForPerson))); | ||
|
|
||
| } | ||
|
|
||
| private static String getRightWord(double n) { | ||
| n = (int) Math.floor(n); | ||
| n = Math.abs(n) % 100; | ||
| int n1 = (int) (n % 10); | ||
| if (n > 10 && n < 20) return "рублей"; | ||
| if (n1 > 1 && n1 < 5) return "рубля"; | ||
| if (n1 == 1) return "рубль"; | ||
| return "рублей"; | ||
|
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,17 @@ | ||
| public class ProdCalc { | ||
|
|
||
| public double summaryCoast; | ||
| public double moneyForPerson; | ||
|
|
||
| public double sum(double coast) { | ||
|
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. Возвращаемое значение метода не используется. Можно сделать метод void или использовать значение |
||
| return summaryCoast += coast; | ||
| } | ||
|
|
||
| public double divide(int guests) { | ||
| moneyForPerson = summaryCoast / guests; | ||
| return moneyForPerson; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class Product { | ||
|
|
||
| public String name; | ||
| public double coast; | ||
|
|
||
| Product(String name, double coast) { | ||
| this.name = name; | ||
| this.coast = coast; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return String.format("%s:\t%.2f", name, coast); | ||
|
|
||
| } | ||
| } |
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.
Можно методы расчета вынести в отдельный класс Калькулятор, сделать не static, а обычными, и в main() создавать объект калькулятора и вызывать методы от него