首页 > 其他分享 >旅游景点人物进出系统[OC项目]

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

时间:2023-06-11 14:01:12浏览次数:33  
标签:count 旅游景点 name int 进出 void OC import 2013


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

}
 
#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
 
#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
 
 

   创建票数的单例 
 
 
#import <Foundation/Foundation.h>
@interface SingleClass : NSObject
{
    int count;
}
@property(assign) int count;
+(SingleClass *)singleClassCreate;
//-(int)Add;
//-(int)Clear;
//-(int)Sub;
@end
 
#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
 
 

   控制人物进出: 
 
 
#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
 
#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
 
#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

标签:count,旅游景点,name,int,进出,void,OC,import,2013
From: https://blog.51cto.com/dingxiaowei/6457694

相关文章

  • OC学习笔记[注意事项]
    alloc new retain之后都必须要调用release方法计数器要变只有这几种方法retainreleaseallocnewcopy方法才会使计数器改变,谁想用人家对象,就对他进行retain操作,但在dealloc释放本对象的时候也释放引用的对象,先释放引用的对象,然后释放自己,内存管理做到”谁污染谁治......
  • npm install 报错如何解决npm ERR! code 128 npm ERR! An unknown git error occurre
    npmERR!code128npmERR!Anunknowngiterroroccurre如何解决 1.发现问题我在通过git工具clonevue-element-admin之后,需要下载相关的第三方包所以我就在对应目录下执行npminstall开始下载文件在安装依赖包node_models开始报错无法安装npmERR!code128npmER......
  • [c/c++/OC]高质量的面试题及答案及注解
    一、选择题C语言:1.声明语句为inta[3][4];下列表达式中与数组元素a[2][1]等价的是(A)。A、*(a[2]+1)B、a[9]C、*(a[1]+2)D、*(*(a+2))+1a[2]<==>*(a+2)是等价的C两个数反过来了,D、1放进去2.请问经过表达式a=5?0:1的运算,变量a的最终值是(C......
  • cocos2d学习笔录1
    CCDirector的主要作用:1.访问和改变场景;2.访问cocos2d-x的配置细节3.访问视图(OPENGL,UIVIEW,UIWINDOW);4.暂停,恢复和结束游戏;5.在UIKit和OpenGL之间切换坐标CCNode常用API:1.生成一个新的节点:CCNode*childNoe=CCNode::create();2.将新节点添加为子节点......
  • 【已解决】MySQL连接错误 ERROR 1129 (00000): Host ” is blocked because of many c
     问题连接MySQL 报错 ERROR1129(00000):Host”isblockedbecauseofmanyconnectionerrors原因同一个IP在短时间内产生太多终端的数据库连接(超过mysql数据库max_connection_errors设置),导致被阻塞。在系统变量:max_connect_errors设置了允许中断的次数,超过了这个次数(或者......
  • 在Windows上无docker直接将基于Solon的jar包通过IDEA部署到Linux的docker上
    为何会选择学习solon?springboot对于我开发小企业应用太重,启动太慢,下班太晚!为何都用windows,还想着不安装dockerdesktop洁癖,运行路径能短就短。步骤(以solon官网的helloword为例)1、下载helloworld代码传送阵:点击我2、通过IDEA打开代码,并运行它(我是下载基于maven版本的)。3......
  • 10、Docker利用数据卷实现容器数据持久化与数据卷容器
    Docker利用数据卷实现容器数据持久化docker容器的分层容器的数据分层目录LowerDir:image镜像层,即镜像本身,只读UpperDir:容器的上层,可读写,容器变化的数据存放在此处,创建好容器,修改了数据,新生的的修改数据放在此处MergedDir:容器的文件系统,使用UnionFS(联合文件系统)......
  • ubuntu安装docker 记录
    2023.6.11上午sudoaptinstalldocker-io//安装systemctlstatusdocker//查看运行状态sudodockerrunhello-world//测试 为学习操作系统做准备,在docker中运行 docker安装以下 nasmgccbinnutilsqemu-system-i386mtools//《30天自制操作系统》......
  • docker 安装并配置nacos2.2.2 单机
    docker拉取文件找镜像参考mysql安装找镜像步骤:https://www.cnblogs.com/CodeLuckly/p/15710643.html拉取命令:dockerpullnacos/nacos-server:v2.2.2挂载目录:就是为了让nacos的容器中文件映射到宿主机上mkdir-p/mydata/nacos/logs/#新建logs目录......
  • nas使用docker部署alist​
    一、下载镜像xhofe/alist:latest二、创建容器第一行:文件/文件夹:此处填写在nas上事先创建好的目录位置装载路径:/opt/alist/data类型选读写启动容器,检查日志是否有报错项。如没有报错,容器应该为运行中的状态。三、打开alist页面点击快捷方式查看应用网址打开alist登录页面http://192......