旅游景点人物进出系统[OC项目]
要求:展覽中心有2條入場(chǎng)通道,在入場(chǎng)處需要登記入場(chǎng)人員的姓名,年齡以及電話。展覽中心最多只能容納100人。當(dāng)展覽中心滿(mǎn)員時(shí)應(yīng)當(dāng)立即通知門(mén)衛(wèi)不再允許人員入場(chǎng)。當(dāng)有人員出場(chǎng)時(shí)才會(huì)允許人員入場(chǎng),但同時(shí)在展覽中心的人員不會(huì)超過(guò)100人。當(dāng)展覽中心關(guān)閉后,輸出所有入場(chǎng)過(guò)的人員信息。
需要實(shí)現(xiàn)以下功能:
a.用戶(hù)在做任一操作時(shí),有入場(chǎng),退場(chǎng),輸出所有人員信息的選項(xiàng)
b.選擇入場(chǎng),登記人員的姓名,年齡以及電話,同時(shí)計(jì)數(shù)器+1
c.選擇退場(chǎng),先判斷登記人員的信息中是否有這個(gè)人,有:計(jì)數(shù)器-1,否則返回
d.人員超過(guò)90人時(shí),給出警告信息
e.人員達(dá)到100時(shí),禁止入場(chǎng)。
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];
? ? ? ? //實(shí)例化售票對(duì)象
? ? ? ? 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;
//動(dòng)態(tài)創(chuàng)建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
創(chuàng)建票數(shù)的單例 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; //初始化的時(shí)候設(shè)置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
控制人物進(jìn)出: PassPeople.h:#import <Foundation/Foundation.h>
@interface PassPeople : NSObject
{
? ? int tickets;//當(dāng)前票數(shù)
? ? int count;//游客總?cè)藬?shù)
? ? NSThread *thread1;//1過(guò)道
? ? NSThread *thread2;//2過(guò)道
? ? //加鎖
? ? 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]]; //一直等待子線程發(fā)送結(jié)束消息
? ? ? ? //[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:2]];//但當(dāng)前時(shí)間等待2秒,如果還沒(méi)反應(yīng)就自動(dòng)結(jié)束
? ? ? ? NSLog(@"Endrunloop.");
? ? }
? ? NSLog(@"OK");
}
-(void)setEnd
{
? ? b = false;
}
-(void)run
{
? ? @synchronized(self) //原子性操作,跟加鎖一樣
? ? {
? ? ? ? while (TRUE) {
? ? ? ? ? ? //[ticketCondition lock];//加鎖
? ? ? ? ? ? if (tickets>0) {
? ? ? ? ? ? ? ? [NSThread sleepForTimeInterval:0.1];//延時(shí)0.1秒
? ? ? ? ? ? ? ? //count = 100 - tickets;
? ? ? ? ? ? ? ? count = [[Police police] count];
? ? ? ? ? ? ? ? tickets = 100 - count;
? ? ? ? ? ? ? ? NSLog(@"已經(jīng)有:%i進(jìn)入? 還能允許:%i人 當(dāng)前線程:%@",count,tickets,[[NSThread currentThread] name]);
? ? ? ? ? ? ? ? NSLog(@"請(qǐng)選擇進(jìn)入還是離開(kāi):1(進(jìn)),2(離開(kāi)),3(目前的人數(shù)),4(當(dāng)前的人),5(所有進(jìn)入過(guò)的人)");
? ? ? ? ? ? ? ? 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(@"請(qǐng)輸入離開(kāi)的人的名字:");
? ? ? ? ? ? ? ? ? ? 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(@"輸入錯(cuò)誤,請(qǐng)重新選擇");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? 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
幾個(gè)空類(lèi),提示異常的類(lèi) PersonWarmingException.h PersonWarmingException.m PersonOverflowException.h PersonOverflowException.m門(mén)衛(wèi) Police.h:
#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
@interface Police : NSObject
//{
//? ? int n;//人數(shù)
//? ? NSMutableArray *array;//記錄person信息
//? ? NSMutableArray *arrayall;//記錄所有入過(guò)場(chǎng)的人的信息
//}
@property(assign) int n;
@property(nonatomic,retain)NSMutableArray *array;
@property(nonatomic,retain)NSMutableArray *arrayall;
+(id)police;
-(int)count;//獲取當(dāng)前的人數(shù)
-(void)addpeople;//允許人進(jìn)來(lái)
-(void)delpeople:(NSString *)name;//允許人出去
-(void)Printe;//打印當(dāng)前的人數(shù)
@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//獲取當(dāng)前的人數(shù)
{
? ? return _n;
}
-(void)setPersonInfoToArray
{
? ? char q[30];
? ? Person *p = [[[Person alloc] init] autorelease];
? ? NSLog(@"請(qǐng)輸入姓名:");
? ? scanf("%s",q);
? ? NSString *s = [NSString stringWithUTF8String:q];
? ? p.name = s;
? ? int age;
? ? NSLog(@"請(qǐng)輸入年齡:");
? ? scanf("%d",&age);
? ? p.age = age;
? ? NSLog(@"請(qǐng)輸入電話");
? ? scanf("%s",q);
? ? NSString *ss = [NSString stringWithUTF8String:q];
? ? p.phoneNumber = ss;
? ? [self.array addObject:p];
? ? [self.arrayall addObject:p];//添加凡是入過(guò)場(chǎng)的人的信息
}
-(void)addpeople//允許人進(jìn)來(lái)
{
? ? 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之前的對(duì)象全部移動(dòng)到一個(gè)數(shù)組,然后將j+1d的對(duì)象全部移動(dòng)到另外一個(gè)數(shù)組
? ? 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(@"一人已經(jīng)離開(kāi)");
? ? [_array removeAllObjects];
? ? [_array addObjectsFromArray:newArray];
? ? return _array; //這里面就存的刪除一個(gè)用的array
}
//判斷當(dāng)前是否有這個(gè)人
-(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(@"沒(méi)有這個(gè)人");
? ? ? ? return -1;
? ? }
? ? return j;
}
-(void)delpeople:(NSString *)name//允許人出去
{
? ? int nn = [_array count];//記錄當(dāng)前有多少人
? ? if (nn<=0) {
? ? ? ? NSLog(@"這時(shí)沒(méi)人");
? ? }
? ? else
? ? {
? ? ? ? //先查找是否有這個(gè)人
? ? ? ? int num = [self IsExitThePeople:name];
? ? ? ? //如果有此人,則退場(chǎng)
? ? ? ? if (num>=0) {
? ? ? ? ? ? [_array removeAllObjects];//先清空,再賦值
? ? ? ? ? ? [_array addObjectsFromArray:[self delPersonInfoFromArray:name]];
? ? ? ? }
? ? ? ? else
? ? ? ? ? ? return;
? ? }
}
-(void)PrintCount//打印當(dāng)前的人數(shù)
{
? ? NSLog(@"當(dāng)前的人數(shù)是:%i",_n);
}
-(void)PrinteNowPeople
{
? ? NSLog(@"當(dāng)前在場(chǎng)的人:%@",_array);
}
-(void)PrintAllPeople
{
? ? NSLog(@"所有入過(guò)場(chǎng)人的信息:%@",_arrayall);
}
-(void)dealloc
{
? ? [_array release];
? ? [_arrayall release];
? ? [super dealloc];
}
@end
結(jié)果:2013-08-02 19:32:43.078 2013-7-31項(xiàng)目作業(yè)[4163:303] Endrunloop.
2013-08-02 19:32:43.063 2013-7-31項(xiàng)目作業(yè)[4163:1903]?已經(jīng)有:0進(jìn)入??還能允許:100人?當(dāng)前線程:Thread1
2013-08-02 19:32:43.079 2013-7-31項(xiàng)目作業(yè)[4163:1903]?請(qǐng)選擇進(jìn)入還是離開(kāi):1(進(jìn)),2(離開(kāi)),3(目前的人數(shù)),4(當(dāng)前的人),5(所有進(jìn)入過(guò)的人)
1
2013-08-02 19:32:45.142 2013-7-31項(xiàng)目作業(yè)[4163:1903]?請(qǐng)輸入姓名:
1
2013-08-02 19:32:46.093 2013-7-31項(xiàng)目作業(yè)[4163:1903]?請(qǐng)輸入年齡:
1
2013-08-02 19:32:46.486 2013-7-31項(xiàng)目作業(yè)[4163:1903]?請(qǐng)輸入電話
1
2013-08-02 19:32:46.978 2013-7-31項(xiàng)目作業(yè)[4163:1903]?已經(jīng)有:1進(jìn)入??還能允許:99人?當(dāng)前線程:Thread1
2013-08-02 19:32:46.978 2013-7-31項(xiàng)目作業(yè)[4163:1903]?請(qǐng)選擇進(jìn)入還是離開(kāi):1(進(jìn)),2(離開(kāi)),3(目前的人數(shù)),4(當(dāng)前的人),5(所有進(jìn)入過(guò)的人)
4
2013-08-02 19:32:48.781 2013-7-31項(xiàng)目作業(yè)[4163:1903]?當(dāng)前在場(chǎng)的人:(
? ? "name:1 age:1 phonenum:1"
)
2013-08-02 19:32:48.883 2013-7-31項(xiàng)目作業(yè)[4163:1903]?已經(jīng)有:1進(jìn)入??還能允許:99人?當(dāng)前線程:Thread1
2013-08-02 19:32:48.883 2013-7-31項(xiàng)目作業(yè)[4163:1903]?請(qǐng)選擇進(jìn)入還是離開(kāi):1(進(jìn)),2(離開(kāi)),3(目前的人數(shù)),4(當(dāng)前的人),5(所有進(jìn)入過(guò)的人)
3
2013-08-02 19:32:51.229 2013-7-31項(xiàng)目作業(yè)[4163:1903]?當(dāng)前的人數(shù)是:1
2013-08-02 19:32:51.331 2013-7-31項(xiàng)目作業(yè)[4163:1903]?已經(jīng)有:1進(jìn)入??還能允許:99人?當(dāng)前線程:Thread1
本文轉(zhuǎn)自蓬萊仙羽51CTO博客,原文鏈接:http://blog.51cto.com/dingxiaowei/1366486,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的旅游景点人物进出系统[OC项目]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux-0.00运行环境搭建【转】
- 下一篇: java信息管理系统总结_java实现科