Answer to Question #41521 in C# for Mridul Mehendiratta

Question #41521
The application used to calculate the cost of carrying additional luggage results in erroneous amount, if the weight of the luggage is a fractional number. Help the development team modify the code snippet so that the cost of carrying additional luggage is calculated correctly.
1
Expert's answer
2014-05-05T14:27:13-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Question38623
{
class Program
{
static void Main(string[] args)
{
int pos = 3;
ConsoleKeyInfo key;
Console.WriteLine("Please select your travelling class: \n(use arrows keys UP, DOWN , ENTER - to submit, ESC - Exit)\n");
Console.WriteLine("A - 40kg max *");
Console.WriteLine("B - 25kg max");
Console.WriteLine("C - 15kg max");
do
{
key=Console.ReadKey(true);
switch(key.Key)
{
case ConsoleKey.UpArrow:
Console.SetCursorPosition(18, pos);
Console.Write(" ");
if (pos == 3) pos = 5;
else pos--;
Console.SetCursorPosition(18, pos);
Console.Write("*");
break;
case ConsoleKey.DownArrow:
Console.SetCursorPosition(18, pos);
Console.Write(" ");
if (pos == 5) pos = 3;
else pos++;
Console.SetCursorPosition(18, pos);
Console.Write("*");
break;
case ConsoleKey.Escape: Environment.Exit(0); break;
}
} while (key.Key != ConsoleKey.Enter);

Console.SetCursorPosition(0,8);
Console.Write("Please enter weight of your luggage: (WHOLE number only)\n");
string s_weight ;
long weight;
bool pass;

s_weight=Console.ReadLine();
pass = Int64.TryParse(s_weight, out weight);
if (!pass||weight<0)
{
Console.WriteLine("\nWrong Input. Good Bye");
Console.ReadKey();
Environment.Exit(0);
}
switch (pos)
{
case 3:
if (weight > 40)
{
Console.WriteLine("Additional payment " + ((weight - 40) * 2) + "$");
}
else
{
Console.WriteLine("No Additional payment");
}
break;
case 4:
if (weight > 25)
{
Console.WriteLine("Additional payment " + ((weight - 25) * 2) + "$");
}
else
{
Console.WriteLine("No Additional payment");
}
break;
case 5:
if (weight > 15)
{
Console.WriteLine("Additional payment " + ((weight - 15) * 2) + "$");
}
else
{
Console.WriteLine("No Additional payment");
}
break;
}
Console.Read();
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS