Beispiel #1
0
 protected void btnCalculate_Click(object sender, EventArgs e)
 {
     double amount = 0;
     bool goodAmount = double.TryParse(txtAmount.Text, out amount);
     if (!goodAmount)
     {
         Response.Write("<script>alert('Please enter a valid amount');</script>");
         txtAmount.Text = "";
         txtAmount.Focus();
         return;
     }
     double percent = 0;
     if (rdbPercentage.SelectedItem.Text != "other")
     {
         percent = double.Parse(rdbPercentage.SelectedValue.ToString());
     }
     else
     {
         bool goodPercent = double.TryParse(txtOther.Text, out percent);
         if (!goodPercent)
         {
             Response.Write("<script>alert('Please enter a valid Percentage');</script>");
             txtAmount.Text = "";
             txtAmount.Focus();
             return;
         }
     }
     TipCalculator Tip = new TipCalculator(amount, percent);
     lblTip.Text = Tip.CalculateTip().ToString("$##0.##");
     lblTotal.Text = Tip.CalculateTotal().ToString("$##0.##");
 }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            TipCalculator calc = new TipCalculator();

            calculateButton.TouchUpInside += (object sender, EventArgs e) =>
            {
                tipAmountLabel.Text = calc.calculateTip(billAmountTextField.Text, tipPercentageTextField.Text);
            };

            billAmountTextField.AddTarget((sender, e) =>
            {
                tipAmountLabel.Text = calc.calculateTip(billAmountTextField.Text, tipPercentageTextField.Text);
            }, UIControlEvent.EditingChanged);

            tipPercentageTextField.AddTarget((sender, e) =>
            {
                tipAmountLabel.Text = calc.calculateTip(billAmountTextField.Text, tipPercentageTextField.Text);
            }, UIControlEvent.EditingChanged);

            tipPercentageSlider.ValueChanged += (sender, e) =>
            {
                int tipPercentage = (int)tipPercentageSlider.Value;

                tipPercentageTextField.Text = tipPercentage.ToString();
                tipAmountLabel.Text         = calc.adjustTipPercentage(billAmountTextField.Text, tipPercentage);
            };
        }
    public static void Main()
    {
        TipCalculator calc = new TipCalculator();

        Console.WriteLine("What is the bill amount?");
        string  billEntry = Console.ReadLine();
        Decimal billAmount;

        while (!Decimal.TryParse(billEntry, out billAmount) || (Decimal.Round(billAmount, 2) != billAmount))
        {
            Console.WriteLine("Enter a valid bill amount.");
            billEntry = Console.ReadLine();
        }

        Console.WriteLine("What is the tip rate?");
        string  tipEntry = Console.ReadLine();
        Decimal tipRate;

        while (!Decimal.TryParse(tipEntry, out tipRate) || (Decimal.Round(tipRate, 2) != tipRate))
        {
            Console.WriteLine("Enter a valid tip rate.");
            tipEntry = Console.ReadLine();
        }

        Decimal tip = calc.CalculateTip(billAmount, tipRate);

        Console.WriteLine("Your tip is: " + tip);
        Decimal total = tip + billAmount;

        Console.WriteLine("Your total is: " + total);
    }
        public void CalculateTipTest()
        {
            var calculator = new TipCalculator();
            var request    = new TipCalculationRequest {
                MealCost = 100, TipPercent = .2M
            };
            var result = calculator.CalculateTip(request);

            Assert.AreEqual(result.TipAmount, 20M);
            Assert.AreEqual(result.TotalCost, 120M);
        }
        public void CalculateTipZeroPercent()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = 10,
                TipPercent = 0
            };

            tipCalculator.CalcTip();

            Assert.AreEqual(tipCalculator.Tip, 0);
        }
        public void CalculateTipTwentyFivePercent()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = 10,
                TipPercent = 25
            };

            tipCalculator.CalcTip();

            Assert.AreEqual(tipCalculator.Tip, 2.50);
        }
        public void CalculateTipOneHundredPercent()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = 10,
                TipPercent = 100
            };

            tipCalculator.CalcTip();

            Assert.AreEqual(tipCalculator.Tip, 10);
        }
        public void CalculateTipPercentageTenPercent()
        {
            var tipCalculator = new TipCalculator
            {
                Total = 10,
                Tip   = 1
            };

            tipCalculator.CalcTipPercentage();

            Assert.AreEqual(tipCalculator.TipPercent, 10);
        }
        public void CalculateGrandTotal()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = 10,
                TipPercent = 50
            };

            tipCalculator.CalcTip();

            Assert.AreEqual(tipCalculator.GrandTotal, 15);
        }
        public void CalculateTipPercentageTwentyFivePercent()
        {
            var tipCalculator = new TipCalculator
            {
                Total = 10,
                Tip   = 2.50
            };

            tipCalculator.CalcTipPercentage();

            Assert.AreEqual(tipCalculator.TipPercent, 25);
        }
        public void CalcTip_18PercentTip100Total_GrandTotal118()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = 100,
                TipPercent = 18
            };

            tipCalculator.CalcTip();

            Assert.AreEqual(decimal.Parse("118"), tipCalculator.GrandTotal);
        }
Beispiel #12
0
        public void CalculateTipTenPercent()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = 10,
                TipPercent = 10
            };

            tipCalculator.CalcTip();

            Assert.AreEqual(tipCalculator.Tip, (decimal)1.00);
        }
Beispiel #13
0
        public void SplitCheck_ThreePersons_TotalPersonEqualsOneHalfGrandTotal()
        {
            var tipCalculator = new TipCalculator
            {
                Total           = (decimal)149.36,
                TipPercent      = 15,
                NumberOfPersons = 3
            };

            tipCalculator.CalcTip();
            tipCalculator.SplitGrandTotal();

            Assert.AreEqual(tipCalculator.TotalPerPerson, Math.Round((tipCalculator.GrandTotal / 3), 2));
        }
Beispiel #14
0
        public void RoundTip_Total49_36TipPercent18_GrandTotal176TipShouldBe26_64()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = (decimal)149.36,
                TipPercent = 18
            };

            tipCalculator.CalcTip();
            tipCalculator.RoundTotal();

            Assert.AreEqual((decimal)176.00, tipCalculator.GrandTotal);
            Assert.AreEqual((decimal)26.64, tipCalculator.Tip);
        }
Beispiel #15
0
        public void SplitCheck_OnePerson_TotalPersonEqualsGrandTotal()
        {
            var tipCalculator = new TipCalculator
            {
                Total           = (decimal)149.36,
                TipPercent      = 15,
                NumberOfPersons = 1
            };

            tipCalculator.CalcTip();
            tipCalculator.SplitGrandTotal();

            Assert.AreEqual(tipCalculator.TotalPerPerson, tipCalculator.GrandTotal);
        }
Beispiel #16
0
        public void RoundTipThenCalcTip_Total49_36TipPercent15_GrandTotal171_76TipShouldBe22_40()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = (decimal)149.36,
                TipPercent = 15
            };

            tipCalculator.RoundTotal();
            tipCalculator.CalcTip();

            Assert.AreEqual((decimal)171.76, tipCalculator.GrandTotal);
            Assert.AreEqual((decimal)22.40, tipCalculator.Tip);
        }
Beispiel #17
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            TipCalculator tc = new TipCalculator();

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            EditText billTextField  = FindViewById <EditText>(Resource.Id.billEditText);
            SeekBar  slider         = FindViewById <SeekBar>(Resource.Id.slider);
            TextView sliderTextView = FindViewById <TextView>(Resource.Id.slideTextView);
            TextView tipTextView    = FindViewById <TextView>(Resource.Id.tipTextView);
            TextView totalTextView  = FindViewById <TextView>(Resource.Id.totalTextView);

            // updating tip and total text view when the user type an amount
            billTextField.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) =>
            {
                if (string.IsNullOrEmpty(billTextField.Text))
                {
                    tipTextView.Text   = tc.DefaultValues();
                    totalTextView.Text = tc.DefaultValues();
                }
                else
                {
                    tipTextView.Text   = tc.CalculatingTip(billTextField.Text, slider.Progress); // !
                    totalTextView.Text = tc.CalculatingTotal(tipTextView.Text, billTextField.Text);
                }
            };

            // updating tip and total text view when the user make use of the slider
            slider.ProgressChanged += (object sender, SeekBar.ProgressChangedEventArgs e) =>
            {
                sliderTextView.Text = tc.ChangingPercentageNumber(slider.Progress);

                if (string.IsNullOrEmpty(billTextField.Text))
                {
                    tipTextView.Text   = tc.DefaultValues();
                    totalTextView.Text = tc.DefaultValues();
                }
                else
                {
                    tipTextView.Text   = tc.CalculatingTip(billTextField.Text, slider.Progress); // !
                    totalTextView.Text = tc.CalculatingTotal(tipTextView.Text, billTextField.Text);
                }
            };
        }
        public ActionResult TipCalculator(TipCalculatorRequest request)
        {
            if (ModelState.IsValid)
            {
                var calc = new TipCalculator();

                var tipData = new MVCappLabs.Models.Level1.TipCalculationRequest();
                tipData.MealCost   = request.MealCost.Value;
                tipData.TipPercent = request.TipPercent.Value / 100;

                var response = calc.CalculateTip(tipData);

                return(View("TipCalculatorResponse", response));
            }
            return(View("TipCalculator"));
        }
Beispiel #19
0
        public ActionResult TipCalculatorInput(TipCalculatorModel request)
        {
            if (ModelState.IsValid)
            {
                var tipCalc = new TipCalculator();
                var tipData = new TipCalculatorRequest();
                tipData.MealTotal  = request.MealTotal.Value;
                tipData.TipPercent = request.TipPercent.Value;

                var result = tipCalc.CalculateTip(tipData);
                return(View("TipCalculatorOutput", result));
            }
            else
            {
                return(View(request));
            }
        }
        public void ResetCalulator_ValidViewModel_CalculatorResetToInitialState()
        {
            var myCalculatorViewModel = new CalculatorPageViewModel(myCalculator, myTipCalcTransaction, myTipDatabase)
            {
                TotalTxt = "35",
                TipTxt   = "6.30"
            };

            var newCalculator          = new TipCalculator();
            var newCalculatorViewModel = new CalculatorPageViewModel(myCalculator, myTipCalcTransaction, myTipDatabase);

            myCalculatorViewModel.ResetCalculator();

            Assert.AreEqual(newCalculatorViewModel.TotalTxt, myCalculatorViewModel.TotalTxt);
            Assert.AreEqual(newCalculatorViewModel.TipTxt, myCalculatorViewModel.TipTxt);
            Assert.AreEqual(newCalculatorViewModel.TipPercent, myCalculatorViewModel.TipPercent);
            Assert.AreEqual(newCalculatorViewModel.TipPercentTxt, myCalculatorViewModel.TipPercentTxt);
            Assert.AreEqual(newCalculatorViewModel.GrandTotalTxt, myCalculatorViewModel.GrandTotalTxt);
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            TipCalculator tc = new TipCalculator();

            // Perform any additional setup after loading the view, typically from a nib.

            billTextField.BecomeFirstResponder();

            // updating tip and total text label when the user type an amount
            billTextField.AddTarget((object sender, EventArgs e) =>
            {
                if (string.IsNullOrEmpty(billTextField.Text))
                {
                    tipLabel.Text   = tc.DefaultValues();
                    totalLabel.Text = tc.DefaultValues();
                }
                else
                {
                    tipLabel.Text   = tc.CalculatingTip(billTextField.Text, slider.Value);
                    totalLabel.Text = tc.CalculatingTotal(tipLabel.Text, billTextField.Text);
                }
            }, UIControlEvent.EditingChanged);

            // updating tip and total text label when the user make use of the slider
            slider.ValueChanged += (object sender, EventArgs e) =>
            {
                sliderPercentage.Text = tc.ChangingPercentageNumber(slider.Value);

                if (string.IsNullOrEmpty(billTextField.Text))
                {
                    tipLabel.Text   = tc.DefaultValues();
                    totalLabel.Text = tc.DefaultValues();
                }
                else
                {
                    tipLabel.Text   = tc.CalculatingTip(billTextField.Text, slider.Value);
                    totalLabel.Text = tc.CalculatingTotal(tipLabel.Text, billTextField.Text); // !
                }
            };
        }
Beispiel #22
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            TipCalculator calc = new TipCalculator();

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            TextView tipAmountResult        = FindViewById <TextView>(Resource.Id.tipAmountResult);
            EditText billAmountTextField    = FindViewById <EditText>(Resource.Id.billAmountText);
            EditText tipPercentageTextField = FindViewById <EditText>(Resource.Id.tipPercentageText);
            SeekBar  tipSeekBar             = FindViewById <SeekBar>(Resource.Id.tipSeekBar);


            Button calculateButton = FindViewById <Button>(Resource.Id.calculateButton);

            calculateButton.Click += (sender, e) =>
            {
                tipAmountResult.Text = calc.calculateTip(billAmountTextField.Text, tipPercentageTextField.Text);
            };

            billAmountTextField.TextChanged += (sender, e) =>
            {
                tipAmountResult.Text = calc.calculateTip(billAmountTextField.Text, tipPercentageTextField.Text);
            };

            tipPercentageTextField.TextChanged += (sender, e) =>
            {
                tipAmountResult.Text = calc.calculateTip(billAmountTextField.Text, tipPercentageTextField.Text);
            };

            tipSeekBar.ProgressChanged += (sender, e) =>
            {
                int tipPercentage = (int)tipSeekBar.Progress;

                tipPercentageTextField.Text = tipPercentage.ToString();
                tipAmountResult.Text        = calc.adjustTipPercentage(billAmountTextField.Text, tipPercentage);
            };
        }
Beispiel #23
0
        public void ResetCalculator_CalculatorPopulatedWithValidValues_CalculatorResetToOriginalState()
        {
            var tipCalculator = new TipCalculator
            {
                Total      = 10,
                TipPercent = 50
            };

            var newTipCalculator = new TipCalculator();

            tipCalculator.CalcTip();
            tipCalculator.Reset();

            Assert.AreEqual(newTipCalculator.GrandTotal, tipCalculator.GrandTotal);
            Assert.AreEqual(newTipCalculator.Tip, tipCalculator.Tip);
            Assert.AreEqual(newTipCalculator.TipPercent, tipCalculator.TipPercent);
            Assert.AreEqual(newTipCalculator.Total, tipCalculator.Total);
            Assert.AreEqual(newTipCalculator.NumberOfPersons, tipCalculator.NumberOfPersons);
            Assert.AreEqual(newTipCalculator.SavedGrandTotal, tipCalculator.SavedGrandTotal);
            Assert.AreEqual(newTipCalculator.SavedTip, tipCalculator.SavedTip);
            Assert.AreEqual(newTipCalculator.TotalPerPerson, tipCalculator.TotalPerPerson);
        }
 public void Initialize()
 {
     myCalculator         = new TipCalculator();
     myTipCalcTransaction = new TipCalcTransaction();
     myTipDatabase        = new TipDatabaseMock(new FileHelperMock());
 }
Beispiel #25
0
        public void CalculateTip_Returns_ExpectedTip(double tip, string rating, int actual)
        {
            var result = TipCalculator.CalculateTip(tip, rating);

            result.Should().Be(actual);
        }
Beispiel #26
0
        public void CalculateTip_Returns_NegativeOne_WhenUnknownRatingIsPassedIn(double tip, string rating, int actual)
        {
            var result = TipCalculator.CalculateTip(tip, rating);

            result.Should().Be(actual);
        }
 public MainPageViewModel()
 {
     _calculator = new TipCalculator();
 }
 public void SampleTest()
 {
     Assert.AreEqual(4, TipCalculator.CalculateTip(20, "Excellent"));
     Assert.AreEqual(3, TipCalculator.CalculateTip(26.95, "good"));
 }