简体   繁体   中英

How do I get both values from my property to be returned to my main method?

The following code should return 2 values which are fields in the properties WallArea and GallonsOfPaint . This should be returned to the main method. The Room class contain 2 private methods CalcWallArea and CalcAmountOfPaint which will set the value for the two properties I just mentioned.

The problem is it will only return one of the other. I cannot get it to return both. The outcome should be when a user enter the length. width and height the methods will tell the square footage of the room and also tell how many gallons of paint it will take to paint the room. Currently when ran it will only tell the square footage or if I call the 2nd method then it will tell how many gallons of paint but it will not do both. Can someone please assist?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Array;

namespace PaintingRoomDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Room aRoom = new Room();
            string numberString;

            WriteLine("Please enter the length of the wall in feet");
            numberString = ReadLine();

            aRoom.Length = Convert.ToInt32(numberString);

            WriteLine("Please enter the width of the wall in feet");
            numberString = ReadLine();

            aRoom.Width = Convert.ToInt32(numberString);

            WriteLine("Please enter the height of the wall in feet");
            numberString = ReadLine();

            aRoom.Height = Convert.ToInt32(numberString);

            Write("The room area is: {0} and requires {1} gallons of paint",
                aRoom.WallArea, aRoom.GallonsOfPaint);
            ReadLine();
        }
    }
    class Room
    {
        private int wallArea; //These are data fields//
        private int numberOfGallonsOfPaintNeeded; //These are data fields//
        private int length; //These are data fields//
        private int width; //These are data fields//
        private int height; //These are data fields//
        private int total;
        public int Length //This is a property which provides access to the data field length
        {
            get {return length;}
            set {length = value;}
        }
        public int Width //This is a property which provides access to the data field width
        {
            get {return width;}
            set {width = value;}
        }
        public int Height //This is a property which provides access to the data field height
        {
            get {return height;}
            set { height = value; CalcWallArea();}
        }
        public int WallArea //This is a property that should return wallArea
        {
            get {return wallArea;}
        }
        public int GallonsOfPaint //This is a property that should return wallArea
        {
            get {return numberOfGallonsOfPaintNeeded;}
        }
        private void CalcWallArea() //This is a private method that needs to be called to add the value to CalcAmountOfPaint field
        {
            wallArea = (Length + Width + Length + Width) * Height;
            CalcAmountOfPaint();
        }
        private void CalcAmountOfPaint() //This is a private method that needs to be called to add the value to CalcAmountOfPaint field
        {
            if (wallArea <= 350)
                numberOfGallonsOfPaintNeeded = 1;

            int x = 1;
            if (wallArea >= 350)
                while (wallArea > 0)
                {
                    x++;
                    wallArea = wallArea - wallArea;
                    numberOfGallonsOfPaintNeeded = x;
                }
        }
    }
}

There are a few changes I would recommend to help you get the desired behavior.

First, I would recommend against calling methods that change the state of your class in a Property setter or getter, as this typically leads to difficult to reason through code.

I would also recommend changing your two functions to return the desired value, rather than having them change the state on the field, and letting the properties simply return those values.

As for your immediate problem, this issue is in your CalcGallonsOfPaint method.

while (wallArea > 0)
{
    x++;
    wallArea = wallArea - wallArea; // <- right here
    numberOfGallonsOfPaintNeeded = x;
}

This part of the calculation will always set the wall area to 0, as it's subtracting it's full value from itself. I suspect that you mean to subtract a value of 350, but you're also changing the field value that's used to return the WallArea. At the least, you should assign wallArea to a temporary variable and subtract from that.

Still, it's likely better to do away with how the state of the object is being affected by calls to these properties and methods.

To that end, I'd adjust your Room class accordingly:

class Room
{
    public int Length {get;set;}
    public int Width {get;set;}
    public int Height {get;set;}
    public int WallArea
    {
        get {return CalcWallArea();}
    }
    public int GallonsOfPaint 
    {
        get {return CalcGallonsOfPaint();}
    }
    private int CalcWallArea()
    {
        // I am assuming this calculation is correct for your needs.
        return (Length + Width + Length + Width) * Height;
    }
    private int CalcAmountOfPaint() 
    {
        var area = WallArea;
        if (area <= 350)
            return 1;

        int x = 0;
        while (area > 0)
        {
            x++;
            area -= 350;
        }
        return x;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM