Advertisement
Guest User

Untitled

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