Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //Exercise 05.
- //Define several constructors for the defined classes that
- //take different sets of arguments (the full information for
- //the class or part of it). Assume that model and manufacturer are
- //mandatory (the others are optional). All unknown data fill with null.
- namespace _01.GSM_All_In_One
- {
- //this is our enumerations with type of batteries
- public enum BatteryType
- {
- LiIon, NiMh, NiCd
- }
- class CheckData
- {
- //check for negative values
- public static bool CheckNegative(double? Idle)
- {
- return Idle < 0;
- }
- //check for null or String.Empty
- public static bool CheckNull(string model)
- {
- return (model == null || model.Length < 1);
- }
- }
- class Battery
- {
- //<fields>
- private string model;
- private double? idle;
- private double? talk;
- //</fields>
- //<properties>
- //battery model
- public string Model
- {
- get
- {
- return this.model;
- }
- set
- {
- if (CheckData.CheckNull(value))
- {
- throw new ArgumentException("The battery model is mandatory field!");
- }
- this.model = value;
- }
- }
- //battery consumptions - hours in idle mode
- public double? Idle
- {
- get
- {
- return this.idle;
- }
- set
- {
- if (CheckData.CheckNegative(value))
- {
- throw new ArgumentException("The Idle Time Can't be Negative!");
- }
- this.idle = value;
- }
- }
- //battery consumptions - hours in talk mode
- public double? Talk
- {
- get
- {
- return this.talk;
- }
- set
- {
- if (CheckData.CheckNegative(value))
- {
- throw new ArgumentException("The Talk Time Can't be Negative!");
- }
- this.talk = value;
- }
- }
- public BatteryType? Type { get; set; } //batterry type of enumeration BatteryType
- //</properties>
- //<constructors>
- //we have one mandatory field for the model, actually it could be null
- public Battery() { }
- public Battery(string model)
- : this() //recalls Battery()
- {
- this.Model = model;
- }
- public Battery(string model, double? idle)
- : this(model) //this recall Battery(string model)
- {
- this.Idle = idle;
- }
- public Battery(string model, double? idle, double? talk)
- : this(model, idle) //this recall Battery(string model, double? idle)
- {
- this.Talk = talk;
- }
- public Battery(string model, double? idle, double? talk, BatteryType? type)
- : this(model, idle, talk) //this recall Battery(string model, double? idle, double? talk)
- {
- this.Type = type;
- }
- //</constructors>
- //<methods>
- //encapsulate values for Talk and Idle
- //check null for mandatory fields
- //Print all Battery information
- public void Print()
- {
- Console.WriteLine("Battery Properties");
- Console.WriteLine("===================");
- Console.WriteLine("Model: {0}", this.Model ?? "[uknown]");
- //(int)this.Idle, (this.Idle - (int)this.Idle)*60
- //calculate the hours and the minutes because it is in double format
- if (this.Idle != null)
- {
- Console.WriteLine("Idle hours: {0}:{1,2} hours", (int)this.Idle, (this.Idle - (int)this.Idle) * 60);
- }
- else
- {
- Console.WriteLine("Idle hours: [unknown]");
- }
- if (this.Talk != null)
- {
- Console.WriteLine("Talk hours: {0}:{1,2} hours", (int)this.Talk, (this.Talk - (int)this.Talk) * 60);
- }
- else
- {
- Console.WriteLine("Talk hours: [unknown]");
- }
- Console.WriteLine("===================");
- }
- //</methods>
- }
- class Display
- {
- //<fields>
- private double? size;
- private double? colors;
- //</fields>
- //<properties>
- //size of the display in inches
- public double? Size
- {
- get
- {
- return this.size;
- }
- set
- {
- if (CheckData.CheckNegative(value))
- {
- throw new ArgumentException("The Size Can't be Negative!");
- }
- this.size = value;
- }
- }
- public double? Colors //colors count in millions
- {
- get
- {
- return this.colors;
- }
- set
- {
- if (CheckData.CheckNegative(value))
- {
- throw new ArgumentException("The Colors Can't be Negative!");
- }
- this.colors = value;
- }
- }
- //</properties>
- //<constructors>
- //we have one mandatory field for the model, actually it could be null
- public Display() { }
- public Display(double? size)
- :this()
- {
- this.size = size;
- }
- public Display(double? size, double? colors)
- : this(size) //this recalls public Display(double? size)
- {
- this.colors = colors;
- }
- //</constructors>
- //print all Display information
- public void Print()
- {
- Console.WriteLine("Display Properties");
- Console.WriteLine("===================");
- Console.WriteLine("Size: {0}''", this.Size);
- Console.WriteLine("Colors: {0} millions", this.Colors);
- Console.WriteLine("===================");
- }
- }
- class GSM
- {
- //<fields>
- private string model;
- private string manafacturer;
- private decimal? price;
- private string owner;
- //</fields>
- //<properties>
- //gsm model
- public string Model
- {
- get { return this.model; }
- set
- {
- if (CheckData.CheckNull(value))
- {
- throw new ArgumentException("The Model field is mandatory");
- }
- this.model = value;
- }
- }
- //the manafacturer of the gsm
- public string Manafacturer
- {
- get { return this.manafacturer; }
- set
- {
- if (CheckData.CheckNull(value))
- {
- throw new ArgumentException("The Manafacturer field is mandatory");
- }
- this.manafacturer = value;
- }
- }
- //the price in decimal type
- public decimal? Price
- {
- get { return this.price; }
- set
- {
- if (value<0)
- {
- throw new ArgumentException("The Price can't be negative");
- }
- this.price = value;
- }
- }
- public string Owner //the gsm owner
- {
- get { return this.owner; }
- set
- {
- //i'm not sure wether to check it for numbers
- //but it can be owner personal ID or Robert 3rd :)
- this.owner = value;
- }
- }
- //</properties>
- //<constructors>
- public GSM(string model, string manafacturer)
- {
- this.Model = model;
- this.Manafacturer = manafacturer;
- }
- public GSM(string model, string manafacturer, decimal? price)
- : this(model, manafacturer) //this recall GSM(string model, string manafacturer)
- {
- this.Price = price;
- }
- public GSM(string model, string manafacturer, decimal? price, string owner)
- : this(model, manafacturer, price) //this recall GSM public GSM(string model, string manafacturer, decimal? price)
- {
- this.Owner = owner;
- }
- //</constructors>
- //adding battery class for the gsm one so
- //we can access Battery.Idle property etc.
- public Battery Battery = new Battery();
- //adding display class for the gsm one so
- //we can access Display.Size property etc.
- public Display Display = new Display(null);
- //Print all GSM information
- public void Print()
- {
- Console.WriteLine("GSM Properties");
- Console.WriteLine("===================");
- Console.WriteLine("Model: {0}", this.Model ?? "[unknown]");
- Console.WriteLine("Manafacturer: {0}", this.Manafacturer ?? "[unknown]");
- Console.WriteLine("Owner: {0}", this.Owner ?? "[unknown]");
- Console.WriteLine("Price: {0} EUR", this.Price);
- Console.WriteLine("===================");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- GSM myGSM = new GSM("3310", "Nokia"); //model and manafacturers are mandatory
- //entering type of battery
- myGSM.Battery.Type = BatteryType.LiIon;
- myGSM.Owner = "Kiril Petrov"; //fake name :)
- myGSM.Price = 345.21m;
- myGSM.Battery.Model = "";
- myGSM.Battery.Idle = 6.5; //hours in idle mode of type double
- myGSM.Battery.Talk = 1.5; //hours in talk mode of type double
- myGSM.Display.Size = 7.4; //size in inches
- myGSM.Display.Colors = 16.5; //colors in millions
- myGSM.Print(); //print GSM properties
- myGSM.Battery.Print(); //print Battery properties
- myGSM.Display.Print(); //print Display properties
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement