面向对象实验报告二投票选举管理程序的实现.docx
《面向对象实验报告二投票选举管理程序的实现.docx》由会员分享,可在线阅读,更多相关《面向对象实验报告二投票选举管理程序的实现.docx(11页珍藏版)》请在冰豆网上搜索。
![面向对象实验报告二投票选举管理程序的实现.docx](https://file1.bdocx.com/fileroot1/2023-1/26/39cc6d10-1b72-4d96-a52a-2be17b297c05/39cc6d10-1b72-4d96-a52a-2be17b297c051.gif)
面向对象实验报告二投票选举管理程序的实现
投票选举管理程序的实现
姓名:
尹鹏飞
学号:
S2*******1
1题目描述
1.1系统的功能需求
投票选举过程
–预先指定若干名侯选人(最多12人)
–预先指定当选标准(给定百分数)
–提供投票记录功能(每张票可以选0-12人)
–支持选举结果的统计输出(当选人和票数)
设计一个监控模块
–作为选举系统的一部分
–仅负责数据统计和结果计算
分析:
对每个候选人分配一个id,编号,进行投票的选举.
1.2功能描述
投票选举系统中有一些候选人和选票。
每张选票可以选举多个候选人。
当每张选票进行选举生效时,需要对选票中的信息进行合法性验证,判断是否存在对应的候选人。
根据系统设置,对每一个候选人产生唯一的数字编号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
属性:
privateListcanList;//统计候选人
privateListticList;//统计票数
方法:
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;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacevoteDemo
{
classCandidate:
IComparable
{
privatestringname;//姓名
publicstringName
{
get{returnname;}
set{name=value;}
}
privateintid;//编号
publicintId
{
get{returnid;}
set{id=value;}
}
privateintage;//年龄
privatestringphone;//电话
privatestringposition;//职位
privateintcount=0;//票数
publicintCount
{
get{returncount;}
set{count=value;}
}
publicvoidAddTicket(){
this.count++;
}
publicCandidate(intid,stringname,intage,stringphone,stringposition){
this.id=id;this.name=name;this.age=age;
this.phone=phone;this.position=position;
}
publicCandidate(intid){
this.id=id;
}
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{
return-1;
}
return0;
}
}
}usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Collections;
namespacevoteDemo
{
classTicketControl
{
privateListcanList;//统计候选人
privateListticList;//统计票数
privateintindex=1;
publicTicketControl(){
canList=newList();
//初始化6个候选人
Candidatec=newCandidate(index,"张三1",12,"123456","职位"+index);
canList.Add(c);index++;
c=newCandidate(index,"张三2",12,"123456","职位"+index);
canList.Add(c);index++;
c=newCandidate(index,"张三3",12,"123456","职位"+index);
canList.Add(c);index++;
c=newCandidate(index,"张三4",12,"123456","职位"+index);
canList.Add(c);index++;
c=newCandidate(index,"张三5",12,"123456","职位"+index);
canList.Add(c);index++;
c=newCandidate(index,"张三6",12,"123456","职位"+index);
canList.Add(c);index++;
ticList=newList();
Tickett=newTicket(newint[]{1,2,3,4});
ticList.Add(t);
t=newTicket(newint[]{1,2,3,4,5});
ticList.Add(t);
t=newTicket(newint[]{1,2});
ticList.Add(t);
t=newTicket(newint[]{1,2,6,7});
ticList.Add(t);
t=newTicket(newint[]{2,3,4});
ticList.Add(t);
t=newTicket(newint[]{2,4,8});
ticList.Add(t);
t=newTicket(newint[]{6,5,4});
ticList.Add(t);
t=newTicket(newint[]{5,6});
ticList.Add(t);
t=newTicket(newint[]{2,3});
ticList.Add(t);
t=newTicket(newint[]{4,7});
ticList.Add(t);
}
//进行结果的显示
publicvoidshowResult(){
//对数据进行排序
canList.Sort();
Candidatec1=canList[0];
Console.WriteLine();
Console.WriteLine("当选人是:
"+canList[0].Name+",票数为:
"+canList[0].Count);
foreach(CandidatecincanList)
{
Console.WriteLine();
Console.WriteLine(c.ToString());
}
}
//进行投票选举过程
publicvoidvote(){
foreach(TickettinticList){
int[]tArr=t.Id;
foreach(intidintArr){
foreach(CandidatecincanList){
if(c.Id==id){
c.AddTicket();
}
}
}
}
}
}
}usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacevoteDemo
{
classProgram
{
staticvoidMain(string[]args)
{
TicketControlt=newTicketControl();
t.vote();
t.showResult();
Console.ReadLine();
}
}
}