首页 > > 详细

讲解Java、Java调试、辅导Java PhoneList class

The PhoneList class:
Please note:
When defining PhoneList FIRST enumerate (list) the data that you will need, in this case:
Name
Phone number
Count (ct data member)
this field is needed to build the list, but is not actually part of the data set that must be saved.
Next focus on the major functionality that your program must perform, i.e.,
A – Add
L – List
C – Change
S – Save
Q – Quit (Not a true “functionality,” but a needed capability so the use can end the program).
File I/O
Linked list manipulation, including moving nodes around
Partial source code listing follows…
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.EOFException;
public class phoneList
{
phoneList()
{
listHead =nul;
}
private staic las PhoneNode
{
PhoneNode()
{
name =new String("Noame");
phonNumbr= ew String(0) 0-00");
ct =0;
linknul;
}
PhoneNode(String fulName, String number)
{
name =fulName;
phonNmber =number;
ct =0;
}
PhoneNode(String fulName, String number, int count)
{
name =fulName;
phonNmber =number;
ct =count;
}
private String name;
phonNumber;
private int ct;
PhoeNodelink
}
private void intialize()
{
try
{
listHead =nul;
PhonNoeptr =nul;
String name;
phon
String lie;
FileRader fr =new FileRader("MaBel.dat"); / *MUST* be in local directory
BufrRadefd=nw BufdRdr(fr
while (true)
{
name =fd.readLine(); / Get he name
if (name = nul)
brak;
phone =fd.readLine(); / Get he telphone number
if (phone = nul)
{
System.out.println("The Mabel.dat file is corupted.");
exi(-1);
}
line =fd.readLine(); / Get he ct value, should be 0, 1, or 2
if (line = nul)
{
System.out.println("The Mabel.dat file is corupted.");
exi(-1);
}
int ct =Integr.parseInt(line);
if (listHead = nul)
{
listHead =new PhoneNode(name.trim(), phone.trim(), ct);
pr =listHad;
}
else
{
ptr.link =new PhoneNode(name.trim(), phone.trim(), ct);
=ptr.lik;
}
}
}
catch (EOFException eof)
{
System.out.println("End of File (EOF) reached");
}
catch (IOException ioe)
{
System.out.println("IO eror: "+ ioe);
}
}
public void menu()
{
System.out.println("Enter option: \n");
A-Ad aphoe umber");
System.out.println("L Loku aphon number");
C-Change e
System.out.println("S Sv Phone List o afile");
Q-Quitadsavelist\n");
}
public void evntLop()
{
InputSreamReader eader =new InputSreamReader(System.in);
BfedRdr consol BuferdRdr();
bolean finshed =false;
initalize();
meu();
while (!finshed)
{
System.out.print("CMD> ");

try
{
String input =console.readLine().toUperCase().trim();
charesose iputchAt(0;
switch (response)
{
case 'A: adPhoneNumber();
break;
case 'L: lokUpPhoneNumber();
break;
case 'C: changePhoneNumber();
break;
case 'S: savePhoneNumbersToFile();
break;
case 'Q: savePhoneNumbersToFile();
finished = true;
break;
default: System.out.println("Unkown comand.\n");
menu();
}
}
catch (IOException e)
{
System.out.println(e);
}
}
}
private PhoneNode findNode(String name) / Is there a better way?
{
PhoneNode ptr =listHead;
blanfoundfa;
while (ptr != nul)
{
if(ptr.name.toUperCase().equals(name)
{
found =true;
break;
}
else
ptr =ptr.link;
}
if (!found)
returnul;
else
return ptr;
}
private PhoneNode listHead; // Is this necessary? Why or why not? Be sure this is understood!
}
Notes:
1. Methods associated with each option shall be provided; for example for option “A” the
addPhoneNumber() method needs to be written…
2. The example, above, is a “structure” that may be helpful for Assignment #5, Question however,
items such as listHead and findNode() are unnecessary since the solution to this problem uses Java
Collections/Containers.
2.1. That said, the overall program approach should be of value.
public class usePhoneList
{
public staic void main(String[] args)
{
PhoneList aPhoneList =new PhoneList();
aPhoneList.evntLop();
}

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!