Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Amicratus wants to merge 3 commits into main
base: main
Choose a base branch
Loading
from dev
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
69 changes: 69 additions & 0 deletions src/main/java/Calculator.java
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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();
}
}

AltStyle によって変換されたページ (->オリジナル) /