要求:展覽中心有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:
@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:
@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];
建立票數的單例
SingleClass.h:
@interface SingleClass : NSObject
int count;
@property(assign) int count;
+(SingleClass *)singleClassCreate;
//-(int)Add;
//-(int)Clear;
//-(int)Sub;
SingleClass.m
@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;
控制人物進出:
PassPeople.h:
@interface PassPeople : NSObject
int tickets;//目前票數
int count;//遊客總人數
NSThread *thread1;//1過道
NSThread *thread2;//2過道
//加鎖
NSCondition *ticketCondition;
NSMutableArray *array;//記錄遊客
@property(assign) int tickets;
@property(nonatomic,retain) NSThread* thread1;
@property(nonatomic,retain) NSThread* thread2;
@property(nonatomic,retain) NSMutableArray *array;
@property(nonatomic,retain) NSCondition *ticketCondition;
-(void)applicationDidFinishLaunching;
-(void)run;
PassPeople.m:
#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;
[thread1 release];
[thread2 release];
[ticketCondition release];
[array release];
幾個空類,提示異常的類
PersonWarmingException.h
PersonWarmingException.m
PersonOverflowException.h
PersonOverflowException.m
門衛
Police.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;//列印目前的人數
Police.m:
@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(@"請輸入電話");
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
-(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++) {
_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++) {
else
continue;
if (i >=[_array count]) {
NSLog(@"沒有這個人");
return -1;
return j;
-(void)delpeople:(NSString *)name//允許人出去
int nn = [_array count];//記錄目前有多少人
if (nn<=0) {
NSLog(@"這時沒人");
//先查找是否有這個人
int num = [self IsExitThePeople:name];
//如果有此人,則退場
if (num>=0) {
[_array removeAllObjects];//先清空,再指派
[_array addObjectsFromArray:[self delPersonInfoFromArray:name]];
return;
-(void)PrintCount//列印目前的人數
NSLog(@"目前的人數是:%i",_n);
-(void)PrinteNowPeople
NSLog(@"目前在場的人:%@",_array);
-(void)PrintAllPeople
NSLog(@"所有入過場人的資訊:%@",_arrayall);
[_array release];
[_arrayall release];
結果:
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] 請輸入姓名:
2013-08-02 19:32:46.093 2013-7-31項目作業[4163:1903] 請輸入年齡:
2013-08-02 19:32:46.486 2013-7-31項目作業[4163:1903] 請輸入電話
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,如需轉載請自行聯系原作者