private void btnPop_Click(object sender, EventArgs e) { if (!sp1.isEmpty()) { CDNode removeCD = sp1.pop(); if (removeCD != null) { lblOutput.Text = removeCD.Artist + " - " + removeCD.AlbumTitle + " has been removed from the spindle"; int remove = lstRemoveSpecificCD.TopIndex; lstRemoveSpecificCD.Items.RemoveAt(remove); } else { lblOutput.Text = "There are no CDs on the spindle"; string error = DateTime.Now + " - There are no CDs on the spindle" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } else { lblOutput.Text = "There are no CDs on the spindle"; string error = DateTime.Now + " - There are no CDs on the spindle" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } }
private void btnPeekSpindle_Click(object sender, EventArgs e) { hidePanels(); pnlPeekSpindle.Show(); clearOutput(); clearTextfields(); if (!sp1.isEmpty()) { CDNode peekCD = sp1.peek(); if (peekCD != null) { lblPeek.Text = "Artist: " + peekCD.Artist + " - Album: " + peekCD.AlbumTitle + " - No. of Tracks: " + peekCD.NoOfTracks + " - Duration: " + peekCD.Duration; } else { lblPeek.Text = "There are no CDs on the spindle"; string error = DateTime.Now + " - There are no CDs on the spindle" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } else { lblPeek.Text = "There are no CDs on the spindle"; string error = DateTime.Now + " - There are no CDs on the spindle" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } }
}//push public CDNode pop() { if (isEmpty()) { return(null); } CDNode removed = top; top = removed.Prev; size--; return(removed); }//pop
}//list public String listAlbumArtist() { if (isEmpty()) { return(" "); } string output = ""; CDNode current = top; while (current != null) { output += current.Artist + " - " + current.AlbumTitle + "\n"; current = current.Prev; } return(output); }//listAlbumArtist
}//peek public String list() { if (isEmpty()) { return("Spindle is empty"); } string output = "Contents of Spindle\nCDs: " + size + "\n"; CDNode current = top; while (current != null) { output += "Artist: " + current.Artist + " - Album: " + current.AlbumTitle + " - No. of Tracks: " + current.NoOfTracks + " - Duration: " + current.Duration + "\n"; current = current.Prev; } return(output); }//list
}//count public bool contains(String find) { if (isEmpty()) { return(false); } CDNode current = top; while (current != null) { if (find == current.Artist) { return(true); } current = current.Prev; } return(false); }//contains
}//contains public bool play(String artist, int track) { if (isEmpty()) { return(false); } CDNode current = top; while (current != null) { if (artist == current.Artist && track <= current.NoOfTracks) { return(true); } current = current.Prev; } return(false); }//play
private void btnPush_Click(object sender, EventArgs e) { CDNode newCD = new CDNode(); if (sp1.getSize() < sp1.getCapacity()) { if (newCD != null && txtAddArtist.Text != "" && txtAddAlbum.Text != "" && txtAddNoOfTracks.Text != "" && txtAddDuration.Text != "") { try { if (Convert.ToDouble(txtAddDuration.Text) > 0 && Convert.ToInt32(txtAddNoOfTracks.Text) > 0) { try { newCD.Artist = txtAddArtist.Text; newCD.AlbumTitle = txtAddAlbum.Text; try { newCD.Duration = Convert.ToDouble(txtAddDuration.Text); try { newCD.NoOfTracks = Convert.ToInt32(txtAddNoOfTracks.Text); try { sp1.push(newCD); lstRemoveSpecificCD.Items.Insert(0, newCD.Artist + " - " + newCD.AlbumTitle); lblOutput.Text = newCD.Artist + " - " + newCD.AlbumTitle + " has been added to the spindle"; clearTextfields(); } catch { lblOutput.Text = "Unable to add CD to spindle, make sure to enter details into the text fields"; string error = DateTime.Now + " - Unable to add CD to spindle, make sure to enter details into the text fields" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } catch (FormatException fex) { MessageBox.Show("Error: Please enter a valid number for number of tracks"); string error = DateTime.Now + " - " + fex.Message + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } catch (FormatException fex) { MessageBox.Show("Error: Please enter a valid format for duration in minutes"); string error = DateTime.Now + " - " + fex.Message + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } catch (Exception ex) { MessageBox.Show("Error: \n" + ex.Message); string error = DateTime.Now + " - " + ex.Message + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } else { lblOutput.Text = "Make sure to to use a number greater than 0 for duration and number of tracks"; string error = DateTime.Now + " - Make sure to to use a number greater than 0 for duration and number of tracks" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } catch (FormatException fex) { MessageBox.Show("Error: Please enter a valid format for duration and number of tracks"); string error = DateTime.Now + " - " + fex.Message + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } else { lblOutput.Text = "Unable to add CD to the spindle, make sure to enter details into the text fields"; string error = DateTime.Now + " - Unable to add CD to the spindle, make sure to enter details into the text fields" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } else { lblOutput.Text = "The spindle is full, unable to add any more CDs"; string error = DateTime.Now + " - The spindle is full, unable to add any more CDs" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } }
private void btnRemoveSpecific_Click(object sender, EventArgs e) { if (!sp1.isEmpty()) { if (lstRemoveSpecificCD.SelectedIndex != -1) { int count = 0; try { int cd = Convert.ToInt32(lstRemoveSpecificCD.SelectedIndex + 1); if (sp1.count() != 0 && cd <= sp1.count()) { for (int i = 0; i < cd - 1; i++) { CDNode popped = sp1.pop(); sp2.push(popped); count++; } sp1.pop(); for (int x = 0; x < count; x++) { CDNode popped = sp2.pop(); sp1.push(popped); } lblOutput.Text = lstRemoveSpecificCD.SelectedItem + " has been removed from the spindle"; lstRemoveSpecificCD.Items.Remove(lstRemoveSpecificCD.SelectedItem); clearTextfields(); } else { lblOutput.Text = "This CD is not on the spindle to be removed"; string error = DateTime.Now + " - This CD is not on the spindle to be removed" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } catch (FormatException fex) { MessageBox.Show("Error: Please enter a number less than or equal to the total number of CDs on the spindle"); string error = DateTime.Now + " - " + fex.Message + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } catch (Exception ex) { MessageBox.Show("Error: \n" + ex.Message); string error = DateTime.Now + " - " + ex.Message + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } else { lblOutput.Text = "Please select an item from the listbox to remove"; string error = DateTime.Now + " - Please select an item from the listbox to remove" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } } else { lblOutput.Text = "There are no CDs on the spindle"; string error = DateTime.Now + " - There are no CDs on the spindle" + Environment.NewLine; File.AppendAllText(@"..\..\ErrorLog.txt", error); } }
}//isEmpty public void push(CDNode cd) { cd.Prev = top; top = cd; size++; }//push
public Spindle() { top = null; size = 0; capacity = 10; }