旅游景点人物进出系统[OC项目]

简介:
 
 

要求:展览中心有2条入场通道,在入场处需要登记入场人员的姓名,年龄以及电话。展览中心最多只能容纳100人。当展览中心满员时应当立即通知门卫不再允许人员入场。当有人员出场时才会允许人员入场,但同时在展览中心的人员不会超过100人。当展览中心关闭后,输出所有入场过的人员信息。

需要实现以下功能:

a.用户在做任一操作时,有入场,退场,输出所有人员信息的选项

b.选择入场,登记人员的姓名,年龄以及电话,同时计数器+1

c.选择退场,先判断登记人员的信息中是否有这个人,有:计数器-1,否则返回

d.人员超过90人时,给出警告信息

e.人员达到100时,禁止入场。

main:
 
 

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Police.h"

#import "PassPeople.h"

#import "PersonWarmingException.h"

#import "PersonOverflowException.h"

#import "SingleClass.h"

extern BOOL  b;

int main(int argc, const char * argv[])

{

    @autoreleasepool {

        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        //实例化售票对象

        PassPeople *pass = [[PassPeople alloc] init];

        //售票

        [pass applicationDidFinishLaunching];

        //[NSThread sleepForTimeInterval:30];

        if(b == true)

        {

            [NSThread sleepForTimeInterval:3];

        }

        [pool release];

        return 0;

    }

    return 0;

}

Person.h:
 
 

#import <Foundation/Foundation.h>

@interface Person : NSObject

//{

//    NSString *name;

//    int age;

//    NSString *phoneNumber;

//}

@property(nonatomic,retain) NSString *name; //姓名

@property(assign) int age; //年龄

@property(nonatomic,retain) NSString *phoneNumber; //电话

@end

Person.m:
 
 

#import "Person.h"

@implementation Person

//@synthesize name = _name;

//@synthesize age = _age;

//@synthesize phoneNumber = _phoneNumber;

//动态创建person 

+(id)personWithName:(NSString *)name andAge:(int)age andPhoneNumber:(NSString *)phoneNumber

{

    Person *person = [[[Person alloc] init] autorelease];

    person.name = name;

    person.age = age;

    person.phoneNumber = phoneNumber;

    return person;

}

-(NSString *)description

{

    return [NSString stringWithFormat:@"name:%@ age:%i phonenum:%@",_name,_age,_phoneNumber];

}

-(void)dealloc

{

    [_name release];

    [_phoneNumber release];

    [super dealloc];

}

@end

创建票数的单例
SingleClass.h:
 
 

#import <Foundation/Foundation.h>

@interface SingleClass : NSObject

{

    int count;

}

@property(assign) int count;

+(SingleClass *)singleClassCreate;

//-(int)Add;

//-(int)Clear;

//-(int)Sub;

@end

SingleClass.m
 
 

#import "SingleClass.h"

@implementation SingleClass

+(SingleClass *)singleClassCreate

{

    static SingleClass *a;

    if (a == nil) {

        a = [[SingleClass alloc] init];

        a.count = 0; //初始化的时候设置0

        return a;

    }

}

-(int)getCount

{

    return count;

}

//-(void)setCount

//{

//    count = 0;

//}

-(int)Add

{

    return count++;

}

-(int)Sub

{

    return count--;

}

-(int)Clear

{

    return count = 0;

}

@end

控制人物进出:
PassPeople.h:
 
 

#import <Foundation/Foundation.h>

@interface PassPeople : NSObject

{

    int tickets;//当前票数

    int count;//游客总人数

    NSThread *thread1;//1过道

    NSThread *thread2;//2过道

    //加锁

    NSCondition *ticketCondition;

    NSMutableArray *array;//记录游客

}

@property(assign) int tickets;

@property(assign) int count;

@property(nonatomic,retain) NSThread* thread1;

@property(nonatomic,retain) NSThread* thread2;

@property(nonatomic,retain) NSMutableArray *array;

@property(nonatomic,retain) NSCondition *ticketCondition;

-(void)applicationDidFinishLaunching;

-(void)run;

@end

PassPeople.m:
 
 

#import "PassPeople.h"

#import "SingleClass.h"

#import "Police.h"

#import "PersonOverflowException.h"

#import "PersonWarmingException.h"

#import <Foundation/NSException.h>

#import <Foundation/NSString.h>

#import <Foundation/NSAutoreleasePool.h>

BOOL b = true;

@implementation PassPeople

-(void)applicationDidFinishLaunching

{

    [[SingleClass singleClassCreate] setCount:0];

    count = [[SingleClass singleClassCreate] getCount];

    tickets = 100 ;

    ticketCondition = [[NSCondition alloc] init];

    thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];

    [thread1 setName:@"Thread1"];

    [thread1 start];

    thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];

    [thread2 setName:@"Thread2"];

    [thread2 start];

    while (b) {

        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; //一直等待子线程发送结束消息

        //[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:2]];//但当前时间等待2秒,如果还没反应就自动结束

        NSLog(@"Endrunloop.");

    }

    NSLog(@"OK");

}

-(void)setEnd

{

    b = false;

}

-(void)run

{

    @synchronized(self) //原子性操作,跟加锁一样

    {

        while (TRUE) {

            //[ticketCondition lock];//加锁

            if (tickets>0) {

                [NSThread sleepForTimeInterval:0.1];//延时0.1

                //count = 100 - tickets;

                count = [[Police police] count];

                tickets = 100 - count;

                NSLog(@"已经有:%i进入  还能允许:%i 当前线程:%@",count,tickets,[[NSThread currentThread] name]);

                NSLog(@"请选择进入还是离开:1(),2(离开),3(目前的人数),4(当前的人),5(所有进入过的人)");

                int i = 0;

                scanf("%d",&i);

                if (i==1) {

                    @try

                    {

                        [[Police police] addpeople];

                        tickets --;

                    }

                    @catch (PersonWarmingException *e) {

                        NSLog(@"%@ %@",[e name],[e reason]);   //输出警告异常信息

                    }

                    @catch (PersonOverflowException *e) {

                        NSLog(@"%@ %@",[e name],[e reason]);   //输出溢出异常信息

                    }

                    @finally {

                    }

                }

                else if(i ==2 )

                {

                    NSLog(@"请输入离开的人的名字:");

                    char s[20];

                    scanf("%s",&s);

                    NSString *name = [NSString stringWithUTF8String:s];

                    [[Police police] delpeople:name];

                }

                else if(i == 3)

                {

                    [[Police police] PrintCount];

                }

              else if (i ==4)

                {

                    [[Police police] PrinteNowPeople];

                }

                else if(i ==5)

                {

                    [[Police police] PrintAllPeople];

                }

                else

                {

                    NSLog(@"输入错误,请重新选择");

                }

            }

            else

            {

                [self performSelectorOnMainThread:@selector(setEnd) withObject:nil waitUntilDone:NO];

                break;

            }

            //[ticketCondition unlock];//解锁

            b=false;

        }

    }

}

-(void)dealloc

{

    [thread1 release];

    [thread2 release];

    [ticketCondition release];

    [array release];

    [super dealloc];

}

@end

几个空类,提示异常的类
PersonWarmingException.h
PersonWarmingException.m
PersonOverflowException.h
PersonOverflowException.m

门卫
Police.h:
 
 

#import <Foundation/Foundation.h>

#import <Foundation/NSObject.h>

@interface Police : NSObject

//{

//    int n;//人数

//    NSMutableArray *array;//记录person信息

//    NSMutableArray *arrayall;//记录所有入过场的人的信息

//}

@property(assign) int n;

@property(nonatomic,retain)NSMutableArray *array;

@property(nonatomic,retain)NSMutableArray *arrayall;

+(id)police;

-(int)count;//获取当前的人数

-(void)addpeople;//允许人进来

-(void)delpeople:(NSString *)name;//允许人出去

-(void)Printe;//打印当前的人数

@end

Police.m:
 
 

#import "Police.h"

#import "PersonOverflowException.h"

#import "PersonWarmingException.h"

#import "SingleClass.h"

#import "Person.h"

@implementation Police

@synthesize n = _n;

@synthesize array = _array;

@synthesize arrayall = _arrayall;

+(id)police

{

    static Police * p;

    if(p == nil)

    {

        p = [Police new];

        p.n = [[SingleClass singleClassCreate] getCount];

        [p initArr];

    }  

    return p;

}

-(void)initArr

{

    self.array = [[[NSMutableArray alloc] init] autorelease];

    self.arrayall = [[[NSMutableArray alloc] init] autorelease];

}

-(int)count//获取当前的人数

{

    return _n;

}

-(void)setPersonInfoToArray

{

    char q[30];

    Person *p = [[[Person alloc] init] autorelease];

    NSLog(@"请输入姓名:");

    scanf("%s",q);

    NSString *s = [NSString stringWithUTF8String:q];

    p.name = s;

    int age;

    NSLog(@"请输入年龄:");

    scanf("%d",&age);

    p.age = age;

    NSLog(@"请输入电话");

    scanf("%s",q);

    NSString *ss = [NSString stringWithUTF8String:q];

    p.phoneNumber = ss;

    [self.array addObject:p];

    [self.arrayall addObject:p];//添加凡是入过场的人的信息

}

-(void)addpeople//允许人进来

{

    if (_n>100) {

        NSException *e = [PersonOverflowException exceptionWithName:@"PersonOverflowException" reason:@"People's number is over 100" userInfo:nil];

        @throw e;

    }

    else if (_n>=90)

    {

        [self setPersonInfoToArray];

        _n++;

        NSException *e = [PersonWarmingException exceptionWithName:@"CupWarningException" reason:@"People's number is above or at 90" userInfo:nil];

        @throw e;  //抛出警告异常

    }

    else

    {

        [self setPersonInfoToArray];

        _n++;

    }

}

-(NSMutableArray *)delPersonInfoFromArray:(NSString *)name

{

    NSMutableArray *newArray;

    int j = 0;

    Person *p = [Person new];

    for (int i = 0; i<[_array count]; i++) {

        p = [_array objectAtIndex:i];

        if (p.name == name) {

            j = i;

            break;

        }

    }

    //j之前的对象全部移动到一个数组,然后将j+1d的对象全部移动到另外一个数组

    for (int i=0; i<j; i++) {

        [newArray addObject:[_array objectAtIndex:i]];

    }

    for (int i = j+1; i<[_array count]; i++) {

        [newArray addObject:[_array objectAtIndex:i]];

    }

    _n--;//n减少1

    NSLog(@"一人已经离开");

    [_array removeAllObjects];

    [_array addObjectsFromArray:newArray];

    return _array; //这里面就存的删除一个用的array

}

//判断当前是否有这个人

-(int)IsExitThePeople:(NSString *)name

{

    Person *p;

    int i=0;

    int j = -1;

    for (i; i<[_array count]; i++) {

        p = [_array objectAtIndex:i];

        if (p.name == name) {

            j = i;

            break;

        }

        else

        {

            continue;

        }

    }

    if (i >=[_array count]) {

        NSLog(@"没有这个人");

        return -1;

    }

    return j;

}

-(void)delpeople:(NSString *)name//允许人出去

{

    int nn = [_array count];//记录当前有多少人

    if (nn<=0) {

        NSLog(@"这时没人");

    }

    else

    {

        //先查找是否有这个人

        int num = [self IsExitThePeople:name];

        //如果有此人,则退场

        if (num>=0) {

            [_array removeAllObjects];//先清空,再赋值

            [_array addObjectsFromArray:[self delPersonInfoFromArray:name]];

        }

        else

            return;

    }

}

-(void)PrintCount//打印当前的人数

{

    NSLog(@"当前的人数是:%i",_n);

}

-(void)PrinteNowPeople

{

    NSLog(@"当前在场的人:%@",_array);

}

-(void)PrintAllPeople

{

    NSLog(@"所有入过场人的信息:%@",_arrayall);

}

-(void)dealloc

{

    [_array release];

    [_arrayall release];

    [super dealloc];

}

@end

结果:

2013-08-02 19:32:43.078 2013-7-31项目作业[4163:303] Endrunloop.

2013-08-02 19:32:43.063 2013-7-31项目作业[4163:1903] 已经有:0进入  还能允许:100人 当前线程:Thread1

2013-08-02 19:32:43.079 2013-7-31项目作业[4163:1903] 请选择进入还是离开:1(进),2(离开),3(目前的人数),4(当前的人),5(所有进入过的人)

1

2013-08-02 19:32:45.142 2013-7-31项目作业[4163:1903] 请输入姓名:

1

2013-08-02 19:32:46.093 2013-7-31项目作业[4163:1903] 请输入年龄:

1

2013-08-02 19:32:46.486 2013-7-31项目作业[4163:1903] 请输入电话

1

2013-08-02 19:32:46.978 2013-7-31项目作业[4163:1903] 已经有:1进入  还能允许:99人 当前线程:Thread1

2013-08-02 19:32:46.978 2013-7-31项目作业[4163:1903] 请选择进入还是离开:1(进),2(离开),3(目前的人数),4(当前的人),5(所有进入过的人)

4

2013-08-02 19:32:48.781 2013-7-31项目作业[4163:1903] 当前在场的人:(

    "name:1 age:1 phonenum:1"

)

2013-08-02 19:32:48.883 2013-7-31项目作业[4163:1903] 已经有:1进入  还能允许:99人 当前线程:Thread1

2013-08-02 19:32:48.883 2013-7-31项目作业[4163:1903] 请选择进入还是离开:1(进),2(离开),3(目前的人数),4(当前的人),5(所有进入过的人)

3

2013-08-02 19:32:51.229 2013-7-31项目作业[4163:1903] 当前的人数是:1

2013-08-02 19:32:51.331 2013-7-31项目作业[4163:1903] 已经有:1进入  还能允许:99人 当前线程:Thread1


















本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366486,如需转载请自行联系原作者


相关文章
|
7月前
|
Web App开发 自然语言处理 JavaScript
一键创建和部署高分电影推荐语音技能
本场景使用天猫精灵技能应用平台提供的技能模板,在2-5分钟内,创建一个好玩的高分电影推荐技能,使用模板后无须代码开发,系统自动配置意图、实体等,新手0基础也可体验创建技能的乐趣。
55 0
|
定位技术 图形学
Unity3D——射击游戏(多地图,多人物,枪支切换,驾车,扔手雷等功能,堪比小型和平精英)
Unity3D——射击游戏(多地图,多人物,枪支切换,驾车,扔手雷等功能,堪比小型和平精英)
Unity3D——射击游戏(多地图,多人物,枪支切换,驾车,扔手雷等功能,堪比小型和平精英)
|
存储 缓存 关系型数据库
「绝密档案」“爆料”完整秒杀架构的设计到技术关键点的“八卦追踪”
「绝密档案」“爆料”完整秒杀架构的设计到技术关键点的“八卦追踪”
143 0
|
存储 定位技术 开发工具
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
|
运维 数据可视化 Serverless
一键创建和部署高分电影推荐语音技能1
一键创建和部署高分电影推荐语音技能1
92 0
|
定位技术 vr&ar
哈迷福利!这个小哥租下一座城堡,用AR和GPS做了张“活点地图”,实时追踪入侵者
哈迷福利!这个小哥租下一座城堡,用AR和GPS做了张“活点地图”,实时追踪入侵者
134 0
|
人工智能 安全 搜索推荐
日本开发戒烟APP!吸烟数据联动医生,认知疗法“辅助”,老烟民了解一下?
日本开发戒烟APP!吸烟数据联动医生,认知疗法“辅助”,老烟民了解一下?
226 0
|
安全 机器人 API
自制App追踪全美麦当劳的“冰激凌机”,哪台坏了一目了然,还被麦记高管点赞
自制App追踪全美麦当劳的“冰激凌机”,哪台坏了一目了然,还被麦记高管点赞
119 0
|
机器学习/深度学习 人工智能 数据挖掘
您家熊猫有喜了,这个“羞耻”的项目用声音检测大熊猫交配情况
您家熊猫有喜了,这个“羞耻”的项目用声音检测大熊猫交配情况
169 0
制作简单太阳追踪器(电子向日葵)
1.准备 原件 说明 LM358模块 1个 6.8~10K的电阻或变阻器 2个 光敏电阻 2个 电机 1个 电源 - 导线电源 - 2.
3610 0