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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {



public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
final Scanner scanner = new Scanner(System.in);
PrintStream ps = new PrintStream(System.out);
double result = 0;
ps.println("Введите количество персон, более 1:");
int amountPeople = 0;
amountPeople = scanner.nextInt();
while (amountPeople < 2) {
System.out.println("Введите количество персон, более 1:");
amountPeople = scanner.nextInt();
}
ArrayList<Position> resultPositions = new Position().counter();
ps.println("Добавленные товары:");
int j = 0;
do {
resultPositions.get(j).printPosition();
j++;
} while (j < resultPositions.size());
ps.println();
result = new Position().calculate(resultPositions) / amountPeople;
ps.print("Каждый должен заплатить: ");
switch ((int) (result % 20)) {
case 1: ps.println(String.format("%.2f", result)+" рубль"); break;
case 2: ps.println(String.format("%.2f", result)+" рубля"); break;
case 3: ps.println(String.format("%.2f", result)+" рубля"); break;
case 4: ps.println(String.format("%.2f", result)+" рубля"); break;
default: ps.println(String.format("%.2f", result)+" рублей"); break;
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/Position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Position {
String positionName;
double positionPrice;
Position(String name, double price) {
positionPrice = price;
positionName = name;
}
Position() {}
public double calculate(ArrayList<Position> positions) {
int i = 0;
double result = 0;
while (i < positions.size()) {
result += positions.get(i).positionPrice;
i++;
}
return result;
}
public void printPosition() {
double d = Math.floor(this.positionPrice);
switch ((int) (d % 20)) {
case 1: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубль"); break;
case 2: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубля"); break;
case 3: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубля"); break;
case 4: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рубля"); break;
default: System.out.println(this.positionName + ": " + String.format("%.2f",this.positionPrice) + " рублей"); break;
}
}
public ArrayList<Position> counter() throws InputMismatchException, NoSuchElementException {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
ArrayList<Position> positions = new ArrayList();
double price = 0;
String closed = "";
String name = "";
Scanner scanner = new Scanner(System.in);
PrintStream ps = new PrintStream(System.out); //коммент для коммита
do {
ps.println("Введите название товара и цену через Enter (дробные цены вводите через .)");
name = scanner.nextLine();
while (price<=0) {
try {
price = Double.parseDouble(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Введите цену!");
}
}
Position buf = new Position(name,price);
positions.add(buf);
price = 0;
System.out.println("Товар добавлен!");
System.out.println("Добавить еще или завершить?");
closed = scanner.nextLine();
} while (!(closed.equalsIgnoreCase("завершить")));
return positions;
}

}