大一上學期閉組考核(借閱功能有邏輯錯誤)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 圖書館管理系統
{
class Program
{
public static void Main(string[] args) { //登陸讀入圖書資料 Book book = new Book(); List<Book> books = new List<Book>(); book.ReadBook(books); //登陸 Console.WriteLine("---------歡迎使用圖書管理系統---------"); Console.WriteLine("您的身份是?"); Console.WriteLine("如果您的身份是學生請輸入“1”"); Console.WriteLine("如果您的身份是教師請輸入“2”"); bool A = false; string identity = Convert.ToString(Console.ReadLine()); switch (identity) { case "1": //學生登陸 Console.WriteLine("您的身份是學生,請登入"); StudentWhetherUser(); Student s1 = new Student(); s1.StudentLogin(); break; case "2": //教師登陸 Console.WriteLine("您的身份是教師,請登入"); TeacherWhetherUser(); A = true; break; } if (A==true) { //進入系統讀取資料 List<Student> array = new List<Student>(); Start(array); bool a = true; do { Console.WriteLine("---------歡迎使用圖書管理系統---------"); Console.WriteLine("輸入1,新增學生資訊"); Console.WriteLine("輸入2,刪除學生資訊"); Console.WriteLine("輸入3,修改學生資訊"); Console.WriteLine("輸入4,檢視全部學生資訊"); Console.WriteLine("輸入5,新增圖書資訊"); Console.WriteLine("輸入6,檢視圖書資訊"); Console.WriteLine("輸入7,退出系統"); Console.WriteLine("請輸入您的選擇"); string sc = Convert.ToString(Console.ReadLine()); switch (sc) { case "1": //Console.WriteLine("新增學生資訊"); addStudent(array); Console.WriteLine(); break; case "2": // Console.WriteLine("刪除學生資訊"); deleteStudent(array); break; case "3": //Console.WriteLine("修改學生資訊"); updateStudent(array); break; case "4": // Console.WriteLine("檢視全部學生資訊"); findallStudent(array); break; case "5": //新增圖書資訊 book.AddBook(books); break; case "6": //檢視圖書資訊 Student student = new Student(); student.ScreachBook(books); break; case "7": //退出系統 Leave(array); Console.WriteLine("期待您的再次使用"); a = false; break; } } while (a); } //新增學生資訊 void addStudent(List<Student> array) { Student sc = new Student(); Console.WriteLine("請輸入學生姓名"); string name = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生年齡"); string age = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生學號"); string studentId = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生班級"); string classId = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生聯絡地址"); string address = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生聯絡電話"); string phoneNumber = Convert.ToString(Console.ReadLine()); Student s = new Student(); s.Name = name; s.Age = age; s.StudentId = studentId; s.ClassId = classId; s.Address = address; s.PhoneNumber = phoneNumber; array.Add(s); Console.WriteLine("新增學生資訊成功"); } //刪除學生資訊 void deleteStudent(List<Student> array) { bool S = false; Console.WriteLine("請輸入您要刪除的學生的學號"); string s = Convert.ToString(Console.ReadLine()); for (int i = 0; i < array.Count; i++) { Student student = array[i]; if (student.StudentId.Equals(s)) { array.Remove(array[i]); S = true; } } if (S==true) { Console.WriteLine("刪除學生資訊成功"); } else { Console.WriteLine("輸入學號有誤"); } } //修改學生資訊 void updateStudent(List<Student> array) { bool c = true; Student s = new Student(); Console.WriteLine("請輸入要修改的學生的學生學號"); String studentId = Convert.ToString(Console.ReadLine()); for (int i = 0; i < array.Count; i++) { Student student = array[i]; if (student.StudentId.Equals(studentId)) { break; } else { Console.WriteLine("輸入學號有誤"); c = false; } } if (c==true) { Console.WriteLine("請輸入學生的新姓名"); string name = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生的新年齡"); string age = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生的新班級"); string classId = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生的新聯絡地址"); string address = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入學生的新聯絡電話"); string phoneNumber = Convert.ToString(Console.ReadLine()); s.Name = name; s.Age = age; s.StudentId = studentId; s.ClassId = classId; s.Address = address; s.PhoneNumber = phoneNumber; for (int i = 0; i < array.Count; i++) { Student student = array[i]; if (student.StudentId.Equals(studentId)) { array[i] = s; break; } } } } //檢視所有學生資訊 void findallStudent(List<Student> array) { Console.WriteLine("姓名\t年齡\t\t學號\t\t\t\t班級\t\t聯絡地址\t\t\t聯絡電話"); for (int i = 0; i < array.Count; i++) { Student s = array[i]; Console.WriteLine(s.Name +"\t"+ s.Age + "歲\t\t" + s.StudentId + "\t\t\t" + s.ClassId + "\t\t" + s.Address + "\t\t\t\t" + s.PhoneNumber+"\t"); } } //退出系統,儲存資料 void Leave(List<Student> array) { using (StreamWriter stream = new StreamWriter(@"D:\C#\圖書館管理系統\圖書館管理系統\StudentInfoemation.txt",false, Encoding.UTF8)) { for (int i = 0; i < array.Count-1; i++) { Student s = array[i]; stream.WriteLine(s.Name); stream.WriteLine(s.Age); stream.WriteLine(s.StudentId); stream.WriteLine(s.ClassId); stream.WriteLine(s.Address); stream.WriteLine(s.PhoneNumber); stream.WriteLine(); } for (int i = array.Count - 1; i < array.Count; i++) { Student s = array[i]; stream.WriteLine(s.Name); stream.WriteLine(s.Age); stream.WriteLine(s.StudentId); stream.WriteLine(s.ClassId); stream.WriteLine(s.Address); stream.WriteLine(s.PhoneNumber); } } } //讀入學生資訊 void Start(List<Student> array) { using (StreamReader stream = new StreamReader(@"D:\C#\圖書館管理系統\圖書館管理系統\StudentInfoemation.txt")) { do { Student s = new Student(); s.Name = stream.ReadLine(); s.Age = stream.ReadLine(); s.StudentId = stream.ReadLine(); s.ClassId = stream.ReadLine(); s.Address = stream.ReadLine(); s.PhoneNumber = stream.ReadLine(); array.Add(s); } while (stream.ReadLine() != null); } } } //判斷是否擁有賬號 public static void StudentWhetherUser() { Console.WriteLine("未擁有賬號?註冊請輸入“1”,若已擁有賬號輸入任意鍵登陸"); string a = Convert.ToString(Console.ReadLine()); if (a == "1") { Student s1 = new Student(); s1.StudentRegister(); } } public static void TeacherWhetherUser() { Console.WriteLine("未擁有賬號?教師註冊需輸入身份驗證碼,若已有賬號請輸入“1”");//1574212 if (Convert.ToString(Console.ReadLine())=="1") { Manager m1 = new Manager(); m1.ManagerLogin(); } else { string A = "1574212"; Console.WriteLine("請輸入:"); string a = Convert.ToString(Console.ReadLine()); if (a == A) { Manager m1 = new Manager(); m1.ManagerRegister(); } else { return; } } } }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace 圖書館管理系統
{ [Serializable]
class Spersonal
{
private string suser;
private string spassword;
public string Suser { get => suser; set => suser = value; } public string Spassword { get => spassword; set => spassword = value; } } class Student { private string name; private string age; private string studentId; private string classId; private string address; private string phoneNumber; public Student() { } public Student(string name, string age, string studentId, string classId, string address, string PhoneNumber) { this.name = name; this.age = age; this.studentId = studentId; this.classId = classId; this.address = address; this.phoneNumber = PhoneNumber; } public string Name { get; set; } public string Age { get; set; } public string StudentId { get; set; } public string ClassId { get; set; } public string Address { get; set; } public string PhoneNumber { get; set; } //學生註冊登入 public void StudentRegister() { Console.WriteLine("請輸入使用者名稱"); string SUser1 = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入密碼"); string Spassworld = Convert.ToString(Console.ReadLine()); using (StreamWriter writer = new StreamWriter(@"D:\C#\圖書館管理系統\圖書館管理系統\Spersonal.txt", true)) { writer.WriteLine(SUser1); writer.WriteLine(Spassworld); } Console.WriteLine("註冊成功"); } public void StudentLogin() { Console.WriteLine("請輸入使用者名稱"); string a1 = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入密碼"); string a2 = Convert.ToString(Console.ReadLine()); using (StreamReader reader = new StreamReader(@"D:\C#\圖書館管理系統\圖書館管理系統\Spersonal.txt")) { bool A = false; bool B = true; do { if (a1 == reader.ReadLine()) { A = true; } } while (reader.ReadLine() != null); do { if (a2 == reader.ReadLine()) { B = true; } } while (reader.ReadLine() != null); if (A == true && B == true) { Console.WriteLine("登陸成功"); Operate(); } else { Console.WriteLine("使用者名稱或者密碼錯誤"); Console.ReadKey(); return; } } } //學生登陸成功後可進行操作 public void Operate() { List<Book> books = new List<Book>(); Book book = new Book(); book.ReadBook(books); bool l = true; do { Console.WriteLine("========歡迎使用圖書管理系統學生端========"); Console.WriteLine("輸入“1”,查詢書籍"); Console.WriteLine("輸入“2”,借閱書籍"); Console.WriteLine("輸入“3”,退出系統"); string f = Convert.ToString(Console.ReadLine()); switch (f) { case "1": ScreachBook(books); break; case "2": BorrowingBooks(books); break; case "3": Console.WriteLine("感謝使用"); l = false; break; } } while (l); } //查詢書籍 public void ScreachBook(List<Book> books) { if (books.Count == 0) { Console.WriteLine("無資訊,請先新增資訊再查詢"); return; } Console.WriteLine("書名\t\t\t作者\t\t是否被借閱\t借閱時間\t預計歸還時間"); for (int i = 0; i < books.Count; i++) { Book B= books[i]; Console.WriteLine(B.Name + "\t\t" + B.Author + "\t\t" + B.Condition + "\t\t" + B.LendingTime + "\t\t" + B.Returntime ); } } //借閱書籍 public void BorrowingBooks(List<Book>books) { Console.WriteLine("請輸入您要借閱的書籍"); string s = Convert.ToString(Console.ReadLine()); for (int i = 0; i < books.Count; i++) { Book B = books[i]; if (B.Name.Equals(s)) { B.Condition = "已借閱"; Console.WriteLine("請輸入借書時間"); B.LendingTime = Convert.ToString(Console.ReadLine()); Console.WriteLine("您的預計歸還時間為:"); B.Returntime = Convert.ToString(Console.ReadLine()); using (StreamWriter writer = new StreamWriter(@"D:\C#\圖書館管理系統\圖書館管理系統\Book.txt", false, Encoding.UTF8)) { for (int j = 0; j< books.Count; j++) { writer.WriteLine(B.Name); writer.WriteLine(B.Author); writer.WriteLine(B.Condition); writer.WriteLine(B.LendingTime); writer.WriteLine(B.Returntime); } } } } } }
}using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace 圖書館管理系統
{
[Serializable]
class Mpersonal
{
private string muser;
private string mpassword;
public string Muser { get => muser; set => muser = value; } public string Mpassword { get => mpassword; set => mpassword = value; } } class Manager { //管理員註冊登入 public void ManagerRegister() { Console.WriteLine("請輸入使用者名稱"); string MUser1 = Convert.ToString(Console.ReadLine()); Console.WriteLine("請輸入密碼"); string Mpassworld = Convert.ToString(Console.ReadLine()); using (StreamWriter writer=new StreamWriter(@"D:\C#\圖書館管理系統\圖書館管理系統\Tpersonal.txt",true)) { writer.WriteLine(MUser1); writer.WriteLine(Mpassworld); } Console.WriteLine("註冊成功"); } //管理員登陸驗證 public void ManagerLogin() { Console.WriteLine("請輸入使用者名稱"); string a1 = Convert.ToString(Console.ReadLine()) ; Console.WriteLine("請輸入密碼"); string a2 = Convert.ToString(Console.ReadLine()); using (StreamReader reader = new StreamReader(@"D:\C#\圖書館管理系統\圖書館管理系統\Tpersonal.txt")) { bool A = false; bool B = true; do { if (a1 == reader.ReadLine()) { A = true; } } while (reader.ReadLine()!=null); do { if (a2 == reader.ReadLine()) { B= true; } } while (reader.ReadLine()!=null); if (A==true&&B==true) { Console.WriteLine("登陸成功"); } else { Console.WriteLine("使用者名稱或者密碼錯誤"); ManagerLogin(); } } } }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace 圖書館管理系統
{
[Serializable]
class Mpersonal
{
private string muser;
private string mpassword;
public string Muser { get => muser; set => muser = value; }
public string Mpassword { get => mpassword; set => mpassword = value; }
}
class Manager
{ //管理員註冊登入
public void ManagerRegister()
{
Console.WriteLine("請輸入使用者名稱");
string MUser1 = Convert.ToString(Console.ReadLine());
Console.WriteLine("請輸入密碼");
string Mpassworld = Convert.ToString(Console.ReadLine());
using (StreamWriter writer=new StreamWriter(@"D:\C#\圖書館管理系統\圖書館管理系統\Tpersonal.txt",true))
{
writer.WriteLine(MUser1);
writer.WriteLine(Mpassworld);
}
Console.WriteLine("註冊成功");
}
//管理員登陸驗證
public void ManagerLogin()
{
Console.WriteLine("請輸入使用者名稱");
string a1 = Convert.ToString(Console.ReadLine()) ;
Console.WriteLine("請輸入密碼");
string a2 = Convert.ToString(Console.ReadLine());
using (StreamReader reader = new StreamReader(@"D:\C#\圖書館管理系統\圖書館管理系統\Tpersonal.txt"))
{
bool A = false;
bool B = true;
do
{
if (a1 == reader.ReadLine())
{
A = true;
}
} while (reader.ReadLine()!=null);
do
{
if (a2 == reader.ReadLine())
{
B= true;
}
} while (reader.ReadLine()!=null);
if (A==true&&B==true)
{
Console.WriteLine("登陸成功");
}
else
{
Console.WriteLine("使用者名稱或者密碼錯誤");
ManagerLogin();
}
}
}
}
}