电话簿管理器练习:
首先为了代码规范,创建类文件。
Data.cs
using System;
/*
* 数据模型
*/
class Book
{
public string name;//书名
public double price;//售价
public Author author;//作者
public Book(string name,string authorName,int authorAge,int authorGender,double price)
{
this.name = name;
this.price = price;
this.author = new Author(authorName, authorAge, authorGender);
}
//返回一个对象的字符串表示形式,不需要手动调用,在使用一个对象的时候会自动的调用。
public override string ToString() {
return $"{{\n\t书名:{name}\n\t售价:{price}元\n\t作者信息:{author}\n}}";
}
}
enum Gender { Male, Female }
class Author
{
public string name;//作者姓名
public int age;//作者年龄
public Gender gender;//作者性别
public Author(string name,int age,int gender)
{
this.name = name;
this.age = age;
this.gender = (Gender)gender;
}
public override string ToString()
{
string genderString = gender == Gender.Male ? "男" : "女";
return $"{{\n\t\t姓名:{name}\n\t\t性别:{genderString}\n\t\t年龄:{age}\n\t}}";
}
}
PhoneBookManager.cs
using System;
/*
* 用来做数据管理的类
* 核心功能
*/
//对于这个类,我们没有必要去实例化一个他的对象,只希望这个类能够给我们提供一些方法就足够了
static class PhoneBookManager
{
//书籍容量
private static int capcity = 10;
//记录当前存储了多少本书
private static int count = 0;
//存储所有的书籍信息的数组
public static Book[] allBooks = new Book[capcity];
//使用///的方法注释,并写清楚方法用途、参数和返回值的说明
/// <summary>
/// 新增一本书
/// </summary>
/// <param name="book">要添加的书的信息</param>
/// <returns>添加的结果</returns>
public static bool AddBook(Book book)
{
//容量已满
if (count >= capcity) return false;
//存在count位,并将计数器+1
allBooks[count++] = book;
return true;
}
/// <summary>
/// 删除所有存储的书
/// </summary>
public static void DeletaAll()
{
//将是数组中的每个元素填充为null
for (int i = 0; i < count; i++)
{
allBooks[i] = null;
}
//将count置为0
count = 0;
}
public static void ShowAll()
{
//依次遍历数组中的每个元素
for (int i = 0; i < count; i++)
{
Console.WriteLine(allBooks[i]);
}
}
/// <summary>
/// 通过书名或作者名查找书籍
/// </summary>
/// <param name="name">用户输入的书名或作者名</param>
/// <returns>返回查找到的书籍结果集</returns>
public static Book[] FindByBookName(string name, FindingFlags flag)
{
//做一个临时的数组,用来存放查询的结果
Book[] temp = new Book[count];
//声明一个变量,用来标记实际查询到的数量
int resultCount = 0;
//遍历数组,进行查询
for (int i = 0; i < count; i++)
{
//通过不同标签返回不同结果
string findName = flag == FindingFlags.ByAuthorName ? allBooks[i].author.name : allBooks[i].name;
if(findName == name)
{
//把这本书放到temp数组中
temp[resultCount++] = allBooks[i];
}
}
//现在,所有满足条件的数都在temp数组中,但是temp数组后面可能是null
//实例化一个真正用来存储结果的数组
Book[] result = new Book[resultCount];
//将临时数组temp中的元素拷贝过来
for (int i = 0; i < resultCount; i++)
{
result[i] = temp[i];
}
return result;
}
}
//查询的标签
enum FindingFlags
{
ByBookName,ByAuthorName
}
Program.cs
using System;
class Program
{
public static void Main(string[] args)
{
string name = "西游记";
int price = 100;
string authorName = "吴承恩";
int authorAge = 19;
int authorGender = 0;
Book xiyouji = new Book(name, authorName, authorAge, authorGender, price);
PhoneBookManager.AddBook(xiyouji);
Book[] result = PhoneBookManager.FindByBookName("西游记", FindingFlags.ByBookName);
Book[] result1 = PhoneBookManager.FindByBookName("吴承恩", FindingFlags.ByAuthorName);
foreach(Book b in result)
{
Console.WriteLine(b);
}
PhoneBookManager.DeletaAll();
PhoneBookManager.ShowAll();
}
}