2013-12-08 87 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

您的變量的檢查範圍。 – Nishant

回答

0

您創建case 'L': case 'l':if塊內的變量head。這是一個局部變量和外界的這種任何地方,如果塊不可見,尤其是不可見在您嘗試訪問它的下一個case 'I': case 'i':

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

現在,我已經設法解決這個錯誤,我有另一個錯誤。編譯器指示我在Xlint中進行編譯:未選中。我得到5個警告 – user2884984

+0

DVDmenu.java:63:警告:未經檢查]未選中轉換 發現:節點 需要:節點 節點頭= DVDinv.getHead(); – user2884984