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

Add sum distribute #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
desska wants to merge 2 commits into dev
base: dev
Choose a base branch
Loading
from develop
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
45 changes: 45 additions & 0 deletions src/main/java/Calc.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.ArrayList;

public class Calc {

private ArrayList<Item> items;

Calc() {

items = new ArrayList<Item>();

}

public void addItem(Item item) {

items.add(item);

}

public String getItemsList() {

StringBuilder builder = new StringBuilder();

for (Item item : items) {
builder.append(item.name);
builder.append("\n");
}
return builder.toString();
}

public double getSum() {

double sum = 0;
for (Item item : items) {
sum += item.price;
}
return sum;
}

public double distributedSum(double sum, int personsAmount) {

return ((personsAmount > 0) ? sum / personsAmount : 0);

}

}
32 changes: 32 additions & 0 deletions src/main/java/Formatter.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Formatter {

public String getFormatSum(double sum) {

return String.format("%.2f %s", sum, getFormattedRub(sum));

}

String getFormattedRub(double sum) {

if(((sum % 100) >= 11) & ((sum % 100) <=19)) {
return "рублей";
}

Double s = new Double(sum);
String sumAsString = String.valueOf(s.intValue());
char lastNum = sumAsString.charAt(sumAsString.length() - 1);
String ending = "";
switch (lastNum) {
case ('1'):
return "рубль";
case ('2'):
case ('3'):
case ('4'):
return "рубля";
default:
return "рублей";
}

}

}
47 changes: 47 additions & 0 deletions src/main/java/Input.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Scanner;

public class Input {

public int nextInt(Scanner scanner) {

boolean isContinue = true;
int val = 0;
while (isContinue) {

if (scanner.hasNextInt()) {
val = scanner.nextInt();
isContinue = false;
} else {
System.out.println("Значение некорректного типа");
scanner.next();
}

}
return val;
}

public double nextDouble(Scanner scanner) {

boolean isContinue = true;
double val = 0;
while (isContinue) {

if (scanner.hasNextDouble()) {
val = scanner.nextDouble();
isContinue = false;
} else {
System.out.println("Значение некорректного типа");
scanner.next();
}

}
return val;
}

public String next(Scanner scanner) {

return scanner.next();

}

}
10 changes: 10 additions & 0 deletions src/main/java/Item.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Item {

public String name;
public double price;

Item(String name, double price) {
this.name = name;
this.price = price;
}
}
78 changes: 76 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,80 @@
import java.util.Scanner;

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

System.out.println("На скольких человек делить счет?");

Scanner scanner = new Scanner(System.in);
Input input = new Input();

int personsAmount = input.nextInt(scanner);

while (!isCorrectPersonsAmount(personsAmount)) {
System.out.println("Введенное значение некорректно. Повторите ввод");
personsAmount = input.nextInt(scanner);
}

if (!isNeedToDistribute(personsAmount)) {
System.out.println("Для одного человека делить ничего не требуется.");
return;
}

Calc calc = new Calc();

boolean isContinue = true;

while (isContinue) {

System.out.println("Введите наименование товара");
String itemName = input.next(scanner);

System.out.println("Введите цену для товара " + itemName);
double itemPrice = input.nextDouble(scanner);
Copy link

@ArturNurtdinov ArturNurtdinov Aug 24, 2023

Choose a reason for hiding this comment

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

нужно проверить, что itemPrice больше 0, в противном случае - запрашивать повторный ввод

while (! isCorrectPrice(itemPrice)) {
System.out.println("Введенная цена должна быть больше нуля");
itemPrice = input.nextDouble(scanner);
}

Item item = new Item(itemName, itemPrice);
calc.addItem(item);

System.out.println("Вы хотите добавить еще один товар? Если нет, введите \"" + finishAddComand() + "\" Для завершения ввода");

String command = scanner.next();
isContinue = !isFinishAddCommand(command);

}

System.out.println("Добавленные товары:\n" + calc.getItemsList());

double sum = calc.getSum();
double distributedSum = calc.distributedSum(sum, personsAmount);
Formatter formatter = new Formatter();
System.out.println("Распределенная на каждого сумма: " + formatter.getFormatSum(distributedSum));

}

static boolean isCorrectPersonsAmount(int personsAmount) {
return personsAmount >= 1;
}

static boolean isNeedToDistribute(int personsAmount) {
return personsAmount > 1;
}

static boolean isFinishAddCommand(String command) {
return command.equalsIgnoreCase(finishAddComand());
}
}

static boolean isCorrectPrice(double price) {
return price > 0;
}
static String finishAddComand() {
return "завершить";
}

}



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