Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Location
    Liverpool
    Posts
    12

    Arrow C# 101 - Week 2 - Homework

    Hey, I'm pretty new to programming with any language and thought C# would be a good place to start.

    Started perhaps a week ago in earnest and went through the C# Fundamentals for Absolute Beginners course over @ Microsoft Virtual Academy then came over here and started going through the free C# 101 - 2013 video series.

    Attempting the homework and this is what I have so far, I think it works okay. I'm positive a lot of the code is wasteful (especially the bolded bit)and am just posting for some feedback on what I should change.

    Code:
    using System;
    using System.Collections.Generic;
    
    internal class Program
    {
        public static void Main()
        {
            #region Vending machine
    
            bool enteredValidData = false;
            int money = 0;
            int menuSelection;
            
            List<string> itemList = new List<string>();
            itemList.Add("Game Console");
            itemList.Add("Car");
            itemList.Add("House");
    
            List<int> itemPrice = new List<int>();
            itemPrice.Add(12);
            itemPrice.Add(27);
            itemPrice.Add(38);
    
            Console.WriteLine("Welcome to Odd Jobs Oddities!");
    
            while (!enteredValidData)
            {
                Console.WriteLine("Please enter how much money you have (In £)");
                enteredValidData = int.TryParse(Console.ReadLine(), out money);
    
                if (!enteredValidData)
                {
                    Console.WriteLine("The data type you entered was not valid. Please enter an integer value.");
                }
            }
    
    
            while (money >= itemPrice[0])
            {
                Console.WriteLine("You have {0:C} to spend.", money);
    
                Console.WriteLine("Please select what you would like.");
                Console.WriteLine("[0] {0} - {1:C}", itemList[0], itemPrice[0]);  //Wasteful
                Console.WriteLine("[1] {0} - {1:C}", itemList[1], itemPrice[1]);
                Console.WriteLine("[2] {0} - {1:C}", itemList[2], itemPrice[2]);
    
                enteredValidData = int.TryParse(Console.ReadLine(), out menuSelection);
                if (menuSelection > 2 || menuSelection < 0)
                {
                    Console.WriteLine("Please Enter Valid Data");
                    enteredValidData = false;
                }
    
                if (enteredValidData && money > itemPrice[menuSelection])
                {
                    Console.WriteLine("You purchased an {0} for {1}.", itemList[menuSelection], itemPrice[menuSelection]);
                    money -= itemPrice[menuSelection];
                    Console.WriteLine("Money: {0:C}", money);
                }
                else if (enteredValidData && money < itemPrice[menuSelection])
                {
                    Console.WriteLine("You don't have enough money to purchase a {0}", itemList[menuSelection]);
                }
    
    
            }
            
            Console.WriteLine("You don't have enough money to purchase anything, goodbye tramp!");
    
            Console.ReadKey();
            #endregion
                
        }
    }

  2. #2
    Join Date
    Jun 2012
    Location
    probably at a PC
    Posts
    309
    Although I believe I did the vending machine in the C++ class, I think it's not a bad first attempt. A few small changes and you'd be good.
    Personally I haven't done the 2013 classes and don't know if some of what I may suggest has even been covered yet, but...

    1) I wouldn't use two separate lists for price and item. They may at some point not be in sync and that would certainly not be desirable.
    I'd probably opt for a 'dictionary<>'

    2) If statements with fixed/hard-coded values, like:
    Code:
    if (menuSelection > 2 || menuSelection < 0)
    ...when used in the context of a list/array etc, don't make much sense, because basically you should never assume that you know the exact size of the list/array (unless there's a good reason for it). A listItem.Count - 1 would be more appropriate to get the max index. There's also the possibility that at this point (this if statement) the input was already determine not to be valid. Which could cause some rather nasty issues. Including the 'enteredValidData' boolean could help with that and would look something like this:
    Code:
    if (enteredValidData && menuSelection >= 0 && menuSelection < itemList.Count)
    then again... when you look back at things like this in the future. There's very few things you'd still do the same way anyways. Such as this approach:

    Consider what int.TryParse does. It 'tries' to parse something. In this case it means that there will be an 'int value' stored in the 'out variable' and when it does that, returns a boolean true for success (or false if it failed). Try to use that to your advantage.
    Code:
    if (int.TryParse(Console.ReadLine(), out menuSelection))
    	// at this point we know a valid INT was entered. No more checks in that regard needed
    	// just do some check for it to be a number within a valid range ;)
    	...code
    else
    	// failure message or something

    May I suggest an additional challenge?! Try to filter out menu-items that are too expensive, while showing items that are still within budget. I believe I did that on the vending machine at some point.
    Last edited by nGAGE; 09-03-2013 at 01:00 PM.

  3. #3
    Join Date
    Jun 2013
    Location
    Liverpool
    Posts
    12
    Wow, when you read things other people have noticed it really makes you want to facepalm at how obvious they are.

    I don't know what a dictionary is but I shall go learn about them.

    Thanks for taking the time to leave such a useful reply =) Much appreciated.
    Last edited by Gintoki; 09-05-2013 at 05:12 AM.

Tags for this Thread

Posting Permissions

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