C#

C# IF ListBox Arrays 2D Arrays Read File Example 1 Example Midterm 1 Example Midterm 2 Example 5 Example 7 Group Project 2 Example 9 Group Project 2
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

C# : Group Project 2

2017-01-01

Group Project 2


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace project1
{
    public partial class AwsomePizzaForm : Form
    {
        //Constants for prices
        const double PIZZA_PRICE = 10.00;
        const double SALAD_PRICE = 6.00;
        const double TAX = 0.08;

        const double PEPPERONI_PIZZA_PRICE = 10.00;
        const double MUSHROOM_PIZZA_PRICE = 11.00;
        const double GARDEN_SALAD_PRICE = 5.00;
        const double CASEAR_SALAD_PRICE = 6.00;
        double subtotal;
        double total;

        public AwsomePizzaForm()
        {
            InitializeComponent();
            toggleSections(1);
        }

        private double calculate_tax()
        {
            return subtotal * TAX;
        }

        private double calculate_total()
        {
            return subtotal + calculate_tax();
        }

        private int randomNumber()
        {
            Random rand = new Random();
            int number = rand.Next(100);
            return number;
        }

        private void calculateSubtotal()
        {

            subtotal = 0;

            foreach (var listBoxItem in lb_orderQuantity.Items)
            {

                //MessageBox.Show(listBoxItem.ToString());

                string item = listBoxItem.ToString().Substring(0, 3);

                switch (item)
                {
                    case "Pep": subtotal = subtotal + PEPPERONI_PIZZA_PRICE; break;
                    case "Mus": subtotal = subtotal + MUSHROOM_PIZZA_PRICE;  break;
                    case "Gar": subtotal = subtotal + GARDEN_SALAD_PRICE;  break;
                    case "Cae": subtotal = subtotal + CASEAR_SALAD_PRICE;  break;
                }

            }

            //MessageBox.Show("subTotal: $" + subtotal);

        }

        private void generateReceipt()
        {
            ReceiteLabel.Text = "Sub Total: $" + subtotal + "

Tax: $" + calculate_tax() + "

Total: $" + calculate_total();
        }

        private void toggleSections(int x)
        {

                locationAndHoursTitleLabel.Visible = false;
                locationAndHourLabel.Visible = false;
                jobOpeningsLabel.Visible = false;
                jobOpeningListBox.Visible = false;
                applyButton.Visible = false;
                label_aboutus.Visible = false;

            if (x == 1)
            {
                label_aboutus.Visible = true;
            }
            if (x == 2)
            {
                locationAndHoursTitleLabel.Visible = true;
                locationAndHourLabel.Visible = true;
            }
            if (x == 3)
            {
                jobOpeningsLabel.Visible = true;
                jobOpeningListBox.Visible = true;
                applyButton.Visible = true;
            }

        }

        //-------

        private void chefJobButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Congradulations! You have applied for chef job");
        }

        private void cashierJobButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Congradulations! You have applied for cashier job");

        }

        private void TotalLabel_Click(object sender, EventArgs e)
        {

        }

        private void OrderButton_Click(object sender, EventArgs e)
        {
            //Declaring and Initiliazing variables
            double pizzaAmount = 0, saladAmount = 0, total = 0, taxTotal = 0, pizzaTotal = 0, saladTotal = 0, subTotal = 0;

            //Try-catch
            try
            {
                //Try.Parse for Pizza amount
                if (double.TryParse(PizzaTextBox.Text, out pizzaAmount))
                {
                    //Try.Parse for Salad amount
                    if (double.TryParse(SaladTextBox.Text, out saladAmount))
                    {
                        //Calculate totals
                        pizzaTotal = PIZZA_PRICE * pizzaAmount;
                        saladTotal = SALAD_PRICE * saladAmount;

                        //Receipt calculations
                        total = (pizzaTotal + saladTotal);
                        subTotal = total;
                        taxTotal = total * TAX;
                        total += taxTotal;

                        //Output to labels
                        TotalLabel.Text = total.ToString("c");
                        ReceiteLabel.Text = "
Pizza count ($10 each): " + pizzaAmount + "

Salad count ($6 each): " + saladAmount +
                            "

Subtotal: $" + subTotal + "

Tax: $" + taxTotal + "

Total: $" + total;
                    }
                    else
                    {
                        MessageBox.Show("Please input a positive integer into Salad Amount"); //error for salad input
                    }
                }
                else
                {
                    MessageBox.Show("Please input a positive integer into Pizza Amount"); //error for pizza input
                }
            }
            catch
            {
                MessageBox.Show("Something went wrong! Please try again."); //overall catch
            }

        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            //Apply to positions button
            if (jobOpeningListBox.SelectedIndex != -1)
            {
                MessageBox.Show("Congratulations! You applied to the " + jobOpeningListBox.SelectedItem.ToString() + " position"); //Job Application message
            }
            else
            {
                MessageBox.Show("Please select a job opening."); //error message
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            //Clear all labels/textboxes
            PizzaTextBox.Text = "";
            SaladTextBox.Text = "";
            PizzaTextBox.Focus();
            jobOpeningListBox.ClearSelected();
            TotalLabel.Text = "";
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            //Close program
            this.Close();
        }

        private void button_addsalad_Click(object sender, EventArgs e)
        {

            if (listBox_salad.SelectedIndex != -1)
            {
                lb_orderQuantity.Items.Add(listBox_salad.SelectedItem);
            }
            else
            {
                MessageBox.Show("Please select a salad to add.");
            }

        }

        private void button_pizza_Click(object sender, EventArgs e)
        {
            if (listBox_pizza.SelectedIndex != -1)
            {
                lb_orderQuantity.Items.Add(listBox_pizza.SelectedItem);
            }
            else
            {
                MessageBox.Show("Please select a pizza to add.");
            }
        }

        private void button_calculateButton_Click(object sender, EventArgs e)
        {
           calculateSubtotal();
           generateReceipt();

        }

        private void button_remove_Click(object sender, EventArgs e)
        {
            if (lb_orderQuantity.SelectedIndex != -1)
            {
                lb_orderQuantity.Items.Remove(lb_orderQuantity.SelectedItem);
            }
            else
            {
                MessageBox.Show("Please select the item you want to remove.");
            }
        }

        private void button_aboutus_Click(object sender, EventArgs e)
        {
            toggleSections(1);
        }

        private void button_locations_Click(object sender, EventArgs e)
        {
            toggleSections(2);
        }

        private void button_careers_Click(object sender, EventArgs e)
        {
            toggleSections(3);
        }

        private void button_prize_Click(object sender, EventArgs e)
        {
            int rando = randomNumber();

            if (rando == 12)
            {
                label_prize.Text = "You won a free pizza!";
            } else
            {
                label_prize.Text = "Sorry your number " + rando + " did not win";
            }

        }

        private void button_credit_Click(object sender, EventArgs e)
        {
            // MessageBox.Show("This will show a message from the file credit.txt");

            try
            {
                string credits;
                StreamReader inputFile;
                inputFile = File.OpenText("credits.txt");
                credits = inputFile.ReadLine();
                while (!inputFile.EndOfStream)
                {
                    credits += "
" + inputFile.ReadLine();
                }

                MessageBox.Show(credits);
                inputFile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button_clear2_Click(object sender, EventArgs e)
        {
            listBox_pizza.SelectedIndex = -1;
            listBox_salad.SelectedIndex = -1;
            ReceiteLabel.Text = "";
            lb_orderQuantity.Items.Clear();
        }
    }
}