何でも書きなさい

25240 ワード

  1 /// <summary>

  2     ///  

  3     /// </summary>

  4     public class Manager

  5     {

  6         private static Manager _singleManager;

  7         private Manager()

  8         {

  9         }

 10         public static Manager GetSingelManager()

 11         {

 12             if (_singleManager == null)

 13                 _singleManager = new Manager();

 14             return _singleManager;

 15         }

 16         public void UpLevel(User user)

 17         {

 18             if (user.Key.UserType == CardType.Gold)

 19             {

 20                 Console.WriteLine(user.UserName + ": Gold ");

 21                 return;

 22             }

 23             user.Key.UserType += 1;

 24             Console.WriteLine(user.UserName + ": " + user.Key.UserType);

 25         }

 26         public void DownLevel(User user)

 27         {

 28             if (user.Key.UserType == CardType.Gold)

 29             {

 30                 user.Key.UserType -= 1;

 31                 Console.WriteLine(user.UserName + ": " + user.Key.UserType);

 32                 return;

 33             }

 34             Console.WriteLine(user.UserName + ": " + user.Key.UserType + " ");

 35         }

 36     }

 37     /// <summary>

 38     ///  

 39     /// </summary>

 40     public enum CardType

 41     {

 42         Sliver,

 43         Gold,

 44     }

 45     /// <summary>

 46     ///  key

 47     /// </summary>

 48     public class Key

 49     {

 50         public CardType UserType { get; set; }

 51         public string SoleString { get; private set; }//Key 

 52         public Key(CardType e, string soleString)

 53         {

 54             UserType = e;

 55             SoleString = soleString;

 56         }

 57         public override string ToString()

 58         {

 59             return UserType+":"+SoleString;

 60         }

 61     }

 62     /// <summary>

 63     /// 64     /// </summary>

 65     public interface IMyDataInterFace

 66     {

 67         string GetData();// 

 68 

 69         bool PostData(string message);// 

 70     }

 71     /// <summary>

 72     ///  

 73     /// </summary>

 74     public class User : IMyDataInterFace

 75     {

 76         public Key Key { get; private set; }

 77         public string UserName { get; private set; }

 78         public MyServer Server { get; private set; }

 79         /// <summary>

 80         /// 81         /// </summary>

 82         /// <param name="key"></param>

 83         /// <param name="userName"></param>

 84         /// <param name="server"></param>

 85         public User(Key key, string userName,MyServer server)

 86         {

 87             Key = key;

 88             UserName = userName;

 89             this.Server = server;

 90         }

 91         public string GetData()

 92         {

 93             return Server.GetMessage(Key);

 94         }

 95         public bool PostData(string message)

 96         {

 97             if (Server.PostMessge(Key, message))

 98             {

 99                 Console.WriteLine(" ");

100                 return true;

101             }

102             Console.WriteLine(" ");

103             return false;

104         }

105     }

106     /// <summary>

107     ///  

108     /// </summary>

109     public class MyServer

110     {

111         private static MyServer _SingleServer;

112         public  Dictionary<Key, string> Dictionary { get; private set; }

113         private MyServer()

114         {

115         }

116         public static MyServer GetSingleServer()

117         {

118             if (_SingleServer==null)

119                 _SingleServer=new MyServer {Dictionary = new Dictionary<Key, string>()};

120             return _SingleServer;

121         }

122 

123         public string GetMessage(Key key)

124         {

125             var result = string.Empty;

126             try

127             {

128                 if(key.UserType==CardType.Gold)

129                    result ="GoleUser:"+ Dictionary[key];

130                 else

131                 {

132                     result = "SliverUser:" + Dictionary[key];

133                 }

134                 return result;

135             }

136             catch (Exception)

137             {

138                 Console.WriteLine(key+"-> ");// key 

139                 return null;

140             }

141             

142         }

143         public bool PostMessge(Key key, string message)

144         {

145             try

146             {

147                 Dictionary[key] = message;

148                 return true;

149             }

150             catch (Exception)

151             {

152                 return false;

153             }

154         }

155     }

156     class Program

157     {

158         static  void Main(string[] args)

159         {

160             var server = MyServer.GetSingleServer();

161             var manager = Manager.GetSingelManager();

162 

163             var key=new Key(CardType.Gold,"33520");

164             var user1 = new User(key, " ",server);

165             

166             var postSuccess=user1.PostData("hello");

167             Console.WriteLine(user1.GetData()); 

168 

169             manager.DownLevel(user1);

170             Console.WriteLine(user1.Key.UserType); 

171 

172         }

173     }