Results 1 to 1 of 1
  1. #1
    Harley84 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Post Linked List Database

    Hi, I am currently working on a linked list database program which keeps track of which movies are entered into the database, along with the removal of videos and displaying how many copies of that video are available(if in the database) when searched. So far I can add and delete but I want to only increase the count number if I add a video which is already in the database and I'm not quite sure how to accomplish this. In addition I am almost positive that the search method is garbage :confused:. I am trying to use a linked list to accomplish this, here is my code so far.... any and all help would be appreciated.



    import java.util.*;

    public class DB
    {
    private static DBnode head=null; //null means "it points nowhere."
    private static Scanner Scan = new Scanner(System.in);


    public static void main (String[] args)
    {

    //loop which does the checking for the entered commands
    int i = 0;
    while (i <= 1)
    {

    //asks the user to enter the proper command
    System.out.print("Please, enter a command: ");
    String command = Scan.nextLine();
    System.out.println("" + command);

    //the following is all command string comparisons
    if (command.compareTo("add") == 0)
    {
    System.out.print("Enter the movie title to add: ");
    String video = Scan.nextLine();
    int count = 0;
    addb(video);
    }
    else if (command.compareTo("ADD") == 0)
    {
    System.out.print("Enter the movie title to add: ");
    String video = Scan.nextLine();
    int count=0;
    addb(video);
    }
    else if (command.compareTo("SEARCH") == 0)
    {
    System.out.print("Enter the movie title you would like to search: ");
    String video = Scan.nextLine();
    search(video);
    }
    else if (command.compareTo("search") == 0)
    {
    System.out.print("Enter the movie title you would like to search: ");
    String video = Scan.nextLine();
    search(video);
    }
    else if (command.compareTo("delete") == 0)
    {
    System.out.print("Enter the movie title you would like to delete: ");
    String video = Scan.nextLine();
    remove(video);
    }
    else if (command.compareTo("DELETE") == 0)
    {
    System.out.print("Enter the movie title you would like to delete: ");
    String video = Scan.nextLine();
    remove(video);
    }
    else if ( command.compareTo("QUIT") == 0)
    {
    printb();
    System.exit(0);
    }
    else if (command.compareTo("quit") == 0)
    {
    printb();
    System.exit(0);
    }
    else
    {
    System.out.println("Error, not a correct command. Use add, search, or delete.");
    }

    }

    }

    public static void add (String videoName)
    {
    DBnode n = new DBnode(videoName,head);
    head = n;
    }
    public static void addb (String videoName)
    {
    if
    (head==null) add (videoName);
    else
    head.addb (videoName);
    }


    public static void search (String videoName)
    {
    if (head != null)
    head = head.search (videoName);
    }

    public static void remove (String videoName) {

    if (head != null)
    head = head.remove (videoName);
    }

    public static int count () {

    if (head==null) return 0;
    return head.count();
    }

    public static int countv (String v) {

    if (head==null) return 0;
    return head.countv (v);
    }

    public static void print () {

    if (head==null) ;
    else head.print ();
    System.out.println ();
    }

    public static void printb () {


    if (head==null) ;
    else head.printb ();
    System.out.println ();
    }
    }


    public class DBnode
    {

    private String video = "";
    private int count = 0;
    private DBnode next=null;

    public DBnode (String v, DBnode n)
    {
    video = v;
    next = n;
    count = count + 1;
    System.out.println (v+" has been added to the database with "+count+" in stock.");
    }

    public void setVideo (String v)
    {
    video = v;
    }

    public String getVideo ()
    {
    return video;
    }

    public void setnext (DBnode n)
    {
    next = n;
    }

    public DBnode getnext ()
    {
    return next;
    }

    public void print ()
    {
    System.out.println (video);
    if (next==null) ;
    else next.print();
    }

    public void printb ()
    {
    if (next==null) ;
    else next.printb();
    System.out.println (video);
    }

    public void addb (String videoName)
    {
    if (next==null)
    next = new DBnode(videoName,null);
    else
    next.addb(videoName);
    }

    public DBnode search (String videoName)
    {
    if (video.compareTo(videoName)==0)
    return next;
    if (next==null)
    return this;

    return this;
    }

    public DBnode remove (String videoName)
    {
    if (video.compareTo(videoName)==0) return next;
    if (next==null)
    return this;

    next = next.remove (videoName);
    return this;
    }

    public int count ()
    {
    if (next==null)
    return 1;
    return next.count() + 1;
    }

    public int countv (String v)
    {
    int ct = 0;
    if (next != null)
    ct += next.count();
    if (video.compareTo(v)==0)
    ct++;
    return ct;
    }
    }
    Last edited by Harley84; 04-18-2011 at 04:13 PM. Reason: ,,,

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 10:34 AM
  2. Linked List Help
    By tjoney in forum New To Java
    Replies: 3
    Last Post: 02-18-2011, 02:09 AM
  3. Need help using linked list
    By tigertomas in forum New To Java
    Replies: 5
    Last Post: 02-12-2011, 04:22 PM
  4. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 12:15 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •