Challenge 1 extended (add your items through console)


#1

Hi just wanted to share this with you guys:
You can add your items to the list and get asked everytime if you are finished or not. If you are finished with input you get to see your list and it get deleted for your next input.

//
//  main.m
//  Groceries


#import <Foundation/Foundation.h>
#import <readline/readline.h>


void addItemsWhenNotFinished(BOOL finished)
{
    // Get the users dokument directory and generate a filepath
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/groceries.dgs",documentsDirectory];
    
    // Load the file into the groceries array and check if its empty
    NSMutableArray *groceriesList = [NSMutableArray arrayWithContentsOfFile];
    if(groceriesList == nil) groceriesList = [NSMutableArray array];
    
    // while the user is not finished with adding items, add new items
    while (!finished) {
        
        // Ask user to enter stuff for the List.
        printf("What you need to buy?");
        // Read the userinput as string...
        const char *items = readline(NULL);
        // Create NSString from char
        NSString *newItem = [[NSString alloc] initWithUTF8String:items];
        [groceriesList addObject:newItem];
        
        // Ask the user if he is finished with that item
        printf("Is that all? YES/NO");
        const char *isThatAll = readline(NULL);
        // Create NSString from char
        NSString *reallyFinished = [[NSString alloc] initWithUTF8String:isThatAll];
        NSString *yes = @"YES";
        
        NSRange match = [yes rangeOfString:reallyFinished options:NSCaseInsensitiveSearch];
        if (match.location == NSNotFound) {
            
            NSLog(@"Ok please go ahead...\n\n");
            // Write Array to file for later use
            [groceriesList writeToFile:filePath atomically:YES];
            //NSLog(@"%@", filePath);
            addItemsWhenNotFinished(NO);
            
        } else {
            // Get the groceries list and show it to the user
            NSLog(@"Thank you! Here is your groceries list:\n\n");
            
            for (NSString *stuff in groceriesList) {
                NSLog(@"- %@\n\n", stuff);
            }
            [groceriesList removeAllObjects];
            [groceriesList writeToFile:filePath atomically:YES];
          addItemsWhenNotFinished(YES);
            
        }
    }
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
    
        addItemsWhenNotFinished(NO);
        
    }
    return 0;
}

#2

I created a similar solution where you add your items at the prompt and enter “done” when you are done:

[code]//
// main.m
// Groceries
//
// Created by Richard Pereira1 on 9/23/14.
// Copyright © 2014 BIg Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <readline/readline.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
//Create an empty Mutable array
NSMutableArray *groceryList = [NSMutableArray array];

    NSLog(@"Enter grocery items. When done enter \"done\"\n");
    NSLog(@"First item: ");
    NSString *item = [NSString stringWithUTF8String:readline(NULL)];
    
    while ([item caseInsensitiveCompare:@"done"]) {
         //Add item to the array
        [groceryList addObject:item];
        
        NSLog(@"Next item: ");
        item = [NSString stringWithUTF8String:readline(NULL)];
    } ;
    /*
    do {
        //Read next item from user input
        NSString *getItem = [NSString stringWithUTF8String:readline(NULL)];
        item = getItem;
        
        //Add item to the array
        [groceryList addObject:item];
        
        NSLog(@"Next item: ");
    } while ([item caseInsensitiveCompare:@"done"]);
    */
    
    for (NSString *i in groceryList) {
        NSLog(@"- %@", i);
    }
}
return 0;

}[/code]