2014-12-04 5 views
-1

주소록 프로그램을 만들고 있는데 사람들을 추가/삭제/찾으려고합니다.이 기능을 위해 스캐너를 구현하는 방법은 무엇입니까?

이 내 연락처 클래스

import java.util.Scanner; 
public class Contacts { 

String name; 
String lastn; 
String phone; 

public Contacts() { 
    Scanner sc = new Scanner (System.in); 
    System.out.println ("Enter the first name >"); 
    String n = sc.next(); 
    System.out.println ("Enter the last name >"); 
    String l = sc.next(); 
    System.out.println ("Enter the phone number (use the format xxx-xxx-xxxx) >"); 
    String p = sc.next(); 
    name = n + " " + l; phone = p; 
} 

public String getName() { return name; } 

public String getPhone() {return phone;} 

public String toString() { 
    String result = name + "\n" + phone; 
    return result; 
} 
} 

이며,이, 내 일이 그래서를 heres 내 메인 클래스

import java.util.Scanner; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.ObjectInput; 
import java.io.PrintWriter; 

public class Main { 
static final String filePath = System.getProperty("user.dir") + "\\src\\files"; 
static final String fileName = "ContactInfo.dat"; 

public static void main(String[] args) { 



    Scanner sc = new Scanner (System.in); 

    BST<String,Contacts> tree1 = new BST <String,Contacts>(); 

    Contacts a1 = new Contacts(); 

    System.out.println (tree1); 

    tree1.insert(a1.getName(), a1); 

    System.out.println (tree1); 

    System.out.println(a1.getName()); 

} 
} 

입니다 제가하는 모든 일의 스캐너를 사용하는 것이 가능하다? 예를 들어. 누군가를 추가하고 싶습니다. 그러나 그 방법을 하나의 변수 a1로 해결했는데, 자동으로 다음 인스턴스에 대해 a2로 해결할 수 있습니까?

기타 스캐너를 사용하여 책에서 누군가를 삭제하는 방법은 무엇입니까? 나는 코드에서 모든 것을 할 수 있지만 분명히 주소록에 이상적이지는 않습니까?

답변

0

귀하의 질문이 명확하지 않다고 생각하지만 이해하는대로 답변 드리겠습니다.

먼저 스캐너 개체가 사용자로부터 입력을받는 것으로 생각하십시오. 사람 추가/삭제는 스캐너와 관련이 없습니다. 주소록에서 a1을 잊어 버리고 싶다면 BST에서 연락처를 가지고 있기 때문에 임시로 사용할 수 있습니다. a2, a3을 가질 필요가 없습니다.

다음은 기본 클래스에 대한 간단한 코드입니다.

import java.util.Scanner; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.ObjectInput; 
import java.io.PrintWriter; 

public class Main { 
static final String filePath = System.getProperty("user.dir") + "\\src\\files"; 
static final String fileName = "ContactInfo.dat"; 

public static void main(String[] args) { 



    Scanner sc = new Scanner (System.in); 

    BST<String,Contacts> tree1 = new BST <String,Contacts>(); 

    for(int i = 1; i <= 5; i++) { 
     Contacts a1 = new Contacts(); 

     System.out.println (tree1); 

     tree1.insert(a1.getName(), a1); 

     System.out.println (tree1); 

     System.out.println(a1.getName()); 
    } 

} 
관련 문제