www.gusucode.com > MATLAB2008应用程序接口编程技术源码程序 > MATLAB2008应用程序接口编程技术源码程序/code/第10章/10.7/phonebook.m

    function phonebook(varargin)
pbname = 'myphone'; % name of data dictionary
%%%处理原始数据文件的名称和路径
if ispc
datadir = char(java.lang.System.getProperty('user.dir'));
else
datadir = getenv('HOME');
end;
pbname = fullfile(datadir, pbname)
%%%如果不存在文件,创建该文件
if ~exist(pbname)
disp(sprintf('Data file %s does not exist.', pbname));
r = input('Create a new phone book (yn)','s');
if r == 'y',
try
FOS = java.io.FileOutputStream(pbname);
FOS.close
catch
error(sprintf('Failed to create %s', pbname));
end;
else
return;
end;
end;
pb_htable = java.util.Properties;
try
FIS = java.io.FileInputStream(pbname);
Catch
error(sprintf('Failed to open %s for reading.', pbname));
end;
pb_htable.load(FIS);
FIS.close;

while 1
%%%显示用户选择的选项
disp ' '
disp ' Phonebook Menu'
disp ' '
disp ' 1. Look up a phone number'
disp ' 2. Add an entry to the phone book'
disp ' 3. Remove an entry from the phone book'
disp ' 4. Change the contents of an entry in the phone book'
disp ' 5. Display entire contents of the phone book'
disp ' 6. Exit this program'
disp ' '
%%%获取用户选择的选项
s = input('Please type the number for a menu selection ','s');
switch s
case '1',
name = input('Enter the name to look up ','s');
if isempty(name)
disp 'No name entered'
else
%%%调用查看函数
pb_lookup(pb_htable, name);
end;
case '2',
%%%添加调用函数
pb_add(pb_htable);
case '3',
name=input('Enter the name of the entry to remove ', 's');
if isempty(name)
disp 'No name entered'
else
%%%调用删除函数
        pb_remove(pb_htable, name);
end;
case '4',
    name=input('Enter the name of the entry to change ', 's');
if isempty(name)
disp 'No name entered'
else
%%%调用修改函数
pb_change(pb_htable, name);
end;
case '5',
%%%调用显示列表函数
pb_listall(pb_htable);
case '6',
try
FOS = java.io.FileOutputStream(pbname);
catch
error(sprintf('Failed to open %s for writing.',pbname));
end;
pb_htable.save(FOS,'Data file for phonebook program');
FOS.close;
return;
otherwise
disp 'That selection is not on the menu.'
end;
end;