0

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;
                }
        }
    }
}
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Demond
  • 109
  • 5
  • So to be clear the portion that is not working is the aRoom.GallonsOfPaint is not returning a value. It should at least return because 1 is the default value if the square footage is 350 or less. I know I need to call the method CalcAmountOfPaint but when I do that then aRoom.WallArea no longer works. It then returns 0 and aRoom.GallonsOfPaint then returns the correct amount. I cannot figure out how to make them both work. Hopefully someone can assist me with this and let me know what I am not understanding. – Demond Oct 06 '18 at 19:52
  • You are better of calculating on the get methods so you are sure all properties are updated correct – Aldert Oct 06 '18 at 19:53
  • @Demond: when you are calculating the gallons, you are also subtracting the wallArea value against itself. This will result in a final wallArea of 0. – Jonathon Chase Oct 06 '18 at 19:55

1 Answers1

0

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;
    }
}
Jonathon Chase
  • 9,396
  • 21
  • 39
  • thank you so much sir. I really do appreciate your assistance as I have been working on this for 3 days and have just ran out of options on things to try. You are awesome sir. – Demond Oct 06 '18 at 20:30