fbpx

 

How did you aproach...
 

How did you aproach Challenge003??  

   RSS

1

This is how I did it:

//Instructions

//Create a program that will calculate the prefered tip for a dinner bill.
//For example, your meal cost $20. Have 3 suggested tip values: 15%, 20% and 25%.
//Include support for a custom percent tip and calculate the tip amount and total bill.
//Use the inspector to input your data. Example Print:
//"Your bill is $20"
//"15% tip = $3 with a final total of: $23"
//"20% tip = $4 with a final total of: $24"
//"25% tip = $5 with a final total of: $25"
//"Your custom tip of 18.5% = $3.70 with a final total of $23.70"

public int bill;

private float tipvalue15 = 0.15f;
private float tipvalue20 = 0.25f;
private float tipvalue25 = 0.25f;
public float customtip;

void Start()
{

}

// Update is called once per frame
void Update()
{
float customTipPercentage = customtip / 100;

Debug.Log("Your bill is $" + bill);
Debug.Log("15% tip = $" + (bill * tipvalue15) + " with a final total of: $" + (bill + (bill * tipvalue15)));
Debug.Log("20% tip = $" + (bill * tipvalue20) + " with a final total of: $" + (bill + (bill * tipvalue20)));
Debug.Log("25% tip = $" + (bill * tipvalue25) + " with a final total of: $" + (bill + (bill * tipvalue25)));
Debug.Log("Your custom tip of " + customtip + "% = $" + (bill * customTipPercentage).ToString("#.00") + " with a final total of $" + (bill + (bill * customTipPercentage)).ToString("#.00"));
}

@emfpc

Hi,
For code formatting, I've found that this works
https://gamedevhq.com/community/community-resources/test-forum-code-formatting/#post-157

Regards,

0

I tried it this way:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ch_003 : MonoBehaviour {

[SerializeField] private float dinnerBill;
[SerializeField] private float customTipPercent;
private const float interest15 = 15f;
private const float interest20 = 20f;
private const float interest25 = 25f;
private float tip, total;

void Start() {

Debug.Log($"Dinner bill was ${dinnerBill:F2}");

DoCalcs(interest15);
Debug.Log($"{interest15:F2}% tip of ${tip:F2} makes the total cost ${total:F2}");

DoCalcs(interest20);
Debug.Log($"{interest20:F2}% tip of ${tip:F2} makes the total cost ${total:F2}");

DoCalcs(interest25);
Debug.Log($"{interest25:F2}% tip of ${tip:F2} makes the total cost ${total:F2}");

DoCalcs(customTipPercent);
Debug.Log($"Your custom {customTipPercent:F2}% tip of ${tip:F2} makes the total cost ${total:F2}");
}

private void DoCalcs(float percent) {
tip = dinnerBill * percent / 100;
total = dinnerBill * (1 + percent / 100);
}
}

@kdub

nice ?. like that you put the calculation in its on function and I just learn something new. Thanks for sharing

0

Oh wow Kerry, very minimalistic! I had a similar idea, but I might have gone a little overboard. The code will run regardless if a custom tip was provided or not.

Also, for that extra color and readability I use  http://hilite.me

I was thinking that one could link an image of a test run, but not entirely sure on how to embed images or if it is desirable by the hosts.

    [Header("Required")]
    [SerializeField]
    private float _mealCost;
    private float _tip15Percent = 15.0f;
    private float _tip20Percent = 20.0f;
    private float _tip25Percent = 25.0f;

    [Header("Optional")]
    [SerializeField]
    private float _customTipPercent = 0;

    private bool _isCustomTip = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Your bill is $" + _mealCost);
            CalculateCost(_mealCost, _tip15Percent, _isCustomTip);
            CalculateCost(_mealCost, _tip20Percent, _isCustomTip);
            CalculateCost(_mealCost, _tip25Percent, _isCustomTip);

            if (_customTipPercent > 0)
            {
                _isCustomTip = true;
                CalculateCost(_mealCost, _customTipPercent, _isCustomTip);
            }
        }
    }

    private void CalculateCost(float mealCost, float tipPercent, bool isCustomTip)
    {
        float tipConversion = 100.0f;
        float tip = mealCost * (tipPercent / tipConversion);
        float totalCost = mealCost + tip;

        if (isCustomTip == false)
        {
            Debug.Log(tipPercent + "% tip = $" + tip + " with a final total of: $" + totalCost);
        }
        else
        {
            Debug.Log("Your custom tip of " + tipPercent + "% = $" + tip.ToString("F2") +
                " with a final total of $" + totalCost.ToString("F2"));
        }
    }
0

@neurothrone

These were a nice touch

[Header("Required")][SerializeField] private float mealCost;
[Header("Optional")][SerializeField] private float customTipPercent;

 

Regards,

Share:

 

 

Scroll to Top

Please Login or Register