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

create Calculator #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
dencharski wants to merge 2 commits into main
base: main
Choose a base branch
Loading
from mydev
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
56 changes: 56 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,56 @@
public class Calculator {
private int peoples;
private double finalPrice;
private String allProductNames = "";

public void setPeoples(int peoples) {
this.peoples = peoples;
}

public String addProduct(Product product) {
finalPrice = finalPrice + product.productPrice;
if (allProductNames.isEmpty()) {
allProductNames = product.productName;
} else {
allProductNames = allProductNames + ", \n" + product.productName;
}

return String.format("Продукт %s по цене %.2f добавлен.", product.productName, product.productPrice);
}

public String getAllProductNames() {
return allProductNames;
}

public String getFinalPrice() {
return ("Итоговая общая цена: " + convertPriceToString(finalPrice));
}

public String getPriceForOnePeople() {
double priceForOnePeople = finalPrice / peoples;
return ("Каждый человек должен заплатить: " + convertPriceToString(priceForOnePeople));
}

private String convertPriceToString(double price) {
String stringPrice = String.format("%.2f", price);

price = Math.floor(price);
double remains = price % 100;

if (11 <= remains && remains <= 14) {
stringPrice = stringPrice + " рублей";
} else {
remains = remains % 10;
if (remains == 1) {
stringPrice = stringPrice + " рубль";
}
else if (2 <= remains && remains <= 4) {
stringPrice = stringPrice + " рубля";
} else {
stringPrice = stringPrice + " рублей";
}
}

return stringPrice;
}
}
89 changes: 88 additions & 1 deletion src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,95 @@
import java.util.Scanner;

public class Main {

static Scanner scanner;
static Calculator calculator;
static int peoples = 0;
static boolean isPeoplesSetCorrectly = true;
static boolean isProductPurchased = false;
static String stopProductPurchased = "Завершить";

public static void main(String[] args) {
scanner = new Scanner(System.in);

// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
while (isPeoplesSetCorrectly) {
System.out.println("На сколько человек делим счет?");

try {
peoples = scanner.nextInt();

if (peoples == 1) {
showErrorMessage("Количество человек должно быть более 1.");
}
if (peoples < 1) {
showErrorMessage("Маловато будет. Это менее 1.");
}
if (peoples > 1) {
isPeoplesSetCorrectly = false;
isProductPurchased = true;
}


} catch (Exception e) {
showErrorMessage(e.getMessage());
}

}

calculator = new Calculator();
calculator.setPeoples(peoples);

while (isProductPurchased) {
System.out.println("Ведите название товара.");
String productName;
double productPrice;
productName = scanner.next();

boolean isPriceSetCorrectly = false;
while (!isPriceSetCorrectly) {

System.out.println("Введите цену товара в формате <рубли.копейки>.");
String stringProductPrice = scanner.next().trim();
if (stringProductPrice.contains(",")) {
stringProductPrice = stringProductPrice.replace(",", ".");
}

try {
productPrice = Double.parseDouble(stringProductPrice);
if (productPrice <= 0.0) {
showErrorMessage("Цена товара слишком мала. " + productPrice);
break;
} else {
Product product = new Product(productName, productPrice);
System.out.println(calculator.addProduct(product));
isPriceSetCorrectly = true;
}
} catch (Exception e) {
System.out.println(e.getMessage());
showErrorMessage("Цена должна быть указана в виде <xx.yy>." + e.getMessage());
}
}
System.out.println("Желаете добавить еще товар? Для завершения наберите <Завершить>.");

String buyerWantsBuy = scanner.next();
if (buyerWantsBuy.trim().equalsIgnoreCase(stopProductPurchased)) {
isProductPurchased = false;
}
}


System.out.println(calculator.getAllProductNames());
System.out.println(calculator.getFinalPrice());

System.out.println(calculator.getPriceForOnePeople());

}


static void showErrorMessage(String errorString) {
System.out.println("Ошибка! " + errorString);
}

}
9 changes: 9 additions & 0 deletions src/main/java/Product.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Product {
String productName;
double productPrice;

public Product(String productName, double productPrice) {
this.productName = productName;
this.productPrice = productPrice;
}
}

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