Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TipCalc {
- public static void main(String[] args) {
- //Prerequisites
- //Variables
- double cost = 0;
- double tipPre = 0;
- double tip = 0;
- double total;
- double splitPre = 0;
- double split = 0;
- boolean rerun = false;
- boolean actualNum = false;
- String goAgain = "yes";
- String addtext = "";
- //scanner
- Scanner kb = new Scanner(System.in);
- //Program Start
- while(goAgain.equals("yes")) {
- //is this an additional tip calculation?
- if(rerun == true) {
- addtext = "again";
- }
- else {
- }
- System.out.println("Hello " + addtext + "! (^ー^)ノ \nPlease enter the price of the meal as a decimal(no $)");
- //id10t proofing
- while(actualNum == false) {
- if(kb.hasNextDouble() == true) {
- cost = kb.nextDouble();
- if(cost <= 0) {
- System.out.println("The food must be pretty bad if they are giving it to you for free or if they are paying you to eat it. Please enter a number above 0");
- }
- else {
- actualNum = true;
- }
- }
- else {
- System.out.println("Please enter an actual number.");
- kb.nextLine();
- }
- }
- actualNum = false;
- System.out.println("Now please eneter the tip precent (no %)");
- //id10t proofing
- while(actualNum == false) {
- if(kb.hasNextDouble() == true) {
- tipPre = kb.nextDouble();
- if(tipPre < 0) {
- System.out.println("The service must be pretty bad if you expect them to pay you. Please enter a number that is not negitive");
- }
- else if(tipPre == 0) {
- System.out.println("Wow, no tip.");
- actualNum = true;
- }
- else {
- actualNum = true;
- }
- }
- else {
- System.out.println("Please enter an actual number.");
- kb.nextLine();
- }
- }
- actualNum = false;
- //math to calculate tip
- tipPre = tipPre / 100;
- tip = cost * tipPre;
- //rounding
- tip = (tip * 100) + 0.5;
- tip = (int) tip;
- tip = (double) tip / 100;
- cost = (cost * 100) + 0.5;
- cost = (int) cost;
- cost = (double) cost / 100;
- //math for total cost
- total = cost + tip;
- //converting this back into a whole number to be displayed later
- tipPre = tipPre * 100;
- System.out.println("For a meal that costs " + cost + " and a tip of " + tipPre + "%, your tip is " + tip + " and your total cost is " + total + ".");
- System.out.println("Please type how many people you are splitting the bill with. If you are alone, just type 1");
- //id10t proofing
- while(actualNum == false) {
- if(kb.hasNextInt() == true) {
- split = kb.nextInt();
- if(split <= 0) {
- System.out.println("You must be really lonely to have no one eating. Please enter a number above 0");
- }
- else {
- actualNum = true;
- }
- }
- else {
- System.out.println("Please enter an actual number.");
- kb.nextLine();
- }
- }
- actualNum = false;
- //math for calculating the percentage of price per person split
- splitPre = total / split;
- //more rounding
- splitPre = (splitPre * 100) + 0.5;
- splitPre = (int) splitPre;
- splitPre = (double) splitPre / 100;
- System.out.println("If you are splitting the bill between " + split + " people evenly, each person pays " + splitPre + ".");
- System.out.println("Would you like to calculate another tip?" // restart?
- + "\nif so, type \"yes\", if not type \"no\".");
- //id10t proofing
- while(actualNum == false) {
- goAgain = kb.next();
- if(goAgain.equals("yes") == false && goAgain.equals("no") == false) {
- System.out.println("Please type \"yes\" or \"no\"");
- }
- else {
- actualNum = true;
- }
- }
- rerun = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement