Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Location
    Maumee, Ohio
    Posts
    55

    More OOP I am trying to do in the Vending Machine.

    I am trying to get into more OOP with this program and 2 things i need to ask, One, When i try to use the switch to "1" in the menu I go right into default, and it is called in the Main, I tested this concept on a new file and it worked, but some reason it isn't working, can somebody check my code please. 2nd question is this code still linear or more towards OOP? thanks..

    Program
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace VM
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			
    			
    			List<CandyFood> candies = new List<CandyFood>();
    			//Console.WriteLine("Total Qty is: " + candies.Count);
    			Console.WriteLine();
    			//Command.ListCandy();
    			
    			candies.Add(new CandyFood("Zero Bar"));
    			candies.Add(new CandyFood("Snickers"));
    			candies.Add(new CandyFood("Butterfinger"));
    			candies.Add(new CandyFood("Kit Kat"));
    			CandyFood TempCandy = candies[0];
    			TempCandy.Update();
    			Console.WriteLine();
    			Console.WriteLine("Total Qty is: " + candies.Count);
    			Console.WriteLine("---------------");
    			Console.WriteLine();
    
    			Command.ShowVender();
    				
    			for (int i = 0; i < candies.Count; i++)
    			{
    				TempCandy = candies[i];
    			}
    
    			candies.RemoveAt(0);
    	
    		}
    
    			
    	}
    	class CandyFood
    	{
    		public string Name;
    		public CandyFood(string name)
    		{
    			Name = name;
    		}
    		public void Update()
    		{
    			Console.WriteLine("Updating....");
    		}
    	}
    }

    Candy Class
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace VM
    {
    	class Candy
    	{
    		#region FIELDS
    
    		private string _zeroBar;
    		private string _kitKat;
    		private string _Snickers;
    		private string _butterFingers;
    		private int _Qty;
    
    		#endregion
    		#region LIST
    
    		public static List<Candy> CandyList;
    
    		#endregion
    		#region PROPERTIES
    
    		public string ZeroBar
    		{
    			get { return _zeroBar; }
    			set { _zeroBar = value; }
    		}
    		public string KitKat
    		{
    			get { return _kitKat; }
    			set { _kitKat = value; }
    		}
    		public string Snickers
    		{
    			get { return _Snickers; }
    			set { _Snickers = value; }
    		}
    		public string ButterFingers
    		{
    			get { return _butterFingers; }
    			set { _butterFingers = value; }
    		}
    
    		public int QTY
    		{
    			get { return _Qty; }
    			set { _Qty = value; }
    		}
    		#endregion
    
    		#region CONSTRUCTOR
    
    		static Candy()
    		{
    			CandyList = new List<Candy>();
    		}
    		public Candy(string ZeroBar, string KitKat, string Snickers, string ButterFingers, int QTY)
    		{
    			Candy.CandyList.Add(this);
    			this._zeroBar = ZeroBar;
    			this._kitKat = KitKat;
    			this._Snickers = Snickers;
    			this._butterFingers = ButterFingers;
    			this._Qty = QTY;
    		}
    		#endregion
    		#region CLASS METHODS
    
    		public void CandyShow()
    		{
    			Console.WriteLine("Zerobar\t\t{0}", _zeroBar);
    			Console.WriteLine("KitKat\t\t{0}", _kitKat);
    			Console.WriteLine("Snickers\t\t{0}", _Snickers);
    			Console.WriteLine("Butterfingers\t\t{0}", _butterFingers);
    			Console.WriteLine("Qty\t\t{0}", _Qty);
    		}
    		#endregion
    	}
    }
    Command Class
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace VM
    {
    	class Command
    	{
    		#region PRIVATE VARIABLES
    
    		//private static string MyCandy;
    		private static string MyZeroBar;
    		private static string MyKitKat;
    		private static string MySnickers;
    		private static string MyButterFingers;
    		private static int MyQty = 10;
    		private static string sweets = "";
    		private static bool running = true;
    		private static double Total = 0;
    		private static double AMT_MONEY = 10.00;
    		private static double KITKAT = 0.50;
    		private static double SNICKERS = 1.00;
    		private static double BUTTERFINGER = 1.00;
    		private static double ZEROBAR = 1.0;
    
    		#endregion
    
    		#region Show Vender
    		public static void ShowVender()
    		{
    			Console.WriteLine("Vending Machine");
    			Console.WriteLine("---------------");
    			Console.WriteLine();
    			Console.WriteLine("You have 10 dollars in your purse!");
    			Console.WriteLine();
    			Console.WriteLine("[1] Kit Kat{0}          (.50) <On Sale>", MyKitKat);
    			Console.WriteLine("[2] Snickers{0}         ($1.00)", MySnickers);
    			Console.WriteLine("[3] Butterfinger{0}     ($1.00)", MyButterFingers);
    			Console.WriteLine("[4] Zero Bar{0}         ($1.00)", MyZeroBar);
    			Console.WriteLine("[5] Add Quanity");
    			Console.WriteLine("[6] Return to Vending Machine");
    			Console.WriteLine("[7] Quit");
    			Console.WriteLine();
    			Console.WriteLine("What do you like to purchase");
    			Console.Write("> ");
    			Console.ReadLine();
    
    			while (running)
    			{
    				switch (sweets)
    				{
    					case "1":
    						MyQty--;
    						AMT_MONEY -= KITKAT;
    						Total = AMT_MONEY - KITKAT;
    						Console.WriteLine("you have purchuse ${0} Kit Kat of ${1}. This leave you ${2} left over in Quanity {3}", KITKAT, AMT_MONEY, Total, MyQty);
    						Console.ReadLine();
    						Console.Clear();
    						Command.ShowVender();
    						break;
    				/*	case "2":
    						AMT_MONEY -= SNICKERS;
    						MyQty--;
    						Total = AMT_MONEY - SNICKERS;
    						Console.WriteLine("you have purchuse ${0} SugarlessGum of ${1} an this leave you ${2} left over {3} in Quanity left", SNICKERS, AMT_MONEY, Total, MyQty);
    						Console.ReadLine();
    						Console.Clear();
    						Command.ShowVender();
    						break;
    					case "3":
    						AMT_MONEY -= BUTTERFINGER;
    						MyQty--;
    						Total = AMT_MONEY - BUTTERFINGER;
    						Console.WriteLine("you have purchuse ${0} Snickers of ${1} an this leave you ${2} left over {3} in Quanity left", BUTTERFINGER, AMT_MONEY, Total, MyQty);
    						Console.ReadLine();
    						Console.Clear();
    						Command.ShowVender();
    						break;
    					case "4":
    						AMT_MONEY -= ZEROBAR;
    						MyQty--;
    						Total = AMT_MONEY - ZEROBAR;
    						Console.WriteLine("you have purchuse ${0} Peanut Butter Cups of ${1} an this leave you ${2} left over {3} in Quanity left", ZEROBAR, AMT_MONEY, Total, MyQty);
    						Console.ReadLine();
    						Console.Clear();
    						Command.ShowVender();
    						break;
    					case "5":
    						MyQty++;
    						Console.WriteLine("{0} Quanity has been added", MyQty);
    						Command.ShowVender();
    						break;
    					case "7":
    						Console.ReadLine();
    						Console.Clear();
    						Command.ShowVender();
    						break;*/
    					default:
    						Console.WriteLine("Something isn't working...");
    						Console.ReadLine();
    						break;
    				}
    			}
    
    }
    		#endregion
    
    		#region CANDY
    		/*public static void GetCandy()
    		{
    			
    		}*/
    		#endregion
    
    		public static void ListCandy()
    		{
    			Console.Clear();
    			foreach (Candy sweets in Candy.CandyList)
    			{
    				sweets.CandyShow();
    			}
    		}
    	}
    
    }
    PS hehe Be easy on me.. explain it that i do not know what i am doing, this will give me a better understanding thanks.

    Cid420

  2. #2
    Join Date
    Mar 2004
    Location
    UK
    Posts
    85
    Hi Cid,

    You're not hitting case "1" because "sweets" is always empty, you're not doing anything with the selection made by the user i.e.

    Code:
    Console.WriteLine("What do you like to purchase");
    			Console.Write("> ");
    			Console.ReadLine();
    if you were to set sweets to equal what was returned by Console.ReadLine, a selection of "1" would hit case "1".

    Code:
    Console.WriteLine("What do you like to purchase");
    			Console.Write("> ");
    			sweets = Console.ReadLine();

  3. #3
    Join Date
    Mar 2013
    Location
    Australia
    Posts
    217
    Hi Cid, i think your getting there. You have the concept of a common Candy class which is very much OOP. The thing that sticks out to me is you have a lot of State that is hard coded. This is a good indication of something that could be presented in a more OOP approach. For example you have a variable for each Candy bar type.

    Code:
    		private string _zeroBar;
    		private string _kitKat;
    		private string _Snickers;
    		private string _butterFingers;
    that means when you want to find what type the candy bar is you need to call each corresponding variable.
    Code:
    		public void CandyShow()
    		{
    			Console.WriteLine("Zerobar\t\t{0}", _zeroBar);
    			Console.WriteLine("KitKat\t\t{0}", _kitKat);
    			Console.WriteLine("Snickers\t\t{0}", _Snickers);
    			Console.WriteLine("Butterfingers\t\t{0}", _butterFingers);
    			Console.WriteLine("Qty\t\t{0}", _Qty);
    		}
    What would happen if you wanted to introduce a new type of candy bar? You would have to create a new variable and update every part of the code that needs to know the type. This can be problematic, if you or another programmer doesn't realize he needs to add a new line to be able to print the type of candy bar then this will introduce a bug. Also once you start to get hundreds of different types this class is going to get out of control and difficult to manage.

    To approach it from a more OOP side we can think about what it is that defines the candy bar and set that up as properties. In the context of a vending machine your really dealing with a line or group of candy bar's. So what is it that defines that group?

    ProductName
    Price
    Quantity.

    I started of thinking about having a CandyBar class to represents a single candy bar but realized you need to manage quantity and price. A single bar would not have a different price to another bar of the same type so you would end up with a class with just a name for the CandyBar and maybe another Class for the Price and Quantity. Instead shift your thinking of OOP to the collection of bars that are the same. I would probably call this a StockLine.

    You have a Candy class and a CandyFood class witch is a bit confusing. It looks like the Candy class is responsible for tracking quantity and CandyFood represents a single bar. I would try to the make a class that manages the current stock. For example a StockManager class that manages a collection of StockLines. Each StockLine has its on Name, like KitKat, an overall Price and the current InStock quantity.

    The vending machine would interact with the StockManager and the StockManager would maintain the Stock. The Main OOP concept here is the fact that you would be managing multiple instances of StockLines, each one representing a different type of candy or other item

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •