/****************************
   Name: PhoneBooks in C++
   Date: 0905/2015
   Author: Hualin Li
   Description: This is the PhoneBook in the C++
*****************************/

#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;
ofstream outfile;

class Entry
{
public:
  string name;
  string phoneNumber;

  Entry()
   {
    name="";
    phoneNumber="";
   }
  
 

  void setName(string str)
  {
    name=str;
  } 

  void setNumber(string str2)
  {
   phoneNumber=str2;
  }
  
  string outName()
  {
    return name;
  }

  string outNumber()
  {
   return phoneNumber;
  }

  void print()
  {
   cout<<"The name "<<outName()<<" Tel: "<<outNumber()<<endl;
   outfile<<"The name "<<outName()<<" Tel: "<<outNumber()<<endl;
  }
};


//The class for phonebook
 
class phoneBook
{
  public:  
  Entry  objs[100]; 
 
  int countNum;
 
  int count(int countNumber)
  {
    countNum=countNumber;
    return countNum;
  }
 
  void add(int num,string name,string number)
  {  
   objs[num].setName(name);
   objs[num].setNumber(number);
  }

  string find(string name)
  {  
     bool found=false;
     string number;
     for(int k=0;k<=countNum;k++)   
     {
     if (name==objs[k].outName()){
         number=objs[k].outNumber();
         found=true;
     }
   } 
      if(found)
      return number;
      else
      return "No existed. (which means that this person is not on this phone book)";
  }

  void print()
  { 
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    cout<<"There are "<<countNum+1<<" People"<<endl;
    outfile<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    outfile<<"There are "<<countNum+1<<" People"<<endl;
    for(int i=0;i<=countNum;i++)
   { 
     objs[i].print();
   }
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    outfile<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  }
};


//Function to output menu
void menu()
{

 cout<<"\t***********************"<<endl;
 cout<<"\t A - Add contact"<<endl;
 cout<<"\t F - Find contact"<<endl;
 cout<<"\t Q - Quit"<<endl;
 cout<<"\t**********************"<<endl;
 cout<<endl;
 cout<<"Please input your option:" <<endl;
 
 outfile<<"\t***********************"<<endl;
 outfile<<"\t A - Add contact"<<endl;
 outfile<<"\t F - Find contact"<<endl;
 outfile<<"\t Q - Quit"<<endl;
 outfile<<"\t**********************"<<endl;
 outfile<<endl;
 outfile<<"Please input your option:" <<endl;
}

int main()
{ 
  phoneBook p;
  char choice;
  string name;
  string number;
  outfile.open("phoneBook.output");
  menu();
  cin>>choice;
  int count=0;  
  
  while(choice !='Q' && choice != 'q'){

  switch(choice){
  case 'A':
  case 'a':
{  cout<<"Please input name"<<endl;
   outfile<<"Please input name"<<endl;
  cin>>name;
  cout<<"Please input number"<<endl;
  outfile<<"Please input number"<<endl;
  cin>>number;
  p.count(count);
  p.add(count,name,number);
  p.print();

  count++;
    break;
}
  case 'F':
  case 'f':
 { cout<<"Please input the name you are looking for"<<endl;
   outfile<<"Please input the name you are looking for"<<endl;
   cin>>name;
   cout<<"The people with name "<<name<<" have number as "<< p.find(name)<<endl;
   outfile<<"The people with name "<<name<<" have number as "<< p.find(name)<<endl;

 }
    
    break;
  case 'Q':
  case 'q':
    cout<<"Thank you for using phonebook."<<endl;
    outfile<<"Thank you for using phonebook."<<endl;
    break;
 default:
   cout<<"Input is not valid. Please input valid option."<<endl;
   outfile<<"Input is not valid. Please input valid option."<<endl;
  }
 
  menu(); 
  cin>>choice;
 }  

return 0;
}

  

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.*;
import java.io.BufferedWriter;

public class PhoneBooks {

 
public static  class Entry{
     public String name;
     public String phoneNumber;
       Entry(){
        name="";
        phoneNumber="";
       }
      
       void setName(String str)
       {
        name=str;
       }
       
       void setNumber(String str2)
       {
        phoneNumber=str2;
       }
     
       String outNumber()
       {
         return phoneNumber;
       }

       String outName()
       {
         return name;
       }

       void print()
       {
        System.out.println("The name "+outName()+" Tel: "+outNumber());
 try{
    PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("PhoneBooks.output", true)));   
    printWriter.println("The name "+outName()+" Tel: "+outNumber());
  
    printWriter.close();}
   catch (IOException e){}
       }
 
     }

//Class for phoneBook
public static class phoneBook
{
 phoneBook(){ countNum=0;}
 Entry[] objs=new Entry[100];
 public int countNum;
 public int total;
 public String inputName;
 public String inputNumber;
 
public void inputNameNumber(String N,String M)
{
  inputName=N;
  inputNumber=M;
}

public String outName()
{
  return inputName;
}

public String outNumber()
{
  return inputNumber;
}

  public void setTotal(int t)
  {
    total=t;
  } 
 
  public int count(int countNumber)
  {
    countNum=countNumber;
    return countNum;
  }
  
  public void add(int c,String name,String number)
  { 
    objs[c]=new Entry();
    objs[c].setName(name);
    objs[c].setNumber(number);
  }

 public String find(String number)
  { 
     String returnString="";
     try{
     PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("PhoneBooks.output", true))); 
     boolean found=false;
     String name="";
     for(int k=0;k<=countNum;k++)   
     {
     if (number.equals(objs[k].outNumber())){
         name=objs[k].outName();
         found=true;
      }
     }
   
     printWriter.println("F");
     printWriter.println("Please input the number you are looking for");
    // printWriter.println(); 
      if(found)
     { 
     printWriter.println(number+" is with name "+name);
     returnString=name;
     }
      else
     {
     printWriter.println("number not found"); 
     returnString="No existed. (which means that this number is not on this phone book)";}
 
    printWriter.close(); 
    }catch(IOException e){}  

  return returnString; 
  }


 public void print()
{
    
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("There are "+total+" People");
    try{
    PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("PhoneBooks.output", true)));
    printWriter.println("A");
    printWriter.println("Please input your name:");
    printWriter.println(outName());
    printWriter.println("Please input the number:");
    printWriter.println(outNumber());
    printWriter.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    printWriter.println("There are "+total+" People");
 
    printWriter.close();
    }
    catch(IOException e){}
     for(int i=0;i<=countNum;i++)
    { 
     objs[i].print();
     }
 
}
 
}

//Funtion to output menu
public static void menu()
{
try{
 PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("PhoneBooks.output", true)));
 System.out.println("***********************");
 System.out.println("A - Add contact");
 System.out.println("F - Find contact");
 System.out.println("Q - Quit");
 System.out.println("**********************");
 System.out.println();
 System.out.println("Please input your option:");
 

 printWriter.println("***********************");
 printWriter.println("A - Add contact");
 printWriter.println("F - Find contact");
 printWriter.println("Q - Quit");
 printWriter.println("**********************");
 printWriter.println();
 printWriter.println("Please input your option:");
 printWriter.close();
  }catch(IOException e){}
}

   //The main function
public static void main(String[] args) throws IOException{

try{
PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("PhoneBooks.output", true))); 
   phoneBook p=new phoneBook();    
   char choice;
   String name="";
   String number="";
   menu();
//   File file=new File("PhoneBooks.output");
   Scanner keyboard = new Scanner(System.in);
   choice=keyboard.next().charAt(0); 
   int count=0;   
   
    while(choice !='Q' && choice != 'q'){
  switch(choice){
  case 'A':
  case 'a':
{ 
  System.out.println("Please input name");
  name=keyboard.next();
   System.out.println("Please input number");
  number=keyboard.next();
  //System.out.println("count="+p.count(count));
  //p.count(count);
  p.add(p.count(count),name,number);
  p.inputNameNumber(name,number);
  p.setTotal(count+1);
  p.print();
  count++;
    break;
}
  case 'F':
  case 'f':
 { System.out.println("Please input the number you are looking for");
//   printWriter.println("Please input the name you are looking for");
   name=keyboard.next();
   System.out.println("The people with name "+name+" have number as "+ p.find(name));
 //  printWriter.println("The people with name "+name+" have number as "+ p.find(name)); 
}
    
    break;
  case 'Q':
  case 'q':
    System.out.println("Thank you for using phonebook.");
    break;
 default:
    System.out.println("Input is not valid. Please input valid option.");
    printWriter.println("Input is not valid. Please input valid option."); 
 }
 
  menu(); 
  //Scanner keyboard = new Scanner(System.in);
  choice=keyboard.next().charAt(0);
}
 printWriter.println("Q");
 printWriter.println("Thank you for using phonebook.");
  printWriter.close();
  }catch(IOException e){ }
 }

}


  
回到主页 回到目录