Hello,

I am taking my first basic computer programming course. We are learning how to use C# in Visual Studio. Recently we started learning about classes and how they work in OOP. I get the idea of what they are and what they do. I am having a problem writing the code that is needed for my homework assignment. Here is the assignment.

A local carpenter creates desks according to customers orders. The cost of the desk is based on the following:
1. the desk length and width in inches
2. the type of wood
3. the number of drawers

The price is calculated as follows:
1 Minimum charge of 250.00
2.If surface area is greater than 720 square inches, there is an additional 50.00 added.
3. The charge for different kinds of wood are as follows:
a. Pine - no charge
b. Mahogany - 160.00
c. Oak - 110.00
4. The basic desk has no drawers. For each drawers added, there is an additional 35.00 charge
5. There is a sales tax of 9%

THE DESK CLASS
1. Create an object oriented class named Desk that has fields for the following:
a. desk length
b. desk width
c. type of wood
d. number of drawers
e. cost for mahogany
f. cost for oak
g. surface charge
h. drawer charge
i. total cost of desk

  1. Create properties for each field as required.
    a. the property for the total cost of the desk should be read only
  2. Create a Method to calculate the cost of the desk
  3. Note:
    a. All calculations are done by the Desk class
    b. No user inputs our outputs are done by the Desk class

THE PROGRAM CLASS:
1. Accepts user inputs for all values required to determine the final cost of the desk. Each input must be preceded by a prompt
2. Creates and uses an object of the Desk class to
a. pass the inputs to the correct properties of the object
b. retrieve the total cost of the desk from the object
3. Displays the user inputs as well as the final cost of the Desk.
4. Note:
a. The program class provides for all inputs/outputs of the program
b. The program class does no calculations.

HERE IS THE CODE I HAVE WRITTEN FOR THE PROGRAM CLASS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            Desk newDesk = new Desk();


            Console.WriteLine("Please enter the length you would like the desk to be in inches.");
            newDesk.DeskLength = Convert.ToInt32 (Console.ReadLine());

            Console.WriteLine("Please enter the width you would like the desk to be in inches.");
            newDesk.DeskWidth = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("What type of wood would you like? Pine, Mahogany (+$160.00), or Oak (+$110.00).");
            newDesk.DeskWood =Convert.ToString( Console.ReadLine());

            Console.WriteLine("There is a $30.00 charge for each drawer. How many drawers would you like?");
            newDesk.DeskDrawers = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Total Cost is,{0}", newDesk.TotalCost );


            Console.ReadKey();


        }
    }


}

HERE IS THE CODE I HAVE WRITTEN FOR THE DESK CLASS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Desk
    {
        int deskLength;
        int deskWidth;
        string deskWood;
        int deskDrawers;
        int mahogcost = 160;
        int oakCost = 110;
        int surfaceCharge;
        int drawerCost;
        double taxAmt = 0.09;
        int totalCost;




        public int DrawerCost
        {
            get{return drawerCost;}
            set{drawerCost = value;}
        }




        public int DeskLength
        {
            get { return deskLength; }
            set { deskLength = value;
                  CalculateAreacost(); }
        }



        public int DeskWidth
        {
            get { return deskWidth; }
            set { deskWidth = value;
                  CalculateAreacost(); }
        }



        public string DeskWood
        {
            get { return deskWood; }
            set { deskWood = value; }
        }


        public int DeskDrawers
        {
            get { return deskDrawers; }
            set { deskDrawers = value;
                  CalculateDrawer(); }
        }



        public int SurfaceCharge
        {
            get { return surfaceCharge; }
            set { surfaceCharge = value; }
        }



        public void CalculateDrawer()
        {
            drawerCost = 30 * DeskDrawers;
        }

        public void CalculateAreacost()
        {
            if (DeskLength * DeskWidth > 720) surfaceCharge = 50;
            else surfaceCharge = 0;
        }



    }



    }

I had my friend help me with a few of the things, he is no longer available though. The main issue I am running into is
1. Creating code that takes the user input given for the wood type and assigning it to a value such as 110 for oak or 160 for mahogany.
2. How to create a method in the Desk class that adds wood type, drawer charge, surface charge, 250.00, 9% sales tax. Then being able to take the total amount and return it and all original user inputs back to main and be able to display them.

Every time I believe to be making progress and write some code. I go to test it in main by plugging it into a readline. Based on user inputs it should calculate a total, however I always yield 0. The reason I posted the full assignment is to see what my specific instructions are. I don't believe I should have as many variables declared in the Desk class. But based upon the instructions, I feel I have to?

Problem One

We could firstly here use a switch statement in the Program class, here is some sudo code for it:

output "Which wood would you like?"
output "1) Mahogany - £160"
output "2) Oak - £110"
output "Choice: "
switch statement using the read input of number 1 or 2
option 1
    set deskWood accordingly
option 2
    set deskwood accordingly

Once you've done this your then left with two guarenteed string values, one for Mahogany and one for Oak. You can then check which one of these it is and apply the appropriate cost.

Problem Two

Make a method in the Desk class that is public (allowing Program class to call it) and returns a double (allows decimal point for currency). This will then return the value of the total of all costs to the program class allowing you to output them.

-

I will happily provide help fixing any code you write from the sudo code above, just didn't want to directly provide answers as it is homework.

commented: Beat me to it, switch statement would be better in... case too. +4

The best thing to do is to use a comboBox with predefined values which you can set and the user will choose from. This limits the amount user input and reduces the chance of human error.

You should use the predefined method called 'SelectedIndexChanged' (this comes built-in to the object so you just need to select it) when the user selects the type of wood they want. In this method do some if statement to check the wood the user has selected and if the condition is true, store the price of the select in a decimal or double variable like below:

Decimal woodPrice = 0.0;

myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    string woodType = myComboBox.SelectedIndex.ToString();
    if (woodType == "Oak")
    {
        woodPrice = 100.00;
    }
    Else if (woodType == "Pine")
    {
        woodPrice = 150.00;
    }
}

As for your second issue it's a case of working the formular out as though you were doing it on a calculator (i.e. to work out 9% of the total price you do ((Total/100)*9)).

@Chris - Only issue with a combo box is looking at the provided code would lead me to the assumption this is a Console application and thus isn't applicable. (WriteLine calls etc, and the namespace, and the using clauses) ;p

True I musnt have paid attention to the code when I replied, my apologies.

Haha no issue whatsoever :) that would be the best approach from a WinForms point of view and is definately still worth knowing for the OP in their future practices.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.