using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApp1
{
//枚举 Enumeration
enum LEVEL
{
INFO,
ERROR,
OK
}
{
//枚举 Enumeration
enum LEVEL
{
INFO,
ERROR,
OK
}
//LOG
//静态类
//全局可用
static class log
{
public static void output(LEVEL level,string format, params object[] arg)
{
//备份当前颜色
ConsoleColor oldColor = Console.ForegroundColor;
//静态类
//全局可用
static class log
{
public static void output(LEVEL level,string format, params object[] arg)
{
//备份当前颜色
ConsoleColor oldColor = Console.ForegroundColor;
//新颜色
ConsoleColor newColor = oldColor;
ConsoleColor newColor = oldColor;
//判定输出类型
switch(level)
{
//对level进行判定
case LEVEL.INFO:
newColor = oldColor; //颜色不变
break; //结束判定
switch(level)
{
//对level进行判定
case LEVEL.INFO:
newColor = oldColor; //颜色不变
break; //结束判定
case LEVEL.ERROR:
newColor = ConsoleColor.Red;//红色
break;
case LEVEL.OK:
newColor = ConsoleColor.Green;//绿色
break;
}
//改变Console颜色
Console.ForegroundColor = newColor;
Console.ForegroundColor = newColor;
//输出
Console.WriteLine(format, arg);
Console.WriteLine(format, arg);
//还原颜色
Console.ForegroundColor = oldColor;
Console.ForegroundColor = oldColor;
}
}
// 声明一个图书系统类
class BookSys
{
// 字典Dictionary
// Container
Dictionary<string, string> userDataBase;
class BookSys
{
// 字典Dictionary
// Container
Dictionary<string, string> userDataBase;
//类的构造函数
//和类名一样
public BookSys()
{
userDataBase = new Dictionary<string, string>();
}
//和类名一样
public BookSys()
{
userDataBase = new Dictionary<string, string>();
}
//封装 Encapsulation
public void command_login(string[] chunks)
{
//登陆
{
//登陆
//判断用户是不是存在
string userName = chunks[1];
string password = chunks[2];
string userName = chunks[1];
string password = chunks[2];
//存在的话判断密码是不是对
string lowerUserName = userName.ToLower();
string lowerUserName = userName.ToLower();
//检查一下是否有此人
if (userDataBase.ContainsKey(lowerUserName))
{
//有此用户
if (userDataBase.ContainsKey(lowerUserName))
{
//有此用户
//检查密码
if(password == userDataBase[lowerUserName])
{
// 登陆成功
log.output(LEVEL.OK, "You have logged in successfully");
}
if(password == userDataBase[lowerUserName])
{
// 登陆成功
log.output(LEVEL.OK, "You have logged in successfully");
}
//密码不正确
else
{
log.output(LEVEL.ERROR, "Password name are not match.");
}
}
else
{
log.output(LEVEL.ERROR, "The user {0} is not registered.");
}
}
public void command_reg(string[] chunks)
{
//让DataBase里面以用户名为键的字符串,变为密码的字符串
string userName = chunks[1];
string password = chunks[2];
{
//让DataBase里面以用户名为键的字符串,变为密码的字符串
string userName = chunks[1];
string password = chunks[2];
//检查一下是否有此人
if (userDataBase.ContainsKey(userName))
{
log.output(LEVEL.ERROR, "The user {0} has been registered already", userName);
return;
}
if (userDataBase.ContainsKey(userName))
{
log.output(LEVEL.ERROR, "The user {0} has been registered already", userName);
return;
}
//写入用户名数据库
//键 key 索引
//值 value 内容
string lowerUserName = userName.ToLower();
userDataBase[userName] = password;
//键 key 索引
//值 value 内容
string lowerUserName = userName.ToLower();
userDataBase[userName] = password;
//输出用户成功注册
log.output(LEVEL.OK, "The user {0} has been registered successfully.", userName);
log.output(LEVEL.OK, "The user {0} has been registered successfully.", userName);
return;
}
}
// 给系统加一个技能,执行命令
public void runCommand(string inputMessage)
{
//判定命令
// 输入的命令会是chen chen 123的形式
// 以空格来分割
// chen chen 123 变成三个独立的字符串
char[] saparator = { ' ' };
string[] paramArray = inputMessage.Split(saparator, StringSplitOptions.RemoveEmptyEntries);
public void runCommand(string inputMessage)
{
//判定命令
// 输入的命令会是chen chen 123的形式
// 以空格来分割
// chen chen 123 变成三个独立的字符串
char[] saparator = { ' ' };
string[] paramArray = inputMessage.Split(saparator, StringSplitOptions.RemoveEmptyEntries);
//取出命令名,也就是输入字符串的第一个元素,以0开始
string commandName = paramArray[0];
string commandName = paramArray[0];
//并转发给对应函数执行
if(commandName == "chen")
{
command_reg(paramArray);
}
else if(commandName == "login")
{
command_login(paramArray);
}
else
{
log.output(LEVEL.ERROR, "The command {0} is not available", commandName);
}
}
}
if(commandName == "chen")
{
command_reg(paramArray);
}
else if(commandName == "login")
{
command_login(paramArray);
}
else
{
log.output(LEVEL.ERROR, "The command {0} is not available", commandName);
}
}
}
class Program
{
static void Main(string[] args)
{
//新建一个系统的实例
BookSys bookSys = new BookSys();
{
static void Main(string[] args)
{
//新建一个系统的实例
BookSys bookSys = new BookSys();
//定义字符变量
string inputMessage;
string inputMessage;
do
{
//接收输入前,先打印系统名称
Console.Write("Book System > ");
{
//接收输入前,先打印系统名称
Console.Write("Book System > ");
//接收输入
inputMessage = Console.ReadLine();
inputMessage = Console.ReadLine();
//将输入的内容,按命令方式执行
bookSys.runCommand(inputMessage);
bookSys.runCommand(inputMessage);
} while (inputMessage != "exit");
Console.Read();
return ;
}
}
}
return ;
}
}
}