I have written the following C# code and am getting an error in the if/elseif lo
ID: 3671376 • Letter: I
Question
I have written the following C# code and am getting an error in the if/elseif loops in the InputWood() class. The error states, "The name 'typeInput' does not exist in the current context."
I can't seem to figure out how to debug this issue. Below are the parameters for the question, and thank you for any help!
Create a console-based application that computes the price of a desk and whose Main() method calls four other methods:
A method to accept the number of drawers in the desk as input from the keyboard. This method returns the number of drawers to the Main() method.
A method to accept as input and return the type of wood - 'm' for mahogany, 'o' for oak, or 'p' for pine.
A method that accepts the number of drawers and wood type, and calculates the cost of the desk based on the following: Pine desks are $100. Oak desks are $140. All other woods are $180. A $30 surcharge is added for each drawer. This method returns the cost to the Main() method. A method to display all the details and the final price.
using System;
namespace Desks
{
class DeskPurchase
{
static void Main(string[] args)
{
}
public static double DrawerInput()
{
int input;
string numberDrawerString;
Console.Write("Enter number of drawers >> ");
numberDrawerString = Console.ReadLine();
input = Convert.ToInt32(numberDrawerString);
return input;
}
public static int woodType()
{
string typeWood;
char typeInput;
Console.Write("Enter type of wood - m, o, or p >> ");
typeWood = Console.ReadLine();
typeInput = Convert.ToChar(typeWood);
return typeInput;
}
public static int InputWood()
{
int type;
const int mohogany = 180;
const int pine = 100;
const int oak = 140;
if (typeInput == 'p')
{
type = pine;
}
else if (typeInput == 'o')
{
type = oak;
}
else
{
type = mohogany;
}
return type;
}
}
}
Explanation / Answer
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int intDrawersPerDesk = 0;
int intAllDesks = 0;
//1. checking if textBox of wood type is empty:
string strWoodType = textBoxWoodType.Text;
if (strWoodType != String.Empty)
{
//2. checking if its a number for number of desks
if (int.TryParse(textBoxDrawsPerDesk.Text.Trim(), out intDrawersPerDesk))
{
//3. checking if its a number for number of drawers
if (int.TryParse(textBoxAllDesks.Text.Trim(), out intAllDesks))
{
//4. calculations, and checking of any of the numbers are zero
int intAllDrawers = NumberOfDrawers(intDrawersPerDesk, intAllDesks);
if (intAllDrawers > 0)
{
//5. checking exact wood type
string strShortWoodType = WoodType(strWoodType);
if (strShortWoodType != "error")
{
float fTotal = CalculateTotalCost(strWoodType, intAllDrawers);
MessageBox.Show("The total code is " + fTotal + ".");
}
else
MessageBox.Show("Please enter the correct wood type (mahogany, oak or pine).");
}
else
MessageBox.Show("Sum of all drawers is zero. Please higher the number of desk or drawers.");
}
}
}
else
MessageBox.Show("Please enter the correct wood type.");
}
private int NumberOfDrawers(int intDrawersPerDesk, int intAllDesks)
{
return intDrawersPerDesk * intAllDesks;
}
public string WoodType(string strTreeType)
{
switch (strTreeType)
{
case "mahogany":
{
strTreeType = "m";
break;
}
case "oak":
{
strTreeType = "o";
break;
}
case "pine":
{
strTreeType = "p";
break;
}
default:
{
strTreeType = "error";
break;
}
}
return strTreeType;
}
private float CalculateTotalCost(string type, int intAllDrawers)
{
float fCost = 0f;
if (type == "p")
fCost = 100 * intAllDrawers;
else if (type == "o")
fCost = 140 * intAllDrawers;
else
fCost = 180 * intAllDrawers;
//adding a surcharge for each drawer:
fCost = fCost + (intAllDrawers * 30);
return fCost;
}
}