In: Computer Science
IN BASIC C#
If It Fits, It Ships! OVERVIEW In this program you will be calculating the total amount the user will owe to ship a rectangular package based on the dimensions and we will add in insurance based on the prices of the items inside of the package.. INSTRUCTIONS: 1. Welcome the user and explain the purpose of the program. 2. In the Main, prompt the user for the width, length and height of their package. a. Each of the 3 variables should be validated and converted. 3. Create a custom function called SortByDimensions a. This function needs 3 parameters: the width, length and height. b. The function will be returning the number of what tier pricing is going to be used. c. Inside of the function, use a series of conditionals and logical operators to determine the tier based on the chart below. d. Return this integer of the matching tier to the main. i. Make sure to catch this in a variable of the correct type. 4. Back in the Main, let the user know what Tier for dimensions the package is. 5. In the Main, prompt the user for a comma separated text string of the prices of each item in the package. a. Remind the user NOT to put in the $. i. I.e. 5.60, 9.99, 10.50 b. Validate that this text string is not left blank. c. Once you have this text string, split about this string into a string array. d. Each element should be one price of an item. TIER # Requirements 1 All dimensions must be 10” and under. 2 Largest of the dimension is between 10” and 48”. 3 Largest of the dimensions is 48” or above. 6. Create a custom function called CalcInsurance. a. This function needs a parameter of the string array of item prices. b. The function will return the cost of the insurance for the package. c. Inside of this function a variable that will hold the total cost of all of the items. d. You will also need a variable to hold the cost of the insurance. e. Loop through the item string array and one at a time, convert each item to a number data type i. Add this price to the variable that holds the total cost. ii. After the loop, you should have a variable that holds the total of all items inside the package. f. Use the chart below to setup a conditional block that will give you the cost of the Insurance for this package. g. Return this cost of insurance to the Main. i. Make sure to catch this in a variable of the correct type. Total Cost of Items In Package Cost Of Insurance $0.00 to and including $10.00 $1.00 Up to $250.00 $5.00 Up to $500.00 $10.00 $500.00 and above $25.00 7. Create a 3rd custom function called TotalShippingCost. a. This function will have 2 parameters: the tier number based on the dimensions and the total cost of the insurance. b. The function will return the total of the shipping cost. c. In the function create a conditional block the will set the shipping base cost. i. This will be based on the chart below. d. Once you have the base price, add in the insurance to create the total shipping cost. e. Return this total for the shipping to the Main. i. Make sure to catch this in a variable of the correct type. 8. Now in the Main you will output to the user the final results. a. It should be in the following format. i. “Your package will cost $X to ship, which includes the $Y insurance price.” 1. Where X is the total cost to ship the package formatted for money and rounded to 2 decimal places. 2. Where Y is the insurance cost formatted for money and rounded to 2 decimal places. TIER # Base Shipping Cost 1 $6.25 2 $15.50 3 $30.00 9. Test, Test, Test a. Test your code at the very least 2 times and put your Test Results in a multi-lined comment at the end of your Main Method. b. Here is one data set that you should test with to make sure your math is correct. i. Package Dimensions: 1. 2. 3. 4. ii. Prices 1. 2. Width = 5 Length = 12 Height =2 For troubleshooting you could check the Tier # is 2. of items in package: 50.50, 25.00, 6.00, 150.00 For troubleshooting you could check the total is 231.50. iii. Final Output should be: 1. “Your package will cost $20.50 to ship, which includes the $5.00 insurance price.” 10. When you are finished zip your whole folder and submit it on FSO.
Screenshot
-------------------------------------------------------------
Program
using System;
namespace ShippingCharge
{
class Program
{
//Method to print
welcome message and instructions
static public void
welcome()
{
Console.WriteLine(" WELCOME TO SHIPPING
SERVICE Instructions:-");
Console.WriteLine("1.Prompt for dimensions of rectangular box in
inches 2.Prompt for item prices in the box, You have to enter comma
separated string. 3.Display total charge with insurance");
}
//Method to find out the
tier count
//First check all
dimensionas within 10 inch range then tier1
//Otherwise find large
dimension
//Large dimension within
10-48 then tier2
//Else tier3
static public int
SortByDimensions(double l,double w,double h)
{
if(l<=10 && w<=10 && h <= 10)
{
return 1;
}
else
{
double max = l > w ? (l > h ? l : h) : (w > h ? w :
h);
if(max>10 && max < 48)
{
return 2;
}
else
{
return 3;
}
}
}
//Method to calculate
insurance
//Take item prizes
array
//Calculate total
//Total within 10 ther
1.00 for insurance
//within 10 -250 then
5.00
//Within 250-500 then
10.00
//Otherwise 25.00
static public double
CalcInsurance(string[] arr)
{
double total = 0;
for(int i = 0; i < arr.Length; i++)
{
total += Convert.ToDouble(arr[i]);
}
if(total>=0 && total <= 10)
{
return 1.00;
}
else if (total >10 && total <=250)
{
return 5.00;
}
else if (total >250 && total <= 500)
{
return 10.00;
}
else
{
return 25.00;
}
}
//Method to find total
charge for shipping
//First calculate base
rate according to tier values
//For tier1=6.25
//For ter2=15.00
//For tier3=30.00
//return
total=base+insurance
static public double
TotalShippingCost(int tier,double ins)
{
double basicRate = 0;
if (tier == 1)
{
basicRate = 6.25;
}
else if (tier == 2)
{
basicRate = 15.50;
}
else
{
basicRate = 30.00;
}
return (basicRate + ins);
}
//Test
static void
Main(string[] args)
{
//Method to display welcome message
welcome();
//Prompt for length
Console.Write(" Enter Length: ");
double length = Double.Parse(Console.ReadLine());
//Length validation
while (length <= 0)
{
Console.WriteLine("Error!!!Length should be positive.
Re-enter");
Console.Write("Enter Length: ");
length = Double.Parse(Console.ReadLine());
}
//Prompt for width
Console.Write("Enter Width: ");
double width = Double.Parse(Console.ReadLine());
//Width validation
while (width <= 0)
{
Console.WriteLine("Error!!!Width should be positive.
Re-enter");
Console.Write("Enter Width: ");
width = Double.Parse(Console.ReadLine());
}
//Prompt for height
Console.Write("Enter Height: ");
double height = Double.Parse(Console.ReadLine());
//Height validation
while (height<= 0)
{
Console.WriteLine("Error!!!Height should be positive.
Re-enter");
Console.Write("Enter Height: ");
height = Double.Parse(Console.ReadLine());
}
//Get tier number
int tier = SortByDimensions(length, width, height);
//Prompt for item price
string prices = "";
Console.Write("Enter prices of item(using commato separate prices
and don't put $): ");
while(string.IsNullOrEmpty(prices=Console.ReadLine()))
{
Console.WriteLine("Error!!!Not enter blank.Please re-enter");
Console.Write("Enter prices of item(using comma to separate prices
and don't put $): ");
}
//Split item price and store into a string array
string[] itemPrices= prices.Split(',');
//Get insurance amount
double insuranceAmt=CalcInsurance(itemPrices);
//Get total charge
double totalCharge = TotalShippingCost(tier, insuranceAmt);
//Display charge
Console.WriteLine(" Your package will cost $" +
totalCharge.ToString("#.##") + " to ship, which includes the $" +
insuranceAmt.ToString("#.##") + " insurance price.");
}
}
}
-------------------------------------------------------
Output
WELCOME TO SHIPPING SERVICE
Instructions:-
1.Prompt for dimensions of rectangular box in inches
2.Prompt for item prices in the box, You have to enter comma
separated string.
3.Display total charge with insurance
Enter Length: 5
Enter Width: 12
Enter Height: 2
Enter prices of item(using comma to separate prices and don't put
$): 50.50,25.00,6.00,150.00
Your package will cost $20.5 to ship, which includes the $5 insurance price.