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:
using System; using System.Collections.Generic; using System.Text; namespace Prog5_22_Rev2 { class Prog5_22 { static void Main(string[] args) { VendingMachine Caffeine = new VendingMachine(); VendingMachine DeCaff = new VendingMachine(10, .50); if (VendingMachine.Select() == 'c') { do { Caffeine.Menu(); } while (Caffeine.cupsCoffeeAvail > 0); } else if (VendingMachine.Choice == 'd') { do { DeCaff.Menu(); } while (DeCaff.cupsCoffeeAvail > 0); } else { Console.WriteLine("Invalid Choice"); Console.WriteLine("So, because you tried to hack me:"); } Console.WriteLine("I am sorry, but my machine is out of coffe cups. =("); } } }
____________________________________
____________________________________
VendingMachine.cs
VB Code:
using System; using System.Collections.Generic; using System.Text; namespace Prog5_22_Rev2 { class VendingMachine { private double moneyInsrtd = 0.0; //exact change only public int cupsCoffeeAvail = 0; private double pricePerCup = 0.0; public static char Choice; public VendingMachine() { cupsCoffeeAvail = 10; pricePerCup = .50; } public VendingMachine(int cupsAvail, double price) { cupsCoffeeAvail = cupsAvail; pricePerCup = price; } public static char Select() { Choice = IO.GetChar("Would you like Caffeinated(c) or Decaffeinated(d) coffe? (c or d): "); return Choice; } public void Menu() { Console.WriteLine("A cup of coffee costs {0:C}, there are {1} cups left. \n Please enter exact change.", pricePerCup, cupsCoffeeAvail); int nmbrQurtrs = IO.GetInt("Enter the number of quarters: "); int nmbrDimes = IO.GetInt("Enter the number of dimes: "); int nmbrNickels = IO.GetInt("Enter the number of nickels: "); Insert(nmbrQurtrs, nmbrDimes, nmbrNickels); } public void Insert(int quarters, int dimes, int nickels) { double q = quarters * .25; double d = dimes * .10; double n = nickels * .05; moneyInsrtd = q + d + n; if (moneyInsrtd == pricePerCup) Success(); else { Console.WriteLine("You didn't use the right amount of change. \n"); Refund(); } } public void Success() { if (cupsCoffeeAvail > 0) { Console.WriteLine("You receive a cup of coffee. \n"); cupsCoffeeAvail -= 1; } else Refund(); } public void Refund() { char rfndChoice = IO.GetChar("Do you want a refund? (y or n): "); if (rfndChoice == 'y') { Console.WriteLine("You are refunded: {0:C} \n", moneyInsrtd); } else { Console.WriteLine("Your donation will be given to the homeless.\n"); Select(); } } } }




Reply With Quote