Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2013
    Posts
    25

    Post-101 Practice

    After completing the C# 101 Spring 2013 course, I wanted to get a little bit of practice. To do so, I wanted to redo the vending machine from the earlier lessons with some OOP code. I would very much appreciate it if someone more experienced than I could take a look at it and see if anything really sticks out to you (because it feels messy to me), without going outside the scope of C# 101. Thanks, you guys are great!

    Code:
    using System;
    using System.Collections.Generic;
    
    namespace VendingMachineReview
    {
        class VendingMachineItem
        {
            public string Name { get; private set; }
            public float Price { get; private set; }
            public int Mass { get; private set; }
    
            public VendingMachineItem(string name, float price, int mass)
            {
                Name = name;
                Price = price;
                Mass = mass;
            }
        }
    
        class Customer
        {
            public string Name { get; private set; }
            public float Wallet { get; private set; }
            public int CarryingCapacity { get; private set; }
    
            public void PurchaseItem(VendingMachineItem item)
            {
                if (Wallet >= item.Price && CarryingCapacity >= item.Mass)
                {
                    Wallet -= item.Price;
                    CarryingCapacity -= item.Mass;
                    Console.WriteLine("\nYou purchased a " + item.Name + ". You now have $" + Wallet.ToString("0.00") + " left in your wallet");
                    Console.WriteLine("and can carry " + CarryingCapacity + " more kgs.");
                }
                else
                {
                    Console.WriteLine("\nYou can't purchase a " + item.Name + " right now.");
                    Console.WriteLine("You have $" + Wallet.ToString("0.00") + " left in your wallet");
                    Console.WriteLine("and can carry " + CarryingCapacity + " more kgs.");
                }
            }
    
            public void CollectCustomerInfo()
            {
                bool validInput = false;
                float moneyInput = 0;
                int capacityInput = 0;
    
                Console.Write("Enter your name: ");
                Name = Console.ReadLine();
                while (!validInput)
                {
                    Console.Write("Enter how much money you have: $");
                    validInput = float.TryParse(Console.ReadLine(), out moneyInput);
    
                    if (moneyInput <= 0)
                    {
                        Console.WriteLine("You must have money to use this vending machine!");
                        validInput = false;
                    }
                }
                validInput = false;
                Wallet = moneyInput;
                while (!validInput)
                {
                    Console.Write("Enter how much mass you can carry (to the nearest kilogram): ");
                    validInput = int.TryParse(Console.ReadLine(), out capacityInput);
    
                    if (capacityInput <= 0)
                    {
                        Console.WriteLine("You must be able to carry at least 1kg to use this vending machine!");
                        validInput = false;
                    }
                }
                CarryingCapacity = capacityInput;
            }
        }
    
        class Program
        {
            static void Main()
            {
                List<VendingMachineItem> vendingMachine = new List<VendingMachineItem>();
                vendingMachine.Add(new VendingMachineItem("Beer", 6.00f, 1));
                vendingMachine.Add(new VendingMachineItem("Vodka", 15.00f, 3));
                vendingMachine.Add(new VendingMachineItem("Mountain Dew", 2.50f, 1));
                vendingMachine.Add(new VendingMachineItem("Arizona Arnold Palmer", .99f, 2));
    
                Customer user = new Customer();
                user.CollectCustomerInfo();
    
                bool isPurchasingItems = true;
                while (isPurchasingItems)
                {
                    Console.WriteLine();
    
                    bool validInput = false;
                    int selectionNumber = 0;
    
                    int i = 1;
                    foreach (VendingMachineItem item in vendingMachine)
                    {
                        Console.WriteLine(i + " - " + item.Name + ": $" + item.Price.ToString("0.00") + " (" + item.Mass + "kg)");
                        i++;
                    }
    
                    while (!validInput)
                    {
                        Console.Write("Enter your selection number (any other number to leave machine): ");
                        validInput = int.TryParse(Console.ReadLine(), out selectionNumber);
                    }
    
                    if (selectionNumber <= 0 || selectionNumber > vendingMachine.Count)
                        isPurchasingItems = false;
                    else
                        user.PurchaseItem(vendingMachine[selectionNumber - 1]);
                }
    
                Console.Write("\nGoodbye, " + user.Name);
                Console.ReadKey();
            }
        }
    }
    Runtime example:
    Click image for larger version. 

Name:	vendingmachinereview.png 
Views:	126 
Size:	85.8 KB 
ID:	73804
    Last edited by Dan Buckles; 08-15-2013 at 11:49 AM.

  2. #2
    Join Date
    Oct 2011
    Posts
    547
    I think that the Customer should contain just the basic info and the extra info, like PurchaseItem, should be placed in their own class.

    As an exercise, look at the class name and think of what you'd expect to see/do if a class presented itself as Customer or whatever it name was/might be.

  3. #3
    Join Date
    Aug 2013
    Posts
    25
    Alright, thanks man. I'll try to revise it later. Although, going through week one of 102, I feel like I should just throw the whole thing out and start over. The content in that course is just blowing me away.

  4. #4
    Join Date
    Oct 2011
    Posts
    547
    Keep it for comparing. It is interesting to see your own progression later on.

  5. #5
    Join Date
    Aug 2013
    Posts
    25
    That's a good point. Thanks for the tip!

Posting Permissions

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