clear

while true
    clc
    disp('new - new cd collection')
    disp('add - add a new cd')
    disp('store - store cd collection')
    disp('(lo) load - load cd collection')
    disp('title - sort by title')
    disp('(ar) artist - sort by artist')
    disp('genre - sort by genre')
    disp('find - find a CD by attribute')
    disp('remove - remove all CDs with attribute')
    disp('value - add value')
    disp('(la) largest - search for largest (field)')
    disp('quit - quit')
    choice = input('Enter choice: ','s');
    switch choice
        case {'n','new'}
            buildCDs
        case {'add'}
            cds(length(cds)+1) = getCD();
        case {'store'}
            save mycds cds
        case {'lo', 'load'}
            load mycds
        case {'t', 'title'}
            clc
            titles = {cds.title};
            [sorted order] = sort(titles);
            fprintf('%-15s; %-15s; %-15s; %s; %s\n\n', ...
                'title', 'artist', 'genre', 'year', 'price');
            for index = 1:length(cds)
                fprintf('%-15s; %-15s; %-15s; %d; %0.2f\n', ...
                                      cds(order(index)).title,...
                                      cds(order(index)).artist,...
                                      cds(order(index)).genre,...
                                      cds(order(index)).year,...
                                      cds(order(index)).price),...
            end
            input('Hit enter please','s');
        case {'ar', 'artist'}
            clc
            artists = {cds.artist};
            [sorted order] = sort(artists);
           fprintf('%-15s; %-15s; %-15s; %s; %s\n\n', ...
                'artist', 'title', 'genre', 'year', 'price');
             for index = 1:length(cds)
                fprintf('%-15s; %-15s; %-15s; %d; %0.2f\n', ...
                                      cds(order(index)).artist,...
                                      cds(order(index)).title,...
                                      cds(order(index)).genre,...
                                      cds(order(index)).year,...
                                      cds(order(index)).price)
            end
            input('Hit enter please','s');
        case {'g', 'genre'}
            clc
            genres = {cds.genre};
            [sorted order] = sort(genres);
           fprintf('%-15s; %-15s; %-15s; %s; %s\n\n', ...
                'genre', 'artist', 'title', 'year', 'price');
            for index = 1:length(cds)
                fprintf('%-15s; %-15s; %-15s; %d; %0.2f\n', ...
                                      cds(order(index)).genre,...
                                      cds(order(index)).artist,...
                                      cds(order(index)).title,...
                                      cds(order(index)).year,...
                                      cds(order(index)).price)
            end
            input('Hit enter please','s');
        case {'la', 'largest'}
            nm = getField(cds);
            it = largestCD(cds, nm)
            input('Hit enter please','s');
        case {'q','quit'}
            break;
        otherwise
            disp('Invalid choice')
    end
end
