I have been working on this for a bit, and I have made some great strides. However, there are just a few things bugging me that I can't seem to figure out. This is an assignment, and I don't expect anyone to toss out an entire program for me. However, help would be greatly appreciate it. If you stopped by and helped, thank you for checking the post out and helping. If not, thanks for checking out the post; maybe it helped you.
I am using a binary tree to sort through a file that has an artist, cd title, favorite track, number of tracks, and cd cost. A sample from the file is :
Cage the Elephant, Cage the Elephant, Aint No Rest for the Wicked, 11, 9.99,
1. I used a comma delimiter to read in from my file into a binary tree.
It works, but I had to put a comma at the end of the lines, which is not allowed.
what I mean is this:
it is supposed to be like this:
Cage the Elephant, Cage the Elephant, Aint No Rest for the Wicked, 11, 9.99
I changed it like this:
Cage the Elephant, Cage the Elephant, Aint No Rest for the Wicked, 11, 9.99,
2. The output puts an extra space in front of the next set of outputs.
what I mean is this:
Artist: Cage the Elephant
CD Title: Cage the Elephant
Favourite Track: Aint No Rest for the Wicked
Numbers Of Tracks: 11
Cost Of CD: 9.99
Artist: Survivor
CD Title: Greatest hits
Favourite Track: Eye of the Tiger
Numbers Of Tracks: 14
Cost Of CD: 11.95
Driver
import java.io.*;
import java.util.*;
public class MainProgVideoStore
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
VideoBinaryTree cdList = new VideoBinaryTree();
int choice;
String group;
try
{
Scanner infile =
new Scanner(new FileReader("c:\\videoDat.txt"));
createCdList(infile, cdList);
displayMenu();
System.out.print("Enter your choice: ");
choice = console.nextInt();
console.nextLine();
System.out.println();
while (choice != 9)
{
switch (choice)
{
case 1: System.out.print("Enter the group: ");
group = console.nextLine();
System.out.println();
if (cdList.cdSearch(group))
System.out.println("Group found.");
else
System.out.println("This CD is not "
+ "in the collection.");
break;
case 2: System.out.print("Enter the group: ");
group = console.nextLine();
System.out.println();
if (cdList.cdSearch(group))
{
if (cdList.isCdAvailable(group))
{
cdList.videoCheckOut(group);
System.out.println("Enjoy your "
+ "CD: " + group);
}
else
System.out.println("Currently "
+ group + " is out "
+ " of stock.");
}
else
System.out.println("The store does "
+ "not carry "
+ group);
break;
case 3: System.out.print("Enter the group: ");
group = console.nextLine();
System.out.println();
if (cdList.cdSearch(group))
{
cdList.videoCheckIn(group);
System.out.println("Thanks for "
+ "returning "
+ group);
}
else
System.out.println("This video is "
+ "not from our store.");
break;
case 4: System.out.print("Enter the group: ");
group = console.nextLine();
System.out.println();
if (cdList.cdSearch(group))
{
if (cdList.isCdAvailable(group))
System.out.println(group + " is "
+ "currently in stock.");
else
System.out.println(group +
" is not in the collection.");
}
else
System.out.println(group + " is "
+ "not from this collection.");
break;
case 5: cdList.cdPrintGroup();
break;
case 6: cdList.inorderTraversal();
break;
default: System.out.println(" Please enter another number.");
}//end switch
displayMenu();
System.out.print("Enter your choice: ");
choice = console.nextInt();
console.nextLine();
System.out.println();
}//end while
}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe.toString());
}
catch (IOException ioe)
{
System.out.println(ioe.toString());
}
}
public static void createCdList(Scanner infile,
VideoBinaryTree cdList)
{
String artist;
String cdGroup;
String favTrack;
String numTracks;
String cdCost;
Video newVideo;
while (infile.hasNext())
{
infile.useDelimiter(",");
artist = infile.next();
cdGroup = infile.next();
favTrack = infile.next();
numTracks = infile.next();
cdCost = infile.next();
infile.nextLine();
newVideo = new Video();
newVideo.setCdInfo(artist, cdGroup, favTrack,
numTracks, cdCost);
cdList.insert(newVideo);
}//end while
}//end createCdList
public static void displayMenu()
{
System.out.println("Select one of the following: ");
System.out.println("1: To check whether a particular "
+ "video is in the store");
System.out.println("2: To check out a video");
System.out.println("3: To check in a video");
System.out.println("4: Check whether a particular "
+ "video is in stock");
System.out.println("5: To print the groups of all "
+ "the videos");
System.out.println("6: To print a list of all "
+ "the videos");
System.out.println("9: To exit");
}
}
Worker:
public class Video implements Cloneable, Comparable
{
//Instance Variables
private String artist1; //variable to store the name
//of the movie
private String cdTitle1; //variable to store the name
//of the star
private String favoriteTrack; //variable to store the name
//of the star
private String numberTracks; //variable to store the name
//of the numbersOfTracks
private String costCD; //variable to store the name
//of the costOfCD
//Default constructor
//Instance variables are initialized to their
//default values.
//Postcondition: artist1 = ""; cdTitle1 = "";
// favoriteTrack= ""; numberTracks = "";
// costCD= "";
public Video()
{
artist1 = "";
cdTitle1 = "";
favoriteTrack= "";
numberTracks = "";
costCD= "";
}
//Constructor with parameters
//Instance variables are set according to the parameters
//Postcondition: artist1 = group; cdTitle1 = cdTitle2;
// favoriteTrack= favouriteTrack; numberTracks = numbersOfTracks;
// costCD= costOfCD;
public Video(String group, String cdTitle2,
String favouriteTrack, String numbersOfTracks,
String costOfCD)
{
setCdInfo(group, cdTitle2, favouriteTrack, numbersOfTracks, costOfCD);
}
//Returns a copy of objects data in store.
//Postcondition: A reference to a clone of video's
// data is returned.
public Object clone()
{
try
{
return super.clone();
}
catch (CloneNotSupportedException e)
{
return null;
}
}
//Method to set the details of a video.
//Instance variables are set according to the
//parameters.
//Postcondition: artist1 = group; cdTitle1 = cdTitle2;
// favoriteTrack= favouriteTrack;
// numberTracks = numbersOfTracks;
// costCD= costOfCD;
public void setCdInfo(String group, String cdTitle2,
String favouriteTrack, String numbersOfTracks,
String costOfCD)
{
artist1 = group;
cdTitle1 = cdTitle2;
favoriteTrack= favouriteTrack;
numberTracks = numbersOfTracks;
costCD= costOfCD;
}
//Method to check the number of copies in stock.
//Postcondition: The value of the instance variable
// copiesInStock is returned.
// public int getNoOfCopiesInStock()
// {
// return copiesInStock;
// }
//Method to check in a video.
//Postcondition: The number of copies in stock is
// incremented by one.
// public void checkIn()
// {
// copiesInStock++;
// }
//Method to rent a video.
//Postcondition: If there is a video in stock, its
// number of copies in stock is
// decremented by one; otherwise,
// an appropriate message is printed.
// public void checkOut()
// {
// if (getNoOfCopiesInStock() > 0)
// copiesInStock--;
// else
// System.out.println("Currently out of stock.");
// }
//Method to print the group of a movie.
public void printGroup()
{
System.out.println("Group: " + artist1);
}
//Method to print the details of a video.
public void printInfo()
{
System.out.println("Artist: " + artist1);
System.out.println("CD Title: " + cdTitle1);
System.out.println("Favourite Track: " + favoriteTrack);
System.out.println("Numbers Of Tracks: " + numberTracks);
System.out.println("Cost Of CD: " + costCD);
System.out.println();
}
//Method to determine whether group is the same as the
//group of the video.
//Postcondition: Returns the value true if group is the
// same as the group of the video,
// false otherwise.
public boolean checkGroup(String group)
{
return(artist1.compareTo(group) == 0);
}
public String getGroup()
{
return artist1;
}
//Method to compare the groups of two videos.
//Postcondition: Returns true if this video's group is
// equal to the group of otherVideo;
// otherwise returns false
public boolean equals(Object otherVideo)
{
Video temp = (Video) otherVideo;
return (artist1.compareTo(temp.artist1) == 0);
}
//Method to compare the groups of two videos.
//Postconditition: Returns a negative value if the
// group of this video is less than
// the group of otherVideo; zero if
// the group of this video is the
// same as the group of otherVideo;
// returns a positive value if the
// group of this video is greater
// than the group of otherVideo
public int compareTo(Object otherVideo)
{
Video temp = (Video) otherVideo;
return (artist1.compareTo(temp.artist1));
}
//Method to return the video info, as a string.
public String toString()
{
String videoInfo;
videoInfo = "Artist: " + artist1 + "\n"
+ "CD Title: " + cdTitle1 + "\n"
+ "Favourite Track: " + favoriteTrack + "\n"
+ "Numbers Of Tracks: " + numberTracks + "\n"
+ "Cost Of CD: " + costCD+ "\n"
+ "\n";
return videoInfo;
}
}
Worker
public class VideoBinaryTree extends BinarySearchTree<Video>
{
//Default constructor
//Postcondition: root = null;
public VideoBinaryTree()
{
super();
}
//Method to search the video list for a
//particular video, specified by the parameter group.
//Postcondition: If the video is found, a reference to
// the node containing the video is returned;
// otherwise, the value null is returned.
private BinaryTreeNode<Video> searchCdList(String group)
{
boolean found = false; //set found to false
BinaryTreeNode<Video> current = null;
Video temp = new Video();
temp.setCdInfo(group, "", "", "", "");
if (root == null) //the tree is empty
System.out.println("Cannot search an empty list. ");
else
{
current = root; //set current point to the root node
//of the binary tree
found = false; //set found to false
while (current != null && !found) //search the tree
if (current.info.equals(temp)) //the item is found
found = true;
else
if (current.info.compareTo(temp) > 0)
current = current.lLink;
else
current = current.rLink;
} //end else
return current;
}//end searchCdList
//Method to search the list to see if a particular
//group, specified by the parameter group, is in the store.
//Postcondition: Returns true if the group is found;
// false otherwise.
public boolean cdSearch(String group)
{
System.out.println("See Programming Exercise 8.");
return false;
}
//Method to determine if the video specified by the
//parameter group is available.
//Postcondition: Returns true if at least one copy of the
// video is available, false otherwise.
public boolean isCdAvailable(String group)
{
System.out.println("See Programming Exercise 8.");
return false;
}
//Method to check in a video returned by a customer.
//The parameter group specifies the video to be checked in.
//Postcondition: If the video returned is from the
// store, its copiesInstock is incremented
// by one; otherwise, an appropriate message
// is output.
public void videoCheckIn(String group)
{
System.out.println("See Programming Exercise 8.");
}
//Method to check out a video, that is, rent a video.
//The parameter group specifies the video to be checked out.
//Postcondition: If a video is available, its copiesInStock
// is decremented by one; otherwise, an
// appropriate message is output.
public void videoCheckOut(String group)
{
BinaryTreeNode<Video> location;
Video temp;
location = searchCdList(group); //search the list
if (location != null)
{
temp = (Video) location.info;
//temp.checkOut();
}
else
System.out.println("The store does not carry "
+ "this video");
}
//Method to update the number of copies of a video
//by adding the value of the parameter num. The
//parameter group specifies the name of the video
//for which the number of copies is to be updated.
//Postcondition: If video is found; then
// copiesInStock = copiesInStock + num;
// otherwise, an appropriate message is
// output.
public void videoUpdateInStock(String group, int num)
{
System.out.println("See Programming Exercise 8.");
}
//Method to reset the number of copies of a video.
//The parameter group specifies the name of the video
//for which the number of copies is to be reset and the
//parameter num specifies the number of copies.
//Postcondition: If video is found, then
// copiesInStock = num;
// otherwise, an appropriate message
// is output.
public void videoSetCopiesInStock(String group, int num)
{
System.out.println("See Programming Exercise 8.");
}
//Method to print the groups of all the videos in stock.
public void cdPrintGroup()
{
inorderGroup(root);
}
//Method to print the groups of all the videos in
//the tree pointed to by p.
private void inorderGroup(BinaryTreeNode<Video> p)
{
Video temp;
if (p != null)
{
inorderGroup(p.lLink);
temp = (Video) p.info;
temp.printGroup();
inorderGroup(p.rLink);
}
}
}
Worker
public class VideoBinaryTree extends BinarySearchTree<Video>
{
//Default constructor
//Postcondition: root = null;
public VideoBinaryTree()
{
super();
}
//Method to search the video list for a
//particular video, specified by the parameter title.
//Postcondition: If the video is found, a reference to
// the node containing the video is returned;
// otherwise, the value null is returned.
private BinaryTreeNode<Video> searchVideoList(String title)
{
boolean found = false; //set found to false
BinaryTreeNode<Video> current = null;
Video temp = new Video();
temp.setCDInfo(title, "", "", "", "");
if (root == null) //the tree is empty
System.out.println("Cannot search an empty list. ");
else
{
current = root; //set current point to the root node
//of the binary tree
found = false; //set found to false
while (current != null && !found) //search the tree
if (current.info.equals(temp)) //the item is found
found = true;
else
if (current.info.compareTo(temp) > 0)
current = current.lLink;
else
current = current.rLink;
} //end else
return current;
}//end searchVideoList
//Method to search the list to see if a particular
//title, specified by the parameter title, is in the store.
//Postcondition: Returns true if the title is found;
// false otherwise.
public boolean videoSearch(String title)
{
System.out.println("See Programming Exercise 8.");
return false;
}
//Method to determine if the video specified by the
//parameter title is available.
//Postcondition: Returns true if at least one copy of the
// video is available, false otherwise.
public boolean isVideoAvailable(String title)
{
System.out.println("See Programming Exercise 8.");
return false;
}
//Method to check out a video, that is, rent a video.
//The parameter title specifies the video to be checked out.
//Postcondition: If a video is available, its copiesInStock
// is decremented by one; otherwise, an
// appropriate message is output.
public void videoCheckOut(String title)
{
BinaryTreeNode<Video> location;
Video temp;
location = searchVideoList(title); //search the list
if (location != null)
{
temp = (Video) location.info;
// temp.checkOut();
}
else
System.out.println("The store does not carry "
+ "this video");
}
//Method to print the titles of all the videos in stock.
public void videoPrintTitle()
{
inorderTitle(root);
}
//Method to print the titles of all the videos in
//the tree pointed to by p.
private void inorderTitle(BinaryTreeNode<Video> p)
{
Video temp;
if (p != null)
{
inorderTitle(p.lLink);
temp = (Video) p.info;
temp.printArtist();
inorderTitle(p.rLink);
}
}
}