面向对象实验报告二投票选举管理程序的实现Word格式.docx
《面向对象实验报告二投票选举管理程序的实现Word格式.docx》由会员分享,可在线阅读,更多相关《面向对象实验报告二投票选举管理程序的实现Word格式.docx(11页珍藏版)》请在冰豆网上搜索。
每张选票可以选举多个候选人。
当每张选票进行选举生效时,需要对选票中的信息进行合法性验证,判断是否存在对应的候选人。
根据系统设置,对每一个候选人产生唯一的数字编号ID作为标识,为了避免候选人的姓名相同。
经过分析,得出,选票应该设计成一个实体类,该类用于保存选票过程中,有哪些候选人被选入其中。
候选人同样需要设计成一个实体类,包括候选人的ID,姓名和其他相应的信息。
选票的选举过程和候选人票数的统计则需要在选票管理类中进行完成。
1.3流程图
否
是
1.3实现方案
本程序采用c#的编程语言,面向对象的设计思想进行功能实现。
开发环境:
win7
开发工具;
visualstudio2010
编程语言:
c#
2静态模型设计
2.1类设计
(1)类“票数”Ticket
属性:
privateint[]id;
//投递的人
(2)类“候选人”Candidate
属性:
privateintage;
//年龄
privatestringphone;
//电话
privatestringposition;
//职位
privateintcount=0;
//票数
privatestringname;
//姓名
privateintid;
//编号,唯一标识一个候选人
(3)类“投票控制类”TicketControl
privateList<
Candidate>
canList;
//统计候选人
Ticket>
ticList;
//统计票数
方法:
publicTicketControl()//实例化初始数据
publicvoidvote()//进行投票结果的统计
publicvoidshowResult()//进行统计结果的显示
2.2类图设计
3动态模型设计
4程序截图
5程序代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacevoteDemo
{
classTicket
{
privateint[]id;
publicint[]Id
get{returnid;
}
set{id=value;
publicTicket(int[]id){
this.id=id;
publicTicket(){
}usingSystem;
classCandidate:
IComparable
privatestringname;
publicstringName
get{returnname;
set{name=value;
privateintid;
//编号
publicintId
privateintage;
publicintCount
get{returncount;
set{count=value;
publicvoidAddTicket(){
this.count++;
publicCandidate(intid,stringname,intage,stringphone,stringposition){
this.name=name;
this.age=age;
this.phone=phone;
this.position=position;
publicCandidate(intid){
publicoverridestringToString()
stringstr="
候选人姓名:
"
+name+"
\n年龄:
+age+"
\n电话:
+phone
+"
\n职位是:
+position+"
\n最后总票数为:
+count;
returnstr;
publicoverrideboolEquals(objectobj)
Candidatec=(Candidate)obj;
if(c.id==this.id){
returntrue;
returnfalse;
publicoverrideintGetHashCode()
returnbase.GetHashCode();
publicintCompareTo(objectobj){
Candidatec=(Candidate)obj;
if(c.count>
this.count){
return1;
if(c.count==this.count)
return0;
if(c.count<
this.count)
return-1;
usingSystem.Collections;
classTicketControl
privateintindex=1;
publicTicketControl(){
canList=newList<
();
//初始化6个候选人
Candidatec=newCandidate(index,"
张三1"
12,"
123456"
"
职位"
+index);
canList.Add(c);
index++;
c=newCandidate(index,"
张三2"
+index);
index++;
张三3"
张三4"
张三5"
张三6"
ticList=newList<
Tickett=newTicket(newint[]{1,2,3,4});
ticList.Add(t);
t=newTicket(newint[]{1,2,3,4,5});
t=newTicket(newint[]{1,2});
t=newTicket(newint[]{1,2,6,7});
t=newTicket(newint[]{2,3,4});
t=newTicket(newint[]{2,4,8});
t=newTicket(newint[]{6,5,4});
t=newTicket(newint[]{5,6});
t=newTicket(newint[]{2,3});
t=newTicket(newint[]{4,7});
//进行结果的显示
publicvoidshowResult(){
//对数据进行排序
canList.Sort();
Candidatec1=canList[0];
Console.WriteLine();
Console.WriteLine("
当选人是:
+canList[0].Name+"
票数为:
+canList[0].Count);
foreach(CandidatecincanList)
Console.WriteLine(c.ToString());
//进行投票选举过程
publicvoidvote(){
foreach(TickettinticList){
int[]tArr=t.Id;
foreach(intidintArr){
foreach(CandidatecincanList){
if(c.Id==id){
c.AddTicket();
classProgram
staticvoidMain(string[]args)
TicketControlt=newTicketControl();
t.vote();
t.showResult();
Console.ReadLine();
}