Advertisement
Guest User

OOP First four tasks

a guest
Feb 1st, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.66 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. /*1. Define a class that holds information about a mobile phone device:
  5.  * model, manufacturer, price, owner, battery characteristics (model, hours idle and hours talk) and
  6.  * display characteristics (size and number of colors).
  7.  * Define 3 separate classes (class GSM holding instances of the classes Battery and Display).*/
  8.  
  9. /*2. Define several constructors for the defined classes that take different sets of arguments
  10.  * (the full information for the class or part of it). Assume that model and manufacturer are mandatory
  11.  * (the others are optional). All unknown data fill with null.*/
  12.  
  13. /*3. Add an enumeration BatteryType (Li-Ion, NiMH, NiCd, …) and use it as a new field for the batteries.*/
  14.  
  15. /*4. Add a method in the GSM class for displaying all information about it. Try to override ToString().*/
  16.  
  17. namespace MobilePhoneInformation
  18. {
  19.     class BaseClass
  20.     {
  21.         static void Main()
  22.         {
  23.             GSM newGSM = new GSM("Galaxy note 2", "Samsung", 1030, "Pesho Peshov");
  24.             Console.WriteLine(newGSM.ToString());
  25.  
  26.             //newGSM.Model = "Galaxy note 2";
  27.             //newGSM.Manufacturer = "Samsung";
  28.             //newGSM.Price = 1030;
  29.             //newGSM.Owner = "Pesho Peshov";
  30.  
  31.             Battery newBattery = new Battery("Li-Ion 3100 mAh", 980, 35, BatteryType.LiIon);
  32.             //newBattery.Print();
  33.  
  34.             //newGSM.Battery.Model = "Li-Ion 3100 mAh battery";
  35.             //newGSM.Battery.HoursIdle = 980;
  36.             //newGSM.Battery.HoursTalk = 35;
  37.  
  38.             //Display newDisplay = new Display(5.55, 16000000);
  39.             //newDisplay.Print();
  40.  
  41.             //newGSM.Display.Size = 5.55;
  42.             //newGSM.Display.NumberOfColors = 16000000;
  43.  
  44.             //newGSM.Print();
  45.             //newGSM.Battery.Print();
  46.             ////newGSM.Display.Print();
  47.         }
  48.     }
  49.  
  50.     class GSM
  51.     {
  52.         private string model;
  53.         private string manufacturer;
  54.         private double? price;
  55.         private string owner;
  56.  
  57.         public GSM(string model)
  58.         {
  59.             this.Model = model;
  60.         }
  61.  
  62.         public GSM(string model, string manufacturer) : this(model)
  63.         {
  64.             this.Manufacturer = manufacturer;
  65.         }
  66.  
  67.         public GSM(string model, string manufacturer, double price)
  68.             : this(model, manufacturer)
  69.         {
  70.             this.Price = price;
  71.         }
  72.  
  73.         public GSM(string model, string manufacturer, double price, string owner)
  74.             : this(model, manufacturer, price)
  75.         {
  76.             this.Owner = owner;
  77.         }
  78.  
  79.         public string Model
  80.         {
  81.             get { return model; }
  82.             set { model = value; }
  83.         }
  84.  
  85.         public string Manufacturer
  86.         {
  87.             get { return manufacturer; }
  88.             set { manufacturer = value; }
  89.         }
  90.  
  91.         public double? Price
  92.         {
  93.             get { return price; }
  94.             set { price = value; }
  95.         }
  96.  
  97.         public string Owner
  98.         {
  99.             get { return owner; }
  100.             set { owner = value; }
  101.         }
  102.  
  103.         Battery newBattery = new Battery("Li-Ion 3100 mAh", 980, 35, BatteryType.LiIon);
  104.         Display newDisplay = new Display(5.55, 16000000);
  105.  
  106.         public void Print()
  107.         {
  108.             Console.WriteLine("===================");
  109.             Console.WriteLine("GSM characteristics");
  110.             Console.WriteLine("===================");
  111.             Console.WriteLine("Model: {0}", this.Model);
  112.             Console.WriteLine("-------------------");
  113.             Console.WriteLine("Manufacturer: {0}", this.Manufacturer);
  114.             Console.WriteLine("-------------------");
  115.             Console.WriteLine("Price: {0} lv.", this.Price);
  116.             Console.WriteLine("-------------------");
  117.             Console.WriteLine("Owner: {0}", this.Owner);
  118.             Console.WriteLine("===================");
  119.         }
  120.  
  121.         public override string ToString()
  122.         {
  123.             StringBuilder sb = new StringBuilder();
  124.  
  125.             sb.AppendLine("Model: " + this.Model);
  126.             sb.AppendLine("Manufacturer: " + this.Manufacturer);
  127.             sb.AppendLine("Price: " + this.Price + "lv");
  128.             sb.AppendLine("Owner: " + this.Owner);
  129.             sb.AppendLine("Battery model: " + newBattery.Model);
  130.             sb.AppendLine("Battery type: " + newBattery.Type);
  131.             sb.AppendLine("Hours idle: " + newBattery.HoursIdle);
  132.             sb.AppendLine("Hours talk: " + newBattery.HoursTalk);
  133.             sb.AppendLine("Display size: " + newDisplay.Size);
  134.             sb.AppendLine("Display colors: " + newDisplay.NumberOfColors);
  135.  
  136.             return sb.ToString();
  137.         }
  138.     }
  139.  
  140.     class Battery
  141.     {
  142.         private string model;
  143.         private double hoursIdle;
  144.         private double hoursTalk;
  145.         private BatteryType type;
  146.  
  147.        
  148.         public Battery(string model)
  149.         {
  150.             this.Model = model;
  151.         }
  152.  
  153.         public Battery(string model, double hoursIdle, double hoursTalk)
  154.             : this(model)
  155.         {
  156.             this.HoursIdle = hoursIdle;
  157.             this.HoursTalk = hoursTalk;
  158.         }
  159.  
  160.         public Battery(string model, double hoursIdle, double hoursTalk, BatteryType type)
  161.             : this(model, hoursIdle, hoursTalk)
  162.         {
  163.             this.Type = type;
  164.         }
  165.  
  166.         public string Model
  167.         {
  168.             get { return model; }
  169.             set { model = value; }
  170.         }
  171.  
  172.         public double HoursIdle
  173.         {
  174.             get { return hoursIdle; }
  175.             set { hoursIdle = value; }
  176.         }
  177.  
  178.         public double HoursTalk
  179.         {
  180.             get { return hoursTalk; }
  181.             set { hoursTalk = value; }
  182.         }
  183.  
  184.         public BatteryType Type
  185.         {
  186.             get { return type; }
  187.             set { type = value; }
  188.         }
  189.  
  190.         public void Print()
  191.         {
  192.             Console.WriteLine("=======================");
  193.             Console.WriteLine("Battery characteristics");
  194.             Console.WriteLine("=======================");
  195.             Console.WriteLine("Model: {0}", this.Model);
  196.             Console.WriteLine("-----------------------");
  197.             Console.WriteLine("Hours idle: {0} h", this.HoursIdle);
  198.             Console.WriteLine("-----------------------");
  199.             Console.WriteLine("Hours talk: {0} h", this.HoursTalk);
  200.             Console.WriteLine("-----------------------");
  201.             Console.WriteLine("Battery type: {0}", this.Type);
  202.             Console.WriteLine("========================");
  203.         }
  204.     }
  205.  
  206.     class Display
  207.     {
  208.         private double size;
  209.         private int numberOfColors;
  210.  
  211.         public Display(double size)
  212.         {
  213.             this.Size = size;
  214.         }
  215.  
  216.         public Display(double size, int numberOfColors)
  217.             : this(size)
  218.         {
  219.             this.NumberOfColors = numberOfColors;
  220.         }
  221.  
  222.         public double Size
  223.         {
  224.             get { return size; }
  225.             set { size = value; }
  226.         }
  227.  
  228.         public int NumberOfColors
  229.         {
  230.             get { return numberOfColors; }
  231.             set { numberOfColors = value; }
  232.         }
  233.  
  234.         public void Print()
  235.         {
  236.             Console.WriteLine("=======================");
  237.             Console.WriteLine("Display characteristics");
  238.             Console.WriteLine("=======================");
  239.             Console.WriteLine("Size: {0}", this.Size);
  240.             Console.WriteLine("-----------------------");
  241.             Console.WriteLine("Number of colors: {0}", this.NumberOfColors);
  242.             Console.WriteLine("========================");
  243.         }
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement