I wrote a Bank.Dll and a soda machine, and every time i try "start without debugging" I always a get a underline blue wavy error. The code below are the bank.dll and the soda-machine code. Can any 1 help me to test this code, and then tell me what i did wrong. Thank you
" CoinService service = new CoinService(); " the error line.
Code:using System; namespace Bank { public class CoinReserve { // Fields private int dimes; private int nickels; private int quarters; // Methods public CoinReserve(int _nickels, int _dimes, int _quarters) { this.nickels = this.dimes = this.quarters = 0; if (_nickels > 0) { this.nickels = _nickels; } if (_dimes > 0) { this.dimes = _dimes; } if (_quarters > 0) { this.quarters = _quarters; } } // Properties public int Dimes { get { return this.dimes; } set { this.dimes = value; } } public int Nickels { get { return this.nickels; } set { this.nickels = value; } } public int Quarters { get { return this.quarters; } set { this.quarters = value; } } } public class CoinService { // Fields private static CoinReserve CR = new CoinReserve(50, 50, 50); private decimal totalcoinreserve = (((CR.Nickels * 0.05M) + (CR.Dimes * 0.1M)) + (CR.Quarters * 0.25M)); // Methods public int CalculateChange(decimal cost, decimal deposit, ref decimal change) { if (cost > deposit) { return -1; } change = deposit - cost; return 0; } public int CheckChange(decimal change) { if (change > 0M) { while (((change != 0M) && ((change% 0.25M) == 0M)) && (CR.Quarters != 0)) { change -= 0.25M; } while (((change != 0M) && ((change% 0.10M) == 0M)) && (CR.Dimes != 0)) { change -= 0.10M; } while (((change != 0M) && ((change% 0.05M) == 0M)) && (CR.Nickels != 0)) { change -= 0.05M; } if (change == 0M) { return 0; } } return -1; } public int DepositCoins(int nickels, int dimes, int quarters) { CR.Nickels += nickels; CR.Dimes += dimes; CR.Quarters += quarters; this.totalcoinreserve = ((CR.Nickels * 0.05M) + (CR.Dimes * 0.1M)) + (CR.Quarters * 0.25M); return 0; } public int DispenseChange(decimal change) { if (change <= 0M) { return -1; } while (((change != 0M) && ((change% 0.25M) == 0M)) && (CR.Quarters != 0)) { CR.Quarters--; change -= 0.25M; } while (((change != 0M) && ((change% 0.10M) == 0M)) && (CR.Dimes != 0)) { CR.Dimes--; change -= 0.10M; } while (((change != 0M) && ((change% 0.05M) == 0M)) && (CR.Nickels != 0)) { CR.Nickels--; change -= 0.05M; } this.totalcoinreserve = ((CR.Nickels * 0.05M) + (CR.Dimes * 0.1M)) + (CR.Quarters * 0.25M); return 0; } public void DisplayCoinReserve() { Console.Clear(); Console.WriteLine("Nickel(s): {0}", CR.Nickels); Console.WriteLine("Dime(s): {0}", CR.Dimes); Console.WriteLine("Quarter(s): (s): {0}", CR.Quarters); Console.WriteLine("Total: {0,-4:c2}]", (CR.Nickels + CR.Dimes) + CR.Quarters); } // Properties public decimal TotalCoinReserve { get { return this.totalcoinreserve; } } } } _________________________________________________________________ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sodamachine { public class CoinInserts { // Fields private int dimes; private int nickels; private int quarters; // Methods public CoinInserts(int n, int d, int q) { this.nickels = n; this.dimes = d; this.quarters = q; } // Properties public int Dimes { get { return this.dimes; } set { this.dimes = value; } } public int Nickels { get { return this.nickels; } set { this.nickels = value; } } public int Quarters { get { return this.quarters; } set { this.quarters = value; } } } public class CoinBox { // Fields private static CoinInserts CI = new CoinInserts(0, 0, 0); // Methods public int ClearCoinInsert() { CI.Nickels = 0; CI.Dimes = 0; CI.Quarters = 0; return 0; } public int GetChange(decimal cost, ref decimal change) { decimal num = 0M; num = ((0.05M * CI.Nickels) + (0.10M * CI.Dimes)) + (0.25M * CI.Quarters); if (num >= cost) { change = num - cost; return 0; } return -1; } public int InsertCoin(ref decimal total) { while (true) { total = ((0.05M * CI.Nickels) + (0.10M * CI.Dimes)) + (0.25M * CI.Quarters); Console.Clear(); Console.WriteLine(" "); Console.WriteLine("***********************"); Console.WriteLine("** INSERT COINS MENU **"); Console.WriteLine("***********************"); Console.WriteLine(" "); Console.WriteLine("1: ({0}) Insert a nickel", CI.Nickels); Console.WriteLine("2: ({0}) Insert a dime", CI.Dimes); Console.WriteLine("3: ({0}) Insert a quarter", CI.Quarters); Console.WriteLine(" "); Console.Write("PLEASE INSERT COINS [{0,-4:c2}] (or Q): ", (decimal)total); string s = Console.ReadLine(); switch (s) { case "Q": case "q": total = ((0.05M * CI.Nickels) + (0.10M * CI.Dimes)) + (0.25M * CI.Quarters); return 0; default: int num; if (int.TryParse(s, out num)) { switch (num) { case 1: CI.Nickels++; break; case 2: CI.Dimes++; break; case 3: CI.Quarters++; break; } } break; } } } // Properties public int Dimes { get { return CI.Dimes; } set { CI.Dimes = value; } } public int Nickels { get { return CI.Nickels; } set { CI.Nickels = value; } } public int Quarters { get { return CI.Quarters; } set { CI.Quarters = value; } } } public class Product { // Fields private decimal cost; private int count; private string name; // Properties public decimal Cost { get { return this.cost; } set { this.cost = value; } } public int Count { get { return this.count; } set { this.count = value; } } public string Name { get { return this.name; } set { this.name = value; } } } public class InventoryManager { // Fields private Dictionary<int, Product> inventory = new Dictionary<int, Product>(); private static int key = 0; // Methods public void AddProduct(string name, decimal cost, int count) { Product product = new Product(); this.inventory.Add(key, product); this.inventory[key].Name = name; this.inventory[key].Cost = cost; this.inventory[key].Count = count; key++; } public int DispenseProduct(int ProductKey) { Product local1 = this.inventory[ProductKey]; local1.Count--; return 0; } public void DisplayInventory() { Console.Clear(); Console.WriteLine(" "); Console.WriteLine("**PRODUCT INVENTORY **"); Console.WriteLine(" "); for (int i = this.inventory.Count; i <= 0; i++) { Console.WriteLine("{0}: {1} [{2,-4:c2}]", i + 1, this.inventory[i].Name, this.inventory[i].Cost); } } public int SelectProduct(ref int key, ref string name, ref decimal cost) { while (true) { int num2; Console.Clear(); Console.WriteLine(" "); Console.WriteLine("*****************************"); Console.WriteLine("** PRODUCT SELECTION MENU: **"); Console.WriteLine("*****************************"); Console.WriteLine(" "); for (int i = 0; i < this.inventory.Count; i++) { Console.WriteLine("{0}: {1} [{2, -4:c2}]", i + 1, this.inventory[i].Name, this.inventory[i].Cost); } Console.WriteLine(" "); Console.Write("PLEASE SELECT A PRODUCT (or Q):"); string s = Console.ReadLine(); switch (s) { case "Q": case "q": Console.Clear(); key = -1; name = "None"; cost = 0M; return -1; } if (int.TryParse(s, out num2) && (this.inventory.ContainsKey(num2 - 1) && (this.inventory[num2 - 1].Count > 0))) { key = num2 - 1; cost = this.inventory[key].Cost; name = this.inventory[key].Name; return 0; } } } } internal class Program { // Methods private static void Main(string[] args) { CoinService service = new CoinService(); CoinBox box = new CoinBox(); InventoryManager manager = new InventoryManager(); manager.AddProduct("Water", 1.5M, 10); manager.AddProduct("Coke", 1.1M, 10); manager.AddProduct("Pepsi", 1.5M, 10); manager.AddProduct("Ice Tea", 1.25M, 10); int key = 0; decimal num3 = 0M; string str = ""; string str2 = ""; decimal num4 = 0M; decimal change = 0M; while (true) { Console.Clear(); Console.WriteLine(" "); Console.WriteLine("****************************"); Console.WriteLine("** SODA MACHINE MAIN MENU **"); Console.WriteLine("****************************"); Console.WriteLine(" "); Console.WriteLine("1: Select a Product [{0}: {1,-4:c2}]", str, num3); Console.WriteLine("2: Insert Coins [{0,-4:c2}]", num4); Console.WriteLine("3: Dispense Product [{0}]", str2); Console.WriteLine(" "); Console.Write("PLEASE MAKE A MENU SELECTION [{0,-4:c2}] (or Q):", service.TotalCoinReserve); string s = Console.ReadLine(); switch (s) { case "Q": case "q": if (num4 > 0M) { Console.WriteLine(" "); Console.WriteLine("Please Pickup Your Change [{0,-4:c2}]", num4); } return; default: int num; if (int.TryParse(s, out num)) { int num6; switch (num) { case 1: num6 = manager.SelectProduct(ref key, ref str, ref num3); break; case 2: num6 = box.InsertCoin(ref num4); break; case 3: if (!(num3 == 0M)) { if (num4 >= num3) { str2 = ""; num6 = box.GetChange(num3, ref change); if (service.CheckChange(change) != 0) { str2 = "Insufficient bank reserve"; } else { num6 = manager.DispenseProduct(key); num6 = service.DepositCoins(box.Nickels, box.Dimes, box.Quarters); num6 = service.DispenseChange(change); num6 = box.ClearCoinInsert(); num4 = 0M; str2 = "Enjoy your " + str; } } else { str2 = "Insert more money"; } break; } break; } } break; } } } } }
Last edited by Orjan; 02-25-2010 at 09:07 PM.
Make sure you add a reference to the .dll in your project or else your program will not recognize it. Also please use code tags next time it is kind of hard to read.
pasted all code into vs and works. Only one problem: Dispense Product doesn't work when you have equal coins and product values. Probably something with CheckChange in CoinService.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks