コード例 #1
0
 public object visit(fruit f)
 {
     Console.WriteLine("I'm eating the fruit "+f);
     cals += f.Calories;
     if (cals<max) return f.nextitem.accept(this);
     else return cals;
 }
コード例 #2
0
 static void Main(string[] args)
 {
     fruit[] allfruits = new fruit[2];
     allfruits[0] = new mango();
     allfruits[1] = new banana();
     for (int i = 0; i < 2; i++)
     {
         allfruits[i].collect();
     }
 }
コード例 #3
0
ファイル: ReferenceType.cs プロジェクト: Alvin35/Assignment
        static void Main(string[] args)
        {
            fruit ex1 = new fruit();
            fruit ex2 = new fruit();

            ex1.amount = 5;
            ex2        = ex1;
            ex2.amount = 6;
            Console.WriteLine(ex1.amount);
            Console.WriteLine(ex2.amount);
        }
コード例 #4
0
    public static void Main(string[] args)
    {
        fruit a = new fruit("apple",50);
        fruit b = new fruit("watermellon",200);
        vegetable c = new vegetable("cabbage soup",150,"white");
        vegetable d = new vegetable("spinach",1000,"green");
        meat e = new meat("chicken",Meattype.white);
        meat f = new meat("beef",Meattype.red);
        c.nextitem = e; e.nextitem=a; a.nextitem=f;
        f.nextitem = b; b.nextitem = d;
        fooditem thefood = c; // start of list;

        // eat visitor
        eater popeye = new eater(4000); // popeye needs 4000 calories
        int ate = (int) thefood.accept(popeye);
        Console.WriteLine("popeye ate "+ate+" calories");

        //problem 0 visitor
        sumFoodList problem0 = new sumFoodList();
        Console.WriteLine("Problem 0 results below:");
        Console.WriteLine(c.accept(problem0));

        // healthinspector picks out healthiest food item
        healthinspector nutritionist = new healthinspector(c);
        fooditem best = (fooditem) thefood.accept(nutritionist);
        Console.WriteLine("nutritionist says the "+best+" is the healthiest.");

        // below is functioning problem 1 code
        beverage problem1 = new beverage("coca cola",400);
        problem1.nextitem = c;
        Console.WriteLine("Problem 1 results below:");
        Console.WriteLine(problem1.accept(new beveragevisitor()));

        //below is functioning problem 3 code
        //c is not ordered so we expect the output to be false
        Console.WriteLine(c.accept(new islistordered()));

        //now lets construct and ordered list and test
        fruit testfruit = new fruit("apple",50);;
        vegetable testvegetable = new vegetable("cabbage soup",150,"white");;
        meat testmeat= new meat("chicken",Meattype.white);
        testfruit.nextitem=testvegetable;
        testvegetable.nextitem=testmeat;
        fooditem orderedTestList= testfruit;
        // Prints out true test case passes ...on a side note this is such a small % of test cases but w.e it works
        Console.WriteLine(orderedTestList.accept(new islistordered()));

        Console.Read();
    }
コード例 #5
0
        private Boolean Lookupfruit(Int32 fruitId)      //Check if fruit exists.
        {
            fruitShopDbContext context = (fruitShopDbContext)Repository.GetDbContext();

            IQueryable <fruit> entityQuery = from fruit in context.fruits
                                             where fruit.Id.Equals(fruitId)
                                             select fruit;

            fruit f = entityQuery.FirstOrDefault();

            if (f == null)
            {
                Logger.ErrorFormat("Unable to find {0}", fruitId);
                throw new UserFriendlyException("Sorry I cannot find the fruit.");
            }
            return(true);
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: wittecode/ArrayOfFruit
        static void Main(string[] args)
        {
            fruit[] fruits = new fruit[4];
            string  userResponse;

            for (int index = 0; index < 4; index++)
            {
                Console.Write($"Enter Fruit #{index + 1}:");
                userResponse = Console.ReadLine();
                Enum.TryParse(userResponse, out fruits[index]);
            }

            Console.WriteLine();

            foreach (fruit Fruit in fruits)
            {
                Console.WriteLine(fruits);
            }

            Console.ReadKey();
        }
コード例 #7
0
 public object visit(fruit f)
 {
     foodSum+= f.Calories;
      	return f.nextitem.accept(this);
 }
コード例 #8
0
    public static void Main(string[] args)
    {
        fruit     a = new fruit("apple", 50);
        fruit     b = new fruit("watermellon", 200);
        vegetable c = new vegetable("cabbage soup", 150, "white");
        vegetable d = new vegetable("spinach", 1000, "green");
        meat      e = new meat("chicken", Meattype.white);
        meat      f = new meat("beef", Meattype.red);

        c.nextitem = e; e.nextitem = a; a.nextitem = f;
        f.nextitem = b; b.nextitem = d;
        fooditem thefood = c; // start of list;

        // eat visitor
        eater popeye = new eater(4000); // popeye needs 4000 calories
        int   ate    = (int)thefood.accept(popeye);

        Console.WriteLine("popeye ate " + ate + " calories");

        //problem 0 visitor
        sumFoodList problem0 = new sumFoodList();

        Console.WriteLine("Problem 0 results below:");
        Console.WriteLine(c.accept(problem0));



        // healthinspector picks out healthiest food item
        healthinspector nutritionist = new healthinspector(c);
        fooditem        best         = (fooditem)thefood.accept(nutritionist);

        Console.WriteLine("nutritionist says the " + best + " is the healthiest.");


        // below is functioning problem 1 code
        beverage problem1 = new beverage("coca cola", 400);

        problem1.nextitem = c;
        Console.WriteLine("Problem 1 results below:");
        Console.WriteLine(problem1.accept(new beveragevisitor()));

        //below is functioning problem 3 code
        //c is not ordered so we expect the output to be false
        Console.WriteLine(c.accept(new islistordered()));


        //now lets construct and ordered list and test
        fruit     testfruit     = new fruit("apple", 50);;
        vegetable testvegetable = new vegetable("cabbage soup", 150, "white");;
        meat      testmeat      = new meat("chicken", Meattype.white);

        testfruit.nextitem     = testvegetable;
        testvegetable.nextitem = testmeat;
        fooditem orderedTestList = testfruit;

        // Prints out true test case passes ...on a side note this is such a small % of test cases but w.e it works
        Console.WriteLine(orderedTestList.accept(new islistordered()));



        Console.Read();
    } // Main
コード例 #9
0
 public object visit(fruit f)
 {
     if (f.Calories < min.Calories) min = f;
       return f.nextitem.accept(this);
 }