一个简易的账号管理系统
using System;
using System.IO;
using System.Reflection;
namespace AccountManagementSystem
{
class Program
{
static void Main(string[] args)
{
bool isRunning = true;
while (isRunning)
{
Console.WriteLine("账号管理系统");
Console.WriteLine("请输入相应的数字进行操作:");
Console.WriteLine("1. 注册新账号");
Console.WriteLine("2. 登录账号");
Console.WriteLine("3. 退出系统");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
RegisterAccount();
break;
case "2":
Login();
break;
case "3":
isRunning = false;
break;
default:
Console.WriteLine("无效的选择,请重新输入!");
break;
}
}
}
static void RegisterAccount()
{
Console.WriteLine("请输入账号:");
string account = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();
FileStream f = new FileStream("accounts.txt", FileMode.OpenOrCreate);
f.Close();
File.WriteAllText("accounts.txt", account+password);
//将账号和密码写入文本文件
using (StreamWriter writer = new StreamWriter("accounts.txt", true))
{
writer.WriteLine("{0}", account + password);
}
Console.WriteLine("账号注册成功!");
}
static void Login()
{
FileStream a = new FileStream("accounts.txt", FileMode.OpenOrCreate);
Console.WriteLine("请输入账号:");
string account = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();
bool cunzai = false;
StreamReader sr = new StreamReader(a);
string inflie = sr.ReadLine();
a.Close();
if (inflie.Trim().Contains(account+password))
{
cunzai = true;
}
if (cunzai)
{
Console.WriteLine("登陆成功");
main();
}
else
{
Console.WriteLine("账号或密码错误");
}
}
static void main()
{
Console.WriteLine("1.查询鲜花数据2.储存鲜花数据3.删除鲜花数据4.更改鲜花数据5.退出系统");
string num = Console.ReadLine();
bool running = true;
while (running)
{
switch (num)
{
case "1":
{
//chaxun();
}break;
case "2":
{
chucun();
}break;
case "3":
{
//sanchu();
}break;
case "4":
{
//gengai();
}break;
case "5":
{
running = false;
}break;
default :
{
Console.WriteLine("无效的选择请重新输入。");
num = Console.ReadLine();
}
break;
}
}
}
索引器
-
索引器(Indexer) 允许一个对象可以像数组一样使用下标的方式来访问。
-
当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。您可以使用数组访问运算符 [ ]来访问该类的的成员。
namespace suoying
{
class text
{
//定义一个简单的索引器
public int this[int index]
{
get//当调用索引器时发生的事情
{
Console.WriteLine(index);
return 100;//索引返回值
}
set//当赋值时所发生的事情
{
Console.WriteLine(index);
Console.WriteLine(value);
}
}
}
}
namespace suoying
{
internal class Program
{
static void Main(string[] args)
{
text t = new text();
t[2] = 200;
Console.WriteLine(t[2]);
Console.ReadLine();
}
}
}
输出:
2
200
2
100
索引器的简单使用
private string[] name = new string[10];
public string this[int index]
{
get
{
return name[index];
}
set
{
name[index] = value;
}
}
//
text t = new text();
t[0] = "小王";
Console.WriteLine(t[0]);
Console.ReadLine();
输出:小王
利用索引器将星期转化为数字形式
namespace suoying
{
internal class Week
{
private string[] days = { "Mon", "Tues", "Web", "Thurs", "Fri", "Sat", "Sun" };
private int GetDay(string day)
{
int i = 0;
foreach (string s in days)
{
if (s == day)
{
return i + 1;
}
i++;
}
return -1;
}
public int this[string day]
{
get
{
return GetDay(day);
}
}
}
}
Week week = new Week();
Console.WriteLine(week["Fri"]);
Console.Read();
输出:5