Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.ArrayList;

public class Calculator {
ArrayList<Pair<String, Double>> listItems = new ArrayList<Pair<String, Double>>();

public void printResult(int guests){
Double allPrice = 0.0;
System.out.println("Добавленные товары:\n");
for(int i = 0; i < listItems.size(); i++){
System.out.printf("%s, стоит %.2f %s\n",listItems.get(i).getLeft(), listItems.get(i).getRight(), printRoubles(listItems.get(i).getRight()));
allPrice = listItems.get(i).getRight() + allPrice;
}
Double sumPerPerson = allPrice/guests;
String roubles = printRoubles(sumPerPerson);
System.out.printf("Каждый заплатит %.2f %s", sumPerPerson, roubles);
}

private String printRoubles(Double dChislo){
String rouble;
String chislo = String.format("%.2f", dChislo).split(",")[0];
if(chislo.length() > 1){
if(chislo.charAt(chislo.length()-2) == '1'){
return "рублей";
}
}
switch(chislo.charAt(chislo.length()-1)){
case '1':
rouble = "рубль";
break;
case '2':
case '3':
case '4':
rouble = "рубля";
break;
default:
rouble = "рублей";
break;
}
return rouble;
}
}
65 changes: 64 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
import java.util.Scanner;

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

Scanner scanner = new Scanner(System.in);

int guests = getGuests(scanner);
Calculator calc = new Calculator();
while(true){
System.out.println("Введите товар");
String name = scanner.next();
System.out.println("Введите стоимость, цену введите дробную с точкой");
scanner.nextLine();
Double cost = insertDouble(scanner);
calc.listItems.add(new Pair(name, cost));
System.out.println("Товар успешно добавлен, завершить подсчет?");
String answer = scanner.next();
if(answer.toLowerCase().contains("завершить")){
break;
}
}
calc.printResult(guests);
scanner.close();
}

public static int getGuests(Scanner scanner){
int count;
while(true){
System.out.println("Сколько гостей?");
count = insertCount(scanner);
if(count <= 1){
System.out.println("Некорректное значение для подсчета");
}
else{
break;
}
}
return count;
}

public static int insertCount(Scanner scanner){
int count;
try{
count = Integer.parseInt(scanner.nextLine());
}
catch (Exception e){
System.out.println("Введите целочисленное");
count = insertCount(scanner);
}
return count;
}

public static Double insertDouble(Scanner scanner){
double count;
try{

count = Double.parseDouble(scanner.nextLine());
if(count < 0){
throw new Exception("Negative Double");
}
}
catch (Exception e){
System.out.println("Введите положительное дробное число с точкой");
count = insertDouble(scanner);
}
return count;
}
}
28 changes: 28 additions & 0 deletions src/main/java/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class Pair<L,R> {

private final L left;
private final R right;

public Pair(L left, R right) {
assert left != null;
assert right != null;

this.left = left;
this.right = right;
}

public L getLeft() { return left; }
public R getRight() { return right; }

@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }

@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}