일단 짠 클래스 3개의 소스를 올려 보면,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDApplication.class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class PDApplication {
public static void main(String args[]) {
if (args.length == 0) {
System.err.println("You must provide the name of the file"
+ " that contains the phone directory.");
System.exit(1);
}
PhoneDirectory phoneDirectory = new ArrayBasedPD();
phoneDirectory.loadData(args[0]);
PDUserInterface phoneDirectoryInterface = new PDGUI();
phoneDirectoryInterface.processCommands(phoneDirectory);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PhoneDirectory.class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class ArrayBasedPD implements PhoneDirectory
{
private static final int INITIAL_CAPACITY = 100;
private int capacity = INITIAL_CAPACITY;
private int size = 0;
private DirectoryEntry[] theDirectory =
new DirectoryEntry[capacity];
private String soureName = null;
private boolean modified = false;
public void loadData(String sourceName) {
this.sourceName = sourceName;
try {
BufferedReader in = new BufferedReader(new FileReader(sourceName));
String name;
String numer;
while ((name = in.readLine()) != null) {
if ((number = in.readLine()) == null) {
break;
}
add(name, number);
}
in.close();
}
catch (FileNotFoundException ex) {
return;
}
}
public String addOrChangeEntry(String name, String number) {
String oldNumber = null;
int index = find(name);
if (index > -1) {
oldNumber = theDirectory[index].getNumber();
theDirectory[index].setNumber(number);
}
else {
add(name, number);
}
modified = true;
return oldNumber;
}
public String lookupEntry(String name) {
int index = find(name);
if (index > -1) {
return theDirectory[index].getNumber();
}
else {
return null;
}
}
public void save() {
if (modified) {
try {
PrintWriter out = new PrintWriter(new FileWriter(sourceName));
for (int i = 0; i < size; i++) {
out.println(theDirectory[i].getName());
out.println(theDirectory[i].getNumber());
}
out.close();
modified = false;
}
catch (Exception ex) {
System.err.println("Save of directory failed");
ex.printStackTrace();
System.exit(1);
}
}
}
private int find(String name) {
for (int i = 0; i < size; i++) {
if (theDirectory[i].getName().equals(name)) {
return i;
}
}
return -1;
}
private void add(String name, String number) {
if (size >= capacity) {
reallocate();
}
theDirectory[size] = new DirectoryEntry(name, number);
size++;
}
private void reallocate() {
capacity *= 2;
DirectoryEntry[] newDirectory = new DirectoryEntry[capacity];
System.arraycopy(theDirectory, 0, newDirectory, 0,
theDirectory.length);
theDirectory = newDirectory;
}
public String removeEntry(String name) {
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PDGUI.class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class PDGUI implements PDUserInterface {
private PhoneDirectory theDirectory = null;
public void processCommands(PhoneDirectory thePhoneDirectory) {
String[] commands = {
"Add/Change Entry",
"Look Up Entry",
"Remove Entry",
"Save Directory",
"Exit"};
theDirectory = thePhoneDirectory;
int choice;
do {
choice = JOptionPane.showOptionDialog(null, "Select a Command",
"PhoneDirectory",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
commands, commands[commands.length - 1]);
switch (choice) {
case 0: doAddChangeEntry(); break;
case 1: doLookupEntry(); break;
case 2: doRemoveEntry(); break;
case 3: doSave(); break;
case 4: doSave(); break;
}
}
while (choice < commands.length - 1);
System.exit(0);
}
private void doAddChangeEntry() {
String newName = JOptionPane.showInputDialog("Enter name");
if (newName == null) {
return;
}
String oldNumber = theDirectory.addOrChangeEntry(newName, newNumber);
String message = null;
if (oldNumber == null) {
message = newName + " was added to the directory"
+ "\\nNew number: " + newNumber;
}
else {
message = "Number for " + newName + " was changed "
+ "\\nOld number: " + oldNumber
+ "\\nNew number: " + newNumber;
}
JOptionPane.showMessageDialog(null, message);
}
private void doLookupEntry() {
String theName = JOptionPane.showInputDialog("Enter name");
if (theName == null) {
return;
}
String theNumber = theDirectory.lookupEntry(theName);
String message = null;
if (theNumber != null) {
message = "The number for " + theName + " is " + theNumber;
}
else {
message = theName + " is not listed in the directory";
}
JOptionPane.showMessageDialog(null, message);
}
private void doRemoveEntry() {
}
private void doSave() {
theDirectory.save();
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
이렇게 3클래스입니다. 소스코드를 보면 여기저기서
~~~ cannot be solved to a type 이라는 에러 문구가 뜨네요. 어떻게 해야 좋을까요?
도움 좀 부탁드립니다. 해결 할 방법 없나요? 제가 짠 소스는 아니고 그냥 책 같은데에
나와있는데로 그대로 한건데 에러가 나네요 ㅠㅠ 도와주세요
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.