Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace serialize
- {
- class Player
- {
- public string Name { get; set; }
- public int LvL { get; set; }
- public int Exp;
- string ID { get; }
- public Player()
- {
- ID = generator(30);
- LvL = 1;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Name - {Name}\nLvL - {LvL}\nExp - {Exp}/{10 * LvL * LvL}\n");
- }
- public void GiveExp(int X)
- {
- Exp += X;
- if (Exp > 10 * LvL * LvL)
- {
- Exp -= 10 * LvL * LvL;
- LvL ++;
- this.GiveExp(0);
- }
- }
- static string generator(int n)
- {
- Random rand = new Random();
- char cur;
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < n; i++)
- {
- cur = (char)('a' + rand.Next(0, 9));
- result.Append(cur);
- }
- return result.ToString();
- }
- }
- class Human
- {
- public string Name { get; set; }
- public int Age { get; set; }
- }
- class Student:Human
- {
- public int Year { get; set; }
- }
- class Worker:Human
- {
- public int Salary { get; set; }
- }
- class Child : Human
- {
- public int Height { get; set; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var player1 = new Player { Name = "forest123" };
- var player2 = new Player { Name = "Decibit" };
- var player3 = new Player { Name = "Chistoffer1996" };
- player1.GiveExp(100);
- player2.GiveExp(300);
- player3.GiveExp(1000);
- player1.ShowInfo();
- player2.ShowInfo();
- player3.ShowInfo();
- List<Player> players = new List<Player>();
- players.Add(player1);
- players.Add(player2);
- players.Add(player3);
- Console.WriteLine(new String('=', 25));
- Console.WriteLine();
- using (StreamWriter sw = new StreamWriter("Text.json"))
- {
- sw.WriteLine(JsonConvert.SerializeObject(players));
- Console.WriteLine("Запись выполнена");
- }
- using (StreamReader sr = new StreamReader("Text.json"))
- {
- List<Player> restoredPlayers = JsonConvert.DeserializeObject<List<Player>>(sr.ReadToEnd());
- foreach (var item in restoredPlayers)
- {
- item.ShowInfo();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement