Skip to content
35 changes: 35 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.ArrayList;
import java.util.Iterator;

public class Calculator {
ArrayList<Good> goodList = new ArrayList<>();

float sum = 0;
int quantityPersons;

float getSum(ArrayList<Good> goodList) {

this.goodList = goodList;
Iterator iterator = goodList.iterator();

while(iterator.hasNext()){
Good good = (Good) iterator.next();
sum = sum + good.price;
}
return sum;
}

float getSumForPerson(int quantityPersons, float sum) {
this.sum = sum;
this.quantityPersons = quantityPersons;
float sumForPerson = sum / quantityPersons;
return sumForPerson;
}

/*

System.out.println(String.format("Каждый должен заплатить " + f%.2f + " р.", sumForPerson));
*/


}
26 changes: 26 additions & 0 deletions src/main/java/Formater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Formater {
float sumForPerson = 0;
String formatItog(float sumForPerson) {
this.sumForPerson = sumForPerson;
int formaterBase = (int)sumForPerson;
String rubl = "";

if(formaterBase >= 100) {
formaterBase = formaterBase % 100;
}
if(formaterBase > 20){
formaterBase = formaterBase % 10;
}

if (formaterBase == 1) {
rubl = "рубль";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Строки можно вынести в константы

} else if (formaterBase > 1 && formaterBase < 5){
rubl = "рубля";
} else {
rubl = "рублей";
}
String itog = String.format("%.2f %s", sumForPerson, rubl);
return itog;
}

}
8 changes: 8 additions & 0 deletions src/main/java/Good.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Good {
String name;
float price;
Good(String name, float price){
this.name = name;
this.price = price;
}
}
106 changes: 104 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,108 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

int quantityPersons = getQuantityPersons();

ArrayList<Good> goodList = getGoodList();

Calculator calculator = new Calculator();
Formater formater = new Formater();
float sum = calculator.getSum(goodList);
float sumForPerson = calculator.getSumForPerson(quantityPersons, sum);
String goodListString = getGoodListString(goodList);
String formatString = formater.formatItog(sumForPerson);

System.out.println(goodListString);
System.out.println("Сумма на человека: " + formatString + ".");

}

public static int getQuantityPersons() {
Scanner scanner = new Scanner(System.in);

int quantityPersons;

while(true) {
System.out.println("На сколько человек разделить счет?");

if (scanner.hasNextInt()) {
quantityPersons = scanner.nextInt();
if(quantityPersons <= 0) {
System.out.println("Некорректное значение.");
} else if (quantityPersons == 1) {
System.out.println("Нечего делить!");
} else {
break;
}
} else {
System.out.println("Введите число.");
scanner.next();
}
}
return quantityPersons;
}
}
public static ArrayList<Good> getGoodList() {
Scanner scanner = new Scanner(System.in);
ArrayList<Good> goodList = new ArrayList<>();
int position = 0;
while(true) {
if(position == 0) {
System.out.println("Введите наименование товара.");
} else {
System.out.println("Введите наименование товара или команду Завершить.");
}

String goodName = scanner.next();
if(goodName.equalsIgnoreCase("Завершить")){
break;
} else {
System.out.println("Введите стоимость товара.");
while(scanner.hasNext()){
if(scanner.hasNextFloat()){
float goodPrice = scanner.nextFloat();
if(goodPrice <= 0){
System.out.println("Стоимость товара не может быть отрицательной или равна нулю.");
} else {
Good good = new Good(goodName, goodPrice);
goodList.add(good);
System.out.println("Товар " + goodName + " успешно добавлен.");
position++;
break;
}
} else {
System.out.println("Стоимость - целое число или число с запятой.");
scanner.next();
}
}
}
}

float sum = 0;
Iterator iterator = goodList.iterator();

while(iterator.hasNext()){
Good good = (Good) iterator.next();
sum = sum + good.price;
}
return goodList;
}

public static String getGoodListString(ArrayList<Good> goodL) {
ArrayList<Good> goodList = goodL;
String goodListString = new String("");
Iterator iterator = goodList.iterator();

while(iterator.hasNext()){
Good good = (Good) iterator.next();
goodListString = goodListString + "\n" +good.name;
}

goodListString = "Добавленные товары:" + goodListString;
return goodListString;
}
}