购票系统自己优化过的代码,,在此分享给大家:
#import <Foundation/Foundation.h>
#import "moveTiclket.h"
@interface moveTiclket : Ticket
@property float price;
@property int ticketNo;
@end
@i@implementation moveTiclket
@end
@interface Ticket : Common
//排数
@property int rowNum;
@property int colNum;
@property NSString* place;
@property NSString* starTime;
-(void)printTicket;
@end
@inte@implementation Ticket
-(void)printTicket{
NSLog(@"打印票的信息");
}
@end
@interface Common : NSObject
//名称
@property NSString* name;
@property int longTime;
@property NSString* actor;
@property NSString* director;
@property NSString* showtime;
@property NSString* no;
@end
@implementation Common
@end
@interface Cinema : NSObject
{
NSString* _cinemaName;
moveTiclket* tickets[5];
}
-(instancetype)initWithCinemaName:(NSString*)name;
-(void)buyTicket;
-(void)list;
-(void)selecMovie;
-(void)selectRow;
-(void)selectCol;
-(void)pay;
-(void)printTicket;
@end
#define ROW 8
#define COL 18
int selectno;
@implementation Cinema
-(instancetype)initWithCinemaName:(NSString*)name{
if (self=[super init]) {
_cinemaName = name;
for (int i=0; i<5; i++) {
moveTiclket* trickets=[moveTiclket new];
trickets.name=[NSString stringWithFormat:@"施瓦辛格%02d",i+1];
trickets.longTime=120;
trickets.actor=@"柴瑞军";
trickets.director=@"小霞";
trickets.showtime=[NSString stringWithFormat:@"201%d-12-12",i];
trickets.no=[NSString stringWithFormat:@"%d",i+1 ];
trickets.starTime=[NSString stringWithFormat:@"1%d:00",i];
trickets.price=35.5f;
trickets.place=[NSString stringWithFormat:@"影厅%02d",i+1];
tickets = trickets;
}
}
return self;
}
-(void)buyTicket{
NSLog(@"\n欢迎来到%@,我们将为您带来优质的观看体验!\n正在进入购票系统中,请等待.....!\n",_cinemaName);
printf("您已成功进入购票系统,正在上映的电影如下:\n");
[self list];
}
-(void)list{
//打印电影信息
//数组遍历
for (int i=0; i<5; i++) {
NSLog(@"\n\n电影名:%@\t\t\t时长:%d\n导演:%@\t\t\t\t主演:%@\t\n上映时间:%@\t\t编号:%@\n电影播放时间: %@\n\n\n",tickets.name,tickets.longTime,tickets.actor,tickets.director,tickets.showtime,tickets.no,tickets.starTime);
}
[self selecMovie];
}
-(void)selecMovie{
printf("请选择你要看的电影:\n");
int num;
scanf("%d",&num);
if (num<1||num>4) {
return;
}else{
selectno = num;
NSLog(@"\n您选择的电影是:%@\n",tickets[selectno-1].name);
}
[self selectRow];
}
-(void)selectRow{
printf("影院座位共:%d排,一排有%d个座位,影院座位布局如下图:\n",ROW-1,COL-1);
for (int i=0; i<ROW; i++) {
for (int j=0; j<COL; j++) {
if (i==0) {
printf("%02d ",j);
}else if(j==0){
printf("%02d ",i);
}else{
printf(" + ");
}
}
printf("\n");
}
printf("请选择排数:\n");
int num;
scanf("%d",&num);
if (num<1||num>7) {
return;
}else{
printf("您已选择了第%d排,请选择座位号:\n",num);
tickets[selectno-1].rowNum=num;
[self selectCol];
}
}
-(void)selectCol{
int num;
scanf("%d",&num);
if (num<1||num>17) {
return;
}else{
printf("您已选择了第%d座,选座成功.\n请查看您观看影片的座位分布位置!!\n",num);
tickets[selectno-1].colNum=num;
}
for (int i=0; i<ROW; i++) {
for (int j=0; j<COL; j++) {
if (i==0) {
printf("%02d ",j);
}else if(j==0){
printf("%02d ",i);
}else if((i==tickets[selectno-1].rowNum)&&j==(tickets[selectno-1].colNum)){
printf(" @ ");
}else
printf(" + ");
}
printf("\n");
}
[self pay];
}
-(void)pay{
printf("购票操作已完成!!\n请确认已购买电影票信息...!\n");
NSLog(@"\n\n您选择的电影名:%@\t\t影厅:%@\n第%d排%d座\n电影播放时间:%@\n票价:%.2f\n\n",tickets[selectno-1].name,tickets[selectno-1].place,tickets[selectno-1].rowNum,tickets[selectno-1].colNum,tickets[selectno-1].starTime,tickets[selectno-1].price);
printf("请确认以上信息,无误后请选择是否付款:1.继续 0.退出\n");
int num;
scanf("%d",&num);
if (num==0) {
return;
}else if(num==1)
[self printTicket];
}
-(void)printTicket{
printf("请支付.....\n");
printf("已完成支付!\n欢迎购买!\n");
}
@end
void buyMovieTicket(){
Cinema *cinema = [[Cinema alloc] initWithCinemaName:@"小霞影院"];
[cinema buyTicket];
}
void buySongTicket(){
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
printf("************欢迎使用购票系统**************\n\n");
printf("您可以使用购票系统购买:1,电影票 2,演唱会\n");
int num;
scanf("%d",&num);
if (num==1) {
buyMovieTicket();
}else if(num==2){
buySongTicket();
}
}
return 0;
} |
|