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
base: main
Are you sure you want to change the base?
Changes from all commits
e1f6893
c521234
b6baf14
e66ec9d
df4c546
05efcb6
75e629f
d8dca14
5587dad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calc { | ||
| ArrayList<Commodity> pList = new ArrayList<Commodity>(); | ||
| double sum = 0; // Полная сумма всех товаров | ||
| static String inputStop= "завершить"; | ||
|
|
||
| // Class functions | ||
| public void startCalc(){ | ||
| Scanner pScanner = new Scanner(System.in); | ||
| String str = ""; // Commodity name | ||
| double price = 0; // Commodity price | ||
| boolean flag = false; | ||
|
|
||
| while(!flag){ | ||
| System.out.println("Введите название товара"); | ||
| str = pScanner.next(); | ||
|
|
||
| boolean correctInput = false; | ||
| System.out.println("Введите соимость товара \"" + str +"\" в формате рубли.копейки' [10.45, 11.40]"); | ||
| while(!correctInput){ | ||
| if(pScanner.hasNextDouble()){ | ||
| price = pScanner.nextDouble(); | ||
| if(price>0) { | ||
| correctInput = true; | ||
| pList.add(new Commodity(str, price)); | ||
| } | ||
| else{ | ||
| System.out.println("Цена не может быть отрицательной или нулевой, введите еще раз"); | ||
| } | ||
| } | ||
| else{ | ||
| System.out.println("Неверный формат, попробуйте снова"); | ||
| pScanner.next(); | ||
| } | ||
| } | ||
| System.out.println("Товар успешно добавлен, хотите продолжить? \n для завершения введите \"завершить\" "); | ||
| str = pScanner.next(); | ||
| if(str.equalsIgnoreCase(inputStop)) break;// Выходим, если введено завершить | ||
| } | ||
| } | ||
| public void showAllCommodities(){ | ||
| System.out.println("Добавленные товары:"); | ||
| this.sum = 0; // Перед расчетом обнулим сумму товаров на всякий случай | ||
| for ( Commodity i:pList){ | ||
| i.showCommodity(); | ||
| sum+= i.price; //Сразу суммируем стоимость, | ||
| // чтобы не прогонять цикл по второму разу отдельной функцией | ||
| } | ||
| } | ||
| public void calculate(int personQuantity){ | ||
| double money = this.sum/personQuantity; | ||
| System.out.println(String.format("Каждый платит по %.2f %s",money,this.getWordEnd(money))); | ||
| } | ||
| public String getWordEnd(double num){ | ||
| num = Math.floor(num); | ||
| int ost = (int)(num % 100); | ||
| if(ost>10 && ost <=14) return "рублей"; | ||
| else ost =(int)(num % 10); | ||
| switch (ost){ | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
| public void releaseArray(){ | ||
|
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. Метод не используется, лучше тогда убрать |
||
| for (Commodity c:this.pList) { | ||
| c.name=""; | ||
| c.price=0; | ||
| } | ||
| this.pList.clear(); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| public class Commodity { | ||
| // Глобальные поля класса еды | ||
| String name = ""; | ||
| double price= 0; | ||
|
|
||
| // Конструктор для создания еды | ||
| Commodity(String name, double price) | ||
| { | ||
| this.name = name; | ||
| this.price=price; | ||
| } | ||
|
|
||
| public void showCommodity() | ||
| { | ||
| // Выводим название и цену | ||
| System.out.println(String.format("%s price %.2f",this.name,this.price)); | ||
| } | ||
|
|
||
| // Деструктор не нужен | ||
| } |
This file was deleted.
This file was deleted.
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.
Лучше давать осмысленные названия переменным, например тут можно назвать
item