Last active
December 31, 2016 23:49
-
-
Save 213edu/8c4d13d66cbbda4602cc24813c241e85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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