Advertisement
Guest User

Untitled

a guest
Jan 19th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _2.LaptopShop
  8. {
  9.     class Laptop
  10.     {
  11.         private string model;
  12.         private int price;
  13.         private string manufacturer;
  14.         private string processor;
  15.         private int ram;
  16.         private string graphics;
  17.         private string hdd;
  18.         private string screen;
  19.         private Battery battery;
  20.  
  21.         public Laptop(string model, int price)
  22.         {
  23.             this.Model = model;
  24.             this.Price = price;
  25.         }
  26.         public Laptop(string model, int price, string manufacturer = null, string processor = null, int ram = 0, string hdd = null, string graphicsCard = null, string screen = null, string type = null, int batteryLife = 0)
  27.             : this(model, price)
  28.         {                                                                                                        
  29.             this.Manufacturer = manufacturer;
  30.             this.Processor = processor;
  31.             this.Ram = ram;
  32.             this.Hdd = hdd;
  33.             this.Graphics = graphics;
  34.             this.Battery = battery;
  35.             this.Screen = screen;
  36.             this.battery = new Battery(type, batteryLife);
  37.         }
  38.  
  39.         public string Model
  40.         {
  41.             get { return this.model; }
  42.             set
  43.             {
  44.                 if (string.IsNullOrEmpty(value))
  45.                 {
  46.                     throw new ArgumentNullException("The model field can not be empty!");
  47.                 }
  48.                 this.model = value;
  49.             }
  50.         }
  51.         public int Price
  52.         {
  53.             get { return this.price; }
  54.             set
  55.             {
  56.                 if (value < 0)
  57.                 {
  58.                     throw new IndexOutOfRangeException("The price can not be negative!");
  59.                 }
  60.                 this.price = value;
  61.             }
  62.         }
  63.         public string Manufacturer
  64.         {
  65.             get { return this.manufacturer; }
  66.             set
  67.             {
  68.                 this.manufacturer = value;
  69.             }
  70.         }
  71.         public string Processor
  72.         {
  73.             get { return this.processor; }
  74.             set
  75.             {
  76.                 this.processor = value;
  77.             }
  78.         }
  79.         public int Ram
  80.         {
  81.             get { return this.ram; }
  82.             set
  83.             {
  84.                 this.ram = value;
  85.             }
  86.         }
  87.         public string Graphics
  88.         {
  89.             get { return this.graphics; }
  90.             set
  91.             {
  92.                 this.graphics = value;
  93.             }
  94.         }
  95.         public string Hdd
  96.         {
  97.             get { return this.hdd; }
  98.             set
  99.             {
  100.                 this.hdd = value;
  101.             }
  102.         }
  103.         public string Screen
  104.         {
  105.             get { return this.screen; }
  106.             set
  107.             { this.screen = value; }
  108.         }
  109.         public Battery Battery
  110.         {
  111.             get { return this.battery; }
  112.             set { this.battery = value; }
  113.         }
  114.  
  115.         public override string ToString()
  116.         {
  117.             StringBuilder laptopStringBuild = new StringBuilder();
  118.             laptopStringBuild.AppendLine("model: " + this.Model);
  119.             if (this.Manufacturer != null)
  120.             {
  121.                 laptopStringBuild.AppendLine("manufacturer: " + this.Manufacturer);
  122.             }
  123.             if (this.Processor != null)
  124.             {
  125.                 laptopStringBuild.AppendLine("processor: " + this.Processor);
  126.             }
  127.             if (this.Ram != 0)
  128.             {
  129.                 laptopStringBuild.AppendLine("RAM: " + this.Ram);
  130.             }
  131.             if (this.Hdd != null)
  132.             {
  133.                 laptopStringBuild.AppendLine("HDD: " + this.Hdd);
  134.             }
  135.             if (this.Screen != null)
  136.             {
  137.                 laptopStringBuild.AppendLine("screen: " + this.Screen);
  138.             }
  139.             if (this.Battery != null)
  140.             {
  141.                 laptopStringBuild.AppendLine(this.Battery.ToString());
  142.             }
  143.             laptopStringBuild.AppendLine("price: " + this.Price);
  144.             return laptopStringBuild.ToString();
  145.         }
  146.         }
  147.  
  148.    
  149.  
  150.     class Battery
  151.     {
  152.         private string type;
  153.         private double batteryLife;
  154.  
  155.         public Battery(string type)
  156.         {
  157.             this.Type = type;
  158.         }
  159.         public Battery(string type, int batteryLife)
  160.             : this(type)
  161.         {
  162.             this.BatteryLife = batteryLife;
  163.         }
  164.  
  165.         public string Type
  166.         {
  167.             get { return this.Type; }
  168.             set
  169.             {
  170.                 if (value == "")
  171.                 {
  172.                     throw new ArgumentException("Invalid type!");
  173.                 }
  174.                 this.type = value;
  175.             }
  176.         }
  177.         public double BatteryLife
  178.         {
  179.             get { return this.BatteryLife; }
  180.             set
  181.             {
  182.                 if (batteryLife < 0)
  183.                 {
  184.                     throw new ArgumentException("Battery life can not be negative!");
  185.                 }
  186.                 this.batteryLife = value;
  187.             }
  188.         }
  189.     }
  190.  
  191.  
  192.     class Program
  193.     {
  194.         static void Main(string[] args)
  195.         {
  196.             Laptop sample = new Laptop("Yoga", 34, "lenovo", "intel", 16, "ekran", "nvidia", "15", "bateriq", 999);
  197.  
  198.             Console.WriteLine(sample.ToString());
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement