C#之图书管理登陆

8 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApp1
{
    //枚举 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;

            //新颜色
            ConsoleColor newColor = oldColor;

            //判定输出类型
            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.WriteLine(format, arg);
            //还原颜色
            Console.ForegroundColor = oldColor;
           
        }
    }

    // 声明一个图书系统类
    class BookSys
    {
        // 字典Dictionary
        // Container
        Dictionary<string, string> userDataBase;

        //类的构造函数
        //和类名一样
        public BookSys()
        {
            userDataBase = new Dictionary<string, string>();
        }

        //封装 Encapsulation
        public void command_login(string[] chunks)
        {
            //登陆

            //判断用户是不是存在
            string userName = chunks[1];
            string password = chunks[2];

            //存在的话判断密码是不是对
            string lowerUserName = userName.ToLower();

            //检查一下是否有此人
            if (userDataBase.ContainsKey(lowerUserName))
            {
                //有此用户
                //检查密码
                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];

            //检查一下是否有此人
            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;

            //输出用户成功注册
            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);

            //取出命令名,也就是输入字符串的第一个元素,以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);
            }
        }
           
    }

    class Program
    {
        static void Main(string[] args)
        {
            //新建一个系统的实例
            BookSys bookSys = new BookSys();

            //定义字符变量
            string inputMessage;
            do
            {
                //接收输入前,先打印系统名称
                Console.Write("Book System > ");

                //接收输入
                inputMessage = Console.ReadLine();

                //将输入的内容,按命令方式执行
                bookSys.runCommand(inputMessage);
            } while (inputMessage != "exit");
           
            Console.Read();
            return ;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值