In this article I present the second solution from the homework from SoftUni – www.softUni.bg – concerning classes in C#.
This is the task:
Define a class Laptop that holds the following information about a laptop device: model, manufacturer, processor, RAM, graphics card, HDD, screen, battery, battery life in hours and price.
- The model and price are mandatory. All other values are optional.
- Define two separate classes: a class Laptop holding an instance of class Battery.
- Define several constructors that take different sets of arguments (full laptop + battery information or only part of it). Use proper variable types.
- Add a method in the Laptop class that displays information about the given instance
- Tip: override the ToString() method
- Put validation in all property setters and constructors. String values cannot be empty, and numeric data cannot be negative. Throw exceptions when improper data is entered.
Sample laptop description (full): | Sample laptop description (mandatory properties only) | |||
model | Lenovo Yoga 2 Pro | model | HP 250 G2 | |
manufacturer | Lenovo | price | 699.00 lv. | |
processor | Intel Core i5-4210U (2-core, 1.70 – 2.70 GHz, 3MB cache) | |||
RAM | 8 GB | |||
graphics card | Intel HD Graphics 4400 | |||
HDD | 128GB SSD | |||
screen | 13.3″ (33.78 cm) – 3200 x 1800 (QHD+), IPS sensor display | |||
battery | Li-Ion, 4-cells, 2550 mAh | |||
battery life | 4.5 hours | |||
price | 2259.00 lv. |
Pretty much, we need to build two classes – one for the Laptop and one for the battery. Additionally, a third class is build, from where the two others are initialized. The process of building the code is present in the YouTube video, at the end of the article.
Here comes the code:
- The class of the battery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LaptopShop { class Battery { private string sType; private float fHours; public string Type { get { return this.sType; } set { if (value != null && value.Length < 1) throw new ArgumentException("Invalid value for battery type!"); this.sType = value; } } public float Hours { get { return this.fHours; } set { if (value < 0) throw new ArgumentException("Invalid value for battery battery hours!"); this.fHours= value; } } public Battery(string sType) { this.Type = sType; } public Battery(string sType, float fHours) : this(sType) { this.Hours = fHours; } public override string ToString() { StringBuilder resultOverride = new StringBuilder(); if (this.Type != null) { resultOverride.AppendLine("battery is :" + this.Type); } if (this.Hours > 0) { resultOverride.AppendLine("battery life is :" + this.Hours + " "); } return resultOverride.ToString(); } } } |
- The class of the laptop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LaptopShop { class laptop { private string sModel; private string sManufacturer; private string sProcessor; private string sRAM; private string sHDD; private string sGraphicsCard; private string sScreen; private decimal dPrice; private Battery bBattery; public laptop(string sModel, decimal dPrice) { this.Model = sModel; this.Price = dPrice; } public laptop(string sModel, decimal dPrice, string sManufacturer = null, string sProcessor = null, string sRAM = null, string sHDD = null, Battery bBattery = null, string screen = null) : this(sModel, dPrice) { this.Manufacturer = sManufacturer; this.Processor = sProcessor; this.RAM = sRAM; this.HDD = sHDD; this.GraphicsCard = sGraphicsCard; this.Battery = bBattery; this.Screen = sScreen; } public string Model { get { return this.sModel; } set { if (value != null && value.Length<1) throw new ArgumentNullException(); this.sModel = value; } } public string Manufacturer { get { return this.sManufacturer; } set { if (value != null && value.Length < 1) throw new ArgumentNullException(); this.sManufacturer = value; } } public string Processor { get { return this.sProcessor; } set { if (value != null && value.Length < 1) throw new ArgumentNullException(); this.sProcessor = value; } } public string RAM { get { return this.sRAM; } set { if (value != null && value.Length < 1) throw new ArgumentNullException(); this.sRAM = value; } } public string HDD { get { return this.sHDD; } set { if (value != null && value.Length < 1) throw new ArgumentNullException(); this.sHDD = value; } } public string GraphicsCard { get { return this.sGraphicsCard; } set { if (value != null && value.Length < 1) throw new ArgumentNullException(); this.sGraphicsCard = value; } } public string Screen { get { return this.sScreen; } set { if (value != null && value.Length < 1) throw new ArgumentNullException(); this.sScreen = value; } } public decimal Price { get { return this.dPrice; } set { if (value<1) throw new ArgumentNullException("Price cannot be less than 1!"); this.dPrice = value; } } public Battery Battery { get { return this.bBattery; } set { this.bBattery = value; } } public override string ToString() { StringBuilder strLaptop = new StringBuilder(); strLaptop.AppendLine("model - " +this.Model); strLaptop.AppendLine("price - " + this.Price); if (this.Manufacturer != null) { strLaptop.AppendLine("Manufacturer -" + this.Manufacturer); } if (this.Processor != null) { strLaptop.AppendLine("Processor -" + this.Processor); } if (this.RAM != null) { strLaptop.AppendLine("RAM -" + this.RAM); } if (this.Screen != null) { strLaptop.AppendLine("Screen -" + this.Screen); } if (this.HDD != null) { strLaptop.AppendLine("HDD -" + this.HDD); } if (this.Battery != null) { strLaptop.AppendLine("Battery -" + this.Battery); } return strLaptop.ToString(); } } } |
The Class with the main method (called RealThing):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LaptopShop { class RealThing { static void Main() { Battery GoodBattery= new Battery("Great battery type"); Battery BetterBattery= new Battery("Great battery type",20); laptop cheapLaptop = new laptop("Cheap model", 200); laptop goodLaptop = new laptop("Good model", 500, "Vit Manufacturing", "Vit Processing", "16 GB", "1TB", BetterBattery, "17 inches"); laptop greatLaptop = new laptop("Good model", 1500, null , null, "32 GB", "2TB", BetterBattery, "17 inches"); Console.WriteLine(cheapLaptop.ToString()); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(goodLaptop.ToString()); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(greatLaptop.ToString()); } } } |
🙂
Enjoy it! ))