Stacks

I am working on an assignment for C#.  As my name states, I am not a programmer and trying to learn how is driving me insane.

This the assigment I am working on.  I have the first part complete, but am trying to work on the extra credit.  This is where I am having major problems.  Any help anyone can give me would be greately appreciated.

"

Homework 2


Sausage Stack

In this assignment you will work with the "stack" class in C#.

When visiting a local breakfast buffet, we were somewhat dismayed when a server added new sausages to a pan by simply dumping the new sausages atop the old sausages.  Anyone who has worked in food service knows that you must rotate the food such that the old items are placed on top of the new items (so that the oldest items will be eaten first and won't continue to "decay" at the bottom of the food tray).  Unfortunately for the late arriving patrons of this restaurant, the bottom-most sausages were indeed the oldest because the restaurant worker employed a "stack-like" approach to the food service.

Your goal is to create a program that simulates this "less than ideal" approach to offering sausages.  The program should allow the user to add and remove sausages from a stack; you can assume that only one sausage will be added at a time, and only one sausage will be removed at a time.  The user will only have access to one item on the top of the "pan" - thus the need for the stack.

The requirements of this program are:

  1. Have a button that lets the user add a sausage to the stack
  2. Have a button that lets the user remove a sausage from the stack.
  3. The "remove" button should only be enabled if there is at least one item in the stack
  4. You must display the "age" of the sausage to the user in a label when a sausage is removed from the stack; this is best accomplished by storing the current time when the user adds a sausage, then subtracting this stored time from the current time when the user removes a sausage.  For example, if the user added a sausage at time 10 and removed the sausage at time 25, the age of the sausage will be 15.

For extra credit:

You can use these images if you want to do the extra credit.

  1. Use an ImageArray to store images of sausages.  When a sausage is removed, show it to the user.  Select the type of sausage (the image that is displayed) at random.
  2. Add a visualization of the sausage stack.  You can add a picture box to your form, and display images of sausages within this picture box.  Notice that you'll have to draw this yourself.  In the example provided, the sausages are placed randomly around the pan, but notice that there is exactly one topmost sausage.  Some hints on this extra credit:
    1. It is useful to add images to an ImageArray class at design time (when you're building your program)
    2. You can use this provided "sausage" class to keep track of the age of the sausage, where it is locate (X,Y - which represents the upper left position of the image) in the pan, and the type of sausage (the image that is displayed).
    3. You'll have to draw the sausages from the bottom of the pan up (so that the stack's top is shown at the top of the image).  To gain access to more than just the top of the stack, you can use the "ToArray()" method of the Stack class.  This will convert the stack to an array so that you can access all of the elements in the collection in any order you wish.
    4. Be sure to set the "transparent color" property of your ImageArray so that the sausage images stack visually on top of each other nicely.

Below is how the program is to be run.  You can run the example and this may give you some insite on what I am trying to do.

 

Thanks for your help and I will be waitinng for someone's reply.

 

This is the code I have so far:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace SausageStack

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void buttonAdd_Click(object sender, EventArgs e)

{

string sausage;

sausage = "sausageStack";

DateTime timeAdded = DateTime.Now;

sausage.Items.Add(sausage);

sausage.Items.Add(timeAdded);

if (SausageStack.Items.Count > 0)

buttonRemove.Enabled = true;

else if (pictureBoxStack)

buttonRemove.Enabled = false;

pictureBoxMainImage.Image = img.ToBitmap();

}

 

private void buttonRemove_Click_1(object sender, EventArgs e)

{

DateTime timeRemoved = DateTime.Now;

DateTime timeAdded = (DateTime)pictureBoxStack.Items[pictureBoxStack.Items.Count - 1];

(double)TimeSpan age;

age = timeRemoved - timeAdded;

pictureBoxStack.Items.RemoveAt(pictureBoxStack.Items.Count - 1);

pictureBoxStack.Items.RemoveAt(pictureBoxStack.Items.Count - 1);

label1.Text = "This sausage was " + age.TotalSeconds + "seconds old!";

if (pictureBoxStack.Items.Count > 0)

buttonRemove.Enabled = true;

else if (pictureBoxStack.Items.Count <= 0)

buttonRemove.Enabled = false;

}

private void pictureBoxStack_Click(object sender, EventArgs e)

{

ImageArray img = new ImageArray(pictureBoxStack.Image);

img.LockImage();

}

private void buttonAdd_Click_1(object sender, EventArgs e)

{

}

private void label1_Click(object sender, EventArgs e)

{/*

pictureBoxStack.Items.RemoveAt(pictureBoxStack.Items.Count - 1);

pictureBoxStack.Items.RemoveAt(pictureBoxStack.Items.Count - 1);

label1.Text = "This sausage was " + age.TotalSeconds + "seconds old!";

*/

}

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)

{

DialogResult sausage;

string pictureName;

sausage = OpenFileDialog.sausage;

if (sausage == DialogResult.OK)

{

sausage = openFileDialog.FileName;

pictureBoxStack.Load(picture);

processImageToolStripMenuItem.Enabled = true;

}

}

}

}

 

 



Answer this question

Stacks

  • JohnC1102

    hi,

    i guess you should first creat a class for single sausage with some properties like (DateOfAdding , get age , picture ... etc then in your program you can add instances from this class to array list or stack,

    i guess this is training for System.collections namespace you can find a tutorial for it here

    http://www.programmersheaven.com/2/Les_CSharp_8_p4

    hope this helps



  • NeilR

    I don't think anyone here is willing to do your homework for you. But if you have any concrete questions we might be able to help.

    FWIW there's no class in the class library called ImageArray. I believe they meant to say ImageList.



  • Frankiee

    The framework does have a Stack class, which the instructor indicated he wants you to use. This would provide your 'storage'. You didn't provide the Sausage class, so your kind of on your own there.


  • Kevin067

    Not asking for anyone to do my homework. I posted the code that I had and was looking for some help and some direction as to where I was going wrong! I am trying to learn to program C# but my teachers are not very good!
  • Crazygon

    I think I need to use a queue. What I want to do is to add a sausage everytime the "add sausage button" is pressed. Then I want to remove a sausage when the "Remove sausage button" is pressed, thus producing the age of the sausage that was removed.

    I found the image list and stored the pictures in it. We have not learned how to create classes as of yet.


  • ulven

    hi,

    its up to you , i guess any of those collections will be better than using normal arrays because you don't know the total number of elements in your array and you can't determine that either in run time also it might help you in FIFO or LIFO thing

    hope this helps



  • Stacks