Advertisement
simonradev

BoxClass

Jun 30th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. namespace ClassBox
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.  
  9.     public class Box
  10.     {
  11.         private double length;
  12.         private double width;
  13.         private double height;
  14.  
  15.         public Box(double length, double width, double height)
  16.         {
  17.             this.Length = length;
  18.             this.Width = width;
  19.             this.Height = height;
  20.         }
  21.  
  22.         public double Length
  23.         {
  24.             get
  25.             {
  26.                 return this.length;
  27.             }
  28.  
  29.             set
  30.             {
  31.                 ThrowExceptionIfSideIsNegative(value, nameof(this.Length));
  32.  
  33.                 this.length = value;
  34.             }
  35.         }
  36.  
  37.         public double Width
  38.         {
  39.             get
  40.             {
  41.                 return this.width;
  42.             }
  43.  
  44.             set
  45.             {
  46.                 ThrowExceptionIfSideIsNegative(value, nameof(this.Width));
  47.  
  48.                 this.width = value;
  49.             }
  50.         }
  51.  
  52.         public double Height
  53.         {
  54.             get
  55.             {
  56.                 return this.height;
  57.             }
  58.  
  59.             set
  60.             {
  61.                 ThrowExceptionIfSideIsNegative(value, nameof(this.Height));
  62.  
  63.                 this.height = value;
  64.             }
  65.         }
  66.  
  67.         private void ThrowExceptionIfSideIsNegative(double side, string sideName)
  68.         {
  69.             if (side <= 0)
  70.             {
  71.                 Console.WriteLine($"{sideName} cannot be zero or negative.");
  72.  
  73.                 Environment.Exit(0);
  74.             }
  75.         }
  76.  
  77.         private double Multiply(double first, double second)
  78.         {
  79.             return (first * second) * 2;
  80.         }
  81.  
  82.         public double LateralSurfaceArea()
  83.         {
  84.             return Multiply(length, height) + Multiply(width, height);
  85.         }
  86.  
  87.         public double SurfaceArea()
  88.         {
  89.             return Multiply(length, width) + Multiply(length, height) + Multiply(width, height);
  90.         }
  91.  
  92.         public double Volume()
  93.         {
  94.             return length * width * height;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement