acm-mjct / Utilities

8 stars 86 forks source link

construct a Tip calculator #38

Open iamasrar011 opened 1 year ago

iamasrar011 commented 1 year ago

Calculate the tip value on an array of bill values depending upon the following conditions:

If the bill value is between 0 and 300, then the tip is 15% of the bill value. If the bill value is greater than 300, then the tip is 20% of the bill value. Print the bill value as well as tip value.

9246-AwaisAhmed commented 1 year ago

import java.util.Scanner;

public class Tipcalc1 {
public static void main(String[] args) { System.out.println("Welcome to Tip Calculator! "); TipCalc2 Calculator = new TipCalc2(); System.out.println("Please enter the bill amount: "); TipCalc2.calBill(); System.out.println("What percentage would you like to tip?: "); Calculator.percTip();

}

}

And the tipCalc2 class which does the dirty work:

import java.util.Scanner;

public class TipCalc2 { static double bill; double tip; double total; double split; double splitPrompt; double Y; double N; double billPerPerson;

    static Scanner scan = new Scanner(System.in);
    public static void calBill()
    {
         bill = scan.nextDouble();
    }

    public void percTip()
    {
         tip = scan.nextDouble();
        if(tip<1)
        {
            total = bill * tip;
        }
    else total = bill * (tip/100);
    System.out.println("Your total is: " + total);
    Split();
    }

    public void Split()
    {
    System.out.println("Would you like to split the bill? ");
    System.out.println("Enter 1 for YES or 0 for NO: ");

    splitPrompt = scan.nextDouble();
    if(splitPrompt == 0)
    {
        System.out.println("Your total is: " + total);
        System.out.println("Thankyou. Goodbye.");
        System.out.println("End Program");  
    }
    if(splitPrompt == 1)
    {
        System.out.println("How many ways would you like to split the bill? ");
        splitPrompt = scan.nextDouble();
        billPerPerson = total / split;
        System.out.println("Each person pays: " + billPerPerson);
        System.out.println("Thankyou. Goodbye.");
        System.out.println("End Program.");  

    }
    else System.out.println("Invalid Entry");
    }

}