找不到符号变量头

时间:2013-12-08 19:18:56

标签: java javac

我正在编写一个程序,作为电影商店的销售管理程序。它应该将.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;
    }
}

1 个答案:

答案 0 :(得分:0)

您在head case 'L': case 'l':区块内创建变量if。它是一个局部变量,在此if块之外的任何位置都不可见,尤其是在您尝试访问它的下一个case 'I': case 'i':中不可见:

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