Monday, April 2, 2012

CIS 217: Structures Assignment [Vending Machine Sim]

As the name of the assignment implied, we were directed to create a vending machine simulator. In regards to the design of the form, I went with how the book displayed it.

My first step was to add an ImageList control ("imgListLogos") to the form, set it's ImageSize to 64 x 64, and add the picture collection for each soda's logo. I also added six PictureBox controls, set their BorderStyle to Fixed3D, and named each after the soda that they would represent (pbCola, pbRootBeer, etc).

My second step, creating the event handlers for each PictureBox, was easy since I wasn't going to overload them - double-clicking each control gives it to me.

My third step was to create the Structure to hold the various properties of each soda (it's name, cost, logo, and how many are remaining in the vending machine). As the book dictates, this was done outside of the class, but inside of the project namespace:

public struct Soda
    {
        public string Name;
        public double Cost;
        public int Remaining;
        public int Logo;
    }

I also created the array of the Soda structure for easier maintenance of the data:

public partial class frmMain : Form
    {
        const int arrSIZE = 5;
        public double totalSales = 0.00;
        public Soda[] Sodas = new Soda[arrSIZE];

My third step, after creating the struct Soda, was to create a few methods to create each drink and it's associated data: the cost, number remaining, and the logo for each drink.

The method for creating each Soda structure, with associated properties:
public void CreateDrinks()
        {
            Sodas[0].Name = "Cola";
            Sodas[0].Cost = 1.00;
            Sodas[0].Remaining = 20;
            Sodas[0].Logo = 0;
        }

The method to display the logo, and the cost for each soda:
public void DisplayDrinks()
        {
            pbCola.Image = imglistLogos.Images[0];
            lblColaCost.Text = Sodas[0].Cost.ToString("c");
        }

And two methods to update the remaining number of drinks after a purchase is made:
public void UpdateTotalSales()
        {
            lblTotalSales.Text = totalSales.ToString("c");
        }

        public void UpdateRemaining()
        {
            lblColaRemaining.Text = Sodas[0].Remaining.ToString();
        }

My fourth step was to finally add code to each event handler to deal with the purchasing of each soda. This checks first to see if there are any remaining sodas of that type left. If there are, it will subtract 1 from the current value of .Remaining, update the totalSales with the cost of the soda, and then call the UpdateTotalSales() and UpdateRemaining() methods (which will update the labels associated with each). I did this for each PictureBox, except for the last (the one related to sales and not a particular soda):

private void pbCola_Click(object sender, EventArgs e)
        {
            if (Sodas[0].Remaining > 0)
            {
                Sodas[0].Remaining = Sodas[0].Remaining - 1;
                totalSales = totalSales + Sodas[0].Cost;
                UpdateTotalSales();
                UpdateRemaining();
            }
            else
            {
                MessageBox.Show("I'm Sorry, But That Drink Is Out Of Stock");
            }
        }

Thoughts:
I've done class-based development before in Visual Basic, and I am glad that the author introduces Structures before Classes, as it is a natural progression. However, I do not like how he "left the student hanging" by saying "you can do a lot more, but we're just going to cover the extreme basics". It would have been nice to give a few advanced examples of Struct use, and then re-visit those same examples with Class use, to show the similarities and differences, but oh well. All in all, a good chapter and something that gets closer to the topic that I want to learn about. Finally getting out of the "basics"!

Here is the code for the entire project:

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DrinkVendingMachineSim
{
    public struct Soda
    {
        public string Name;
        public double Cost;
        public int Remaining;
        public int Logo;
    }

    public partial class frmMain : Form
    {
        const int arrSIZE = 5;
        public double totalSales = 0.00;
        public Soda[] Sodas = new Soda[arrSIZE];

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            CreateDrinks();
            DisplayDrinks();
            UpdateRemaining();
        }

        public void UpdateTotalSales()
        {
            lblTotalSales.Text = totalSales.ToString("c");
        }

        public void UpdateRemaining()
        {
            lblColaRemaining.Text = Sodas[0].Remaining.ToString();
            lblRBRemaining.Text = Sodas[1].Remaining.ToString();
            lblLLRemaining.Text = Sodas[2].Remaining.ToString();
            lblGSRemaining.Text = Sodas[3].Remaining.ToString();
            lblCSRemaining.Text = Sodas[4].Remaining.ToString();
        }

        public void DisplayDrinks()
        {
            pbCola.Image = imglistLogos.Images[0];
            lblColaCost.Text = Sodas[0].Cost.ToString("c");

            pbRootBeer.Image = imglistLogos.Images[1];
            lblRBCost.Text = Sodas[1].Cost.ToString("c");

            pbLemonLime.Image = imglistLogos.Images[2];
            lbLLCost.Text = Sodas[2].Cost.ToString("c");

            pbGrapeSoda.Image = imglistLogos.Images[3];
            lbGSCost.Text = Sodas[3].Cost.ToString("c");

            pbCreamSoda.Image = imglistLogos.Images[4];
            lbCSCost.Text = Sodas[4].Cost.ToString("c");
        }

        public void CreateDrinks()
        {
            Sodas[0].Name = "Cola";
            Sodas[0].Cost = 1.00;
            Sodas[0].Remaining = 20;
            Sodas[0].Logo = 0;

            Sodas[1].Name = "Root Beer";
            Sodas[1].Cost = 1.00;
            Sodas[1].Remaining = 20;
            Sodas[1].Logo = 1;

            Sodas[2].Name = "Lemon Lime";
            Sodas[2].Cost = 1.00;
            Sodas[2].Remaining = 20;
            Sodas[2].Logo = 2;

            Sodas[3].Name = "Grape Soda";
            Sodas[3].Cost = 1.50;
            Sodas[3].Remaining = 20;
            Sodas[3].Logo = 3;

            Sodas[4].Name = "Cream Soda";
            Sodas[4].Cost = 1.50;
            Sodas[4].Remaining = 20;
            Sodas[4].Logo = 4;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pbCola_Click(object sender, EventArgs e)
        {
            if (Sodas[0].Remaining > 0)
            {
                Sodas[0].Remaining = Sodas[0].Remaining - 1;
                totalSales = totalSales + Sodas[0].Cost;
                UpdateTotalSales();
                UpdateRemaining();
            }
            else
            {
                MessageBox.Show("I'm Sorry, But That Drink Is Out Of Stock");
            }
        }

        private void pbRootBeer_Click(object sender, EventArgs e)
        {
            if (Sodas[1].Remaining > 0)
            {
                Sodas[1].Remaining = Sodas[1].Remaining - 1;
                totalSales = totalSales + Sodas[1].Cost;
                UpdateTotalSales();
                UpdateRemaining();
            }
            else
            {
                MessageBox.Show("I'm Sorry, But That Drink Is Out Of Stock");
            }
        }

        private void pbLemonLime_Click(object sender, EventArgs e)
        {
            if (Sodas[2].Remaining > 0)
            {
                Sodas[2].Remaining = Sodas[2].Remaining - 1;
                totalSales = totalSales + Sodas[2].Cost;
                UpdateTotalSales();
                UpdateRemaining();
            }
            else
            {
                MessageBox.Show("I'm Sorry, But That Drink Is Out Of Stock");
            }
        }

        private void pbGrapeSoda_Click(object sender, EventArgs e)
        {
            if (Sodas[3].Remaining > 0)
            {
                Sodas[3].Remaining = Sodas[3].Remaining - 1;
                totalSales = totalSales + Sodas[3].Cost;
                UpdateTotalSales();
                UpdateRemaining();
            }
            else
            {
                MessageBox.Show("I'm Sorry, But That Drink Is Out Of Stock");
            }
        }

        private void pbCreamSoda_Click(object sender, EventArgs e)
        {
            if (Sodas[4].Remaining > 0)
            {
                Sodas[4].Remaining = Sodas[4].Remaining - 1;
                totalSales = totalSales + Sodas[4].Cost;
                UpdateTotalSales();
                UpdateRemaining();
            }
            else
            {
                MessageBox.Show("I'm Sorry, But That Drink Is Out Of Stock");
            }
        }
    }
}

No comments:

Post a Comment