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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

蓬莱仙羽 2013-08-02 10:37:53 浏览630
展开阅读全文

要求:展览中心有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

网友评论

登录后评论
0/500
评论
蓬莱仙羽
+ 关注