C# – Building classes in C# – Video
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
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:
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):
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! ))
