Nackey

BoxDataValidation

Feb 22nd, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2.  
  3. public class StartUp
  4. {
  5.     public static void Main()
  6.     {
  7.         double length = double.Parse(Console.ReadLine());
  8.         double width = double.Parse(Console.ReadLine());
  9.         double height = double.Parse(Console.ReadLine());
  10.  
  11.         var box = new Box(length, width, height);
  12.  
  13.         try
  14.         {
  15.             double surfaceArea = box.SurfaceArea();
  16.             double lateralSurfaceArea = box.LateralSurfaceArea();
  17.             double volume = box.Volume();
  18.  
  19.             Console.WriteLine($"Surface Area - {surfaceArea:f2}");
  20.             Console.WriteLine($"Lateral Surface Area - {lateralSurfaceArea:f2}");
  21.             Console.WriteLine($"Volume - {volume:f2}");
  22.         }
  23.         catch (ArgumentException argEx)
  24.         {
  25.             Console.WriteLine(argEx.Message);
  26.         }
  27.     }
  28. }
  29.  
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Text;
  33.  
  34. public class Box
  35. {
  36.    
  37.     private double length;
  38.  
  39.     public double Length
  40.     {
  41.         set
  42.         {
  43.             if (value <= 0)
  44.             {
  45.                 throw new ArgumentException("Length cannot be zero or negative.");
  46.             }
  47.             this.length = value;
  48.         }
  49.     }
  50.  
  51.     private double width;
  52.  
  53.     public double Width
  54.     {
  55.         set
  56.         {
  57.             if (value <= 0)
  58.             {
  59.                 throw new ArgumentException("Width cannot be zero or negative.");
  60.             }
  61.             this.width = value;
  62.         }
  63.     }
  64.  
  65.     private double height;
  66.  
  67.     public double Height
  68.     {
  69.         set
  70.         {
  71.             if (value <= 0)
  72.             {
  73.                 throw new ArgumentException("Height cannot be zero or negative.");
  74.             }
  75.             this.height = value;
  76.         }
  77.     }
  78.  
  79.     public Box(double length, double width, double height)
  80.     {
  81.         this.Length = length;
  82.         this.Width = width;
  83.         this.Height = height;
  84.     }
  85.  
  86.     public double SurfaceArea()
  87.     {
  88.         double surfaceArea = 2 * length * width + 2 * length * height + 2 * width * height;
  89.         return surfaceArea;
  90.     }
  91.  
  92.     public double LateralSurfaceArea()
  93.     {
  94.         double lateralSurfaceArea = 2 * length * height + 2 * width * height;
  95.         return lateralSurfaceArea;
  96.     }
  97.  
  98.     public double Volume()
  99.     {
  100.         double volume = length * width * height;
  101.         return volume;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment