Skip to content

Instantly share code, notes, and snippets.

@213edu
Last active December 31, 2016 23:49
Show Gist options
  • Save 213edu/8c4d13d66cbbda4602cc24813c241e85 to your computer and use it in GitHub Desktop.
Save 213edu/8c4d13d66cbbda4602cc24813c241e85 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class BillSplitter {
public static void main(String[] args) {
double total = 0;
double preTax = 0;
double ratio = 0;
double totalShare = 0;
int peopleCount;
Scanner intScanner = new Scanner(System.in);
Scanner stringScanner = new Scanner(System.in);
Scanner doubleScanner = new Scanner(System.in);
System.out.println("Total?");
total = doubleScanner.nextDouble();
System.out.println("How many people in totoal?");
peopleCount = intScanner.nextInt();
Person[] persons;
persons = new Person[peopleCount];
for (int i = 0; i < peopleCount; i++) {
String name;
double subtotal;
System.out.println("Person " + i + " name:");
name = stringScanner.nextLine();
System.out.println(name + " 's subtotal");
subtotal = doubleScanner.nextDouble();
persons[i] = new Person(name, subtotal);
}
for (int i = 0; i < peopleCount; i++) {
preTax += persons[i].value;
}
ratio = total / preTax;
for (int i = 0; i < peopleCount; i++) {
persons[i].share = shareCalc(persons[i].value, ratio);
totalShare += persons[i].share;
}
for (int i = 0; i < peopleCount; i++) {
System.out.println(persons[i].name + ": " + persons[i].share);
}
System.out.println("PreTax Total: "+ preTax);
System.out.println("Totoal: " + totalShare);
}
static double shareCalc(double value, double ratio) {
return ratio * value;
}
}
public class Person {
double value;
String name;
double share;
public Person() {
value = 0;
name = "";
}
public Person(String name, double value) {
this.name = name;
this.value = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment