16,000,645 members

See more:

I have this program and I need it to use the prices I've declared multiplied by the number of rooms a customer needs multiplied by the amount of days they need the room(s). I've declared all the room prices as form level variables and all the calculations I need are in the region Calculations (basically one button).

Currently the only room that is being calculated correctly is the Standard Double room size. I cannot figure out why the program is returning $0 for the other three rooms. Any help is appreciated!

Currently the only room that is being calculated correctly is the Standard Double room size. I cannot figure out why the program is returning $0 for the other three rooms. Any help is appreciated!

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace RoomLogic { public partial class Form1 : Form { private const decimal StandardDoubleRate = 189.99M; //price for a standard double room private const decimal DeLuxeDoubleRate = 199.99M; //price for a DeLuxe double room private const decimal LuxuryDoubleRate = 229.99M; //price for a Luxury Double room private const decimal LuxuryKingRate = 339.99M; //price for a Luxury King rooom //sales tax for the state private const decimal SalesTaxRate = 0.0825M; //current rate private decimal StandardRate, DeLuxeRate, DoubleRate, KingRate, TotalRoomsRate; //date time variables to get the check in and check out dates private DateTime DateIn; private DateTime DateOut; //variables to hold the number of children and adults in each room private decimal AdultsTotal; private decimal ChildrenTotal; private decimal TotalRooms; private decimal ExtraAdultCost; private decimal TotalCost; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Text = this.Text + " " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortDateString(); dtpIn.Value = DateTime.Today; //shows the check in date time picker as today's date dtpOut.Value = dtpIn.Value.AddDays(1); //shows the check out date time picker as today's date. }//end form load event //check changed event private void chkStandard_CheckedChanged(object sender, EventArgs e) { StandardRate = StandardDoubleRate * numStandard.Value; } private void chkDeLuxe_CheckedChanged(object sender, EventArgs e) { DeLuxeRate = DeLuxeDoubleRate * numDeLuxe.Value; } private void chkDouble_CheckedChanged(object sender, EventArgs e) { DoubleRate = LuxuryDoubleRate * numDouble.Value; } private void chkKing_CheckedChanged(object sender, EventArgs e) { KingRate = LuxuryKingRate * numKing.Value; } # region Calculations public void btnCalc_Click(object sender, EventArgs e) { //clears list for neatness each time the room rate is calculated lstOutput.Items.Clear(); long Days; decimal CostDays; /* All calculations for room cost will take place inside this button. * Steps: * 1. Calc the number of days the people will be staying in the room(s). * 2. Calcuate the cost of the rooms(s) based on that. * * calculate the number of elasped days and return a long integer to calculate number of days from start to finish. */ DateIn = dtpIn.Value; DateOut = dtpOut.Value; Days = DateOut.Subtract(DateIn).Days; MessageBox.Show("Total days = " + Days.ToString()); MessageBox.Show("Standard Total: " + StandardRate); MessageBox.Show("DeLuxe Total: " + DeLuxeRate); MessageBox.Show("King Total: " + KingRate); //calculate the Room cost based on days needed TotalRoomsRate = StandardRate + DeLuxeRate + DoubleRate + KingRate; CostDays = TotalRoomsRate * Days; AdultsTotal = this.numAdults.Value; ChildrenTotal = this.numChildren.Value; decimal TotalPeople = AdultsTotal + ChildrenTotal; TotalRooms = numStandard.Value + numDeLuxe.Value + numDouble.Value + numKing.Value; //MessageBox.Show("Total Rooms = " + TotalRooms.ToString()); Working //if statment to decide if enough rooms are purchased for the party. if (AdultsTotal > 4 & TotalRooms < 2) { MessageBox.Show("Must reserve/purchase additional rooms to accomodate party amount."); } else if (ChildrenTotal > 4 & TotalRooms < 2) { MessageBox.Show("Must reserve/purchase additional rooms to accomodate party amount."); } if (AdultsTotal > 2 & AdultsTotal < 5 & TotalRooms < 2) { ExtraAdultCost = (AdultsTotal - 2) * 10; } else if (AdultsTotal > 4 & AdultsTotal < 9 & TotalRooms < 3) { ExtraAdultCost = (AdultsTotal - 4) * 10; } else if (AdultsTotal > 6 & AdultsTotal < 13 & TotalRooms < 4) { ExtraAdultCost = (AdultsTotal - 6) * 10; } TotalCost = ((CostDays + ExtraAdultCost) * SalesTaxRate) + CostDays + ExtraAdultCost; lstOutput.Items.Add("The total room cost for your stay is: " + CostDays.ToString("C")); lstOutput.Items.Add("The cost for extra adults staying is: " + ExtraAdultCost.ToString("C")); lstOutput.Items.Add("The total cost of your stay is: " + TotalCost.ToString("C")); #endregion } } }

I don't see anywhere in your code where you are actually calculating the total cost of the room that is booked. You add a number of uninitialised variables together and multiply by the number of days which looks to provide a total of zero. Your calculation needs to compute the room cost times the number of days times the number of adults (if applicable) for each booking.

Permalink

Share this answer

If you skip the checkboxes that initiate the calculations of the room rates:

(these)

and instead put these calculations in the btncalc function it works.

so btnCalc should start like this:

(well apart from being a day of :) )

the date calculation should look like this:

DateOut.Date.Subtract(DateIn.Date).Days

Furthermore it's not good practice to have rates + vat hardcoded into the program. (Every time the rate changes you have to change it in the program and recompile...)

These should come from an external source -(file/database etc)

(these)

C#

private void chkStandard_CheckedChanged(object sender, EventArgs e) { StandardRate = StandardDoubleRate * numStandard.Value; }

and instead put these calculations in the btncalc function it works.

so btnCalc should start like this:

C#

public void btnCalc_Click(object sender, EventArgs e) { //clears list for neatness each time the room rate is calculated lstOutput.Items.Clear(); long Days; decimal CostDays; StandardRate = StandardDoubleRate * numStandard.Value; DeLuxeRate = DeLuxeDoubleRate * numDeLuxe.Value; DoubleRate = LuxuryDoubleRate * numDouble.Value; KingRate = LuxuryKingRate * numKing.Value;

(well apart from being a day of :) )

the date calculation should look like this:

DateOut.Date.Subtract(DateIn.Date).Days

Furthermore it's not good practice to have rates + vat hardcoded into the program. (Every time the rate changes you have to change it in the program and recompile...)

These should come from an external source -(file/database etc)

Permalink

Share this answer

v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900