2
\$\begingroup\$

Here's a small program to tell our employees the price of our diapers. Is there a way such as an int array or an optimized way to set brandLevelNumber & sizeLevelNumber to the selected items in the brandComboBox & the sizeComboBox?

Also, do you guys think there's a better way for the method calculatePricePerDiaper() to work than a switch within a switch, or any other suggestions at all? I just started coding about a year ago, so any feedback will be appreciated.

The only case for brandLevelNumber is 0 because I haven't finished it yet with the methodology I had in mind, but rather just wrote enough for that brandLevelNumber to work.

This information was gathered across various websites (Walmart.com, Target.Com, etc.) and compiled in Excel.

class Diaper
{

    public string brandLevel;
    public string sizeLevel;
    public int brandLevelNumber;
    public int sizeLevelNumber;

    public string[] brands = {"Earth's Best", "Huggies Snug & Dry", "Huggies Snugglers & Movers",
                              "Luvs", "Pampers Baby Dry", "Pampers Swaddlers & Cruisers", "Seventh Generation",
                        "Store"};
    public string[] size = {"Size 1", "Size 2", "Size 3", "Size 4", "Size 5", "Size 6 & 7",
                            "Size N","2T-3T", "3T-4T", "4T-5T","Big Kids" };
    public double count;
    public double price;
    private double pricePerDiaper;
    public double finalPrice;


    public double calculatePricePerDiaper()
    {

            switch (brandLevelNumber)
            {
                case 0:
                    switch (sizeLevelNumber)
                    {
                        case 1:
                            if (count > 0 && count < 50)
                                pricePerDiaper = 0.12;
                            if (count > 50 && count <= 160)
                                pricePerDiaper = 0.11;
                            if (count > 160)
                                pricePerDiaper = 0.10;
                            break;
                        case 2:
                            if (count > 0 && count <= 42)
                            pricePerDiaper = 0.13;
                            if (count > 42 && count <=105)
                            pricePerDiaper = 0.12;
                            if (count > 150)
                            pricePerDiaper = 0.11;
                            break;
                        case 3:
                             if (count > 0 && count <=36)
                            pricePerDiaper = 0.16;
                            if (count >36)
                            pricePerDiaper = 0.12;
                            break;
                        case 4:
                            if (count > 0 && count <=31)
                            pricePerDiaper = 0.18;
                            if (count > 31 && count <=85)
                            pricePerDiaper = 0.15;
                            if (count >85)
                            pricePerDiaper = 0.14;
                            break;
                        case 5:
                            if (count > 0 && count <=28)
                            pricePerDiaper = 0.21;
                            if (count > 28 && count <= 100)
                            pricePerDiaper = 0.17;
                            if (count > 100)
                            pricePerDiaper = 0.16;
                            break;
                        case 6:
                            if (count > 0 && count <=25)
                            pricePerDiaper = 0.24;
                            if (count > 25)
                            pricePerDiaper = 0.20;
                            break;
                        case 7:
                            if (count > 0 && count <=50)
                            pricePerDiaper = 0.12;
                            if (count > 50)
                            pricePerDiaper = 0.11;
                            break;
                        case 8:
                            if (count > 0 && count <=30)
                            pricePerDiaper = 0.23;
                            if (count >30 && count <= 160)
                            pricePerDiaper = 0.22;
                            break;
                        case 9:
                            if (count > 0 && count <=25)
                            pricePerDiaper = 0.26;
                            if (count > 25 && count <= 55)
                            pricePerDiaper = 0.25;
                            if (count > 61)
                            pricePerDiaper = 0.24;
                            break;
                        case 10:
                            if (count > 0 && count <=22)
                            pricePerDiaper = 0.32;
                            if (count > 22 && count <= 40)
                            pricePerDiaper = 0.31;
                            if (count > 40 && count <= 60)
                            pricePerDiaper = 0.29;
                            if (count>60)
                            pricePerDiaper = 0.27;
                            break;
                        case 11:
                            if (count > 0 && count <=20)
                            pricePerDiaper = 0.41;
                            if (count > 20 && count <= 37)
                            pricePerDiaper = 0.37;
                            if (count >37)
                            pricePerDiaper = 0.36;
                            break;
                    }
                break;
            }

        return pricePerDiaper;
    }
    public double calculatePrice(double pricePerDiaper, double count)
    {
        price = pricePerDiaper * count;
        return price;
    }

}


    public partial class Form1 : Form
    {
        Diaper item = new Diaper();
        public Form1()
        {
            InitializeComponent();
            brandComboBox.DataSource = item.brands;
            sizeComboBox.DataSource = item.size;
  }

    private void Form1_Load(object sender, EventArgs e)
    {
        //Make the comboboxes appear empty at initial load
        //brandComboBox.SelectedItem = null;
        //sizeComboBox.SelectedItem = null;
    }

    private void countTextBox_TextChanged(object sender, EventArgs e)
    {
        //So exception isn't thrown if user deletes countTextBox.Text
        string i = countTextBox.Text;
        if (double.TryParse(i, out item.count))
            item.count = Convert.ToDouble(countTextBox.Text);
    }

    private void doneButton_Click(object sender, EventArgs e)
    {
        item.finalPrice=(item.calculatePrice(item.calculatePricePerDiaper(), Convert.ToDouble(countTextBox.Text)));
        priceLabel.Text = "$" + item.finalPrice;
    }

    private void brandComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        item.brandLevel = (string)brandComboBox.SelectedItem;
        switch (item.brandLevel)
        {
            case "Huggies Snug & Dry":
            case "Pampers Baby Dry":
                item.brandLevelNumber = 0;
                break;
            case "Earth's Best":
            case "Huggies Snugglers & Movers":
            case "Pampers Swaddlers & Cruisers":
            case "Seventh Generation":
                item.brandLevelNumber = 1;
                break;
            case "Luvs":
                item.brandLevelNumber = 2;
                break;
            case "Store":
                item.brandLevelNumber = 3;
                break;
        }
    }

    private void sizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        item.sizeLevel = (string)sizeComboBox.SelectedItem;
        switch (item.sizeLevel)
        {
            case "Size 1":
                item.sizeLevelNumber = 1;
                break;
            case "Size 2":
                item.sizeLevelNumber = 2;
                break;
            case "Size 3":
                item.sizeLevelNumber = 3;
                break;
            case "Size 4":
                item.sizeLevelNumber = 4;
                break;
            case "Size 5":
                item.sizeLevelNumber = 5;
                break;
            case "Size 6 & 7":
                item.sizeLevelNumber = 6;
                break;
            case "Size N":
                item.sizeLevelNumber = 7;
                break;
            case "2T-3T":
                item.sizeLevelNumber = 8;
                break;
            case "3T-4T":
                item.sizeLevelNumber = 9;
                break;
            case "4T-5T":
                item.sizeLevelNumber = 10;
                break;
            case "Big Kids":
                item.sizeLevelNumber = 11;
                break;
        }
    }
  }
}
\$\endgroup\$
0
\$\begingroup\$

This fails on count = 50
You don't need the count > 50 && at all

case 1:
    if (count > 0 && count < 50)
        pricePerDiaper = 0.12;
    if (count > 50 && count <= 160)
        pricePerDiaper = 0.11;
    if (count > 160)
        pricePerDiaper = 0.10;
        break;

You should pass to brandLevelNumber and sizeLevelNumber to calculatePricePerDiaper()

You can assign a integer value to an enum
With this you can get rid of a switch

public enum Weighted { one = 1, two = 2, three = 3, four = 4, once = 1}
int value = (int)Weighted.one;  

All the integers must be unique

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.