-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartPhone.cs
More file actions
114 lines (97 loc) · 3.61 KB
/
Copy pathSmartPhone.cs
File metadata and controls
114 lines (97 loc) · 3.61 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.Json;
namespace SharpPhone
{
public class SmartPhone
{
public int id { get; set; }
public string brand { get; set; }
public string model { get; set; }
public int size { get; set; }
public double price { get; set; }
public int stock { get; set; }
public SmartPhone(string Brand, string Model, int Size, double Price, int Stock)
{
this.id = SharpPhoneDataBase.phoneList.Count;
this.brand = Brand;
this.model = Model;
this.size = Size;
this.price = Price;
this.stock = Stock;
SharpPhoneDataBase.phoneList.Add(this);
JsonStore.Save();
}
}
public class UserAccount
{
public string username { get; set; } = "";
public string password { get; set; } = "";
public int failedAttempts { get; set; } = 0;
public bool locked { get; set; } = false;
public UserAccount(string username, string password, int failedAttempts, bool locked)
{
this.username = username;
this.password = password;
this.failedAttempts = failedAttempts;
this.locked = locked;
SharpPhoneDataBase.userAccounts.Add(this);
JsonStore.Save();
}
}
public class JsonStore
{
public static void Load()
{
var path = @"C:\Users\ryanl\source\repos\SharpPhone\phones.json";
if (!File.Exists(path)) return;
var json = File.ReadAllText(path);
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
if (root.TryGetProperty("phones", out var phones))
{
var phonesList = JsonSerializer.Deserialize<List<SmartPhone>>(phones.GetRawText());
SharpPhoneDataBase.phoneList = phonesList ?? new List<SmartPhone>();
}
if (root.TryGetProperty("users", out var users))
{
var usersList = JsonSerializer.Deserialize<List<UserAccount>>(users.GetRawText());
SharpPhoneDataBase.userAccounts = usersList ?? new List<UserAccount>();
}
}
public static void Save()
{
var data = new
{
phones = SharpPhoneDataBase.phoneList,
users = SharpPhoneDataBase.userAccounts
};
var json = JsonSerializer.Serialize(data, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText("C:\\Users\\ryanl\\source\\repos\\SharpPhone\\phones.json", json);
}
public static void Delete(int id)
{
SharpPhoneDataBase.phoneList.RemoveAll(p => p.id == id);
Save();
}
public static void Modify(int id, TextBox brand, TextBox model, TextBox size, TextBox price, TextBox stock)
{
SharpPhoneDataBase.phoneList[id].brand = brand.Text;
SharpPhoneDataBase.phoneList[id].model = model.Text;
SharpPhoneDataBase.phoneList[id].size = int.Parse(size.Text);
SharpPhoneDataBase.phoneList[id].price = double.Parse(price.Text);
SharpPhoneDataBase.phoneList[id].stock = int.Parse(stock.Text);
Save();
}
}
public class SharpPhoneDataBase
{
public static List<SmartPhone> phoneList = new List<SmartPhone>();
public static List<UserAccount> userAccounts = new List<UserAccount>();
}
}