2013-12-08 4 views
0

저는 영화 가게에서 판매 관리 프로그램으로 일할 프로그램을 작성하고 있습니다. .dat 파일을 DVDmenu.java 파일로 읽어 들이고 case/switch 문을 사용하여 메뉴를 처리합니다. 나는이 프로그램을 작성하고 다시 작성했지만 코드에서 컴파일 오류를 발견 할 수 없다.기호 변수 머리를 찾을 수 없습니다

import java.io.*;  // this includes the input output library files 
import java.util.*; // this includes the utility libraray files 
public class DVDmenu 
{ 
    public static void main(String[] args) 
    { 
     Scanner scanF = null;  // This creates an empty scanner for the .dat system. 
     PrintWriter pw = null;  // This creates a print writer to write info to the .dat 
     String fileIn;    // this creates the file input name of the .dat 
     LinkedList DVDinv = new LinkedList(); // this creates a linked list 

     if(args.length != 1) 
     { 
      System.out.println("USAGE: DVD Menu <input_file>"); 
      System.exit(1); 
     } 

     fileIn = args[0]; 

     try 
     { 
      scanF = new Scanner(new File(fileIn)); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("Error: file \"" + fileIn + "\" does not exist"); 
      System.exit(2); 
     } 

     while(scanF.hasNext()) 
     { 
      String title = scanF.nextLine(); 
      DVD newDVD = new DVD(title); 
      DVDinv.insert(newDVD); 
     }     
     String input = "null"; 

      System.out.println("Type a letter then press enter"); 
     System.out.println("H:help,L:list,I:inquire, A:add, S:sell,Q:quit."); 
     scanF.close(); 
     Scanner key = new Scanner(System.in); 

     do 
     { 

      char in = key.nextLine().charAt(0);; 
      switch(in) 
      { 
        case 'H': 
        case 'h': 
         System.out.println("Enter H for help, L to list, I <movieTitle> to" 
         + " inquire, A <movieTitle> to add, S <movieTitle> to sell, M <amount>" 
         + " <movieTitle> and Q to quit."); 
         break; 

       case 'L': 
       case 'l': 
         if(!DVDinv.isEmpty()) 
         { 
          Node<DVD> head = DVDinv.getHead(); 
          while(head != null) 
          { 
           System.out.println(head.getDVD().getStock() + " Has " 
           + head.getDVD().getTitle() + "copies in stock."); 
           head = head.getNext(); 
          } 
         } 
         break; 

       case 'I': 
       case 'i': 
         String title = input.substring(2); 
         DVD newDVD = new DVD(newDVD); 
         System.out.println(head.getDVD().getTitle() + " has " 
         + head.getDVD().getStock() + " copies in stock."); 
         break; 

       case 'A': 
       case 'a': 
         String title = input.substring(2); 
         DVD newDVD = new DVD(title); 
         DVDinv.insert(newDVD); 
         System.out.println("One Copy of " + head.getDVD().getTitle() + " was added."); 
         break; 

       case 'S': 
       case 's': 
         String title = input.substring(2); 
         DVD newDVD = new DVD(title); 
         DVDinv.delete(newDVD); 
         System.out.println("All copies of " + head.getDVD().getTitle() + " have been sold."); 
         break; 

       case 'Q': 
       case 'q': 
         System.out.println("Thank you for using this system."); 
         break; 
       } 
     }while(in != 'Q' && in != 'q'); 

     try 
     { 
      pw = new PrintWriter (new FileWriter(fileIn)); 
     } 
     catch (IOException e) 
     { 
      System.out.println("General I/O exception: " + e.getMessage()); 
     }   

     Node<DVD> current = DVDinv.getHead(); 
     while(current != null) 
     { 
      for(int i=0; i < current.getDVD().getStock(); i++) 
      { 
       pw.println(current.getDVD().getTitle()); 
      }current = current.getNext(); 
     }pw.close(); 
    } 
} 

public class DVD implements Comparable<DVD> 
{ 
    private String title; // this sets a private string variable for a movie 
    private int Stock; // this sets a private int variable for the amount 

    public DVD(String movieTitle) 
    { 
     title = movieTitle; // this sets the movie title to title 
     Stock = 1;   // this automatically sets the # of copies to 1 
    } 

    public String getTitle() 
    { 
     return title;  // this returns the movie title to the user 
    } 

    public int getStock() 
    { 
     return Stock;  // returns the amount of films are available for sale 
    } 

    public void setStock(int newStock) 
    { 
     Stock = newStock;  // adds inventory amount to available films for sale 
    } 

    public int compareTo(DVD DVDtoCompare) 
    { 
     return title.compareToIgnoreCase(DVDtoCompare.getTitle()); // compares two film title strings to 
     // determine which one is alphabetically above or below one another. 
    } 
} 

public class Node<DVD extends Comparable<DVD>> 
{ 
    private DVD dvd; 
    private Node<DVD> next; 

    public Node(DVD movieTitle) 
    { 
     dvd = movieTitle; 
     next = null; 
    } 

    public void setDVD(DVD newDVD) 
    { 
     dvd = newDVD; 
    } 

    public void setNext(Node<DVD> newNext) 
    { 
     next = newNext; 
    } 

    public DVD getDVD() 
    { 
     return dvd; 
    } 

    public Node<DVD> getNext() 
    { 
     return next; 
    } 
} 
public class LinkedList 
{ 
    private Node<DVD> head; 

    public LinkedList() 
    { 
     head = null; 
    } 

    public Node<DVD> contains(DVD thedvd) 
    { 
     Node<DVD> current = head; 

     while(current != null) 
     { 
      if(thedvd.compareTo(current.getDVD()) == 0) 
      { 
       return current; 
      } 
      else if(thedvd.compareTo(current.getDVD()) < 0) 
      { 
       return null; 
      } 
      else 
      { 
       current = current.getNext(); 
      } 
     } 
     return null; 
    } 

    public void insert(DVD thedvd) 
    { 
     Node<DVD> toAdd = new Node<DVD>(thedvd); 

     if(this.isEmpty()) 
     { 
      head = toAdd; 
     } 
     else 
     { 
      Node<DVD> current = head; 
      Node<DVD> previous = null; 

      while(current != null) 
      { 
       if(thedvd.compareTo(current.getDVD()) == 0) 
       { 
        current.getDVD().setStock(current.getDVD().getStock() + 1); 
        return; 
       } 
       else if(thedvd.compareTo(current.getDVD()) < 0) 
       { 
        toAdd.setNext(current); 
        if(previous != null) 
        { 
         previous.setNext(toAdd); 
        } 
        else 
        { 
         head = toAdd; 
        } 
        return; 
       } 
       else 
       { 
        previous = current; 
        current = current.getNext(); 
       } 
      } 

      previous.setNext(toAdd); 
     } 
    } 

    public DVD delete(DVD thedvd) 
    { 
     if(this.isEmpty()) 
     { 
      return null; 
     } 

     if (thedvd.compareTo(head.getDVD()) == 0) 
     { 
      if(head.getDVD().getStock() == 1) 
      { 
       head = head.getNext(); 
       return thedvd; 
      } 
      else 
      { 
       head.getDVD().setStock(head.getDVD().getStock() - 1); 
       return thedvd; 
      } 
     } 

     Node<DVD> previous = head; 
     Node<DVD> current = head.getNext(); 

     while(current != null) 
     { 
      if(thedvd.compareTo(current.getDVD()) == 0) 
      { 
       if(current.getDVD().getStock() == 1) 
       { 
        previous.setNext(current.getNext()); 
        return thedvd; 
       } 
       else 
       { 
        current.getDVD().setStock(current.getDVD().getStock() - 1); 
       } 
      } 
      else if(thedvd.compareTo(current.getDVD()) < 0) 
      { 
       return null; 
      } 
      else 
      { 
       previous = current; 
       current = current.getNext(); 
      } 
     } 
     return null; 
    } 

    public boolean isEmpty() 
    { 
     return (head==null); 
    } 

    public Node getHead() 
    { 
     return head; 
    } 

    public int getStock(DVD thedvd) 
    { 
     if(this.contains(thedvd) != null) 
     { 
      return ((this.contains(thedvd)).getDVD()).getStock(); 
     } 
     return 0; 
    } 
} 
+0

검사 범위 : 그것은 특히 보이지 당신이 그것을 액세스하려고 다음 case 'I': case 'i':에서 지역 변수 어디서나 차단하면이 외부에서 보이지이다. – Nishant

답변

0

당신은 당신의 case 'L': case 'l':if 블록 내부의 변수 head을 만들 수 있습니다. 당신의 변수의

System.out.println(head.getDVD().getTitle() + " has " 
        + head.getDVD().getStock() + " copies in stock."); 
+0

이제 그 오류를 해결할 수 있었고 다른 오류가 있습니다. 컴파일러가 Xlint에서 컴파일하도록 지시했습니다 : 선택하지 않았습니다. 5 경고가 발생합니다 – user2884984

+0

DVDmenu.java:63 경고 : [확인되지 않음] 선택 취소 된 변환 : 노드 노드 : 노드 노드 head = DVDinv.getHead(); – user2884984

관련 문제