旅游景点人物进出系统[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,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的旅游景点人物进出系统[OC项目]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux-0.00运行环境搭建【转】
- 下一篇: 从HttpServletRequest获