function Array() {
}

var id_count = 100;
var person_array = new Array();
var FILE_NAME = "person.txt";
var did_save = false;

function person_load() {
}

function person_save() {

}

function person_output() {
   print("name: " + this.name + "\tphone number: " + this.phone_number);
}

function person_input() {
   this.name = input("please enter the new person name: ");
   this.phone_number = input("please enter the new person phone number: ");
}

function Person(name,phone_number) {
   this.id = id_count;
   id_count++;
   this.name = name;
   this.phone_number = phone_number;
   this.output = person_output;
   this.input = person_input;
   this.load = person_load;
   this.save = person_save;
}

function program_load() {
   var file = read_file(FILE_NAME);
   person_array = new Array();
   var sep = "\n";
   var array = split(file, sep);
   for(var i = 0; i < array.length; i += 2) {
      person_array[person_array.length] = new Person(array[i], array[i+1]);
   }
}

function program_save() {
   var file = "";
   for(var i = 0; i < person_array.length; i++) {
      file += person_array[i].name + "\n";
      file += person_array[i].phone_number + "\n";
   }
   write_file(FILE_NAME, file);
}

function program_show_menu() {
   print("main menu:");
   print("1: add new number");
   print("2: delete number");
   print("3: find number");
   print("4: display all");
   print("5: exit");
}

var running = true;

function program_exit() {
   var choice = "no";
   do {
      choice = input("quit program? [yes/no]: ");
      if(choice == "yes") {
         running = false;
      }
   }
   while(choice != "no" && choice != "yes");
}

function program_execute(choice) {
   switch(choice) {
   case 1:
      this.add_new_number();
   break;

   case 2:
      this.delete_number();
   break;

   case 3:
      this.find_number();
   break;

   case 4:
      this.display_all();
   break;

   case 5:
      this.exit();
   break;
   }
}

function program_run() {
   print("JavaScript Address Book 1.0");
   print("(C) Copyright 2019. All rights reserved.");
   print("This software is the intellectual property of Matthew William Coan.");
   print("Author: Matthew W. Coan mcoan@slack.net http://slack.net/~mcoan/");
   print("");

   this.load();

   while(running) {
      this.show_menu();
      var choice = input("choice> ");
      choice = parseInt(choice);
      this.execute(choice);
   }

   this.save();
}

function program_display_all() {
   for(var i = 0; i < person_array.length; i++) {
      person_array[i].output();
   }
   input("[press enter to continue]\n");
}

function program_add_new_number() {
   var name = input("please enter the name: ");
   var phone_number = input("please enter the phone number: ");
   person_array[person_array.length] = new Person(name, phone_number);
}

function program_delete_number() {
   var phone_number = input("please enter the phone number to delete: ");
   var temp = new Array();
   for(var i = 0; i < person_array.length; i++) {
      if(phone_number != person_array[i].phone_number) {
         temp[temp.length] = person_array[i];
      }
   }
   person_array = temp;
}

function program_find_number() {
   var name = input("please enter the name: ");
   for(var i = 0; i < person_array.length; i++) {
      if(person_array[i].name == name) {
         person_array[i].output();
         var choice = input("[press enter continue]\n");
         break;
      }
   }
}

function AddressBookApp() {
   this.load = program_load;
   this.save = program_save;
   this.run = program_run;
   this.show_menu = program_show_menu;
   this.add_new_number = program_add_new_number;
   this.delete_number = program_delete_number;
   this.find_number = program_find_number;
   this.display_all = program_display_all;
   this.execute = program_execute;
   this.exit = program_exit;
}

try {
   var prog = new AddressBookApp();
   prog.run();
}
catch(ex) {
   print("address book exception...");
   print(ex);
}



