-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.cs
More file actions
116 lines (108 loc) · 3.83 KB
/
Copy pathItem.cs
File metadata and controls
116 lines (108 loc) · 3.83 KB
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
namespace Assignment2
{
internal class Item
{
//? Files
private const char DELIMITER = '|';
private const string DIR = "../../../Items.txt";
//? Variables
private static int MaxId = 0;
public int Id;
public string Type;
public string Cut;
public string Color;
public string Fabric;
public string Size;
public string Brand;
public double BasePrice;
public double? Surcharge;
//? All items
public static Dictionary<int, Item> Items = new Dictionary<int, Item>();
//? Input
public Item(string Type, string Cut,
string Color, string Fabric, string Size,
string Brand, double BasePrice, double? Surcharge)
{
Id = ++MaxId;
this.Type = Type;
this.Cut = Cut;
this.Color = Color;
this.Fabric = Fabric;
this.Size = Size;
this.Brand = Brand;
this.BasePrice = BasePrice;
this.Surcharge = Surcharge;
Items.Add(Id, this);
}
//? Output
public Item(string line)
{
string[] data = line.Split(DELIMITER);
this.Id = int.Parse(data[0]);
if (MaxId < Id) MaxId = Id;
this.Type = data[1];
this.Cut = data[2];
this.Color = data[3];
this.Fabric = data[4];
this.Size = data[5];
this.Brand = data[6];
this.BasePrice = double.Parse(data[7]);
this.Surcharge = double.Parse(data[8]);
Items.Add(Id, this);
}
//? Functions
//* Default
public int GetId() { return Id; }
public string GetType() { return Type; }
public string GetCut() { return Cut; }
public string GetColor() { return Color; }
public string GetFabric() { return Fabric; }
public string GetSize() { return Size; }
public string GetBrand() { return Brand; }
public double GetBasePrice() { return BasePrice; }
public double GetSurcharge()
{
if (Surcharge == null) return 0;
else return (double)Surcharge;
}
public double GetPrice()
{
if (Surcharge == null) return BasePrice;
else return BasePrice + (double)Surcharge;
}
//* Saving and loading
private string SaveConstruct()
{
return GetId().ToString() + DELIMITER + GetType() + DELIMITER + GetCut() + DELIMITER +
GetColor() + DELIMITER + GetFabric() + DELIMITER + GetSize() + DELIMITER +
GetBrand() + DELIMITER + GetBasePrice() + DELIMITER + GetSurcharge();
}
public static void SaveItems()
{
if (!File.Exists(DIR)) File.WriteAllText(Item.DIR, string.Empty);
StreamWriter sw = new StreamWriter(DIR);
foreach (Item item in Items.Values)
sw.WriteLine(item.SaveConstruct());
sw.Close();
}
public static void LoadItems()
{
if (!File.Exists(DIR)) return;
string _ItemsReaded = File.ReadAllText(DIR);
string[] _Items = _ItemsReaded.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (string item in _Items)
{
if (string.IsNullOrEmpty(item)) continue;
new Item(item);
}
}
//* Editing
public void Delete(int Id)
{ if (Items.ContainsKey(Id)) Items.Remove(Id); }
public static Item GetItem(int Id)
{
if (Items.ContainsKey(Id)) return Items[Id];
else return null;
}
}
}