I'm trying to create a booking form in C#, where a user can select multiple tickets for one or more attraction site. They can also add a promotion code for a 10% discount on the total price if the code contains an "X". I'm having trouble with calculating the total price before the promotion discount can be taken away. I thought I could make the default value of each attraction "0" so when the individual prices are added for dblPrice, but that doesn't work.
Here's what I have so far -
//declared variables
double dblPrice;
double dblDiscount;
double dblRollprice, dblWaterprice, dblKidsprice, dblLiveprice, dblActionprice, dblThrillprice, dbl4dprice;
double dblTotalprice;
string strRollquantity, strWaterquantity, strKidsquantity, strLivequantity, strActionquantity, strThrillquantity, str4dquantity;
int intWaterquantity, intRollquantity, intKidsquantity, intLivequantity, intActionquantity, intThrillquantity, int4dquantity;
string strPromocode;
strPromocode = txtPromocode.Text;
strRollquantity = txtRollquantity.Text;
strWaterquantity = txtWaterquantity.Text;
strKidsquantity = txtKidsquantity.Text;
strLivequantity = txtLivequantity.Text;
strActionquantity = txtActionquantity.Text;
strThrillquantity = txtThrillquantity.Text;
str4dquantity = txt4dquantity.Text;
int.TryParse(strRollquantity, out intRollquantity);
int.TryParse(strWaterquantity, out intWaterquantity);
int.TryParse(strKidsquantity, out intKidsquantity);
int.TryParse(strLivequantity, out intLivequantity);
int.TryParse(strActionquantity, out intActionquantity);
int.TryParse(strThrillquantity, out intThrillquantity);
int.TryParse(str4dquantity, out int4dquantity);
dblRollprice = 15.00;
dblWaterprice = 15.00;
dblActionprice = 15.00;
dblKidsprice = 15.00;
dblLiveprice = 15.00;
dbl4dprice = 15.00;
dblThrillprice = 15.00;
if (chkRoll.Checked)
{
dblRollprice = dblRollprice * intRollquantity;
}
if (chkWater.Checked)
{
dblWaterprice = dblWaterprice * intWaterquantity;
}
if (chkKids.Checked)
{
dblKidsprice = 15.00 * intKidsquantity;
}
if (chkLive.Checked)
{
dblLiveprice = 15.00 * intLivequantity;
}
if (chkAction.Checked)
{
dblActionprice = 15.00 * intActionquantity;
}
if (chkThrill.Checked)
{
dblThrillprice = 15.00 * intThrillquantity;
}
if (chk4d.Checked)
{
dbl4dprice = 15.00 * int4dquantity;
}
dblPrice = dblRollprice + dblWaterprice + dblThrillprice + dblLiveprice + dblActionprice + dbl4dprice + dblKidsprice;
if (strPromocode == "X")
{
dblDiscount = dblPrice / 100 * 10;
dblTotalprice = dblPrice - dblDiscount;
Response.Write("Price is: " + dblTotalprice + "<br />");
}
Related
Setting up a fairly simple undetermined linear system solution using Google OrTools (in c#). I'm having trouble constraining certain values to whole numbers. In the following code example, I'd like variables a,b,c, and d to be whole numbers, but cash1 and cash2 as doubles. Please ignore the poor variable naming!
Code:
// setup
double VTI = 221.17;
double BND = 81.92;
double CASH = 1;
double account1 = 10000;
double account2 = 5000;
double total = account1 + account2;
// allocation
var VTIAmount = total * .8;
var BNDAmount = total * .2;
var solver = Solver.CreateSolver("GLOP");
// variables
var a = solver.MakeIntVar(0.0, int.MaxValue, "a");
var b = solver.MakeIntVar(0.0, int.MaxValue, "b");
var cash1 = solver.MakeNumVar(0.0, double.MaxValue, "cash1");
var c = solver.MakeIntVar(0.0, int.MaxValue, "c");
var d = solver.MakeIntVar(0.0, int.MaxValue, "d");
var cash2 = solver.MakeNumVar(0.0, double.MaxValue, "cash2");
// constraints
solver.Add(a * VTI + b * BND + cash1 * CASH == account1);
solver.Add(c * VTI + d * BND + cash2 * CASH == account2);
solver.Add(a * VTI + c * VTI == VTIAmount);
solver.Add(b * BND + d * BND == BNDAmount);
// objective function
solver.Maximize(d);
// solve
solver.Solve();
// results
Console.WriteLine("Solution:");
Console.WriteLine("Objective value = " + solver.Objective().Value());
Console.WriteLine("a = " + a.SolutionValue());
Console.WriteLine("b = " + b.SolutionValue());
Console.WriteLine("c = " + c.SolutionValue());
Console.WriteLine("d = " + d.SolutionValue());
Console.WriteLine("cash1 = " + cash1.SolutionValue());
Console.WriteLine("cash2 = " + cash2.SolutionValue());
Output:
Solution:
Objective value = 36.62109375
a = 45.21408871004205
b = 0
c = 9.04281774200841
d = 36.62109375
cash1 = 0
cash2 = 0
Changing the code snippets in the OG post to:
var solver = Solver.CreateSolver("SCIP");
And this:
solver.Add(a * VTI + c * VTI >= VTIAmount*.99);
solver.Add(b * BND + d * BND >= BNDAmount*.99);
// objective function
solver.Maximize(d-cash1-cash2);
Solves the solution fairly elegantly:
Solution:
Objective value = 8.22
a = 44
b = 3
c = 10
d = 34
cash1 = 22.760000000000446
cash2 = 3.019999999999982
Hi I am trying to create a amortization schedule , which shows the EMI , Principal , Interest and the new Principal for next month. The problem is that the monthly principal instead of increasing keeps on decreasing. As far as from searching the net , I am doing the right calculations. What am I missing?
decimal principal = 312500;
decimal rate = 3.50M;
decimal EMI;
decimal monthlyInterest;
decimal monthlyPrincipal;
decimal newPrincipalBalance;
decimal downPayment = 62500;
decimal actualPrincipal = principal - downPayment;
for (int i = 0; i <= 24; i++)
{
Console.WriteLine("principal " + actualPrincipal);
EMI = Math.Round(monthlyPayments(actualPrincipal, rate, 30));
Console.WriteLine("EMI " + EMI);
monthlyInterest = actualPrincipal * rate / 12;
monthlyInterest = Math.Round((actualPrincipal * rate / 100) / 12);
Console.WriteLine("monthlyInterest " + monthlyInterest);
monthlyPrincipal = Math.Round(EMI - monthlyInterest);
Console.WriteLine("monthlyPrincipal " + monthlyPrincipal);
newPrincipalBalance = Math.Round(actualPrincipal - monthlyPrincipal);
Console.WriteLine("newPrincipalBalance " + newPrincipalBalance);
Console.WriteLine("===================================");
actualPrincipal = newPrincipalBalance;
}
}
public static decimal monthlyPayments(decimal actualPrincipal, decimal rate, int years)
{
rate = rate / 1200;
years = years * 12;
decimal F = (decimal)Math.Pow((double)(1 + rate), years);
return actualPrincipal * (rate * F) / (F - 1);
}
These are first few of my results where the monthly principal is decreasing
This is what the expected result is :
This is the formula for calculating the monthlyPayments
I found what I was doing wrong. As the EMI remains fixed for the entire period of the loan, it should have been kept outside the loop.
Console.WriteLine("principal " + actualPrincipal);
EMI = Math.Round(monthlyPayments(actualPrincipal, rate, 30));
Console.WriteLine("EMI " + EMI);
for (int i = 0; i <= 24; i++)
{
monthlyInterest = actualPrincipal * rate / 12;
monthlyInterest = Math.Round((actualPrincipal * rate / 100) / 12);
Console.WriteLine("monthlyInterest " + monthlyInterest);
monthlyPrincipal = Math.Round(EMI - monthlyInterest);
Console.WriteLine("monthlyPrincipal " + monthlyPrincipal);
newPrincipalBalance = Math.Round(actualPrincipal - monthlyPrincipal);
Console.WriteLine("newPrincipalBalance " + newPrincipalBalance);
Console.WriteLine("===================================");
actualPrincipal = newPrincipalBalance;
}
I use the following code to attempt to do a few calculations using values from two separate arrays. I've been trying to aggregate a list of values as a final step but every time I run the program it only has one set of values in the list. abcXLoopVars is a custom class to store the different variables which I aggregate later.
Parallel.For<abcXLoopVars>(0, colX.Count(),
() => { return new abcXLoopVars(); },
(i, pls, state) =>
{
state = new abcXLoopVars();
double x = Math.Abs(colX[i]);
double y = Math.Abs(colY[i]);
double lnx = Math.Log(x);
double lny = Math.Log(y);
double xminxbarsq = Math.Pow(colX[i] - xbar, 2);
double xminxbarcub = Math.Pow(colX[i] - xbar, 4);
state.sumxminxbarsq = xminxbarsq;
state.sumxminxbarcub = xminxbarcub;
state.sumlnxxminxbarsq = lnx * xminxbarsq;
state.sumlnxlny = lnx * lny;
state.sumlnxsq = Math.Pow(lnx, 2);
state.sumlnx = sumlnx + lnx;
state.sumlnyxminxbarsq = lny * xminxbarsq;
state.sumlny = lny;
state.posneg = colY[i] / colX[i];
return state;
},
(state) => { lock (lockMe) abxList.Add(state);}
);
I suggest you to rewrite it as PLINQ:
colX.Zip(colY, (x, y) =>
{
var state = new abcXLoopVars();
double lnx = Math.Log(x);
double lny = Math.Log(y);
double xminxbarsq = Math.Pow(x - xbar, 2);
double xminxbarcub = Math.Pow(x - xbar, 4);
state.sumxminxbarsq = xminxbarsq;
state.sumxminxbarcub = xminxbarcub;
state.sumlnxxminxbarsq = lnx * xminxbarsq;
state.sumlnxlny = lnx * lny;
state.sumlnxsq = Math.Pow(lnx, 2);
state.sumlnx = sumlnx + lnx;
state.sumlnyxminxbarsq = lny * xminxbarsq;
state.sumlny = lny;
state.posneg = y / x;
return state;
}).AsParallel().ToList();
My goal is to use GPS to measure the distance that I moved with my phone. My problem is, that the results are imprecise. I've used the following code to calculate the distance:
public double getDistance(GeoCoordinate p1, GeoCoordinate p2)
{
double d = p1.Latitude * 0.017453292519943295;
double num3 = p1.Longitude * 0.017453292519943295;
double num4 = p2.Latitude * 0.017453292519943295;
double num5 = p2.Longitude * 0.017453292519943295;
double num6 = num5 - num3;
double num7 = num4 - d;
double num8 = Math.Pow(Math.Sin(num7 / 2.0), 2.0) + ((Math.Cos(d) * Math.Cos(num4)) * Math.Pow(Math.Sin(num6 / 2.0), 2.0));
double num9 = 2.0 * Math.Atan2(Math.Sqrt(num8), Math.Sqrt(1.0 - num8));
return (6376500.0 * num9);
}
This is my OnLocationChanged implementation:
bool begin = true;
public void OnLocationChanged(Location location)
{
_aktuellerOrt = location;
//aktuellerOrt.Speed is always 0, so I cannot use that.
if (_aktuellerOrt == null)
{
//message
}
else
{
if (_aktuellerOrt.Accuracy > 70) //I found values around 130 to be more or less good
{
_locationText.Text = String.Format("{0}, {1}", _aktuellerOrt.Latitude, _aktuellerOrt.Longitude);
GeoJetzt = new GeoCoordinate();
GeoJetzt.Latitude = _aktuellerOrt.Latitude;
GeoJetzt.Longitude = _aktuellerOrt.Longitude;
if (beginn)
{
GeoVorher = new GeoCoordinate();
GeoVorher.Latitude = _aktuellerOrt.Latitude;
GeoVorher.Longitude = _aktuellerOrt.Longitude;
beginn = false;
}
else
{
double abstand = getDistance(GeoVorher, GeoJetzt);
weg += abstand;
if (weg >= 1)
_distanzText.Text = weg + " kilometers";
else
_distanzText.Text = (weg * 1000) + " meters";
_distanzText.Text += " (" + abstand + ", " + _aktuellerOrt.Accuracy + ")";
GeoVorher = new GeoCoordinate();
GeoVorher.Latitude = _aktuellerOrt.Latitude;
GeoVorher.Longitude = _aktuellerOrt.Longitude;
}
}
else
_locationText.Text = "Too coarse, now " + _aktuellerOrt.Accuracy + ".";
}
}
The problem is, I get values for abstand within the kilometer range while not moving the phone.
What are best practices for imprecise signals? My goal is to measure the distance while jogging or running, so 10 - 16 km/h.
for some reason this will compile but it comes out with an error at the end and I can't figure out why. The first part of the code is to display a table from a text file which works correctly, the second part doesn't.
I don't think it even gets to the Console.WriteLine bit, which was a way of checking whether it did. Can anyone see why?
Thanks for any help you can give!
class Program
{
static void Main(string[] args)
{
List<float> inputList = new List<float>();
TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
String input = Convert.ToString(tr.ReadToEnd());
String[] items = input.Split(',');
Console.WriteLine("Point Latitude Longtitude Elevation");
for (int i = 0; i < items.Length; i++)
{
if (i % 3 == 0)
{
Console.Write((i / 3) + "\t\t");
}
Console.Write(items[i]);
Console.Write("\t\t");
if (((i - 2) % 3) == 0)
{
Console.WriteLine();
}
}
Console.WriteLine();
Console.WriteLine();
// Ask for two bits of data which are then stored in Longtitude, Latitude and Elevation
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
string point = Console.ReadLine();
string[] pointInput = point.Split(' ');
int pointNumber = Convert.ToInt16 (pointInput[0]);
int pointNumber2 = Convert.ToInt16 (pointInput[1]);
int Latitude = (Convert.ToInt16(items[pointNumber*3]));
int Longtitude = (Convert.ToInt16(items[(pointNumber*3)+1]));
int Elevation = (Convert.ToInt16(items[(pointNumber*3)+2]));
int Latitude2 = (Convert.ToInt16(items[pointNumber2 * 3]));
int Longtitude2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 1]));
int Elevation2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 2]));
Console.WriteLine("Latitude");
Console.WriteLine("Latitude2");
You are using decimal values, which cannot be converted into Int16. So use float.
Also, Outputting "Latitude" will write the variable's name, not its value.
I modified your code:
float Latitude = (float.Parse(items[pointNumber*3]));
float Longtitude = (float.Parse(items[(pointNumber*3)+1]));
float Elevation = (float.Parse(items[(pointNumber*3)+2]));
float Latitude2 = (float.Parse(items[pointNumber2 * 3]));
float Longtitude2 = (float.Parse(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (float.Parse(items[(pointNumber2 * 3) + 2]));
Console.WriteLine(Latitude);
Console.WriteLine(Latitude2);
Your input strings are of floating values, not integers. You can parse to floats like this:
float Latitude = (Convert.ToSingle(items[pointNumber * 3]));
float Longtitude = (Convert.ToSingle(items[(pointNumber * 3) + 1]));
float Elevation = (Convert.ToSingle(items[(pointNumber * 3) + 2]));
float Latitude2 = (Convert.ToSingle(items[pointNumber2 * 3]));
float Longtitude2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 2]));