Advertisement
vlad0

Test

Feb 20th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. //Exercise 05.
  7. //Define several constructors for the defined classes that
  8. //take different sets of arguments (the full information for
  9. //the class or part of it). Assume that model and manufacturer are
  10. //mandatory (the others are optional). All unknown data fill with null.
  11.  
  12.  
  13. namespace _01.GSM_All_In_One
  14. {
  15.     //this is our enumerations with type of batteries
  16.     public enum BatteryType
  17.     {
  18.         LiIon, NiMh, NiCd
  19.     }
  20.  
  21.     class CheckData
  22.     {
  23.         //check for negative values
  24.         public static bool CheckNegative(double? Idle)
  25.         {
  26.             return Idle < 0;
  27.         }
  28.  
  29.         //check for null or String.Empty
  30.         public static bool CheckNull(string model)
  31.         {
  32.             return (model == null || model.Length < 1);
  33.            
  34.         }
  35.     }
  36.  
  37.     class Battery
  38.     {
  39.        
  40.         //<fields>
  41.         private string model;
  42.         private double? idle;
  43.         private double? talk;
  44.         //</fields>
  45.  
  46.         //<properties>
  47.         //battery model
  48.         public string Model
  49.         {
  50.             get
  51.             {
  52.                 return this.model;
  53.             }
  54.             set
  55.             {
  56.                 if (CheckData.CheckNull(value))
  57.                 {
  58.                     throw new ArgumentException("The battery model is mandatory field!");
  59.                 }
  60.  
  61.                 this.model = value;
  62.             }
  63.         }
  64.  
  65.         //battery consumptions - hours in idle mode
  66.         public double? Idle
  67.         {
  68.             get
  69.             {
  70.                 return this.idle;
  71.             }
  72.             set
  73.             {
  74.                 if (CheckData.CheckNegative(value))
  75.                 {
  76.                     throw new ArgumentException("The Idle Time Can't be Negative!");
  77.                 }
  78.                 this.idle = value;
  79.             }
  80.         }
  81.  
  82.         //battery consumptions - hours in talk mode
  83.         public double? Talk
  84.         {
  85.             get
  86.             {
  87.                 return this.talk;
  88.             }
  89.             set
  90.             {
  91.                 if (CheckData.CheckNegative(value))
  92.                 {
  93.                     throw new ArgumentException("The Talk Time Can't be Negative!");
  94.                 }
  95.                 this.talk = value;
  96.             }
  97.         }
  98.         public BatteryType? Type { get; set; } //batterry type of enumeration BatteryType
  99.         //</properties>
  100.  
  101.         //<constructors>
  102.         //we have one mandatory field for the model, actually it could be null
  103.         public Battery() { }
  104.  
  105.         public Battery(string model)
  106.             : this() //recalls Battery()
  107.         {
  108.  
  109.             this.Model = model;
  110.         }
  111.  
  112.         public Battery(string model, double? idle)
  113.             : this(model) //this recall Battery(string model)
  114.         {
  115.             this.Idle = idle;
  116.         }
  117.  
  118.         public Battery(string model, double? idle, double? talk)
  119.             : this(model, idle) //this recall Battery(string model, double? idle)
  120.         {
  121.             this.Talk = talk;
  122.         }
  123.  
  124.         public Battery(string model, double? idle, double? talk, BatteryType? type)
  125.             : this(model, idle, talk) //this recall Battery(string model, double? idle, double? talk)
  126.         {
  127.             this.Type = type;
  128.         }
  129.         //</constructors>
  130.  
  131.         //<methods>
  132.         //encapsulate values for Talk and Idle
  133.        
  134.  
  135.         //check null for mandatory fields
  136.        
  137.  
  138.         //Print all Battery information
  139.         public void Print()
  140.         {
  141.             Console.WriteLine("Battery Properties");
  142.             Console.WriteLine("===================");
  143.             Console.WriteLine("Model: {0}", this.Model ?? "[uknown]");
  144.  
  145.             //(int)this.Idle, (this.Idle - (int)this.Idle)*60
  146.             //calculate the hours and the minutes because it is in double format
  147.             if (this.Idle != null)
  148.             {
  149.                 Console.WriteLine("Idle hours: {0}:{1,2} hours", (int)this.Idle, (this.Idle - (int)this.Idle) * 60);
  150.             }
  151.             else
  152.             {
  153.                 Console.WriteLine("Idle hours: [unknown]");
  154.             }
  155.             if (this.Talk != null)
  156.             {
  157.                 Console.WriteLine("Talk hours: {0}:{1,2} hours", (int)this.Talk, (this.Talk - (int)this.Talk) * 60);
  158.             }
  159.             else
  160.             {
  161.                 Console.WriteLine("Talk hours: [unknown]");
  162.             }
  163.  
  164.             Console.WriteLine("===================");
  165.         }
  166.         //</methods>
  167.     }
  168.    
  169.     class Display
  170.     {
  171.  
  172.         //<fields>
  173.         private double? size;
  174.         private double? colors;
  175.         //</fields>
  176.         //<properties>
  177.         //size of the display in inches
  178.         public double? Size
  179.         {
  180.             get
  181.             {
  182.                 return this.size;
  183.             }
  184.             set
  185.             {
  186.                 if (CheckData.CheckNegative(value))
  187.                 {
  188.                     throw new ArgumentException("The Size Can't be Negative!");
  189.                 }
  190.                 this.size = value;
  191.             }
  192.         }
  193.        
  194.         public double? Colors //colors count in millions
  195.         {
  196.             get
  197.             {
  198.                 return this.colors;
  199.             }
  200.             set
  201.             {
  202.                 if (CheckData.CheckNegative(value))
  203.                 {
  204.                     throw new ArgumentException("The Colors Can't be Negative!");
  205.                 }
  206.                 this.colors = value;
  207.             }
  208.         }  
  209.         //</properties>
  210.  
  211.         //<constructors>
  212.         //we have one mandatory field for the model, actually it could be null
  213.         public Display() { }
  214.         public Display(double? size)
  215.             :this()
  216.         {
  217.             this.size = size;
  218.         }
  219.         public Display(double? size, double? colors)
  220.             : this(size) //this recalls public Display(double? size)
  221.         {
  222.             this.colors = colors;
  223.         }
  224.         //</constructors>
  225.  
  226.         //print all Display information
  227.         public void Print()
  228.         {
  229.             Console.WriteLine("Display Properties");
  230.             Console.WriteLine("===================");
  231.             Console.WriteLine("Size: {0}''", this.Size);
  232.             Console.WriteLine("Colors: {0} millions", this.Colors);
  233.             Console.WriteLine("===================");
  234.         }
  235.  
  236.     }
  237.     class GSM
  238.     {
  239.         //<fields>
  240.         private string model;
  241.         private string manafacturer;
  242.         private decimal? price;
  243.         private string owner;
  244.         //</fields>
  245.  
  246.         //<properties>
  247.         //gsm model
  248.         public string Model
  249.         {
  250.             get { return this.model;  }
  251.             set
  252.             {
  253.                 if (CheckData.CheckNull(value))
  254.                 {
  255.                     throw new ArgumentException("The Model field is mandatory");
  256.                 }
  257.                 this.model = value;
  258.             }
  259.         }
  260.  
  261.         //the manafacturer of the gsm
  262.         public string Manafacturer
  263.         {
  264.             get { return this.manafacturer; }
  265.             set
  266.             {
  267.                 if (CheckData.CheckNull(value))
  268.                 {
  269.                     throw new ArgumentException("The Manafacturer field is mandatory");
  270.                 }
  271.                 this.manafacturer = value;
  272.             }
  273.         }
  274.          //the price in decimal type
  275.         public decimal? Price
  276.         {
  277.             get { return this.price; }
  278.             set
  279.             {
  280.                 if (value<0)
  281.                 {
  282.                     throw new ArgumentException("The Price can't be negative");
  283.                 }
  284.                 this.price = value;
  285.             }
  286.         }
  287.         public string Owner   //the gsm owner
  288.         {
  289.             get { return this.owner; }
  290.             set
  291.             {
  292.                 //i'm not sure wether to check it for numbers
  293.                 //but it can be owner personal ID or Robert 3rd :)
  294.                 this.owner = value;
  295.             }
  296.         }
  297.          
  298.         //</properties>
  299.  
  300.  
  301.         //<constructors>
  302.  
  303.         public GSM(string model, string manafacturer)
  304.         {
  305.             this.Model = model;
  306.             this.Manafacturer = manafacturer;
  307.         }
  308.  
  309.         public GSM(string model, string manafacturer, decimal? price)
  310.             : this(model, manafacturer) //this recall GSM(string model, string manafacturer)
  311.         {
  312.             this.Price = price;
  313.         }
  314.  
  315.         public GSM(string model, string manafacturer, decimal? price, string owner)
  316.             : this(model, manafacturer, price) //this recall GSM public GSM(string model, string manafacturer, decimal? price)
  317.         {
  318.             this.Owner = owner;
  319.         }
  320.         //</constructors>
  321.  
  322.         //adding battery class for the gsm one so
  323.         //we can access Battery.Idle property etc.
  324.         public Battery Battery = new Battery();
  325.  
  326.         //adding display class for the gsm one so
  327.         //we can access Display.Size property etc.
  328.         public Display Display = new Display(null);
  329.  
  330.         //Print all GSM information
  331.         public void Print()
  332.         {
  333.             Console.WriteLine("GSM Properties");
  334.             Console.WriteLine("===================");
  335.             Console.WriteLine("Model: {0}", this.Model ?? "[unknown]");
  336.             Console.WriteLine("Manafacturer: {0}", this.Manafacturer ?? "[unknown]");
  337.             Console.WriteLine("Owner: {0}", this.Owner ?? "[unknown]");
  338.             Console.WriteLine("Price: {0} EUR", this.Price);
  339.             Console.WriteLine("===================");
  340.  
  341.         }
  342.     }
  343.  
  344.     class Program
  345.     {
  346.         static void Main(string[] args)
  347.         {
  348.             GSM myGSM = new GSM("3310", "Nokia"); //model and manafacturers are mandatory
  349.  
  350.             //entering type of battery
  351.             myGSM.Battery.Type = BatteryType.LiIon;
  352.  
  353.             myGSM.Owner = "Kiril Petrov"; //fake name :)
  354.             myGSM.Price = 345.21m;
  355.  
  356.             myGSM.Battery.Model = "";
  357.             myGSM.Battery.Idle = 6.5; //hours in idle mode of type double
  358.             myGSM.Battery.Talk = 1.5; //hours in talk mode of type double
  359.  
  360.             myGSM.Display.Size = 7.4; //size in inches
  361.             myGSM.Display.Colors = 16.5; //colors in millions
  362.  
  363.             myGSM.Print(); //print GSM properties
  364.             myGSM.Battery.Print(); //print Battery properties
  365.             myGSM.Display.Print(); //print Display properties
  366.  
  367.         }
  368.     }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement