Results 1 to 8 of 8

Thread: [RESOLVED] I so thought I had this but I don't - I am not creating multiple objects I don't thin

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] I so thought I had this but I don't - I am not creating multiple objects I don't thin

    I thought I had two objects and that I was taking cups away from each of these two unique vendingmachine objects, called decaff and caffeinated. But after testing how many cups are left, I find that I am always having the same number of cups left in either object. Or whats happening is I have one object or maybe I am only accessing one of the two. I am not sure. I really need someone to explain to me what is going wrong with this.

    I am trying to create two vendingmachine objects. Choose which one I want to buy from, then actually remove cups from that one as a cup of coffee is bought.

    I am failing to somehow keep them seperate, create them properly or access them properly.

    Any help would be so appreciated.

    Prog5_22.cs

    VB Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4.  
    5. namespace Prog5_22_Rev2
    6. {
    7.     class Prog5_22
    8.     {
    9.         static void Main(string[] args)
    10.         {
    11.             VendingMachine Caffeine = new VendingMachine();
    12.  
    13.             VendingMachine DeCaff = new VendingMachine(10, .50);
    14.  
    15.             if (VendingMachine.Select() == 'c')
    16.             {
    17.                 do
    18.                 {
    19.                     Caffeine.Menu();
    20.                 } while (Caffeine.cupsCoffeeAvail > 0);
    21.             }
    22.             else if (VendingMachine.Choice == 'd')
    23.             {
    24.                 do
    25.                 {
    26.                     DeCaff.Menu();
    27.                 } while (DeCaff.cupsCoffeeAvail > 0);
    28.             }
    29.             else
    30.             {
    31.                 Console.WriteLine("Invalid Choice");
    32.                 Console.WriteLine("So, because you tried to hack me:");
    33.             }
    34.             Console.WriteLine("I am sorry, but my machine is out of coffe cups. =(");
    35.  
    36.        
    37.         }
    38.     }
    39. }
    ____________________________________
    ____________________________________
    ____________________________________

    VendingMachine.cs

    VB Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4.  
    5. namespace Prog5_22_Rev2
    6. {
    7.     class VendingMachine
    8.     {
    9.         private double moneyInsrtd = 0.0; //exact change only
    10.         public int cupsCoffeeAvail = 0;
    11.         private double pricePerCup = 0.0;
    12.         public static char Choice;
    13.  
    14.         public VendingMachine()
    15.         {
    16.             cupsCoffeeAvail = 10;
    17.             pricePerCup = .50;
    18.         }
    19.  
    20.         public VendingMachine(int cupsAvail, double price)
    21.         {
    22.             cupsCoffeeAvail = cupsAvail;
    23.             pricePerCup = price;
    24.         }
    25.  
    26.         public static char Select()
    27.         {
    28.             Choice = IO.GetChar("Would you like Caffeinated(c) or Decaffeinated(d) coffe? (c or d): ");
    29.             return Choice;
    30.         }
    31.  
    32.  
    33.         public void Menu()
    34.         {
    35.             Console.WriteLine("A cup of coffee costs {0:C}, there are {1} cups left. \n Please enter exact change.", pricePerCup, cupsCoffeeAvail);
    36.             int nmbrQurtrs = IO.GetInt("Enter the number of quarters: ");
    37.             int nmbrDimes = IO.GetInt("Enter the number of dimes: ");
    38.             int nmbrNickels = IO.GetInt("Enter the number of nickels: ");
    39.             Insert(nmbrQurtrs, nmbrDimes, nmbrNickels);
    40.         }
    41.  
    42.         public void Insert(int quarters, int dimes, int nickels)
    43.         {
    44.             double q = quarters * .25;
    45.             double d = dimes * .10;
    46.             double n = nickels * .05;
    47.             moneyInsrtd = q + d + n;
    48.  
    49.             if (moneyInsrtd == pricePerCup)
    50.                 Success();
    51.             else
    52.             {
    53.                 Console.WriteLine("You didn't use the right amount of change. \n");
    54.                 Refund();
    55.             }
    56.  
    57.         }
    58.  
    59.         public void Success()
    60.         {
    61.             if (cupsCoffeeAvail > 0)
    62.             {
    63.                 Console.WriteLine("You receive a cup of coffee. \n");
    64.                 cupsCoffeeAvail -= 1;
    65.             }
    66.             else
    67.                 Refund();
    68.         }
    69.  
    70.         public void Refund()
    71.         {
    72.             char rfndChoice = IO.GetChar("Do you want a refund? (y or n): ");
    73.             if (rfndChoice == 'y')
    74.             {
    75.                 Console.WriteLine("You are refunded: {0:C} \n", moneyInsrtd);
    76.             }
    77.             else
    78.             {
    79.                 Console.WriteLine("Your donation will be given to the homeless.\n");
    80.                 Select();
    81.             }
    82.         }
    83.  
    84.  
    85.     }
    86. }
    Last edited by gjon; Oct 24th, 2006 at 11:21 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I so thought I had this but I don't - I am not creating multiple objects I don't thin

    I very rarely download other people's projects and you'll find that many others here are the same. I very much doubt that it wouldn't be possible for you to simply post the relevant code directly. Also, I don't know if you have or not but the majority of people who post projects just zip up the solution folder. If you've done that without removing the 'bin' and 'obj' folders, or at least the binariy files they contain, then you're breaking forum rules.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: I so thought I had this but I don't - I am not creating multiple objects I don't thin

    Thanks for the heads up. I posted it, I hope that helps.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I so thought I had this but I don't - I am not creating multiple objects I don't thin

    I wouldn't have chosen to structure things quite like that but at a glance I can't see anything obvious that would cause your issue. You should be able to debug and find the issue though. Place a breakpoint on the line that calls Select and then add a few appropriate properties to the Watch window and just step through your code using F10 and F11. You should be able to see exactly what's happening and exactly where what DOES happen doesn't match what SHOULD happen.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: I so thought I had this but I don't - I am not creating multiple objects I don't thin

    Ok, I think I understand more of what's going on.

    I redid my class for the object and I realized that I was putting all my methods in the object class which might be causing some of my problems.

    Ok, now this is the biggee for me.

    VB Code:
    1. public static void Insert(int q, int d, int n)
    2.             {
    3.                 double quarters = q * .25;
    4.                 double dimes = d * .10;
    5.                 double nickels = n * .05;
    6.                 CoffeeVendingMachine.mnyInsrtd = q + d + n;
    7.  
    8.                 if (mnyInsrtd == pricePerCup)
    9.                     Success();
    10.                 else
    11.                 {
    12.                     Console.WriteLine("You didn't use the right amount of change. \n");
    13.                     Refund();
    14.                 }
    15.             }

    If I want to take cups away from a decaff object instead of the caffeine object. How can I do this?
    At method Success()
    {
    amountOfCups -= 1;
    }
    The thing is I don't understand how I can make it so that when I had already chosen Decaff, that this method will have the ability to take away the right cup from the right object.
    The problem with Object.amountOfCups -= 1; is that I don't know what object they picked, the Decaff or the Caffeinated. How can I pass that kind of information around is what I guess I am looking for.
    God, I hope that helps. It might take a time or two for me to get this detailed enough to understand what I am trying to figure out.

  6. #6

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: I so thought I had this but I don't - I am not creating multiple objects I don't thin

    VB Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4.  
    5. namespace VendingMachine
    6. {
    7.     class CoffeeVendingMachine
    8.     {
    9.         private int cupsAvail = 0;
    10.         private double costPerCup = 0.0;
    11.         public static double mnyInsrtd = 0.0;
    12.  
    13.         public CoffeeVendingMachine()
    14.         {
    15.             cupsAvail = 2;
    16.             costPerCup = .75;
    17.         }
    18.  
    19.         public CoffeeVendingMachine(int cups, double cost)
    20.         {
    21.             cupsAvail = cups;
    22.             costPerCup = cost;
    23.         }
    24.  
    25.         public double getCostPrCp()
    26.         {
    27.             return costPerCup;
    28.         }
    29.  
    30.         public double getCpsAvail()
    31.         {
    32.             return cupsAvail;
    33.         }
    34.  
    35.     }
    36.  
    37. }
    VB Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Text;
    4.  
    5. namespace VendingMachine
    6. {
    7.     class Prog5_22
    8.     {
    9.         static void Main(string[] args)
    10.         {
    11.             Menu();
    12.         }
    13.  
    14.         private static void Menu()
    15.         {
    16.             char Choice;
    17.             Choice = IO.GetChar("Choose Decaffeinated(d) or Caffeinated(c): ");
    18.             if(Choice == 'd')
    19.             {
    20.                 CoffeeVendingMachine Decaff = new CoffeeVendingMachine(10, .5);
    21.                 Console.WriteLine("The price of Decaffeinated Coffee is: {0:C}", Decaff.getCostPrCp());
    22.                 Console.WriteLine("There are {0} cups of Decaff left:", Decaff.getCpsAvail());
    23.                 Console.WriteLine("Please insert exact change.");
    24.                 Select();
    25.             }
    26.             else if (Choice == 'c')
    27.             {
    28.                 CoffeeVendingMachine Caffeinated = new CoffeeVendingMachine();
    29.                 Console.WriteLine("The price of Caffeinated Coffee is: {0:C}", Caffeinated.getCostPrCp());
    30.                 Console.WriteLine("There are {0} cups of Caffeine left:", Caffeinated.getCpsAvail());
    31.                 Console.WriteLine("Please insert exact change.");
    32.                 Select();
    33.             }
    34.             else
    35.             {
    36.                 Console.WriteLine("You made an incorrect Selection.\n");
    37.                 Menu();
    38.             }
    39.  
    40.          }
    41.  
    42.         public static void Select()
    43.             {
    44.                 int nmbrQurtrs = IO.GetInt("Enter the number of quarters: ");
    45.                 int nmbrDimes = IO.GetInt("Enter the number of dimes: ");
    46.                 int nmbrNickels = IO.GetInt("Enter the number of nickels: ");
    47.                 Insert(nmbrQurtrs, nmbrDimes, nmbrNickels);
    48.             }
    49.  
    50.         public static void Insert(int q, int d, int n)
    51.             {
    52.                 double quarters = q * .25;
    53.                 double dimes = d * .10;
    54.                 double nickels = n * .05;
    55.                 CoffeeVendingMachine.mnyInsrtd = q + d + n;
    56.  
    57.                 if (mnyInsrtd == pricePerCup)
    58.                     Success();
    59.                 else
    60.                 {
    61.                     Console.WriteLine("You didn't use the right amount of change. \n");
    62.                     Refund();
    63.                 }
    64.             }
    65.        
    66.     }
    67. }
    The thing is: When I get to the method Success(); How can I take the cups away from the right vending machine object. How can I know that it's Decaff.cups or Caffeine.cups at this point for the method to take away from the right object. After redoing this whole project, I think that has now become the big hurdle for me.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I so thought I had this but I don't - I am not creating multiple objects I don't thin

    This is what I meant about the structure. It makes no sense that the Insert method be declared 'static'. Think about what a static method is. It's a method of the class itself, not an instance of that class. In the real world is there just one place that you can put money and some internal mechanism then decides which vending machine that money will go to? Absolutely not. Every individual vending machine has its own coin slot and you put money into that slot for that machine. Insert should ABSOLUTELY be an instance method.

    Also, your Select method shouldn't be a member of the VendingMachine class at all. What has your choice got to do with the vending machine? If you were standing in front of two vending machines and you chose one, is that a function of the vending machines? Were they in any way involved in that choice? Absolutely not. Your selection should be completely independent of the VendingMachine class.

    Here's a quickie of how I'd do it:
    Code:
    class VendingMachine
    {
        public const decimal price = 2.50M;
    
        private int _cupCount;
        private decimal amountPaid = 0.0M;
    
        public int CupCount
        {
            get
            {
                return this._cupCount;
            }
        }
    
        public VendingMachine(int cupCount)
        {
            this._cupCount = cupCount;
        }
    
        public void AddCups(int cupCount)
        {
            this._cupCount += cupCount;
        }
    
        public bool Dispense()
        {
            if (this._cupCount > 0 && this.amountPaid >= price)
            {
                this._cupCount--;
                this.amountPaid -= price;
    
                return true;
            }
            else
            {
                return false;
            }
        }
    
        public bool Pay(decimal amount)
        {
            this.amountPaid += amount;
    
            return this.amountPaid >= price;
        }
    
        public decimal Refund()
        {
            decimal value = this.amountPaid;
            this.amountPaid = 0.0M;
    
            return value;
        }
    }
    Note that that is, as I said, just a quickie. With time I'd refine that considerably. I'd then use it something like this:
    Code:
    VendingMachine regular = New VendingMachine(50);
    VendingMachine decaf = New VendingMachine(50);
    
    // Make a choice which should be completely independent of the VendingMachine class.
    VendingMachine selected = decaf;
    
    selected.Pay(3M);
    selected.Dispense();
    
    decimal change = selected.Refund();
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: I so thought I had this but I don't - I am not creating multiple objects I don't thin

    I don't know why I didn't realize that. You make it sound like perfect sense. I do have to construct my objects and their properties better. With practice I hope I will be able to do so.

    At the point:
    // Make a choice which should be completely independent of the VendingMachine class.
    VendingMachine selected = decaf;

    selected.Pay(3M);
    selected.Dispense();

    decimal change = selected.Refund();

    I believe selected now becomes a variable with a reference to the object you told it to hold. That was exactly what I was trying to figure out how to do. I can set up a variable to hold which drink object was chosen and then manipulate it from there.
    OMG, thank you so much!

Posting Permissions

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



Click Here to Expand Forum to Full Width