标签:null 年龄 console hone ring .so index name .com
class Contact
{
public string name {
get;
set;
}
public string phoneNum {
get;
set;
}
public int age {
get;
set;
}
public string danger {
get;
set;
}
public string address {
get;
set;
}
public Contact (string name, string phoneNum, int age, string danger, string address)
{
this.name = name;
this.age = age;
this.address = address;
this.danger = danger;
this.phoneNum = phoneNum;
}
}
class ContactManager
{
private static ContactManager instance;
private ContactManager ()
{
contacts = new Contact[4];
}
public static ContactManager Instance {
get {
if (instance == null) {
instance = new ContactManager ();
}
return instance;
}
}
public Contact[] contacts;
public void addContactArray (params Contact[]per)
{
for (int i = 0; i < per.Length; i++) {
contacts [i] = per [i];
}
}
public void printContactOne (Contact per)
{
Console.WriteLine ("姓名:{0}\t年龄:{1}\t地址:{2}\t性别:{3}\t手机:{4}\t", per.name, per.age, per.address, per.danger, per.phoneNum);
}
public void printContactArray (Contact[]per)
{
for (int i = 0; i < per.Length; i++) {
printContactOne (per [i]);
}
}
public Contact[]sortContactByAge (Contact[]per)
{
for (int i = 0; i < per.Length - 1; i++) {
for (int j = 0; j < per.Length - 1 - i; j++) {
if (per [j].age > per [j + 1].age) {
Contact team = per [j];
per [j] = per [j + 1];
per [j + 1] = team;
}
}
}
return per;
}
public Contact[]sortContactByName (Contact[]per)
{
for (int i = 0; i < per.Length - 1; i++) {
for (int j = 0; j < per.Length - 1 - i; j++) {
if (per [j].name.CompareTo (per [j + 1].name) == 1) {
Contact team = per [j];
per [j] = per [j + 1];
per [j + 1] = team;
}
}
}
return per;
}
public Contact[]searchContactByDanger (string danger)
{
int index = 0;
Contact[] p = new Contact[2];
for (int i = 0; i < contacts.Length; i++) {
if (contacts [i].danger == danger) {
p [index] = contacts [i];
index++;
}
}
return p;
}
public Contact[]searchContactByDangerAndAge (string danger, int age)
{
int index = 0;
Contact[] p = new Contact[1];
for (int i = 0; i < contacts.Length; i++) {
if (contacts [i].age > age && contacts [i].danger == danger) {
p [index] = contacts [i];
index++;
}
}
return p;
}
}
class MainClass
{
public static void Main (string[] args)
{
Contact per1 = new Contact ("王明", "13987657483", 21, "男", "河北");
Contact per2 = new Contact ("杨帅", "13987657483", 55, "女", "河北");
Contact per3 = new Contact ("李强", "13987657483", 23, "男", "河北");
Contact per4 = new Contact ("张伟", "13987657483", 31, "女", "河北");
ContactManager.Instance.addContactArray (per1, per2, per3, per4);
//输出
ContactManager.Instance.printContactArray (ContactManager.Instance.contacts);
Console.WriteLine ();
//排序输出 按照年龄
ContactManager.Instance.printContactArray (ContactManager.Instance.sortContactByAge (ContactManager.Instance.contacts));
Console.WriteLine ();
//排序输出 按照姓名
ContactManager.Instance.printContactArray (ContactManager.Instance.sortContactByName (ContactManager.Instance.contacts));
Console.WriteLine ();
//查找 男性 输出
ContactManager.Instance.printContactArray (ContactManager.Instance.searchContactByDanger ("男"));
Console.WriteLine ();
//查找 女性且大于50岁 输出
ContactManager.Instance.printContactArray (ContactManager.Instance.searchContactByDangerAndAge ("女", 50));
}
标签:null 年龄 console hone ring .so index name .com
原文地址:http://www.cnblogs.com/XIaooQ/p/6067630.html