Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create the following in Swift code. C reate a class called StockHolding to repre

ID: 3891348 • Letter: C

Question

Create the following in Swift code.


Create a class called StockHolding to represent a stock that you have purchased. It will be a subclass of NSObject. For instance variables, it will have two floats named purchaseSharePrice and currentSharePrice, one int named numberOfShares and one string named companyName. Create accessor methods for the instance variables. Create two other instance methods:

- (float)costInDollars; // purchaseSharePrice * numberOfShares

- (float)valueInDollars; // currentSharePrice * numberOfShares

Populate an array with three instances of StockHolding. Display all the stocks sorted by company name in alphabetical order.

NOTE: Picture below does not show the companyName but you should implement it in your class.

2. Create a subclass of StockHolding called ForeignStockHolding. Give ForeignStockHolding an additional instance variable: conversionRate, which will be a float. (The conversion rate is what you need to multiply the local price by to get a price in Canadian dollars. Assume the purchasePrice and currentPrice are in the local currency.) Override costInDollarsand valueInDollars to do the right thing.

Add at least two instances of ForeignStockHolding to the existing array from part one.

Display all the stocks sorted by company name in reverse alphabetical order.

3. Modify the application so the user can choose how many stocks they want to enter as well as the type of the stock (maximum 8 stocks). The application should accept user data for every stock and after the data has been entered it should display the following menu:

Explanation / Answer

---------------------

#import <Foundation/Foundation.h>

#import "StockHolding.h"

int main(int argc, const char * argv[])
{

@autoreleasepool {
StockHolding *firstInstance = [[StockHolding alloc] init];
StockHolding *secondInstance = [[StockHolding alloc] init];
StockHolding *thirdInstance = [[StockHolding alloc] init];

// Create an empty mutable array
NSMutableArray *stockArray = [NSMutableArray array];

// Add instances
[stockArray addObject:firstInstance];
[stockArray addObject:secondInstance];
[stockArray addObject:thirdInstance];

[firstInstance setPurchaseSharePrice:2.3];
[firstInstance setCurrentSharePrice:4.5];
[firstInstance setNumberOfShares:40];

[secondInstance setPurchaseSharePrice:12.10];
[secondInstance setCurrentSharePrice:10.56];
[secondInstance setNumberOfShares:20];

[thirdInstance setPurchaseSharePrice:45.10];
[thirdInstance setCurrentSharePrice:48.51];
[thirdInstance setNumberOfShares:210];

for (id stock in stockArray)
{
// Calculate the (current) value of the stock using the valueInDollars method
float value = [stock valueInDollars];
// Calculate the purchase price of the stock using the costInDollars method
float cost = [stock costInDollars];
// Calculate the profit gained thus far
float profit = (value - cost);

// Return the results to the log
NSLog(@"You've got %d stocks, which costed you %.2f and are now worth %.2f. The profit for this stock thus far is %.2f", [stock numberOfShares], cost, value, profit);
}
}
return 0;
}

--------------------------

----------------------------

2 ) Create a subclass of StockHolding called ForeignStockHolding.

main.m :

#import <Foundation/Foundation.h>
#import “StockHolding.h”
#import “ForeignStockHolding.h”

int main (int argc, const char * argv[])
{

@autoreleasepool {
StockHolding *apple = [[StockHolding alloc] init];
StockHolding *ibm = [[StockHolding alloc] init];
StockHolding *oracle = [[StockHolding alloc] init];

ForeignStockHolding *foreignStockApple = [[ForeignStockHolding alloc] init];
ForeignStockHolding *foreignStockIbm = [[ForeignStockHolding alloc] init];
ForeignStockHolding *foreignStockOracle = [[ForeignStockHolding alloc] init];
  
[apple setPurchaseSharePrice:2.30];
[apple setNumberOfShares:40];
[apple setCurrentSharePrice:4.50];

[ibm setPurchaseSharePrice:5.30];
[ibm setNumberOfShares:50];
[ibm setCurrentSharePrice:5.50];

[oracle setPurchaseSharePrice:8.30];
[oracle setNumberOfShares:90];
[oracle setCurrentSharePrice:6.50];

[foreignStockApple setPurchaseSharePrice:12.5];
[foreignStockApple setNumberOfShares:10];
[foreignStockApple setCurrentSharePrice:5];
[foreignStockApple setConversionRate:22.5];

[foreignStockIbm setCurrentSharePrice:10.8];
[foreignStockIbm setNumberOfShares:10];
[foreignStockIbm setCurrentSharePrice:50];
[foreignStockIbm setConversionRate:20];

[foreignStockOracle setCurrentSharePrice:10.8];
[foreignStockOracle setNumberOfShares:10];
[foreignStockOracle setCurrentSharePrice:50];
[foreignStockOracle setConversionRate:220];

NSArray *allStocks = [NSArray arrayWithObjects:apple,ibm,oracle,nil];

NSArray *allForeignStocks = [NSArray arrayWithObjects:foreignStockApple,foreignStockIbm,foreignStockOracle, nil];
  
for (StockHolding *stocks in allStocks) {
NSLog(@"Value: %2.f€, Cost: %2.f€", [stocks valueInDollars], [stocks costInDollars]);
}

for (ForeignStockHolding *fstocks in allForeignStocks) {
NSLog(@"Value after Conversion: %2.f€",[fstocks valueInDollars]);
}
}
return 0;
}

ForeignStockHolding.h :

#import <Foundation/Foundation.h>
#import "StockHolding.h"

@interface ForeignStockHolding : StockHolding
{
float conversionRate;
}

@property float conversionRate;

- (float)costInDollars;
- (float)valueInDollars;

@end

StockHolding.h :

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
  
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

- (float)costInDollars; // purchaseSharePrice * numberOfShares
- (float)valueInDollars; // currentSharePrice * numberOfShares

@end

StockHolding.m :

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

- (float)costInDollars
{
return purchaseSharePrice * numberOfShares;
}

- (float)valueInDollars
{
return currentSharePrice * numberOfShares;
  
}
@end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote