@fiy-fish
2015-07-15T20:11:35.000000Z
字数 1017
阅读 1363
Objective-c
// main.m
// day05-01-数组练习
//
// Created by Aaron on 15/7/7.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Company.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Company *c = [[Company alloc] init];
NSLog(@"%ld",[c leftForMonth:11]);
}
return 0;
}
#import <Foundation/Foundation.h>
/*
2.设计一个工厂类,创建一个手机厂
该手机厂每个月生产1000部手机,A品及格率80%(不及格的B品要回炉再造)
从7月到11月是销售旺季,每个月能卖出1100部手机
其他月份是销售淡季,每个月能卖出500部手机
问:1月创建厂,实现一个方法,要求输入该年某月,求出当月该手机厂有多少库存?
*/
@interface Company : NSObject
{
NSInteger _product;
float _percent;
NSArray *_saleArray;
}
-(NSInteger)leftForMonth:(NSInteger)month;
@end
#import "Company.h"
@implementation Company
-(instancetype)init
{
if(self = [super init])
{
_saleArray = @[@500,@500,@500,@500,@500,@500,@1100,@1100,@1100,@1100,@1100,@500];
_product = 1000;
_percent = 0.8;
}
return self;
}
-(NSInteger)leftForMonth:(NSInteger)month
{
//1.求总产量
NSInteger total = _product*_percent*month;
//2.求销售量
NSInteger sale = 0;
for(int i = 0; i < month; i++)
{
sale = sale + [_saleArray[i] integerValue];
}
//3.求库存
return total-sale >= 0 ?total-sale:0;
}
@end