コード例 #1
0
        static void Main(string[] args)
        {
            //            from README
            //            Visual studio 2010 - console application

            //A vending machine sells items for various prices and can give change. At the start of the day it is loaded with a certain number of coins of various denominations e.g. 100 x 1p, 50 x 5p, 50 x 10p etc.
            //When an item is requested a certain number of coins are provided.
            //Write code that models the vending machine and calculates the change to be given when an item is purchased (e.g. 2 x 20p used to purchase an item costing 25p might return 1 x 10p and 1 x 5p).

            //The application - allows any number of items to be purchased. Not just one.

            //Use of the application: run commands like: p UI_interact.txt.
            //The application was tested using the 3 text files provided. You can modify them or create new ones using the same structure as described below.

            //Input - from text files
            //structure:
            //first line in each file is about the items. Elements of the line are separated by commas. First element shows the fields for the item e.g value or quantity. The rest of the elements are the items (the fields are separated by tabs)
            //second line in each file is about the money. Elements of the line are separated by commas. First element shows the fields for the money e.g value or quantity. The rest of the elements are the money (the fields are separated by tabs)

            //Output - change given and status of money and items in the vending machine for every interaction.


            //about the coding
            //all done in one file (for easy archiving and explanations)
            //input is done through txt files (in the same folder as the executable) chosen by the user from the console.
            //two important classes for modelling the vending machine and the users actions - vendingMachine and userInteraction
            //two classes for objects - items from the vending machine and money
            //the interactions will succeed if items or money are available. if not, user is notified by messages
            //the use of 'ref' was just for debugging the program faster and to show the flow of operations easier in the lines below
            //hope all cases where covered. The 3 text files worked OK, in any succession in the same session. you can enter any number of commands like "p fileName.txt" in the same succession


            Console.WindowHeight = 60;  //increase height of the console
            Console.WindowWidth  = 140; //increase height of the console
            Program p = new Program();

            Console.WriteLine("Welcome to Vending Machine 1.0!");
            Console.WriteLine();

            //VM = vending machine
            vendingMachine VM = new vendingMachine();

            //init VM
            VM.items          = new List <item>();
            VM.money          = new List <money>();
            VM.VM_moneyGained = new List <money>();
            //other inits?

            //input
            VM.fill_VM(ref VM);
            //display VM initial items
            Console.Write("VM items filled today: ");
            p.display(VM.items);
            //display VM initial money
            Console.Write("VM money filled today: ");
            p.display(VM.money);
            Console.WriteLine();

            Console.WriteLine("Write 'p [fileName]' to purchase items. e.g.   p UI_interact.txt   or   p UI_interact2.txt");
            Console.WriteLine("Write 'q' to leave VM");
            Console.WriteLine();


            //cicle interactions
            int counter = 0;

            while (true)
            {
                string userConsole = Console.ReadLine();
                if (userConsole.Equals("q"))
                {
                    Console.Write("Money that came in VM today: ");
                    p.display(VM.VM_moneyGained);
                    break;
                }
                else if (userConsole.Contains('p'))
                {
                    string fileName = "UI_interact.txt";
                    try
                    {
                        fileName = userConsole.Split(' ')[1];
                        //try the path
                        StreamReader sr = new StreamReader(fileName);
                        sr.Dispose();
                    }
                    catch
                    {
                        Console.WriteLine("We do not have that product. Input error");
                        continue;
                    }

                    counter += 1;
                    Console.WriteLine("Interaction" + counter);

                    //interaction with VM
                    userInteraction UI = new userInteraction();
                    //init UI
                    UI.UI_itemsRequested = new List <item>();
                    UI.UI_moneyUsed      = new List <money>();
                    UI.UI_changeGiven    = new List <money>();
                    //other inits?

                    UI.interact(ref UI, fileName);
                    //display money used for interaction
                    Console.Write("Money used: ");
                    p.display(UI.UI_moneyUsed);

                    //calculate change
                    //(update vending machine) - items and money
                    //and give items and change! (update interaction)
                    VM.giveChange(ref VM, ref UI);

                    //display change
                    Console.Write("Change received: ");
                    p.display(UI.UI_changeGiven);
                    if (UI.UI_changeGiven.Count == 0)
                    {
                        //cannot give change
                        Console.WriteLine(VM.message);
                    }

                    //display VM money
                    Console.Write("VM money after interaction: ");
                    p.display(VM.money);

                    //display VM items
                    Console.Write("VM items after interaction: ");
                    p.display(VM.items);

                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Please don't hit the vending machine");
                }
                //ending interactions? or continue
            }

            //keep console open.
            Console.WriteLine();
            Console.WriteLine("Press any key to exit. Thank you for using our vending machine");
            Console.ReadKey();
        }