-1

In my Orange class I have this method:

    public static List<Orange> AddOrange()
    {
        List<Orange> oranges = new List<Orange>();
        oranges.Add(new Orange() { Weight = 150, Measure = 6 });
        oranges.Add(new Orange() { Weight = 160, Measure = 6 });
        oranges.Add(new Orange() { Weight = 160, Measure = 6 });
        oranges.Add(new Orange() { Weight = 150, Measure = 6 });
        oranges.Add(new Orange() { Weight = 160, Measure = 6 });
        oranges.Add(new Orange() { Weight = 160, Measure = 6 });

        return oranges;
    }

And in my OrangeJuice class I have this method

public static int CreateJuice(List<Orange> oranges )
    {
        var bottle = new Bottle();
        var bottle2 = new Bottle();
        var cork = new Cork();
        var cork2 = new Cork();

        var orangeJuice = new OrangeJuice(1, 33, oranges, bottle, cork, 20);
        var orangeJuice2 = new OrangeJuice(2,33,oranges, bottle2,cork2, 20);

        var order = new Order();
        order.OrangeJuices.Add(orangeJuice);
        order.OrangeJuices.Add(orangeJuice2);

        var totalPrice = order.OrangeJuices.Sum(x => x.Price);

        return totalPrice;
    }

What I want to do to take the oranges I created on AddOrange method (six oranges) and put them in the CreateJuice method. So I get the list from the first method must somehow into the second method.

I'm wondering if I'm doing it correctly? And if not, how can I solve it?

slugster
  • 49,403
  • 14
  • 95
  • 145
LittleBird
  • 65
  • 9
  • 1
    You are doing it correctly! oragnes is your list of oranges in CreateJuice – Sam Marion Nov 29 '16 at 20:25
  • Where do you call the first method? It looks like the first method returns a list of oranges and the second method accepts a list of oranges. So whatever code is calling these methods would handle that. – David Nov 29 '16 at 20:26
  • 1
    var myJuice = CreateJuice(AddOrange()); – slugster Nov 29 '16 at 20:26
  • I have not called the first method (AddOrange) yet. I'm not sure how and where I can call it. All I want to do is to take the list from AddOrange method and insert it so I can create OrangeJuice – LittleBird Nov 29 '16 at 20:30
  • @slugster I think that's exactly what the OP is looking for. +1. – ispiro Nov 29 '16 at 21:15
  • I can't get it to work! When I type in this code: var totalSum = OrangeJuice.CreateJuice(AddOranges()); I get this error: The name AddOranges does not exist in the current context – LittleBird Nov 30 '16 at 16:46

1 Answers1

2

Normally the method called AddOrange should have been named as GetOranges

public static List<Orange> GetOranges()
{
    List<Orange> oranges = new List<Orange>();
    oranges.Add(new Orange() { Weight = 150, Measure = 6 });
    oranges.Add(new Orange() { Weight = 160, Measure = 6 });
    oranges.Add(new Orange() { Weight = 160, Measure = 6 });
    oranges.Add(new Orange() { Weight = 150, Measure = 6 });
    oranges.Add(new Orange() { Weight = 160, Measure = 6 });
    oranges.Add(new Orange() { Weight = 160, Measure = 6 });
    return oranges;
}

Then in the place you call the CreateJuice in you code, you only have to pass GetOranges.

CreateJuice(GetOranges());

or

var oranges = GetOranges();
var juice = CreateJuice(oranges);
Christos
  • 53,228
  • 8
  • 76
  • 108