forked from zanikoleh/PMI21_TeachingPractice
/
Product.cs
136 lines (123 loc) · 3.86 KB
/
Product.cs
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
//-----------------------------------------------------------------------
// <copyright file="Product.cs" company="PMI21_TeachingPractice">
// Copyright PMI21_TeachingPractice. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace PMI21_TeachingPractice
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
/// <summary>
/// Class which represents product.
/// </summary>
public class Product
{
/// <summary>
/// ID of product. For database.
/// </summary>
private int id;
/// <summary>
/// Name of product. For user.
/// </summary>
private string name;
/// <summary>
/// Price of product.
/// </summary>
private double price;
/// <summary>
/// Initializes a new instance of the <see cref="Product" /> class with default values: (0, "Product", 0).
/// </summary>
public Product()
{
this.id = 0;
this.name = "Product";
this.price = 0;
}
/// <summary>
/// Initializes a new instance of the <see cref="Product" /> class with values from parameters of constructor.
/// </summary>
/// <param name="id">ID of product.</param>
/// <param name="name">Name of product.</param>
/// <param name="price">Price of product.</param>
public Product(int id, string name, double price)
{
this.id = id;
this.name = name;
this.price = price;
}
/// <summary>
/// Initializes a new instance of the <see cref="Product" /> class with values from Product from parameters box.
/// </summary>
/// <param name="p">Object of class Product.</param>
public Product(Product p)
{
this.id = p.id;
this.name = p.name;
this.price = p.price;
}
/// <summary>
/// Gets or sets ID.
/// </summary>
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
/// <summary>
/// Sets fields for object from Xml file.
/// </summary>
/// <param name="reader">Reader module of file with data.</param>
public void Read(XmlTextReader reader)
{
while (reader.Read())
{
if (reader.Name == "id")
{
this.id = reader.ReadElementContentAsInt();
}
if (reader.Name == "name")
{
this.name = reader.ReadElementContentAsString();
}
if (reader.Name == "price")
{
this.price = reader.ReadElementContentAsDouble();
break;
}
}
}
/// <summary>
/// Fills xml file with data.
/// </summary>
/// <param name="writer">Writer module of file which will be filled with data.</param>
public void Write(XmlTextWriter writer)
{
writer.WriteStartElement("id");
writer.WriteValue(this.id);
writer.WriteEndElement();
writer.WriteStartElement("name");
writer.WriteValue(this.name);
writer.WriteEndElement();
writer.WriteStartElement("price");
writer.WriteValue(this.price);
writer.WriteEndElement();
}
/// <summary>
/// Writes data to console.
/// </summary>
public void Write()
{
Console.Write(this.id);
Console.Write(this.name);
Console.Write(this.price);
}
}
}