forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Alpha version 1.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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
69 changes: 69 additions & 0 deletions
src/main/java/Calculator.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,69 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
| public class Calculator { | ||
|
|
||
| private int numberOfPeople; | ||
| private double totalCost = 0; | ||
| private final ArrayList<String> listOfProducts; | ||
| private final ArrayList<Double> costOfProducts; | ||
|
|
||
| private final Formater format = new Formater(); | ||
|
|
||
| private final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| Calculator(){ | ||
| listOfProducts = new ArrayList<>(); | ||
| costOfProducts = new ArrayList<>(); | ||
| } | ||
|
|
||
| public void start(){ | ||
| System.out.print("Сколько человек будет делить счет: "); | ||
| this.numberOfPeople = format.scanInt(); | ||
| addProduct(); | ||
|
|
||
| while(true){ | ||
| System.out.print("Желаете добавить ещё один товар? (Да/Завершить): "); | ||
| String str = scanner.next(); | ||
| if(str.equalsIgnoreCase("Завершить")){ | ||
| show(); | ||
| break; | ||
| } | ||
| if(str.equalsIgnoreCase("Да")) { | ||
| addProduct(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void addProduct(){ | ||
| while(true){ | ||
| System.out.print("Введите название продукта: "); | ||
| String str = scanner.next(); | ||
| if(!str.isEmpty()){ | ||
| this.listOfProducts.add(str); | ||
| addProductCost(); | ||
| System.out.println("Товар успешно добавлен!"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void addProductCost(){ | ||
| System.out.print("Введите цену товара: "); | ||
| double cost = format.scanDouble(); | ||
| this.costOfProducts.add(cost); | ||
| this.totalCost += cost; | ||
|
|
||
| } | ||
|
|
||
| public void show(){ | ||
| double result = totalCost/numberOfPeople; | ||
| String rubl = format.checkRes(result); | ||
| System.out.println("Добавленные товары: "); | ||
| for(int i = 0; i < listOfProducts.size(); i++){ | ||
| System.out.printf("%-10s %20.2f \n", listOfProducts.get(i), costOfProducts.get(i)); | ||
| } | ||
| System.out.printf("\nИтого с человека:%10.2f %-2s\n(Поделено на %d человек)\n", | ||
| result, rubl, numberOfPeople); | ||
| } | ||
|
|
||
| } |
60 changes: 60 additions & 0 deletions
src/main/java/Formater.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,60 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Formater { | ||
|
|
||
| private Scanner scanner = new Scanner(System.in); | ||
|
|
||
|
|
||
| public int scanInt(){ | ||
| while(true){ | ||
| if(scanner.hasNextInt()){ | ||
| int numbForScan = scanner.nextInt(); | ||
| if(numbForScan > 1){ | ||
| return numbForScan; | ||
| } | ||
| else{ | ||
| System.out.println("Ошибка! Введите корректное число."); | ||
| scanner = null; | ||
| scanner = new Scanner(System.in); | ||
| } | ||
| } | ||
| else { | ||
| System.out.println("Ошибка! Введите корректное число."); | ||
| scanner = null; | ||
| scanner = new Scanner(System.in); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public double scanDouble(){ | ||
| while(true){ | ||
| if(scanner.hasNextDouble()){ | ||
| double numbForScan = scanner.nextDouble(); | ||
| if(numbForScan > 0){ | ||
| return numbForScan; | ||
| } | ||
| else{ | ||
| System.out.println("Ошибка! Введите корректное число."); | ||
| scanner = null; | ||
| scanner = new Scanner(System.in); | ||
| } | ||
| } | ||
| else { | ||
| System.out.println("Ошибка! Введите корректное число."); | ||
| scanner = null; | ||
| scanner = new Scanner(System.in); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public String checkRes(double numb){ | ||
| int a = (int) Math.floor(numb); | ||
| if(a % 10 == 1) { | ||
| if(a % 100 == 11) return "рублей"; | ||
| else return "рубль"; | ||
| } | ||
| else if (a % 100 >= 12 && a % 100 <= 15) return "рублей"; | ||
| else if (a % 10 > 1 && a % 10 < 5) return "рубля"; | ||
| else return "рублей"; | ||
| } | ||
| } |
4 changes: 2 additions & 2 deletions
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,6 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Calculator calculator = new Calculator(); | ||
| calculator.start(); | ||
| } | ||
| } |
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.