help with classes and structs

this is the instructions for the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
You are to implement a program that 
uses both a Array-based Stack and a Linked Stack (the choice of how to use them is up to you!).    
The program will mimic two typical soda machines – one that holds cans and one that holds bottles.  
As you know, sodas are put into the machine in 
LIFO order (Last-In-First-Out), so the most recent 
sodas put into the machine are the first out when 
you buy one. Your program will stock the machines 
with sodas, and then allow the user to purchase 
sodas and/or retrieve information about the machines, 
using the 
appropriate Stack functions.

Here are the details:

TASK #1:
Define a object type called Drink.  A Drink has the following members:
•	A name (string)
•	A price (something between $0.0 and $1.25, depending on the type)
•	A type (a character that will be ‘B’ for bottle and ‘C’ for can)

TASK #2
Your task is to create two Stacks – an Array-Based Stack to hold one type of soda (bottle or can) and a LinkedStack 
that can hold objects of the other type (you can decide which will hold which).  You can simply modify the code given 
to you in class (see notes).  
Call these classes whatever you wish, as long as the names are appropriate .


TASK #3:
Create a main program that will allow 
the user to create and use these classes.  
The main program should create both machines, 
and fill them up with data from a file called “sodainventory.h”.  
The program will open the file, 
and read each line, where each line contains data for a Drink:

                       Name       Price            Type

The program should 
create the soda machines, read the file and fill
 up the machines with the sodas in the file, based
 on the Type. The program should present the user 
with the following options in menu format:

1)	Add another soda to the machine
2)	Buy a soda
3)	Find a soda by name (see if it is in there)
4)	List the total value of all sodas in a machine (how much they are worth)
5)	Quit

 


here is what I have come up with so far:
implementation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Machine.cpp
template <class DataType>
bottleArrayStack<DataType>::Stack( ):elements(2),top(-1)
{
}

void bottleArrayStack<DataType>::push( DataType elementToPush )
{
if(++top== elements.length()) // checks to see if array is full
   elements.changeSize(elements.length()<<1); // doubles array if it's full
elements[top] = elementToPush;
}

bool bottleArrayStack<DataType>::pop( DataType & poppedElement )
{
if (top == -1)
          return false; // returns false if array is empty
poppedElement = elements[top]; // stores popped element into variable
top--;
}

bool bottleArrayStack<DataType>::peek( DataType & topElement )
{
if (top == -1)
    return false;
topElement = elements[top]
    return true;
}

void bottleArrayStack<DataType>::makeEmpty()
{
top = -1;
}

bool bottleArrayStack<DataType>::isEmpty() const
{
if (top == -1)
   return true;
return false;
}

header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <class DataType>
    class bottleArrayStack // Array Stack
    {
   public:
 Stack( );
 void push( DataType elementToPush );  

	// removes an element from the top of the stack and returns it in poppedElement; 
	// returns false if called on an empty stack; otherwise, returns true
 bool pop( DataType & poppedElement );

	// returns the element at the top of the stack in topElement without removing it
	// returns false if called on an empty stack; otherwise, returns true
 bool peek( DataType & topElement ); 
 bool isEmpty( ) const;  // returns true if the stack is empty;
	// otherwise, returns false
 void makeEmpty( )
   private:
     Array<DataType> elements;
	   int top;
};


#include "machine.cpp"  

main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;



drink Sprite;
drink Coca;

do
{
cout<<"\n Menu (order 1-4, 5 exit)"<<"\n";
cout<<"\n 0thers: Exit";
cout<<"\n 1: Add another soda to the machine ";
cout<<"\n 2: Buy a soda ";
cout<<"\n 3: Find a soda by name ";
cout<<"\n 4: How much is the soda ";
cout<<"\n\n Enter a choice : ";
cin>>selection;
}while(selection != 5);
// true for 1, 2, 3, and 4 ONLY, then repeat
// false for other numbers including 0, then stop.
// The do loop is repeated if the while expression is true.
 

return 0;
}



My coding is not completed yet but I believe there is enough to comprehend where I am heading.
What I need help with is I can't seem to put together how to incorporate my struct with my class to store the drink types into the array.

Do i have to declare the struct in the main program and then do something like:

struct.pop() to store data? and vice versa
I realize my question might have been a bit ambiguous. I think I know how to more specifially answer it now. I get how to do the syntax and everything for the program but I am having trouble wrapping my mind around 1 thing.

1) He instructs us to create an object called Drink(with drinkName drinkType drinkPrice) I think a struct would be the best way to go but is it possible to store the entire struct in one stack space?
Last edited on
Topic archived. No new replies allowed.