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

Threaded View

  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.

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