Austerity(Misaka10548)
Lv1
这一篇主要是记录自己编程写的一个C#原始的代码的,
可惜的地方在于自己的一些想法忘了(实际上是现在有点难组织成语言)
对比用
挺难看的
Program.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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 using System.Text;using System.Text.Json;namespace Test ;public static class Program { private static CliStatus status; private static Company? company; private static JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions { IncludeFields = true }; private enum CliStatus { Init, Loaded, Exit } public static void Main (string [] args ) { status = CliStatus.Init; do { switch (status) { case CliStatus.Init: init (); break ; case CliStatus.Loaded: menu(); break ; } } while (status != CliStatus.Exit); } private static void menu () { bool success = false ; do { Console.WriteLine(""" 菜单: q):退出 1):添加一个职工 2):列举所有职工 3):删除职工 4):修改职工信息 5):查找职工 6):排序 7):清空当前Company的内容 8):将当前company保存到company.json """ ); var option = Console.ReadKey().KeyChar; Console.WriteLine("" ); switch (option) { case 'q' : status = CliStatus.Exit; success = true ; break ; case '1' : success = company!.TryAddStaff(); break ; case '2' : Console.WriteLine(company!.GetStaffs()); success = true ; break ; case '3' : success = company!.TryRemoveStaff(); break ; case '4' : success = company!.TryModifyStaff(); break ; case '5' : success = company!.TrySearchStaff(); break ; case '6' : Console.WriteLine("是否要按id降序排序,是输入yes,否则视为不是" ); if (Console.ReadLine() == "yes" ) company!.Order(true ); else company!.Order(false ); success = true ; break ; case '7' : Console.WriteLine("确定操作请输入yes" ); if (Console.ReadLine() == "yes" ) { company = new Company(); } else { Console.WriteLine("操作取消" ); } success = true ; break ; case '8' : var content = JsonSerializer.Serialize(company, jsonSerializerOptions); using (StreamWriter writer = new StreamWriter("company.json" , false , Encoding.UTF8)) { writer.Write(content); } success = true ; break ; default : Console.WriteLine("选择无效,请重新选择" ); break ; } if (!success) Console.WriteLine("操作失败" ); } while (!success); } private static void init () { bool success = false ; Console.WriteLine(""" 选择一个初始化的方法: 1):从company.json读取 2):创建一个新Company对象 """ ); do { var option = Console.ReadKey().KeyChar; switch (option) { case '1' : success = loadFromFile(); break ; case '2' : company = new Company(); success = true ; break ; default : Console.WriteLine("" ); Console.WriteLine("选择无效,请重新选择" ); break ; } } while (!success); status = CliStatus.Loaded; } private static bool loadFromFile () { if (!File.Exists("company.json" )) { Console.WriteLine("" ); Console.WriteLine("未找到company.json" ); return false ; } try { using var fs = new FileStream("company.json" , FileMode.Open); company = JsonSerializer.Deserialize<Company>(fs); } catch (JsonException) { Console.WriteLine("company.json的数据无效" ); return false ; } return true ; } }
Company.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 using System.Text;using System.Text.Json.Serialization;namespace Test ;internal class Company { [JsonInclude ] private List<Staff> staffs = new (); public string GetStaffs () { StringBuilder sb = new StringBuilder(); foreach (var staff in staffs) { sb.Append(staff); sb.Append(Environment.NewLine); } return sb.ToString(); } public void AddStaff (Staff staff ) { if (staffs.Contains(staff)) throw new ArgumentException($"The {staff.Name} had already been added." ); if (staffs.Find(s => s.Id == staff.Id) is not null ) throw new ArgumentException($"The {staff.Name} have a duplicate ID." ); staffs.Add(staff); } public void RemoveStaff (int staffId ) { var staff = staffs.Find(staff => staff.Id == staffId) ?? throw new ArgumentException($"Couldn't find a staff has ID {staffId} " ); staffs.Remove(staff); } public Staff? SearchStaff(int staffId) { return staffs.Find(staff => staff.Id == staffId); } public void Order (bool descending ) { if (descending ) { staffs.Sort((x, y) => y.Id.CompareTo(x.Id)); } else { staffs.Sort((x, y) => x.Id.CompareTo(y.Id)); } } }
CompanyExtension.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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 namespace Test ;internal static class CompanyExtension { internal static bool TryAddStaff (this Company company ) { bool inputValid = false ; Console.WriteLine(""" 输入职工信息,以空格分隔 姓名 编号 职工类型 部门 """ ); do { var input = (Console.ReadLine() ?? string .Empty).Split(' ' ); if (input.Length == 4 ) { string name = input[0 ]; bool idParse = int .TryParse(input[1 ], out var id); bool staffTypeParse = Enum.TryParse(typeof (StaffType), input[2 ], out var staffType); string department = input[3 ]; if (idParse && staffTypeParse) { StaffType staffsType = (StaffType)staffType!; var staff = new Staff(name, id, staffsType, department); try { company.AddStaff(staff); } catch (ArgumentException) { return false ; } inputValid = true ; } } if (!inputValid) Console.WriteLine("输入无效,请重新输入" ); } while (!inputValid); return true ; } internal static bool TryRemoveStaff (this Company company ) { bool inputValid = false ; Console.WriteLine("输入要删除的职工id" ); do { var input = Console.ReadLine() ?? string .Empty; if (int .TryParse(input, out var id)) { inputValid = true ; if (company.SearchStaff(id) is null ) { Console.WriteLine("不存在指定的职工" ); return false ; } company.RemoveStaff(id); } if (!inputValid) Console.WriteLine("输入无效,请重新输入" ); } while (!inputValid); return true ; } internal static bool TryModifyStaff (this Company company ) { bool inputValid = false ; bool inputInfoVaild = false ; Console.WriteLine("输入要修改的职工个人信息的id" ); do { var input = (Console.ReadLine() ?? string .Empty); if (int .TryParse(input, out var id)) { inputValid = true ; var staff = company.SearchStaff(id); if (staff is null ) { Console.WriteLine("不存在指定的职工" ); return false ; } Console.WriteLine(""" 输入职工信息,以空格分隔 姓名 职工类型 部门 """ ); do { var info = (Console.ReadLine() ?? string .Empty).Split(' ' ); if (info.Length == 3 ) { string name = info[0 ]; bool staffTypeParse = Enum.TryParse(typeof (StaffType), info[1 ], out var staffType); StaffType staffsType = (StaffType)staffType!; string department = info[2 ]; if (staffTypeParse) { staff.Name = name; staff.Type = staffsType; staff.Department = department; inputInfoVaild = true ; } } if (!inputInfoVaild) Console.WriteLine("输入的信息无效,请重新输入" ); } while (!inputInfoVaild); } if (!inputValid) Console.WriteLine("输入无效,请重新输入" ); } while (!inputValid); return true ; } internal static bool TrySearchStaff (this Company company ) { bool inputValid = false ; Console.WriteLine("输入要搜索的职工个人信息的id" ); do { var input = (Console.ReadLine() ?? string .Empty); if (int .TryParse(input, out var id)) { inputValid = true ; var staff = company.SearchStaff(id); if (staff is null ) { Console.WriteLine("不存在指定的职工" ); return false ; } Console.WriteLine(staff); } if (!inputValid) Console.WriteLine("输入无效,请重新输入" ); } while (!inputValid); return true ; } }
Staff.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 namespace Test ;internal enum StaffType{ Empolyee, Boss, Manager } internal class Staff { public string Name { get ; set ; } public int Id { get ; init ; } public StaffType Type { get ; set ; } public string Department { get ; set ; } public Staff (string name, int id, StaffType type, string department ) { (Name, Id, Type, Department) = (name, id, type, department); } public override string ToString () { return $"Staff(ID: {Id} , Name: {Name} , Type: {Type} , Department: {Department} )" ; } }
算是给自己留的赛博黑历史吧,即使是第一次写这样的代码(以前最多写俩没啥效率的脚本)