-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitExpenses.java
More file actions
61 lines (50 loc) · 1.87 KB
/
SplitExpenses.java
File metadata and controls
61 lines (50 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* SplitExpenses.java
* @description performs calculations of individual weighted expenses when sharing a meal or any expense
* author R Samman
* version 2.0 28-12-2023
*/
import java.util.Scanner;
public class SplitExpenses {
private int numberOfPeople;
private String[] names;
private double[] percentages;
private double totalAmount;
private Scanner scanner = new Scanner(System.in);
/** constructor **/
public SplitExpenses() {}
/** gets user's input */
public void getInput() {
System.out.println("Enter the total amount of money spent: ");
totalAmount = scanner.nextDouble();
System.out.println("Enter the number of people who shared the meal: ");
numberOfPeople = scanner.nextInt();
scanner.nextLine();
names = new String[numberOfPeople];
percentages = new double[numberOfPeople];
// Get names and percentages
for (int i = 0; i < numberOfPeople; i++) {
System.out.println("Enter the name of person " + (i + 1) + ": ");
names[i] = scanner.nextLine();
System.out.println("Enter the percentage of how much " + names[i] + " ate: ");
percentages[i] = scanner.nextDouble();
if (i < numberOfPeople - 1) {
scanner.nextLine();
}
}
}
//* calculates weighted expenses */
public void calculateExpenses() {
System.out.println("\nIndividual total expenses:");
for (int i = 0; i < numberOfPeople; i++) {
double individualExpense = (percentages[i] / 100) * totalAmount;
System.out.println(names[i] + ": $" + individualExpense);
}
}
//* main method */
public static void main(String[] args) {
SplitExpenses expensesCalculator = new SplitExpenses();
expensesCalculator.getInput();
expensesCalculator.calculateExpenses();
}
}