程序功能:
程序实现:
li 1234
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Remoting.Messaging;
using System.Security.AccessControl;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace community_library_chsarp
{
class showmenu
{
public static void showmainmenu()
{
string str = @"
welcome to the Community Library
================================
1.Staff Login
2.Member Login
0.Exit
================================
Please make a selection (1-2,or 0 to exit);";
Console.Write(str);
}
public static void showsubmenu1()
{
string str = @"
===============Staff Menu============
1.Add a new movie DVD
2.Remove a movie DVD
3.Register a new Member
4.Find a registered member's phone number
0.Return to main menu
======================================
Please make a selection (1-4,or 0 to return to main menu);";
Console.Write(str);
}
public static void showsubmenu2()
{
string str = @"
==============Member Menu==============
1.Display all movies
2.Borrow a movie DVD
3.Return a movie DVD
4.List current borrowed movie DVDS
5.Display top 10 most popular movies
0.Return to main menu
=======================================
Please make a selection (1-5,or 0 to return to main menu);";
Console.Write(str);
}
public static void showaddstaff()
{
string str = @"
==============ADD Staff Or Customer==============
1.ADD a staff
2.ADD a customer
0.Return to main menu
=======================================
Please make a selection (1-2,or 0 to return to main menu);";
Console.Write(str);
}
public static void showregistmenu()
{
string str = @"
==============Register Menu==============
1.Register for a Satff
2.Register for a Customer
0.Return to last menu
=======================================
Please make a selection (1-2,or 0 to return to last menu);";
Console.Write(str);
}
}
public class DVD
{
public bool isborrowed
{
get;
set;
}
public DateTime borrowdate
{
get;
set;
}
public DateTime returndate
{
get;
set;
}
public Dictionary<DateTime, DateTime> borrowrecord;
public int borrowcount
{
get;
set;
}
public string name
{
get;
set;
}
public int id
{
get;
set;
}
public DVD(string n, int i)
{
name = n;
id = i;
isborrowed = false;
borrowdate = new DateTime();
borrowrecord = new Dictionary<DateTime, DateTime>();
}
public DVD(string n, int i, string borrowinfo)
{
name = n;
id = i;
try
{
int mill = 0;
string[] info = borrowinfo.Split(';');
borrowrecord = new Dictionary<DateTime, DateTime>();
for (int j = 0; j < info.Length - 1; j++)
{
int byear = int.Parse(info[j].Split('-')[0].Split(':')[0]);
int bmonth = int.Parse(info[j].Split('-')[0].Split(':')[1]);
int bday = int.Parse(info[j].Split('-')[0].Split(':')[2]);
int ryear = int.Parse(info[j].Split('-')[1].Split(':')[0]);
int rmonth = int.Parse(info[j].Split('-')[1].Split(':')[1]);
int rday = int.Parse(info[j].Split('-')[1].Split(':')[2]);
if (ryear==1975&& rmonth==12&& rday==1)
{
isborrowed = true;
}
borrowrecord.Add(new DateTime(byear, bmonth, bday,1,1,1,mill++), new DateTime(ryear, rmonth, rday, 1, 1, 1, mill));
}
}
catch (Exception)
{
throw new Exception();
}
}
}
public class BTnode
{
//节点本身的数据
public DVD data;
//左孩子
public BTnode left;
//右孩子
public BTnode right;
public BTnode(DVD d)
{
data = d;
left = right = null;
}
}
public class BinaryTreeDVD
{
public static void Insert(BTnode newBTnode, ref BTnode rootDVD)
{
//如果为空树,则插入根节点
if (rootDVD == null)
{
rootDVD = newBTnode;
}
//否则找到合适叶子节点位置插入
else
{
BTnode Current = rootDVD;
while (true)
{
BTnode Parent = Current;
if (newBTnode.data.id < Current.data.id)
{
Current = Current.left;
if (Current == null)
{
Parent.left = newBTnode;
//插入叶子后跳出循环
break;
}
}
else
{
Current = Current.right;
if (Current == null)
{
Parent.right = newBTnode;
//插入叶子后跳出循环
break;
}
}
}
}
}
public static int borrowsum(DVD d)
{
if (d.borrowrecord != null)
{
return d.borrowrecord.Count;
}
return -1;
}
public static void Findtop10(BTnode theRoot,ref List<DVD> l)
{
if (theRoot != null)
{
if (l.Count == 0)
l.Add(theRoot.data);
else
{
if (borrowsum(theRoot.data) > borrowsum(l[l.Count - 1]))
l.Add(theRoot.data);
else
{
int i;
for ( i = 0; i < l.Count; i++)
{
if (borrowsum(theRoot.data) <= borrowsum(l[i]))
break;
}
l.Insert(i, theRoot.data);
}
}
}
else
return ;
Findtop10(theRoot.left,ref l);
Findtop10(theRoot.right, ref l);
}
static string str = "";
public static string PreOrder(BTnode theRoot, bool flag)
{
Dictionary<DateTime, DateTime> dc;
if (theRoot != null)
{
if (flag)
{
Console.WriteLine("Nmae:" + theRoot.data.name + " id:" + theRoot.data.id + " " + (theRoot.data.isborrowed == true ? "borrowed" : "unborrowed"));
}
else
{
str += theRoot.data.name + " " + theRoot.data.id +" ";
dc = theRoot.data.borrowrecord;
foreach (KeyValuePair<DateTime, DateTime> keyval in dc)
{
str +=keyval.Key.Year + ":" + keyval.Key.Month + ":" + keyval.Key.Day + "-";
str +=keyval.Value.Year + ":" + keyval.Value.Month + ":" + keyval.Value.Day + ";";
}
str += "\r\n";
}
PreOrder(theRoot.left, flag);
PreOrder(theRoot.right, flag);
}
return str;
}
//找到最大节点
public static BTnode FindMax(BTnode rootDVD)
{
BTnode current = rootDVD;
//找到最右边的节点即可
while (current.right != null)
{
current = current.right;
}
return current;
}
//找到最小节点
public static BTnode FindMin(BTnode rootDVD)
{
BTnode current = rootDVD;
//找到最左边的节点即可
while (current.left != null)
{
current = current.left;
}
return current;
}
public static BTnode Search(int id, BTnode rootDVD)
{
BTnode current = rootDVD;
while (true)
{
if (id < current.data.id)
{
if (current.left == null)
break;
current = current.left;
}
else if (id > current.data.id)
{
if (current == null)
break;
current = current.right;
}
else
{
return current;
}
}
if (current.data.id != id)
{
return null;
}
return current;
}
public static bool delete(BTnode root, int key)
{
Stack<BTnode> stack = new Stack<BTnode>();
BTnode p = root, q;
while (p != null)
{
// System.out.println(p.val);
stack.Push(p);
if (p.data.id == key)
{
//删除节点的左右子树都为空 直接将父节点的该左子树或者右子树置空
if (p.left == null && p.right == null)
{
//System.out.println(" p.left==null&&p.right==null" + p.val);
if (stack.Count!=0)
{
stack.Pop();
q = stack.Peek();
if (q.left == p)
{
q.left = null;
return true;
}
else
{
q.right = null;
return true;
}
}
else
{
return false;
}
//左子树为空 将右子树直接放到父节点的删除节点位置上
}
else if (p.left == null && p.right != null)
{
// System.out.println(" p.left==null&&p.right!=null" + p.val);
if (stack.Count!=0)
{
stack.Pop();
q = stack.Peek();
if (q.left == p)
{
q.left = p.right;
}
else
{
q.right = p.right;
}
}
else
{
return false;
}
//右子树为空 左子树放到该节点位置上
}
else if (p.left != null && p.right == null)
{
//System.out.println(" p.left!=null&&p.right==null" + p.val);
if (stack.Count!=0 )
{
stack.Pop();
q = stack.Peek();
if (q.left == p)
{
q.left = p.left;
}
else
{
q.right = p.left;
}
return true;
}
else
{
return false;
}
}
else
{
//左右子树都不为空 将左子树节点放到右子树的最左边
// System.out.println(" p.left!=null&&p.right!=null" + p.val);
BTnode n = p.right;
while (n.left != null)
{
n = n.left;
}
if (!(stack.Count==0))
{
stack.Pop();
q = stack.Peek();
if (q.left == p)
{
q.left = p.right;
n.left = p.left;
}
else
{
q.right = p.right;
n.left = p.left;
}
return true;
}
else
{
return false;
}
}
}
else
{
if (p.data.id > key)
{
p = p.left;
}
else
{
p = p.right;
}
}
}
return false;
}
//删除二叉查找树中的节点,最麻烦的操作
public static BTnode Delete(int key, BTnode rootDVD)
{
if(rootDVD==null)
{
return null;
}
BTnode parent = rootDVD;
BTnode current = rootDVD;
//首先找到需要被删除的节点&其父节点
while (true)
{
if (key < current.data.id)
{
if (current.left == null)
break;
parent = current;
current = current.left;
}
else if (key > current.data.id)
{
if (current == null)
break;
parent = current;
current = current.right;
}
//找到被删除节点,跳出循环
else
{
break;
}
}
//找到被删除节点后,分四种情况进行处理
//情况一,所删节点是叶子节点时,直接删除即可
if (current.left == null && current.right == null)
{
//如果被删节点是根节点,且没有左右孩子
if (current == rootDVD && rootDVD.left == null && rootDVD.right == null)
{
rootDVD = null;
}
else if (current.data.id < parent.data.id)
parent.left = null;
else
parent.right = null;
}
//情况二,所删节点只有左孩子节点时
else if (current.left != null && current.right == null)
{
if (current.data.id < parent.data.id)
parent.left = current.left;
else
parent.right = current.left;
}
//情况三,所删节点只有右孩子节点时
else if (current.left == null && current.right != null)
{
if (current.data.id < parent.data.id)
parent.left = current.right;
else
parent.right = current.right;
}
//情况四,所删节点有左右两个孩子
else
{
//current是被删的节点,temp是被删左子树最右边的节点
BTnode temp;
//先判断是父节点的左孩子还是右孩子
if (current.data.id < parent.data.id)
{
parent.left = current.left;
temp = current.left;
//寻找被删除节点最深的右孩子
while (temp.right != null)
{
temp = temp.right;
}
temp.right = current.right;
}
//右孩子
else if (current.data.id > parent.data.id)
{
parent.right = current.left;
temp = current.left;
//寻找被删除节点最深的左孩子
while (temp.left != null)
{
temp = temp.left;
}
temp.right = current.right;
}
//当被删节点是根节点,并且有两个孩子时
else
{
temp = current.left;
while (temp.right != null)
{
temp = temp.right;
}
temp.right = rootDVD.right;
rootDVD = current.left;
}
}
return current;
}
}
public class MovieCollection
{
static BTnode DVDROOT = null;
public static BTnode initReadFromTXT()
{
StreamReader sr = new StreamReader("DVDsinfo.txt", Encoding.ASCII);
string[] info = sr.ReadToEnd().Split('\r');
DVD d;
for (int i = 0; i < info.Length-1; i++)
try
{
string[] infos = info[i].Trim('\n').Split(' ');
if (infos.Length == 2)
{
d = new DVD(infos[0].Trim('\n'), int.Parse(infos[1].Trim('\n')));
BinaryTreeDVD.Insert(new BTnode(d),ref DVDROOT);
}
else if (infos.Length == 3)
{
d = new DVD(infos[0].Trim('\n'), int.Parse(infos[1].Trim('\n')), infos[2].Trim('\n'));
BinaryTreeDVD.Insert(new BTnode(d), ref DVDROOT);
}
else
{
throw new Exception();
}
}
catch (Exception)
{
Console.WriteLine("READ FILE FAILED!");
Console.ReadKey();
return null;
}
sr.Close();
return DVDROOT;
}
public static bool WriteToTXT( BTnode btroot)
{
try
{
StreamWriter sw = new StreamWriter("DVDsinfo.txt", false, Encoding.ASCII);
sw.Write(BinaryTreeDVD.PreOrder(btroot, false));
sw.Close();
}
catch ( Exception)
{
Console.WriteLine("WRITE FILE ERROR!SAVE FAILED! ");
Console.ReadKey();
return false;
}
return true;
}
}
class staff
{
public string name
{
get;
set;
}
public string pwd
{
get;
set;
}
public staff(string n, string p)
{
name = n;
pwd = p;
}
}
class custom : staff
{
public string phone;
public custom(string n, string p, string ph) : base(n, p)
{
phone = ph;
}
public custom(string n, string p):base(n,p)
{
phone = "";
}
public List<DVD> borrowed=new List<DVD> ();
}
class MemberCollection
{
public static List<custom> mc=new List<custom> ();
public static bool addmem(string n, string p)
{
try
{
mc.Add(new custom(n, p));
}
catch (Exception)
{
Console.WriteLine("ADD staff FAILED!");
Console.ReadKey();
return false;
}
return true;
}
public static bool addmem(string n, string p, string ph)
{
try
{
mc.Add(new custom(n, p, ph));
}
catch (Exception)
{
Console.WriteLine("ADD custom FAILED!");
Console.ReadKey();
return false;
}
return true;
}
public static bool findphone(string name)
{
bool flag = false;
foreach (custom c in mc)
{
if (c.name == name)
{
flag = true;
Console.WriteLine("phone number is:" + c.phone);
Console.ReadKey();
}
}
if (flag)
return true;
else
return false;
}
public static void findborrowed(custom c)
{
foreach (DVD d in c.borrowed)
{
Console.WriteLine(d.name + " " + d.id);
}
}
public static custom judgestaff(string n, string p)
{
foreach (custom c in mc)
{
if (c.name == n && c.pwd.TrimEnd('\r') == p)
{
return c;
}
}
return null;
}
public static bool initstafffromTXT(BTnode btroot)
{
StreamReader sr;
try
{
sr = new StreamReader("staffInfo.txt", Encoding.ASCII);
string [] info = sr.ReadToEnd().Split('\r');
for(int i=0;i<info.Length-1;i++)
{
if(info[i].Split(' ').Length==3)
{
custom c = new custom(info[i].Trim('\n').Split(' ')[0], info[i].Trim('\n').Split(' ')[1]);
string[] borr = info[i].Trim('\n').Split(' ')[2].Split('-');
for (int j = 0; j<borr.Length - 1; j++)
{
c.borrowed.Add(BinaryTreeDVD.Search(int.Parse(borr[j]), btroot).data);
}
mc.Add(c);
}
else if(info[i].Split(' ').Length == 4)
{
custom c = new custom(info[i].Trim('\n').Split(' ')[0], info[i].Trim('\n').Split(' ')[1], info[i].Trim('\n').Split(' ')[2]);
string[] borr = info[i].Trim('\n').Split(' ')[3].Split('-');
for (int j = 0; j < borr.Length - 1; j++)
{
c.borrowed.Add(BinaryTreeDVD.Search(int.Parse(borr[j]), btroot).data);
}
mc.Add(c);
}
else
{
return false;
}
}
sr.Close();
return true;
}
catch (Exception)
{
return false; ;
}
}
public static bool savestafffromTXT()
{
try
{
StreamWriter sw = new StreamWriter("staffInfo.txt", false, Encoding.ASCII);
foreach (custom c in mc)
{
if (c.phone == "")
{
sw.Write(c.name + " " + c.pwd+" ");
}
else
{
sw.Write(c.name + " " + c.pwd + " " + c.phone+" ");
}
foreach (DVD d in c.borrowed)
{
sw.Write(+d.id+"-");
}
sw.Write("\r\n");
}
sw.Close();
return true;
}
catch (Exception)
{
return false;
}
}
class Program
{
static void Main(string[] args)
{
bool FLAG = false;
BTnode btroot = MovieCollection.initReadFromTXT();
if (!MemberCollection.initstafffromTXT(btroot))
{
Console.WriteLine(" init failed,press any key to exit");
Console.ReadKey();
return;
}
while (true)
{
Console.Clear();
showmenu.showmainmenu();
int s;
try
{
s = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("input invalidly,input again!");
continue;
}
switch (s)
{
case 1:
{
string name, pwd;
Console.WriteLine("please input username or input # to return:");
name = Console.ReadLine();
if (name == "#")
{
break;
}
Console.WriteLine("please input userpassword:");
pwd = Console.ReadLine();
custom c = MemberCollection.judgestaff(name, pwd);
if (c != null)
{
while (true)
{
Console.Clear();
showmenu.showsubmenu1();
int s1;
try
{
s1 = int.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Input invalidly,please input again!");
break;
}
bool flag = false; ;
switch (s1)
{
case 1:
{
Console.WriteLine("please inupt name and id of movie,use space to split!inpu # to return");
try
{
string str = Console.ReadLine();
if (str == "#")
break;
string[] input = str.Split(' ');
BinaryTreeDVD.Insert(new BTnode(new DVD(input[0], int.Parse(input[1]))), ref btroot);
Console.WriteLine("ADD SUCCESS!");
Console.ReadKey();
}
catch (Exception)
{
Console.WriteLine("ADD FAILED!");
Console.ReadKey();
Console.ReadKey();
}
}
break;
case 2:
{
Console.WriteLine("please inupt the DVD's id!");
string str = Console.ReadLine();
if (str == "#")
break;
try
{
if (BinaryTreeDVD.delete( btroot,int.Parse(str)))
{
Console.WriteLine("REMOVE SUCCESS!");
}
else
{
Console.WriteLine("REMOVE FAILED!");
}
Console.ReadKey();
}
catch (Exception)
{
Console.WriteLine("REMOVE FAILED!");
Console.ReadKey();
}
}
break;
case 3:
{
while (true)
{
string s11;
int pwd1,se=-1;
Console.Clear();
bool flag1 = false;
showmenu.showregistmenu();
try
{
se = int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("input invalidly");
Console.ReadKey();
break;
}
switch (se)
{
case 1:
{
Console.WriteLine("please input name and password,use space to split!");
try
{
s11 = Console.ReadLine();
pwd1 = int.Parse(s11.Split(' ')[1]);
if (pwd1 > 999 && pwd1 < 10000)
{
MemberCollection.addmem(s11.Split(' ')[0], s11.Split(' ')[1]);
}
else
{
throw new Exception();
}
}
catch (Exception)
{
Console.WriteLine("ADD FALIED!");
Console.ReadKey();
}
}
break;
case 2:
{
Console.WriteLine("please input name ,password and phonenumber,use space to split!");
try
{
s11 = Console.ReadLine();
pwd1 = int.Parse(s11.Split(' ')[1]);
if (pwd1 > 999 && pwd1 < 10000)
{
MemberCollection.addmem(s11.Split(' ')[0], s11.Split(' ')[1], s11.Split(' ')[2]);
}
else
{
throw new Exception();
}
}
catch (Exception)
{
Console.WriteLine("ADD FALIED!");
Console.ReadKey();
}
}
break;
case 0:
{
flag1 = true;
}
break;
}
if (flag1)
{
break;
}
}
}
break;
case 4:
{
Console.WriteLine("please input name");
if (!MemberCollection.findphone(Console.ReadLine()))
{
Console.WriteLine("NOT FOUND");
Console.ReadKey();
}
}
break;
case 0:
{
flag = true;
}
break;
}
if (flag)
break;
}
}
else
{
Console.WriteLine("username or password is uncorrect!");
Console.ReadKey();
}
}
break;
case 2:
{
Console.WriteLine("please input user name and password,use space to split!");
string input = Console.ReadLine();
custom c = null;
try
{
c = MemberCollection.judgestaff(input.Split(' ')[0], input.Split(' ')[1]);
}
catch(Exception )
{
Console.WriteLine("input invalidly");
Console.ReadKey();
break;
}
if (c != null)
{
while (true)
{
Console.Clear();
showmenu.showsubmenu2();
bool flag2 = false;
switch (int.Parse(Console.ReadLine()))
{
case 1:
{
BinaryTreeDVD.PreOrder(btroot, true);
Console.ReadKey();
}
break;
case 2:
{
Console.WriteLine("input the id of movie");
try
{
int id = int.Parse(Console.ReadLine());
BTnode bt = BinaryTreeDVD.Search(id, btroot);
if (bt != null)
{
if (bt.data.isborrowed)
{
Console.WriteLine("borrowed!");
Console.ReadKey();
}
else
{
bt.data.borrowrecord.Add(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second), new DateTime(1975, 12, 1));
bt.data.isborrowed = true;
c.borrowed.Add(new DVD(bt.data.name, bt.data.id));
Console.WriteLine("BORROW SUCEES!");
Console.ReadKey();
}
}
else
{
Console.WriteLine("NOT FOUND!");
Console.ReadKey();
}
}
catch(System.ArgumentException)
{
Console.ReadKey();
}
catch (Exception)
{
Console.WriteLine("NOT FOUND!");
Console.ReadKey();
}
}
break;
case 3:
{
Console.WriteLine("input the id of movie");
try
{
BTnode bt = BinaryTreeDVD.Search(int.Parse(Console.ReadLine()), btroot);
if (bt != null)
{
Dictionary<DateTime, DateTime> tmp =new Dictionary<DateTime, DateTime>( bt.data.borrowrecord);
bt.data.borrowrecord.Clear();
foreach(KeyValuePair<DateTime,DateTime> kvp in tmp)
{
if (kvp.Value.Year == 1975)
{
bt.data.borrowrecord.Add(kvp.Key, new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute,DateTime.Now.Second, DateTime.Now.Millisecond));
bt.data.isborrowed = false;
}
else
{
bt.data.borrowrecord.Add(kvp.Key,kvp.Value);
}
}
for (int i=0;i<c.borrowed.Count;i++)
{
if(c.borrowed[i].id==bt.data.id)
c.borrowed.RemoveAt(i);
}
Console.WriteLine("RETURN SUCCESS!");
Console.ReadKey();
}
else
{
Console.WriteLine("NOT FOUND!");
Console.ReadKey();
}
}
catch (Exception)
{
Console.WriteLine("NOT FOUND!");
Console.ReadKey();
}
}
break;
case 4:
{
foreach (DVD d in c.borrowed)
Console.WriteLine("Nmae:" + d.name + " Id:" + d.id);
Console.ReadKey();
}
break;
case 5:
{
List<DVD> l = new List<DVD>();
BinaryTreeDVD.Findtop10(btroot,ref l);
l.Reverse();
for (int i = 0; i < 10 && i < l.Count; i++)
{
Console.WriteLine("name:" + l[i].name + " id:" + l[i].id + " times:" + BinaryTreeDVD.borrowsum(l[i]));
}
Console.ReadKey();
}
break;
case 0:
{
flag2 = true;
}
break;
}
if (flag2)
{
break;
}
}
}
else
{
Console.WriteLine("username or password is uncorrect!");
Console.ReadKey();
}
};
break;
case 0:
{
FLAG = true;
};
break;
}
if (FLAG)
{
MemberCollection.savestafffromTXT();
MovieCollection.WriteToTXT(btroot);
return;
}
}
}
}
}
}