Tuples and Classes
Tuples and Classes
Fundamentals of Tuples and Classes
Introduction
We saw how to declare tuple variables. In the same way, you can declare a tuple variable in the body of a class, in which case the variable is treated as a field. Here is an example:
public class Processor
{
/* We are combining these pieces of information of the
* processor because processors specifications are
* related by generation and tied to a manufacturer. */
(string make, string model) identification;
}
Practical Learning: Introducing Conditions
Initializing a Tuple
As you may know already, a constructor is a special method that is used to initialize a variable or a field. Therefore, if you create a regular tuple field, you can use a constructor to initialize it. Here is an example:
public class Processor { /* We are combining these pieces of information of the * processor because processors specifications are * related by generation and tied to a manufacturer. */ (string make, string model) identification; public Processor() { identification = ("HP", "XL2X0 GEN9 E5-2620V3"); } }
A Read-Only Tuple
You can create a constant tuple but whose value depends on specific objects. This is the case for a read-only tuple. You can create it in the body of a class and initialize it in a constructor. Here is an example:
public class Memory { public readonly (int numberOfSockets, int memoryInGB) partition; public Memory() { partition = (2, 8); } public Memory(int capacity) { partition = (2, 16); } }
Initializing the Properties of a Class
Consider a class as follows:
public class Trapezoid { public double Bottom { get; set; } public double Top { get; set; } public double Height { get; set; } public Trapezoid(double bottom, double height) { Bottom = bottom; Height = height; } public Trapezoid(double bottom, double top, double height) { Bottom = bottom; Top = top; Height = height; } }
Notice that the class has an overloaded constructor with one constructor that takes two arguments and the constructor that takes three arguments. Also notice that, to initialize the properties, each constructor assigns the desired value or a parameterized value to a property. Using the characteristics of a tuple, you can initialize the properties by putting them in parentheses and assigning another set of parentheses to it. In the second parentheses, pur a list of the values for the parentheses. The values must be in the order of the properties in the first set of parentheses. These can be done as follows:
using static System.Console; Trapezoid trap = new Trapezoid(708.83, 140.07); WriteLine("==========================="); WriteLine("Trapezoid"); WriteLine("---------------------------"); WriteLine("Top: {0}", trap.Top); WriteLine("Bottom: {0}", trap.Bottom); WriteLine("Height: {0}", trap.Height); WriteLine("---------------------------"); WriteLine("Area: {0}", trap.Area); WriteLine("==========================="); trap = new Trapezoid(708.83, 486.39, 140.07); WriteLine("Trapezoid"); WriteLine("---------------------------"); WriteLine("Top: {0}", trap.Top); WriteLine("Bottom: {0}", trap.Bottom); WriteLine("Height: {0}", trap.Height); WriteLine("---------------------------"); WriteLine("Area: {0}", trap.Area); WriteLine("==========================="); public class Trapezoid { private double _top_; public double Bottom { get; set; } public double Height { get; set; } public Trapezoid(double bottom, double height) { (Bottom, Height) = (bottom, height); } public Trapezoid(double bottom, double top, double height) { (Bottom, Top, Height) = (bottom, top, height); } public double Top { get { if (_top_ == 0.00) return Bottom * .75; return _top_; } set { _top_ = value; } } public double Area { get { return ((Bottom + Top) / 2.00) * Height; } } }
This would produce:
=========================== Trapezoid --------------------------- Top: 531.6225000000001 Bottom: 708.83 Height: 140.07 --------------------------- Area: 86875.0908375 =========================== Trapezoid --------------------------- Top: 486.39 Bottom: 708.83 Height: 140.07 --------------------------- Area: 83707.2327 =========================== Press any key to close this window . . .
Method Overloading and Tuples
In our introduction to method overloading, we saw that a method can get overloaded if you create more than one version in the same class. We also saw that the versions of the method must differ by their syntax or signature. Remember that the signature of a method doesn't include its return type. Based on this, you cannot overload a method based on the fact that one version returns a tuple and another does not.
To overload a method that involves tuples, you will rely on the parameters. You can create a method that has different versions. Two or more versions of a method can take one tuple parameter each; each tuple-parameter can have the same number of elements but the elements can be different. Consider the following example:
public class Algebra { /* The following two versions of an overloaded method take one tuple * as parameter, but each tuple-parameter has different types of parameters. */ void Calculate((string, string) a) { } void Calculate((string, double) a) { } /* The following additional three versions of an overloaded method take * one tuple as parameter, but each tuple-parameter has different number of parameters. */ void Calculate((int, bool) a) { } void Calculate((string, int, double) a) { } void Calculate((string, int, bool, double) a) { } }
On the other hand, you can overload a method by passing a mixture of primitive types and tuples.
Tuples and Properties
Introduction
You can create a property whose type is a tuple. To start, in the body of the class, you can declare a field variable that is a tuple type. Here is an example:
public class Member
{
public (int, string, double) id;
}
To get the property, create a property that has a return type as a tuple. Here is an example:
public class Member
{
private (int, string, double) id;
public (int, string, double) Identification
{
}
}
A Read-Only Tuple Property
A read-only property is a a property with only a get clause. For a tuple property, make the get clause return a private field that has the same return type as the property. Here is an example:
public class Member
{
private (int, string, double) id;
public (int, string, double) Identification
{
get
{
return id;
}
}
}
If you want to use the property outside its class, it must have a value, which it cannot get on its own because it is a read-only property. The most common way you can initialize this property is by using a constructor that uses a parameter of the same tuple type as the property. After initializing the property, you can get its value and use it. Here is an example:
using static System.Console; (int nbr, string name, double fee) account = (295_380, "Elisabeth Merlins", 98.50); Member mbr = new Member(account, 0); WriteLine("Membership Details"); WriteLine("-------------------------------"); WriteLine($"Member #: {mbr.Identification.number}"); WriteLine($"Full Name: {mbr.Identification.name}"); WriteLine($"Account Fee: {mbr.Identification.fee}"); WriteLine("==============================="); public class Member { private (int, string, double) id; public Member((int, string, double) identifier, int amt) { id = identifier; } public (int number, string name, double fee) Identification { get { return id; } } }
This would produce:
Membership Details ------------------------------- Member #: 295380 Full Name: Elisabeth Merlins Account Fee: 98.5 =============================== Press any key to close this window . . .
Practical Learning: Creating a Read-Only Property
namespace StellarWaterPoint2.Models { public class WaterBill { private double nd; private double crs; private string typeOfAccount; public WaterBill(string category) { typeOfAccount = category; } public string AccountType { get { if (typeOfAccount == "1") return "Residential Household"; else if (typeOfAccount == "2") return "Social/Non-Profit Organization"; else if (typeOfAccount == "3") return "Water Intensive Business (Laudromat, etc)"; else if (typeOfAccount == "4") return "General Business"; else return "Other"; } } public double CounterReadingStart { get { return crs; } set { crs = value; } } public double CounterReadingEnd { get { return nd; } set { nd = value; } } public double Gallons { get { return CounterReadingEnd - CounterReadingStart; } } public double HCFTotal { get { return Gallons / 748; } } public (double First25Therms, double Next15Therms, double Above40Therms) Therms { get { double first, next, above; if (HCFTotal > 40.00) { first = 25.00 * 1.5573; next = 15.00 * 1.2264; above = (HCFTotal - 40.00) * 0.9624; } else if (HCFTotal > 25.00) { first = 25.00 * 1.5573; next = (HCFTotal - 25.00) * 1.2264; above = 0.00; } else // if (HCFTotal > 40.00) { first = HCFTotal * 1.5573; next = 0.00; above = 0.00; } return (first, next, above); } } public double WaterUsageCharges { get { return Therms.First25Therms + Therms.Next15Therms + Therms.Above40Therms; } } public double SewerCharges { get { double charges; switch (AccountType) { case "Residential Household": charges = WaterUsageCharges * 0.2528; break; case "Social/Non-Profit Organization": charges = WaterUsageCharges * 0.0915; break; case "Water Intensive Business (Laudromat, etc)": charges = WaterUsageCharges * 1.5865; break; case "General Business": charges = WaterUsageCharges * 0.6405; break; default: charges = WaterUsageCharges * 1.2125; break; } return charges; } } public double EnvironmentCharges { get { double charges; switch (AccountType) { case "Residential Household": charges = WaterUsageCharges * 0.0025; break; case "Social/Non-Profit Organization": charges = WaterUsageCharges * 0.0015; break; case "Water Intensive Business (Laudromat, etc)": charges = WaterUsageCharges * 0.0105; break; case "General Business": charges = WaterUsageCharges * 0.0045; break; default: charges = WaterUsageCharges * 0.0125; break; } return charges; } } public double ServiceCharges { get { double charges = 0.00; switch (AccountType) { case "Residential Household": charges = WaterUsageCharges * 0.0328; break; case "Social/Non-Profit Organization": charges = WaterUsageCharges * 0.0115; break; case "Water Intensive Business (Laudromat, etc)": charges = WaterUsageCharges * 0.1015; break; case "General Business": charges = WaterUsageCharges * 0.0808; break; default: charges = WaterUsageCharges * 0.1164; break; } return charges; } } public double TotalCharges { get { return WaterUsageCharges + SewerCharges + EnvironmentCharges + ServiceCharges; } } public double LocalTaxes { get { double charges; switch (AccountType) { case "Residential Household": charges = TotalCharges * 0.115; break; case "Social/Non-Profit Organization": charges = TotalCharges * 0.085; break; case "Water Intensive Business (Laudromat, etc)": charges = TotalCharges * 0.825; break; case "General Business": charges = TotalCharges * 0.315; break; default: charges = TotalCharges * 0.625; break; } return charges; } } public double StateTaxes { get { double charges; switch (AccountType) { case "Residential Household": charges = TotalCharges * 0.035; break; case "Social/Non-Profit Organization": charges = TotalCharges * 0.015; break; case "Water Intensive Business (Laudromat, etc)": charges = TotalCharges * 0.105; break; case "General Business": charges = TotalCharges * 0.065; break; default: charges = TotalCharges * 0.815; break; } return charges; } } public double AmountDue { get { return TotalCharges + LocalTaxes + StateTaxes; } } } }
using StellarWaterPoint2.Models; using static System.Console; WriteLine("Stellar Water Point"); WriteLine("==========================================================="); WriteLine("To prepare an invoice, enter the following information"); WriteLine("Types of Accounts"); WriteLine("1. Residential Household"); WriteLine("2. Social/Non-Profit Organization"); WriteLine("3. Water Intensive Business (Laudromat, etc)"); WriteLine("4. General Business"); WriteLine("0. Other"); Write("Enter Type of Account: "); string answer = ReadLine(); WriteLine("-----------------------------------------------------------"); WaterBill bill = new WaterBill(answer); Write("Counter Reading Start: "); bill.CounterReadingStart = int.Parse(ReadLine()); Write("Counter Reading End: "); bill.CounterReadingEnd = int.Parse(ReadLine()); WriteLine("==========================================================="); WriteLine("Stellar Water Point - Customer Invoice"); WriteLine("Account Type: {0}", bill.AccountType); WriteLine("==========================================================="); WriteLine("Meter Reading"); WriteLine("-----------------------------------------------------------"); WriteLine("Counter Reading Start: {0,10}", bill.CounterReadingStart); WriteLine("Counter Reading End: {0,10}", bill.CounterReadingEnd); WriteLine("Total Gallons Consumed: {0,10}", bill.Gallons); WriteLine("==========================================================="); WriteLine("Therms Evaluation"); WriteLine("-----------------------------------------------------------"); WriteLine("HCF Total: {0,10:n}", bill.HCFTotal); WriteLine("First 35 Therms (* 1.5573): {0,10:n}", bill.Therms.First25Therms); WriteLine("First 20 Therms (* 1.2264): {0,10:n}", bill.Therms.Next15Therms); WriteLine("Above 40 Therms (* 0.9624): {0,10:n}", bill.Therms.Above40Therms); WriteLine("-----------------------------------------------------------"); WriteLine("Water Usage Charges: {0,10:n}", bill.WaterUsageCharges); WriteLine("Sewer Charges: {0,10:n}", bill.SewerCharges); WriteLine("==========================================================="); WriteLine("Bill Values"); WriteLine("-----------------------------------------------------------"); WriteLine("Environment Charges: {0,10:n}", bill.EnvironmentCharges); WriteLine("Service Charges: {0,10:n}", bill.ServiceCharges); WriteLine("Total Charges: {0,10:n}", bill.TotalCharges); WriteLine("Local Taxes: {0,10:n}", bill.LocalTaxes); WriteLine("State Taxes: {0,10:n}", bill.StateTaxes); WriteLine("-----------------------------------------------------------"); WriteLine("Amount Due: {0,10:n}", bill.AmountDue); Write("===========================================================");
Stellar Water Point =========================================================== To prepare an invoice, enter the following information Types of Accounts 1. Residential Household 2. Social/Non-Profit Organization 3. Water Intensive Business (Laudromat, etc) 4. General Business 0. Other Enter Type of Account: 1 ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 =========================================================== Stellar Water Point - Customer Invoice Account Type: Residential Household =========================================================== Meter Reading ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 Total Gallons Consumed: 132063 =========================================================== Therms Evaluation ----------------------------------------------------------- HCF Total: 176.55 First 35 Therms (* 1.5573): 38.93 First 20 Therms (* 1.2264): 18.40 Above 40 Therms (* 0.9624): 131.42 ----------------------------------------------------------- Water Usage Charges: 188.75 Sewer Charges: 47.72 =========================================================== Bill Values ----------------------------------------------------------- Environment Charges: 0.47 Service Charges: 6.19 Total Charges: 243.13 Local Taxes: 27.96 State Taxes: 8.51 ----------------------------------------------------------- Amount Due: 279.60 =========================================================== Press any key to close this window . . .
namespace StellarWaterPoint2.Models { internal class WaterBill { private double nd; private double crs; private string typeOfAccount; public WaterBill(string category) => typeOfAccount = category; public string AccountType { get { if (typeOfAccount == "1") return "Residential Household"; else if (typeOfAccount == "2") return "Social/Non-Profit Organization"; else if (typeOfAccount == "3") return "Water Intensive Business (Laudromat, etc)"; else if (typeOfAccount == "4") return "General Business"; else return "Other"; } } public double CounterReadingStart { get { return crs; } set { crs = value; } } public double CounterReadingEnd { get { return nd; } set { nd = value; } } public double Gallons => CounterReadingEnd - CounterReadingStart; public double HCFTotal => Gallons / 748; public (double First25Therms, double Next15Therms, double Above40Therms) Therms { get { double first, next, above; if (HCFTotal > 40.00) { first = 25.00 * 1.5573; next = 15.00 * 1.2264; above = (HCFTotal - 40.00) * 0.9624; } else if (HCFTotal > 25.00) { first = 25.00 * 1.5573; next = (HCFTotal - 25.00) * 1.2264; above = 0.00; } else // if (HCFTotal > 40.00) { first = HCFTotal * 1.5573; next = 0.00; above = 0.00; } return (first, next, above); } } public double WaterUsageCharges =>; Therms.First25Therms + Therms.Next15Therms + Therms.Above40Therms; public double SewerCharges { get { double charges; switch (AccountType) { case "Residential Household": charges = WaterUsageCharges * 0.2528; break; case "Social/Non-Profit Organization": charges = WaterUsageCharges * 0.0915; break; case "Water Intensive Business (Laudromat, etc)": charges = WaterUsageCharges * 1.5865; break; case "General Business": charges = WaterUsageCharges * 0.6405; break; default: charges = WaterUsageCharges * 1.2125; break; } return charges; } } public double EnvironmentCharges { get { double charges; switch (AccountType) { case "Residential Household": charges = WaterUsageCharges * 0.0025; break; case "Social/Non-Profit Organization": charges = WaterUsageCharges * 0.0015; break; case "Water Intensive Business (Laudromat, etc)": charges = WaterUsageCharges * 0.0105; break; case "General Business": charges = WaterUsageCharges * 0.0045; break; default: charges = WaterUsageCharges * 0.0125; break; } return charges; } } public double ServiceCharges { get { double charges = 0.00; switch (AccountType) { case "Residential Household": charges = WaterUsageCharges * 0.0328; break; case "Social/Non-Profit Organization": charges = WaterUsageCharges * 0.0115; break; case "Water Intensive Business (Laudromat, etc)": charges = WaterUsageCharges * 0.1015; break; case "General Business": charges = WaterUsageCharges * 0.0808; break; default: charges = WaterUsageCharges * 0.1164; break; } return charges; } } public double TotalCharges => WaterUsageCharges + SewerCharges + EnvironmentCharges + ServiceCharges; public double LocalTaxes { get { double charges; switch (AccountType) { case "Residential Household": charges = TotalCharges * 0.115; break; case "Social/Non-Profit Organization": charges = TotalCharges * 0.085; break; case "Water Intensive Business (Laudromat, etc)": charges = TotalCharges * 0.825; break; case "General Business": charges = TotalCharges * 0.315; break; default: charges = TotalCharges * 0.625; break; } return charges; } } public double StateTaxes { get { double charges; switch (AccountType) { case "Residential Household": charges = TotalCharges * 0.035; break; case "Social/Non-Profit Organization": charges = TotalCharges * 0.015; break; case "Water Intensive Business (Laudromat, etc)": charges = TotalCharges * 0.105; break; case "General Business": charges = TotalCharges * 0.065; break; default: charges = TotalCharges * 0.815; break; } return charges; } } public double AmountDue => TotalCharges + LocalTaxes + StateTaxes; } }
Stellar Water Point =========================================================== To prepare an invoice, enter the following information Types of Accounts 1. Residential Household 2. Social/Non-Profit Organization 3. Water Intensive Business (Laudromat, etc) 4. General Business 0. Other Enter Type of Account: 2 ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 =========================================================== Stellar Water Point - Customer Invoice Account Type: Social/Non-Profit Organization =========================================================== Meter Reading ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 Total Gallons Consumed: 132063 =========================================================== Therms Evaluation ----------------------------------------------------------- HCF Total: 176.55 First 35 Therms (* 1.5573): 38.93 First 20 Therms (* 1.2264): 18.40 Above 40 Therms (* 0.9624): 131.42 ----------------------------------------------------------- Water Usage Charges: 188.75 Sewer Charges: 17.27 =========================================================== Bill Values ----------------------------------------------------------- Environment Charges: 0.28 Service Charges: 2.17 Total Charges: 208.47 Local Taxes: 17.72 State Taxes: 3.13 ----------------------------------------------------------- Amount Due: 229.32 =========================================================== Press any key to close this window . . .
namespace StellarWaterPoint2.Models { internal class WaterBill { private double nd; private double crs; private string typeOfAccount; public WaterBill(string category) => typeOfAccount = category; public string AccountType { get { . . . } } . . . public double SewerCharges { get { switch (AccountType) { case "Residential Household": return WaterUsageCharges * 0.2528; case "Social/Non-Profit Organization": return WaterUsageCharges * 0.0915; case "Water Intensive Business (Laudromat, etc)": return WaterUsageCharges * 1.5865; case "General Business": return WaterUsageCharges * 0.6405; default: return WaterUsageCharges * 1.2125; } } } public double EnvironmentCharges { get { switch (AccountType) { case "Residential Household": return WaterUsageCharges * 0.0025; case "Social/Non-Profit Organization": return WaterUsageCharges * 0.0015; case "Water Intensive Business (Laudromat, etc)": return WaterUsageCharges * 0.0105; case "General Business": return WaterUsageCharges * 0.0045; default: return WaterUsageCharges * 0.0125; } } } public double ServiceCharges { get { switch (AccountType) { case "Residential Household": return WaterUsageCharges * 0.0328; case "Social/Non-Profit Organization": return WaterUsageCharges * 0.0115; case "Water Intensive Business (Laudromat, etc)": return WaterUsageCharges * 0.1015; case "General Business": return WaterUsageCharges * 0.0808; default: return WaterUsageCharges * 0.1164; } } } public double TotalCharges => WaterUsageCharges + SewerCharges + EnvironmentCharges + ServiceCharges; public double LocalTaxes { get { switch (AccountType) { case "Residential Household": return TotalCharges * 0.115; case "Social/Non-Profit Organization": return TotalCharges * 0.085; case "Water Intensive Business (Laudromat, etc)": return TotalCharges * 0.825; case "General Business": return TotalCharges * 0.315; default: return TotalCharges * 0.625; } } } public double StateTaxes { get { switch (AccountType) { case "Residential Household": return TotalCharges * 0.035; case "Social/Non-Profit Organization": return TotalCharges * 0.015; case "Water Intensive Business (Laudromat, etc)": return TotalCharges * 0.105; case "General Business": return TotalCharges * 0.065; default: return TotalCharges * 0.815; } } } public double AmountDue => TotalCharges + LocalTaxes + StateTaxes; } }
Stellar Water Point =========================================================== To prepare an invoice, enter the following information Types of Accounts 1. Residential Household 2. Social/Non-Profit Organization 3. Water Intensive Business (Laudromat, etc) 4. General Business 0. Other Enter Type of Account: 3 ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 =========================================================== Stellar Water Point - Customer Invoice Account Type: Water Intensive Business (Laudromat, etc) =========================================================== Meter Reading ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 Total Gallons Consumed: 132063 =========================================================== Therms Evaluation ----------------------------------------------------------- HCF Total: 176.55 First 35 Therms (* 1.5573): 38.93 First 20 Therms (* 1.2264): 18.40 Above 40 Therms (* 0.9624): 131.42 ----------------------------------------------------------- Water Usage Charges: 188.75 Sewer Charges: 299.45 =========================================================== Bill Values ----------------------------------------------------------- Environment Charges: 1.98 Service Charges: 19.16 Total Charges: 509.34 Local Taxes: 420.20 State Taxes: 53.48 ----------------------------------------------------------- Amount Due: 983.02 =========================================================== Press any key to close this window . . .
namespace StellarWaterPoint2.Models { internal class WaterBill { private double nd; private double crs; private string typeOfAccount; public WaterBill(string category) => typeOfAccount = category; public string AccountType { get { if (typeOfAccount == "1") return "Residential Household"; else if (typeOfAccount == "2") return "Social/Non-Profit Organization"; else if (typeOfAccount == "3") return "Water Intensive Business (Laudromat, etc)"; else if (typeOfAccount == "4") return "General Business"; else return "Other"; } } public double CounterReadingStart { get { return crs; } set { crs = value; } } public double CounterReadingEnd { get { return nd; } set { nd = value; } } public double Gallons => CounterReadingEnd - CounterReadingStart; public double HCFTotal => Gallons / 748; public (double First25Therms, double Next15Therms, double Above40Therms) Therms { get { double first, next, above; if (HCFTotal > 40.00) { first = 25.00 * 1.5573; next = 15.00 * 1.2264; above = (HCFTotal - 40.00) * 0.9624; } else if (HCFTotal > 25.00) { first = 25.00 * 1.5573; next = (HCFTotal - 25.00) * 1.2264; above = 0.00; } else // if (HCFTotal > 40.00) { first = HCFTotal * 1.5573; next = 0.00; above = 0.00; } return (first, next, above); } } public double WaterUsageCharges => Therms.First25Therms + Therms.Next15Therms + Therms.Above40Therms; public double SewerCharges { get { double result = AccountType switch { "Residential Household" => WaterUsageCharges * 0.2528, "Social/Non-Profit Organization" => WaterUsageCharges * 0.0915, "Water Intensive Business (Laudromat, etc)" => WaterUsageCharges * 1.5865, "General Business" => WaterUsageCharges * 0.6405, _ => WaterUsageCharges * 1.2125 }; return result; } } public double EnvironmentCharges { get { double result = AccountType switch { "Residential Household" => WaterUsageCharges * 0.0025, "Social/Non-Profit Organization" => WaterUsageCharges * 0.0015, "Water Intensive Business (Laudromat, etc)" => WaterUsageCharges * 0.0105, "General Business" => WaterUsageCharges * 0.0045, _ => WaterUsageCharges * 0.0125 }; return result; } } public double ServiceCharges { get => AccountType switch { "Residential Household" => WaterUsageCharges * 0.0328, "Social/Non-Profit Organization" => WaterUsageCharges * 0.0115, "Water Intensive Business (Laudromat, etc)" => WaterUsageCharges * 0.1015, "General Business" => WaterUsageCharges * 0.0808, _ => WaterUsageCharges * 0.1164 }; } public double TotalCharges => WaterUsageCharges + SewerCharges + EnvironmentCharges + ServiceCharges; public double LocalTaxes { get => AccountType switch { "Residential Household" => TotalCharges * 0.115, "Social/Non-Profit Organization" => TotalCharges * 0.085, "Water Intensive Business (Laudromat, etc)" => TotalCharges * 0.825, "General Business" => TotalCharges * 0.315, _ => TotalCharges * 0.625 }; } public double StateTaxes => AccountType switch { "Residential Household" => TotalCharges * 0.035, "Social/Non-Profit Organization" => TotalCharges * 0.015, "Water Intensive Business (Laudromat, etc)" => TotalCharges * 0.105, "General Business" => TotalCharges * 0.065, _ => TotalCharges * 0.815 }; public double AmountDue => TotalCharges + LocalTaxes + StateTaxes; } }
Stellar Water Point =========================================================== To prepare an invoice, enter the following information Types of Accounts 1. Residential Household 2. Social/Non-Profit Organization 3. Water Intensive Business (Laudromat, etc) 4. General Business 0. Other Enter Type of Account: 4 ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 =========================================================== Stellar Water Point - Customer Invoice Account Type: General Business =========================================================== Meter Reading ----------------------------------------------------------- Counter Reading Start: 92863 Counter Reading End: 224926 Total Gallons Consumed: 132063 =========================================================== Therms Evaluation ----------------------------------------------------------- HCF Total: 176.55 First 35 Therms (* 1.5573): 38.93 First 20 Therms (* 1.2264): 18.40 Above 40 Therms (* 0.9624): 131.42 ----------------------------------------------------------- Water Usage Charges: 188.75 Sewer Charges: 120.89 =========================================================== Bill Values ----------------------------------------------------------- Environment Charges: 0.85 Service Charges: 15.25 Total Charges: 325.74 Local Taxes: 102.61 State Taxes: 21.17 ----------------------------------------------------------- Amount Due: 449.53 =========================================================== Press any key to close this window . . .
A Fully-Implemented Tuple Property
As seen in our introduction to properties, if you want to control the details of processing a property, you can create a private field that is the same tuple type as the property. Then add either or both a get and a set clauses. Here is an example:
public class Employee { private (int, string, bool) id; public (int, string, bool) Identification { get { return id; } set { id = value; } } }
An Automatic Tuple Property
If you are not planning to validate, accept, or reject the values of the property, you can create the property as an automatic one. Here is an example:
public enum PayFrequency { Daily, Monthly, Yearly }
public class Employee
{
private (int, string, bool) id;
public (int, string, bool) Identification
{
get
{
return id;
}
set
{
id = value;
}
}
public (bool, string, PayFrequency, double) Salary { get; set; }
}
When creating the property, it is a good idea, although not a requirement, to name the elements of the tuple. It may also be a good idea to add comments that explain the roles of the elements of the tuple. Here is an example:
public enum PayFrequency { Daily, Monthly, Yearly } public class Employee { private (int, string, bool) id; /* An employee is identified with three pieces of information: * the code is a type of employee number or contractor code, * the name represents the full name. It could include the * first name, the middle initial, and a last name. * The last piece of information indicates whether the * employee is part-time (false) or full-time (true). */ public (int code, string name, bool full_time) Identification { get { return id; } set { id = value; } } /* We want various pieces of information that provide details about * employee or contractor's salary. * The isFixed item indicates whether an employee can work overtime * (and get paid the hourly salary and half). * The Pay Frequency indicates what the value of the salary * represents, such as hourly, monthly, etc. * The last value is the actual salary. Again, that value depends * on the pay frequency. */ public (bool isFixed, PayFrequency often, double number) Salary { get; set; } }
As mentioned in the previous lesson, if you don't name the elements of a tuple, the compiler gives them some default names as Item1, Item2, etc.
When using the property, you may need to access the elements of its type. You will access them by their names. From inside the class, such as in the body of a clause of another property or in the body of a method of the same name, type the name of the desired property, type a period, and select the element of your choice. Here are examples:
When using the property outside the class, if you have declared a variable of the class that owns the property, type the name of the object, a period, the name of the property, a period, and the desired element. Here is an example:
Once you have accessed the property or any of its elements, you can use it like any property as we have done in previous lessons.
If you create an automatic tuple property, you cannot individually initialize the elements of the tuple property. Still, you can access the elements to present to the user. Here is an example:
using static System.Console; Processor proc = new(); public class Processor { /* We are combining these pieces of information of the * processor because processors specifications are * related by generation and tied to a manufacturer. */ public (string make, string model, string socket) Manufacture { get; set; } public (int, int) Count { get; set; } public double Speed { get; set; } public Processor() { Count = (6, 12); Manufacture = ("AMD", "RYZEN 5", "AM4"); Present(); } private void Present() { Write("Make: "); WriteLine(Manufacture.make); Write("Model: "); WriteLine(Manufacture.model); Write("Socket: "); WriteLine(Manufacture.socket); Write("Processor: "); Write(Count.Item1); Write("-Core, "); Write(Count.Item2); WriteLine("-Thread."); WriteLine("=============================="); } }
This would produce:
Make: AMD Model: RYZEN 5 Socket: AM4 Processor: 6-Core, 12-Thread. ============================== Press any key to continue . . .
Topics on Tuples
Methods and Tuples
We saw how to involve tuples with functions. Everything we saw about passing a tuple as argument and returning a tuple can be applied exactly the same way to the methods of a class. Normally, methods deal with tuples exactly as we described for functions, with just minor adjustments. It is important to remember (as stated in our introductory lesson on functions) that a function is a section of code that behaves as if it written outside of a class. Otherwise, everything we studied about involving tuples and functions also applies to methods. This means that you can pass a tuple to a method and you can return a tuple from a method.
As you know already, a method is a function created inside a class. As you know already, if you create a method, it has direct access to other members of the same class. In a method, you have direct access to the names of the tuples. To access an element of a tuple, type the name of the member, a period, and the desired member. That way, you can initialize a tuple. Here is an example:
public class Processor
{
/* We are combining these pieces of information of the
* processor because processors specifications are
* related by generation and tied to a manufacturer. */
private (string make, string model, string socket) identification;
private void Create()
{
identification = ("AMD", "RYZEN 5", "AM4");
}
}
An Object in a Tuple
All the elements we used so far in tuples were of regular types. In reality, each element is a placeholder for practically any type you want. Based on this, an element of a tuple can be an object of a structure or class type. Of course, you must have a class. You can create and use your own class. Here is an example of a class named Trapezoid created in a Windows Forms application named Geometry:
namespace Geometry { public class Trapezoid { public double TopBase { get; set; } public double BottomBase { get; set; } public double Height { get; set; } public double Area { get { return Height * (TopBase + BottomBase) / 2.00; } } } }
When creating the tuple, specify the desired element using the name of the class. Here is an example:
Trapezoid isosceles = new Circle(); (Trapezoid shape, int value) definition; public class Trapezoid { public double TopBase { get; set; } public double BottomBase { get; set; } public double Height { get; set; } public double Area { get { return Height * (TopBase + BottomBase) / 2.00; } } }
When initializing the tuple or when specifying its value, you must define the object used as element. You have various options:
using static System.Console; Trapezoid trap = new Trapezoid(); trap.TopBase = 244.86; trap.BottomBase = 384.92; trap.Height = 175.74; string msg = null; (Trapezoid figure, string) shape = (trap, msg); WriteLine("Area: {0}", shape.figure.Area); public class Trapezoid { public double TopBase { get; set; } public double BottomBase { get; set; } public double Height { get; set; } public double Area { get { return Height * (TopBase + BottomBase) / 2.00; } } }This would produce:
Area: 55338.7686 Press any key to close this window . . .To access a member of the class, such as its property or methof, type the name of the tuple, a period, the name you gave to the element or the item_x default name, a period, and the name of the class member. Here is an example
using static System.Console; Trapezoid trap = new Trapezoid(); trap.TopBase = 244.86; trap.BottomBase = 384.92; trap.Height = 175.74; string msg = "An isosceles trapezoid is a quadrilateral with two opposing sidesl."; (Trapezoid figure, string description) shape = (trap, msg); WriteLine("Area: {0}", shape.figure.Area); public class Trapezoid { public double TopBase { get; set; } public double BottomBase { get; set; } public double Height { get; set; } public double Area { get { return Height * (TopBase + BottomBase) / 2.00; } } }
using static System.Console;
string msg = "An isosceles trapezoid is a quadrilateral with two opposing sidesl.";
Write("Top Base: ");
double tb = double.Parse(ReadLine());
Write("Bottom Base: ");
double bb = double.Parse(ReadLine());
Write("Height: ");
double height = double.Parse(ReadLine());
(Trapezoid figure, string description) shape = (new Trapezoid()
{
TopBase = tb,
BottomBase = bb,
Height = height
}, msg);
WriteLine("-------------------------");
WriteLine("Area: {0}", shape.figure.Area);
WriteLine("=========================");
public class Trapezoid
{
public double TopBase { get; set; }
public double BottomBase { get; set; }
public double Height { get; set; }
public double Area
{
get
{
return Height * (TopBase + BottomBase) / 2.00;
}
}
}
Here is an example of running the program:
Top Base: 228.97 Bottom Base: 406.66 Height: 214.83 ------------------------- Area: 68276.19645 ========================= Press any key to close this window . . .
using static System.Console; Trapezoid Create() { Trapezoid holder = new Trapezoid(); Write("Top Base: "); double tb = double.Parse(ReadLine()); Write("Bottom Base: "); double bb = double.Parse(ReadLine()); Write("Height: "); double height = double.Parse(ReadLine()); holder.TopBase = tb; holder.BottomBase = bb; holder.Height = height; return holder; } string msg = "An isosceles trapezoid is a quadrilateral with two opposing sidesl."; (Trapezoid figure, string description) shape = (Create(), msg); WriteLine("-------------------------"); WriteLine("Area: {0}", shape.figure.Area); WriteLine("========================="); public class Trapezoid { public double TopBase { get; set; } public double BottomBase { get; set; } public double Height { get; set; } public double Area { get { return Height * (TopBase + BottomBase) / 2.00; } } }Here is an example of running the program:
Top Base: 228.68 Bottom Base: 369.37 Height: 273.69 ------------------------- Area: 81840.15225 ========================= Press any key to close this window . . .
In the above example, we used a tuple that has one element that is a class type. In the same way, you can create a tuple with more than one element that are of class or structure type. The elements can be of the same class (or structure) or different classes (or structures).
In the above example, we used our own class. On the other hand, we know that the .NET Framework provides a large collection of classes and structures. You can use any of most of the many classes of the .NET Framework for an element of a tuple.
Practical Learning: Ending the Lesson
|
|||
Previous | Copyright © 2001-2023, C# Key | Saturday 29 April 2023, 22:44 | Next |
|