-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDividingprice.java
77 lines (61 loc) · 2.31 KB
/
Dividingprice.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.Scanner;
import java.text.DecimalFormat;
class Main {
public static void main(String[] args) {
System.out.println("How many people are there?");
Scanner sc = new Scanner(System.in);
int scanned = sc.nextInt();
int people = scanned;
System.out.print("\033[H\033[2J");
System.out.flush();
// System.out.println(people);
double price = 00.00;
System.out.println("What is the price? ");
Scanner newscan1 = new Scanner(System.in);
price = newscan1.nextDouble();
float array1[];
array1 = new float[people];
for (int i = 0; i<array1.length; i++){
System.out.println("Person " + (i+1) + ", enter your guess:");
Scanner scan = new Scanner(System.in);
float newscanned = sc.nextFloat();
float guess = newscanned;
array1[i] = guess;
System.out.print("\033[H\033[2J");
System.out.flush();
}
/*
// testing if guesses were stored
for (int i = 0; i<array1.length; i++){
System.out.println("Person: " + (i+1) + " , value: " + array1[i]);
}
*/
float array2[];
array2 = new float[people];
float difference;
for (int i = 0; i<people; i++){
float number = array1[i];
if (array1[i] > price){
difference = number - (float)price;
array2[i] = difference;
}else{
difference = (float)price - number;
array2[i] = difference;
}
}
float biggest_difference = array2[0];
int place = 0;
// Finding the largest difference
for (int i = 0; i<people; i++){
if (biggest_difference < array2[i]){
biggest_difference = array2[i];
place = i;
}
}
DecimalFormat f1 = new DecimalFormat("##.00");
double final_price = (double)(price/(people-1));
double final_diff = (double)array2[place];
System.out.println("Person " + (place+1) + ", you must pay the difference of $" + f1.format(final_diff) + " as the tip. \n");
System.out.println("The rest of you must pay $" + f1.format(final_price) + " each, for the total meal price of $" + f1.format(price));
}
}