C#基础第七天-作业答案-利用面向对象的思想去实现名片-动态添加
class Card
{
private string name; public string Name
{
get { return name; }
set { name = value; }
}
private string age; public string Age
{
get { return age; }
set { age = value; }
}
private string sex; public string Sex
{
get { return sex; }
set { sex = value; }
}
private string interest; public string Interest
{
get { return interest; }
set { interest = value; }
}
private string telephone; public string Telephone
{
get { return telephone; }
set { telephone = value; }
}
DataTable table = new DataTable();
public Card()
{
//DataTable table = new DataTable();
table.Columns.Add("姓名");
table.Columns.Add("年龄");
table.Columns.Add("性别");
table.Columns.Add("爱好");
table.Columns.Add("联系方式");
table.PrimaryKey = new DataColumn[] { table.Columns["姓名"] };
DataRow row = table.NewRow();
row["姓名"] = "小明";
row["年龄"] = "";
row["性别"] = "男";
row["爱好"] = "足球";
row["联系方式"] = "";
table.Rows.Add(row);
}
// public Card() { }
public void Add(string name)
{
if (!(table.Rows.Contains(name)))
{
Console.WriteLine("请输入年龄:");
string age = Console.ReadLine();
Console.WriteLine("请输入性别:");
string sex = Console.ReadLine();
Console.WriteLine("请输入爱好:");
string interest = Console.ReadLine();
Console.WriteLine("请输入联系方式:");
string telephone = Console.ReadLine();
DataRow row = table.NewRow();
row["姓名"] = name;
row["年龄"] = age;
row["性别"] = sex;
row["爱好"] = interest;
row["联系方式"] = telephone;
table.Rows.Add(row);
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("人名重复!");
} }
public void Select(string name)
{
if (table.Rows.Contains(name))
{
for (int i = ; i < table.Columns.Count; i++)
{
Console.Write(table.Columns[i] + "\t");
}
Console.WriteLine();
Console.WriteLine(table.Rows.Find(name)["姓名"].ToString() + "\t" + table.Rows.Find(name)["年龄"].ToString() + "\t" + table.Rows.Find(name)["性别"].ToString() + "\t" + table.Rows.Find(name)["爱好"].ToString() + "\t" + table.Rows.Find(name)["联系方式"].ToString());
}
else
{
Console.WriteLine("查无此人!");
} }
public void SelectAll()
{
Console.WriteLine("你想要查找的信息如下:");
for (int i = ; i < table.Columns.Count; i++)
{
Console.Write(table.Columns[i] + "\t");
}
Console.WriteLine();
for (int i = ; i < table.Rows.Count; i++)
{
Console.WriteLine(table.Rows[i]["姓名"] + "\t" + table.Rows[i]["年龄"] + "\t" + table.Rows[i]["性别"] + "\t" + table.Rows[i]["爱好"] + "\t" + table.Rows[i]["联系方式"]);
}
Console.WriteLine();
}
public void Delete(string name)
{
if (table.Rows.Contains(name))
{
table.Rows.Remove(table.Rows.Find(name));
Console.WriteLine("删除成功!");
}
else
{
Console.WriteLine("查无此人!");
} }
public void Update(string name)
{
if (table.Rows.Contains(name))
{
Console.WriteLine("请输入年龄:");
string age = Console.ReadLine();
Console.WriteLine("请输入性别:");
string sex = Console.ReadLine();
Console.WriteLine("请输入爱好:");
string interest = Console.ReadLine();
Console.WriteLine("请输入联系方式:");
string telephone = Console.ReadLine();
int index = table.Rows.IndexOf(table.Rows.Find(name));
table.Rows[index]["年龄"] = age;
table.Rows[index]["性别"] = sex;
table.Rows[index]["爱好"] = interest;
table.Rows[index]["联系方式"] = telephone;
for (int i = ; i < table.Columns.Count; i++)
{
Console.Write(table.Columns[i] + "\t");
}
Console.WriteLine();
Console.WriteLine("修改成功,信息如下");
Console.WriteLine(table.Rows[index]["姓名"].ToString() + "\t" + table.Rows[index]["年龄"].ToString() + "\t" + table.Rows[index]["性别"].ToString() + "\t" + table.Rows[index]["爱好"].ToString() + "\t" + table.Rows[index]["联系方式"].ToString());
Console.WriteLine("修改成功!"); }
else
{
Console.WriteLine("查无此人!");
}
}
public void AddMore() {
while (true) {
Console.WriteLine("是否要批量增加Y/N");
string choice = Console.ReadLine();
if ("Y".Equals(choice))
{
Console.WriteLine("请输入你要增加的人的姓名");
string name = Console.ReadLine();
if (!(table.Rows.Contains(name)))
{
Console.WriteLine("请输入年龄:");
string age = Console.ReadLine();
Console.WriteLine("请输入性别:");
string sex = Console.ReadLine();
Console.WriteLine("请输入爱好:");
string interest = Console.ReadLine();
Console.WriteLine("请输入联系方式:");
string telephone = Console.ReadLine();
DataRow row = table.NewRow();
row["姓名"] = name;
row["年龄"] = age;
row["性别"] = sex;
row["爱好"] = interest;
row["联系方式"] = telephone;
table.Rows.Add(row);
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("人名重复!");
}
}
else if ("N".Equals(choice))
{
Console.WriteLine("已经添加成功了!");
break;
}
}
}
} //测试类。
class Program
{
static void Main(string[] args)
{
// DataTable table = new DataTable();
Card card = new Card();
while (true)
{
Console.WriteLine("请输入你要的操作1.增加2.查询3.查询全部4.删除5.修改6.增加多人");
int a = int.Parse(Console.ReadLine());
if (a == )
{
Console.WriteLine("请输入姓名:");
string name = Console.ReadLine();
card.Add(name);
}
else if (a == )
{
Console.WriteLine("请输入你要查询的人的姓名");
string name = Console.ReadLine();
card.Select(name);
}
else if (a == )
{
card.SelectAll();
}
else if (a == )
{
Console.WriteLine("请输入你要删除的人的姓名");
string name = Console.ReadLine();
card.Delete(name); }
else if (a == )
{
Console.WriteLine("请输入你要修改的人的姓名");
string name = Console.ReadLine();
card.Update(name); }
else if (a == )
{
card.AddMore(); }
else if (a == )
{
break;
}
else
{
Console.WriteLine("您的输入有误,请重新输入!");
}
}
}
}
以上做法不一定就是最好的方法,只是起到练习技术的作用。
本系列教程:
C#基础总结之八面向对象知识点总结-继承与多态-接口-http://www.cnblogs.com/spring_wang/p/6113531.html
C#基础总结之七面向对象知识点总结1http://www.cnblogs.com/spring_wang/p/6113526.html
C#基础总结之六 DataTable (临时表/数据源) 和Datatable 名片练习http://www.cnblogs.com/spring_wang/p/6113520.html
C#基础总结之五Dictionary<string, string[]>和while循环http://www.cnblogs.com/spring_wang/p/6113514.html
C#基础总结之四List-Hashtable-冒泡排序http://www.cnblogs.com/spring_wang/p/6113504.html
C#基础总结之三循环控制-for-数组-乘法表-arraylisthttp://www.cnblogs.com/spring_wang/p/6113496.html
C#基础总结之二循环控制-运算符http://www.cnblogs.com/spring_wang/p/6113484.html
C#基础总结之一变量常量-if嵌套语句-witch结构-类型转换http://www.cnblogs.com/spring_wang/p/6113476.html
C#基础课程之六(临时表)DataTable使用方法http://www.cnblogs.com/spring_wang/p/6113454.html
C#基础课程之五集合(HashTable,Dictionary)http://www.cnblogs.com/spring_wang/p/6113404.html
C#基础课程之四集合(ArrayList、List<泛型>)http://www.cnblogs.com/spring_wang/p/6113396.html
C#基础课程之三循环语句http://www.cnblogs.com/spring_wang/p/6113383.html
C#基础课程之二变量常量及流程控制http://www.cnblogs.com/spring_wang/p/6113372.html
C#基础课程之一注释和控制台、一些常识http://www.cnblogs.com/spring_wang/p/6113361.html
C#基础第九天-作业答案-储蓄账户(SavingAccount)和信用账户(CreditAccount) http://www.cnblogs.com/spring_wang/p/6113291.html
C#基础第九天-作业-储蓄账户(SavingAccount)和信用账户(CreditAccount) http://www.cnblogs.com/spring_wang/p/6113285.html
C#基础第八天-作业答案-设计类-面向对象方式实现两个帐户之间转账http://www.cnblogs.com/spring_wang/p/6113274.html
C#基础第八天-作业-设计类-面向对象方式实现两个帐户之间转账http://www.cnblogs.com/spring_wang/p/6113258.html
C#基础第七天-作业答案-利用面向对象的思想去实现名片-动态添加http://www.cnblogs.com/spring_wang/p/6113232.html
C#基础第七天-作业-利用面向对象的思想去实现名片-动态添加http://www.cnblogs.com/spring_wang/p/6113224.html
C#基础第六天-作业-利用面向对象的思想去实现名片http://www.cnblogs.com/spring_wang/p/6113028.html
C#基础第六天-作业答案-利用面向对象的思想去实现名片http://www.cnblogs.com/spring_wang/p/6113033.html
C#基础第五天-作业答案-用DataTable制作名片集http://www.cnblogs.com/spring_wang/p/6113022.html
C#基础第五天-作业-用DataTable制作名片集http://www.cnblogs.com/spring_wang/p/6113013.html
C#基础第四天-作业答案-Hashtable-list<KeyValuePair>泛型实现名片http://www.cnblogs.com/spring_wang/p/6113005.html
C#基础第四天-作业-Hashtable-list<KeyValuePair>泛型实现名片http://www.cnblogs.com/spring_wang/p/6113000.html
C#基础第三天-作业答案-集合-冒泡排序-模拟名片http://www.cnblogs.com/spring_wang/p/6112888.html
C#基础第三天-作业-集合-冒泡排序-模拟名片http://www.cnblogs.com/spring_wang/p/6112885.html
C#基础第二天-作业答案-九九乘法表-打印星星http://www.cnblogs.com/spring_wang/p/6112881.html
C#基础第二天-作业-九九乘法表-打印星星http://www.cnblogs.com/spring_wang/p/6112875.html
C#基础第一天-作业答案http://www.cnblogs.com/spring_wang/p/6112872.html
C#基础第一天-作业http://www.cnblogs.com/spring_wang/p/6112867.html
C#-string.Format对C#字符串格式化http://www.cnblogs.com/spring_wang/p/6077098.html
C#基础第七天-作业答案-利用面向对象的思想去实现名片-动态添加的更多相关文章
- C#基础第七天-作业-利用面向对象的思想去实现名片-动态添加
1.利用面向对象的思想去实现: (增加,修改,删除,查询,查询全部)需求:根据人名去(删除/查询).指定列:姓名,年龄,性别,爱好,电话. 多条添加 , 动态添加 名片 本系列教程: C#基础总结之八 ...
- C#基础第六天-作业答案-利用面向对象的思想去实现名片
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#基础第六天-作业-利用面向对象的思想去实现名片
1.利用面向对象的思想去实现: (增加,修改,删除,查询,查询全部)需求:根据人名去(删除/查询).指定列:姓名,年龄,性别,爱好,电话. 本系列教程: C#基础总结之八面向对象知识点总结-继承与多态 ...
- C#基础第五天-作业答案-用DataTable制作名片集
.DataTable 实现 DataTable PersonCard = new DataTable(); //创建一个DataTable DataTable PersonCardCopy = new ...
- C#基础第四天-作业答案-Hashtable-list<KeyValuePair>泛型实现名片
.Hashtable 实现 Hashtable table = new Hashtable(); while (true) { Console.WriteLine("------------ ...
- C#基础第三天-作业答案-集合-冒泡排序-模拟名片
.冒泡排序 Console.WriteLine("对集合里的数进行排序,请输入第一个数:"); int a = int.Parse(Console.ReadLine()); Con ...
- 【Java并发基础】利用面向对象的思想写好并发程序
前言 下面简单总结学习Java并发的笔记,关于如何利用面向对象思想写好并发程序的建议.面向对象的思想和并发编程属于两个领域,但是在Java中这两个领域却可以融合到一起.在Java语言中,面向对象编程的 ...
- C#基础第九天-作业答案-储蓄账户(SavingAccount)和信用账户(CreditAccount)
class Bank { //Dictionary<long,Account> dictionary=new Dictionary<long,Account>(); DataT ...
- C#基础第八天-作业答案-设计类-面向对象方式实现两个帐户之间转账
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
随机推荐
- JAVA GC优化入门
为什么需要优化GC? JAVA的GC是面试必考的题目,可是在实际项目中什么时候使用GC哪?或者应该什么时候优化GC哪?有句名言:“GC优化永远是最后一项任务”. 在使用GC之前,应该考虑一下进行GC的 ...
- 使用QML LocalStorage来存储我们的数据
在先前的样例中.我们能够"使用SQLite offline storage API来存储应用的设置".我们也在例程"怎样在QML应用中动态改动ListModel中的数据并 ...
- Linux进程共享通信 -- mmap实现
https://blog.csdn.net/y396397735/article/details/50651633 使用mmap内存映射实现一端写,另一端读的进程间通信 写端代码write.c /*w ...
- visual studio运行时库MT、MTd、MD、MDd的研究
在开发window程序是经常会遇到编译好好的程序拿到另一台机器上面无法运行的情况,这一般是由于另一台机器上面没有安装响应的运行时库导致的,那么这个与编译选项MT.MTd.MD.MDd有什么关系呢?这是 ...
- [JavaScript模块演化简史]摘要
来源于:https://zhuanlan.zhihu.com/p/26231889 # JavaScript 模块化 早期的JavaScript并没有模块化解决方案.随着单页应用与富客户端的流行,出现 ...
- jquery.uploadify 在firefox会出现httperror
原来是因为我的上传处理页面的page 继承了一个基类影响到的 然后这个基类 好像是因为在别的项目里面的原因 希望对也遇到这样的问题的人有帮助咯
- HTTP 接口响应数据解析
转自:https://blog.csdn.net/hubanbei2010/article/details/79878567 作为产品线的支撑角色QA/CI/CD等,http api解析是互联网公司中 ...
- spring hiberante 集成出现异常 java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor
出现如题的异常是由于hibernate和spring集成时的的版本不一致所导致. 如下面,所示,如果你用的hibneate 4.0及以上版本,那么将会报错,因为这里用的事务管理是hibernate 3 ...
- iOS 如何写出更加严谨的应用
本文旨在介绍一些能够帮助大家避开一些开发误区的经验. 一: 在开发中,经常能够遇到共用同一个界面的情况,一般情况下,我们会根据传入的model去做数据处理和保存. 当然如果不存在复用的情况下,根本不需 ...
- Lua初学
Lua很火啊,而且跟C,c++可以无缝结合,表示很给力,算是我的第三门语言吧,哈哈! 在官网上下载了源码了,和windows版的,表示编译器也很给力,直接可以用SciTE就可以写代码了. a = 1; ...