1. #1

    Visual Basic 2010

    Anyone here know anything about Visual Basic 2010? I tried posting a question on StackOverflow a long time ago but never got a response.

    I have a project due tomorrow night that involves making an app containing 2 classes that simulates a vending machine. It has to use specific types of change, and run through a loop until 1.50 has been added, and then ask the user what drink they want.

    I've got it to the point that it sort of lets the user input change, but it gives me the error message I programmed in for invalid change input everytime I run it no matter what the change is. It also isn't actually adding any change beyond the first change input value.

    So aside from fixing that, I still have to figure out where to put the prompt for the user's choice of drink, and then give them the drink as well as remaining change.

    Needless to say I've been lost since chapter 4 in terms of this whole.. Two classes working together crap. I just don't get it! I'm trying to take this online with no previous experience in programming, while working full time. It's really difficult, but far more difficult than I feel it should be and would be if I could just get this basic understanding of the concept down as a foundation.

    I imagine the drink choice could be made easier with arrays, but I don't know how to do those just yet and they aren't until next chapter so I want to make sure I'm using everything that we HAVE learned thus far for this project without jumping into other stuff.

    MSDN doesn't really help me all that much. They have a bad habit of showing examples but not really explaining the reasoning behind it.

    If anyone has any experience and could help let me know and I'll post my code that I have so far. Actually, I'm just going to do that regardless in case I get lucky and someone sees this and responds to it. I don't know why I'm not catching on quicker than this.

    This is one of my classes.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DrinkMachine
    {
    class DrinkMachine
    {

    private decimal balance;




    public DrinkMachine(decimal initialBalance)
    {
    Balance = initialBalance;
    }

    public void Deposit(decimal depositAmount)
    {
    Balance = Balance + depositAmount;
    }



    public decimal Balance
    {
    get
    {
    return balance;
    }
    set
    {

    if (value == .25m)
    balance = Decimal.Add(balance, .25m);
    if (value == .10m)
    balance = Decimal.Add(balance, .10m);
    if (value == .05m)
    balance = Decimal.Add(balance, .05m);
    if (value == 1.00m)
    balance = Decimal.Add(balance, 1.00m);

    else
    {
    Console.WriteLine("You have entered incorrect change.");
    }
    }

    }
    }


    }


    The class with the main method is this one:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DrinkMachine
    {
    class DrinkMachineTest
    {
    static void Main(string[] args)
    {

    DrinkMachine drinkMachine = new DrinkMachine(20.00m);
    Console.WriteLine("Your current amount: {0:C}", drinkMachine.Balance);


    decimal Balance = .0m;
    {
    while (Balance < 1.50m)
    {
    decimal depositAmount;
    Console.Write("Accepted currencies are .25, .10, .05, and 1.00. Please enter your change amount: ");
    depositAmount = Convert.ToDecimal(Console.ReadLine());
    Console.WriteLine("You've added {0:C}", depositAmount);
    drinkMachine.Deposit(depositAmount);

    Console.WriteLine("Your current balance is: {0:C}", drinkMachine.Balance);

    }
    if (Balance == 1.50m)
    {
    Console.WriteLine("You have sufficient funds. Please make your choice. Coke - 1078, Mr. Pibb - 0179, Mountain Dew-0180, Sprite-0181");
    }





    }
    }
    }
    }



    Any tips on how to fix the fact that it's continuously giving me the invalid change writeline? Or why it's not adding the new change beyond the first one? I'm sure it has to do with not constantly replacing the value of Balance, but I don't know enough to know how or where to fix that. And which class or where should I write the prompt for making the decision for the drink choice and returning change? I feel like I'm about a fourth of the way done but have no clue what to do from here.

  2. #2
    Looks like C#, not VB

    Off the bat, the reason you're getting the "You have entered incorrect change." message is because
    in your Balance class constructor you have :

    Balance = initialBalance;


    This is using the Balance property's setter (instead of setting the balance field), which looks like this:
    set
    {
    if (value == .25m)
    balance = Decimal.Add(balance, .25m);
    if (value == .10m)
    balance = Decimal.Add(balance, .10m);
    if (value == .05m)
    balance = Decimal.Add(balance, .05m);
    if (value == 1.00m)
    balance = Decimal.Add(balance, 1.00m);
    else
    {
    Console.WriteLine("You have entered incorrect change.");
    }
    }


    The first line in main is: "DrinkMachine drinkMachine = new DrinkMachine (20.00m);" 20.00m is not one of the four values you allow in the setter, so you'll always see "You have entered incorrect change."
    Last edited by Levaustian; 2013-11-17 at 08:46 AM.

  3. #3
    Deleted

  4. #4
    Quote Originally Posted by Shiift View Post
    the buildup for that was so good lol

Posting Permissions

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