Source code of the RSA phone book Java program

Source: Internet
Author: User

At your urgent needs, I uploaded the RSA phone book Java code above.

//////////////////////////////////////// //// Phonebookview. java
Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;

/*
* Created on Nov 23,200 5
*/

/**
* @ Author TNG
* View interface for the phone book application
* A simple line command interface is implemented
*/

Public class phonebookview {
/*
* Attributes transformed from UML by RSA:
*/

// Phonebookcontroller and phonebookmodel are generated by RSA
// To represent the Association Relationship
Private phonebookcontroller;
Private phonebookmodel;

/*
* Add my own attributes
*/

// The following are some questions to be asked to user
// Via the line command INTERFACE
Public static string add_name_q = "Please enter the exact person name ";
Public static string add_number_q = "Please enter the phone number ";
Public static string search_q = "Please enter the exact person name .";
Public static string idle_q =
"Please enter your choice of action,/" +
Phonebookcontroller. add_command + "/" to add a phone entry or/"" +
Phonebookcontroller. search_command +
"/" To search for a phone number or/"" +
Phonebookcontroller. quit_command + "/" to end the application .";
Public static string search_result_q =
"-This is the located phone number. Enter/" "+
Phonebookcontroller. start_command +
"/" To do more with the application or/"" +
Phonebookcontroller. quit_command + "/" to end the application .";
Public static string search_not_found_q =
"Phone number not found. Enter/" "+
Phonebookcontroller. start_command +
"/" To do more with the application or/"" +
Phonebookcontroller. quit_command + "/" to end the application .";
Public static string error_q =
"You 've entered an invalid choice. Enter/" +
Phonebookcontroller. start_command +
"/" To do more with the application or/"" +
Phonebookcontroller. quit_command + "/" to end the application .";

/**
* Operations transformed from UML by RSA
*/

/**
* Get called when the State has been changed
* @ Param newstate
*/
Public void statehaschanged (phonebookmodel model, string newstate ){
Phonebookmodel = model;
Changeview (newstate );
}

/**
* Change the view based on the new State
* @ Param newstate
*/
Public void changeview (string newstate ){
If (newstate. Equals (phonebookmodel. idle_state )){
Getuserinput (idle_q );
}
Else if (newstate. Equals (phonebookmodel. add_name_state )){
Getuserinput (add_name_q );
}
Else if (newstate. Equals (phonebookmodel. add_number_state )){
Getuserinput (add_number_q );
}
Else if (newstate. Equals (phonebookmodel. search_state )){
Getuserinput (search_q );
}
Else if (newstate. Equals (phonebookmodel. search_result_state )){
String result = phonebookmodel. getsearchresult ();
If (result = NULL | result. Length () = 0 ){
Getuserinput (search_not_found_q );
}
Else {
Getuserinput (result + search_result_q );
}
}
Else if (newstate. Equals (phonebookmodel. error_state )){
Getuserinput (error_q );
}
Else if (newstate. Equals (phonebookmodel. exit_state )){
System. Out. println ("Bye bye ");
}
}

/**
* Get user input based on question
* @ Param question
*/
Public void getuserinput (string question ){
Bufferedreader in =
New bufferedreader (New inputstreamreader (system. In ));
System. Out. println (question );
Try {
String answer = in. Readline (). Trim ();
Phonebookcontroller. userhasinput (answer );
}
Catch (exception e ){
E. printstacktrace ();
}
}

/**
* Add my own operations
*/

/**
* Constructor
* @ Param Controller
*/
Public phonebookview (phonebookcontroller Controller ){
Phonebookcontroller = controller;
}
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////

Phonebookmodel. Java

Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java. Io. printstream;
Import java. util. enumeration;
Import java. util. hashtable;

/*
* Created on Nov 23,200 5
*/

/**
* @ Author TNG
* Model data for the phone book application.
* The phone book entries are persisted to a file
*/

Public class phonebookmodel {

/*
* Attributes transformed from UML by RSA
*/

// Phonebookview is generated by RSA
// To represent the Association Relationship
Private phonebookview;

/*
* Add my own attributes
*/

// The following are varous States captured by the Model
Public static string add_name_state = "add_name ";
Public static string add_number_state = "add_number ";
Public static string search_state = "Search ";
Public static string idle_state = "idle ";
Public static string search_result_state = "search_result ";
Public static string error_state = "error ";
Public static string exit_state = "exit ";

// Private fields used to track various model data
Private string state = idle_state;
Private string searchresult = NULL;
Private hashtable phonebook = NULL;

// For persisting the phone book entries into a file
Private Static final string record_file = "phonebook.txt ";
Private Static final string record_separator = "_ Sep _";

/**
* Operations transformed from UML by RSA
*/

/**
* Set the state
* @ Param astate
*/
Public void setstate (string astate ){
State = astate;
Phonebookview. statehaschanged (this, State );
}

/**
* Add a phone entry
* @ Param name
* @ Param number
*/
Public void addanentry (string name, string number ){
Phonebook. Put (name, number );
}

/**
* Search the phone number and set the searchresult Field
* @ Param name
*/
Public void searchphonenumber (string name ){
Searchresult = (string) Phonebook. Get (name );
}

/**
* Return the search result
*/
Public String getsearchresult (){
Return searchresult;
}

/**
* Get the state
*/
Public String getstate (){
Return state;
}

/**
* Add my own operations
*/

/**
* Constructor
* @ Param View
*/
Public phonebookmodel (phonebookview view ){
Phonebookview = view;
Phonebook = new hashtable ();
Readrecord ();
}

/**
* Save all the phone entries
*/
Public void saveall (){
Writerecord ();
}

/**
* Read the phone entries from the file
*/
Private void readrecord ()
{
File recordfile;

Try {
Recordfile = new file (record_file );
Int I = 0;
If (! Recordfile. createnewfile ()){
Bufferedreader in =
New bufferedreader (New filereader (recordfile ));

For (I = 0; I <3; I ++ ){
String record = in. Readline ();
If (record! = NULL & record. Length ()! = 0 ){
Int separatorindex =
Record. indexof (record_separator );
If (separatorindex! =-1 ){
String name = record. substring
(0, separatorindex );
String number = record. substring
(Separatorindex + record_separator.length ());
Phonebook. Put (name, number );
}
}
}
In. Close ();
}
}
Catch (exception e ){
E. printstacktrace ();
}
}

/**
* Write the phone entries to file
*/
Private void writerecord ()
{
File recordfile;
Try {
Recordfile = new file (record_file );
Fileoutputstream out = new fileoutputstream (record_file );
Printstream P = new printstream (out );

Enumeration E = phonebook. Keys ();
While (E. hasmoreelements ()){
String name = (string) E. nextelement ();
String number = (string) Phonebook. Get (name );
String newrecord = Name + record_separator + number;
P. println (newrecord );
}
P. Close ();
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
}

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////

Phonebookcontroller. Java

/*
* Created on Nov 23,200 5
*/

/**
* @ Author TNG
* Controller for the phone book application
* No sophisticated error handling is implemented
*/

Public class phonebookcontroller {
/*
* Attributes transformed from UML by RSA:
*/

// Phonebookmodel and phonebookview are generated by RSA
// To represent the Association Relationship
Private phonebookmodel;
Private phonebookview;

/*
* Add my own attributes
*/

// The following are some commands that initiated
// User defined selection
Public static string start_command = "start ";
Public static string quit_command = "quit ";
Public static string add_command = "add ";
Public static string search_command = "Search ";

// A private field used to track user defined input of Person Name
Private string name;

/**
* Operations transformed from UML by RSA
*/

/**
* Called by phonebookview to your y user has input
* @ Param userinput
*/
Public void userhasinput (string userinput ){
String currentstate = phonebookmodel. getstate ();

If (userinput! = NULL & userinput. Length ()! = 0 ){
If (currentstate. Equals (phonebookmodel. idle_state )){
If (userinput. Equals (add_command )){
Phonebookmodel. setstate
(Phonebookmodel. add_name_state );
}
Else if (userinput. Equals (search_command )){
Phonebookmodel. setstate
(Phonebookmodel. search_state );
}
Else if (userinput. Equals (quit_command )){
Phonebookmodel. saveall ();
Phonebookmodel. setstate
(Phonebookmodel. exit_state );
}
Else {
Phonebookmodel. setstate
(Phonebookmodel. error_state );
}
}
Else if (currentstate. equals
(Phonebookmodel. add_name_state )){
Name = userinput;
Phonebookmodel. setstate
(Phonebookmodel. add_number_state );
}
Else if (currentstate. equals
(Phonebookmodel. add_number_state )){
Phonebookmodel. addanentry (name, userinput );
Phonebookmodel. setstate (phonebookmodel. idle_state );
}
Else if (currentstate. Equals (phonebookmodel. search_state )){
Phonebookmodel. searchphonenumber (userinput );
Phonebookmodel. setstate
(Phonebookmodel. search_result_state );
}
Else if (currentstate. equals
(Phonebookmodel. search_result_state) |
Currentstate. equals
(Phonebookmodel. error_state )){
If (userinput. Equals (start_command )){
Phonebookmodel. setstate
(Phonebookmodel. idle_state );
}
Else if (userinput. Equals (quit_command )){
Phonebookmodel. saveall ();
Phonebookmodel. setstate
(Phonebookmodel. exit_state );
}
Else {
Phonebookmodel. setstate
(Phonebookmodel. error_state );
}
}
}
Else {
Phonebookmodel. setstate (phonebookmodel. error_state );
}
}

/**
* Start the application
*/
Public void start (){
Phonebookmodel. setstate (phonebookmodel. idle_state );
}

/**
* Add my own operations
*/

/**
* Constructor
*/
Public phonebookcontroller (){
Phonebookview = new phonebookview (this );
Phonebookmodel = new phonebookmodel (phonebookview );
}

/**
* Main
* @ Param ARGs
*/
Public static void main (string [] ARGs)
{
Phonebookcontroller controller = new phonebookcontroller ();
Controller. Start ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.