# Arup Guha
# 11/18/2020
# Solution to COP 2930 Program #11: Test Results.

# Store all results in this dictionary.
results = {}

# Get whether or not they want to upload another file.
ans = input("Would you like to upload another file(yes/no)?\n")

# Keep on going as long as there is more data to coalesce.
while ans == "yes":

    # Get the file and open it.
    filename = input("What is the name of the file?\n")
    file = open(filename, "r")

    # Read the # of entries.
    numEntries = int(file.readline())

    # Go through each entry.
    for j in range(numEntries):

        # Read the name and result.
        name = file.readline().rstrip()
        result = file.readline().rstrip()

        # If this is the first time we encountered the name, add it to the dictionary.
        if not name in results:
            results[name] = result

        # If we've seen this before, we only change the entry if this one is positive.
        elif result == "positive":
            results[name] = result

    # Message of receipt.
    print("File", filename,"has been uploaded.")
    
    # Get whether or not they want to upload another file.
    ans = input("Would you like to upload another file(yes/no)?\n")

# Get output file name and open it.
outfile = input("Which file would you like the final results stored?\n")
out = open(outfile, "w")

# Write out the requested information to the file and close it.
out.write(str(len(results))+"\n")
for name in sorted(results.keys()):
    out.write(name+" "+results[name]+"\n")
out.close()

# Write out final greeting
print("Your results have been stored.")
print("Thank you for using the test data accumulator program!")
