Skip to content

Instantly share code, notes, and snippets.

@connlark
Created February 3, 2016 03:48
Show Gist options
  • Save connlark/f5bedfd02b20b1b4f522 to your computer and use it in GitHub Desktop.
Save connlark/f5bedfd02b20b1b4f522 to your computer and use it in GitHub Desktop.
dis is good sheet
/**
* Created by Connor on 1/21/16.
*/
public interface Information {
int getLength();
String getName();
int comparable(Media m);
}
import javax.swing.*;
/**
* Created by Connor on 1/19/16.
*/
class InputDialog {
public static String getInput(String prompt){
return JOptionPane.showInputDialog(null, prompt);
}
public static void showDialog(String in){
JOptionPane.showMessageDialog(null,in); // pop up dialog
}
}
import java.util.ArrayList;
import java.util.Random;
/**
* Created by Connor on 1/20/16.
*/
public class Media extends InputDialog implements Information{
private ArrayList<Song> songs;
private ArrayList<Playlist> playlists;
private String owner;
private static Random random = new Random();
//constructors
public Media(){
songs = new ArrayList<Song>();
playlists = new ArrayList<Playlist>();
owner = genString();
}
public Media(String name){
songs = new ArrayList<Song>();
playlists = new ArrayList<Playlist>();
owner = name;
}
public Media(ArrayList<Song> songs, ArrayList<Playlist> playlists, String owner) {
this.songs = songs;
this.playlists = playlists;
this.owner = owner;
}
//adding to media
public void addSong(Song foo){
songs.add(foo);
}
public void addPlaylist(Playlist list){
playlists.add(list);
}
//random string generator for testing
public static String genString(){
String out = "";
int length = random.nextInt(9) + 3;
while (out.length() != length){
out += Character.toString((char) (random.nextInt((122 - 97) + 1) + 97));
}
return out;
}
public ArrayList<Song> getSongs() {
return songs;
}
public Song getSongFromToString(String str){
for (int i = 0; i < songs.size(); i++) {
if (songs.get(i).toString().equals(str)){
return songs.get(i);
}
}
return null;
}
//overriding interface methods
@Override
public int getLength() {
return songs.size();
}
@Override
public String getName() {
return owner;
}
@Override
public int comparable(Media m) {
if (songs.size() > m.songs.size())
return -1;
else if (songs.size() < m.songs.size())
return 1;
else if (songs.size() == m.songs.size())
return 0;
throw new IllegalArgumentException("comparable error :(");
}
public ArrayList<Playlist> getPlaylists() {
return playlists;
}
@Override
public String toString() {
return "Media{" +
"songs=" + songs +
", playlists=" + playlists +
", owner='" + owner + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Media media = (Media) o;
if (songs != null ? !songs.equals(media.songs) : media.songs != null) return false;
if (playlists != null ? !playlists.equals(media.playlists) : media.playlists != null) return false;
return owner != null ? owner.equals(media.owner) : media.owner == null;
}
public static void main(String[] args) {
Song song1 = new Song("Arun Fun","kk",166,"Rock n Roll");
Song song2 = new Song();
for (int i = 0; i < 20; i++) {
System.out.println(genString());
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
/**
* Created by Connor on 1/20/16.
*/
public class Playlist extends Media implements Information {
private ArrayList<Song> songs;
private int numberOfSongs = 0;
private int duration;
private String name;
//constructors
public Playlist(){
songs = new ArrayList<Song>();
numberOfSongs = songs.size();
name = super.genString();
super.addPlaylist(this);
}
public Playlist(String name){
songs = new ArrayList<Song>();
numberOfSongs = songs.size();
this.name = name;
super.addPlaylist(this);
}
public Playlist(ArrayList<Song> songs) {
this.songs = songs;
this.numberOfSongs = songs.size();
super.addPlaylist(this);
}
@Override
public String toString() {
if (songs.size() == 0) return "Empty Playlist";
String out = "";
for (Song s:songs) {
out += " | " + s.toString();
}
return out;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Playlist playlist = (Playlist) o;
if (numberOfSongs != playlist.numberOfSongs) return false;
if (duration != playlist.duration) return false;
if (!songs.equals(playlist.songs)) return false;
if(!name.equals(((Playlist) o).getName())) return false;
for (int i = 0; i < songs.size(); i++) {
if (!(songs.get(i).equals(((Playlist) o).getSongs().get(i))))
return false;
}
return true;
}
public void addToPlaylist(Song song){
numberOfSongs++;
songs.add(song);
}
public ArrayList<Song> getSongs(){
numberOfSongs = this.songs.size();
return songs;
}
public void sortAlpha(){
ArrayList<Song> foo = new ArrayList<Song>();
String[] titles = new String[songs.size()];
for (int i = 0; i < songs.size(); i++) {
titles[i] = songs.get(i).getTitle();
}
Arrays.sort(titles);
for (int i = 0; i < songs.size(); i++) {
for (int j = 0; j < songs.size(); j++) {
if (titles[i].equals(songs.get(j).getTitle())){
foo.add(songs.get(j));
}
}
}
songs = foo;
}
public void sortByDuration() {
ArrayList<Song> foo = new ArrayList<Song>();
int[] size = new int[songs.size()];
for (int i = 0; i < songs.size(); i++) {
size[i] = songs.get(i).getLength();
}
Arrays.sort(size);
for (int i = 0; i < songs.size(); i++) {
for (int j = 0; j < songs.size(); j++) {
if (size[i] == songs.get(j).getLength()){
foo.add(songs.get(j));
}
}
}
songs = foo;
}
public void setSongs(ArrayList<Song> songs) {
this.songs = songs;
}
public int getNumberOfSongs() {
return numberOfSongs;
}
public void setNumberOfSongs(int numberOfSongs) {
this.numberOfSongs = numberOfSongs;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int removeSong(Object o){
if (!(o instanceof Song))
showDialog("Cheeky Error: for a song m8 nuttin else");
if (!songs.contains(o)) {
showDialog("Error: song not found");
return Integer.parseInt(null);
}
for (int i = 0; i < songs.size(); i++) {
if (o.equals(songs.get(i))){
songs.remove(i);
System.out.println("Song:\n\n" + o.toString() + "\n\nRemoved!");
return i;
}
}
showDialog("Error: song not found");
return Integer.parseInt(null);
}
public static void main(String[] args) {
Playlist list = new Playlist();
for (int i = 0; i < 1000; i++) {
Song foo = new Song();
list.addToPlaylist(foo);
}
list.sortByDuration();
System.out.println(list.toString());
list.removeSong(list.getSongs().get(2));
Song foo = new Song();
System.out.println(foo.getName());
}
}
/**
* Created by 20160118 on 1/25/2016.
*/
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
public class Simulator extends JPanel{
Media media = new Media();
String title;
JButton addSongButton;
JTextField songName, artistName, genreName, songLength;
JLabel songNameLabel, artistNameLabel, songLabel;
JFrame defaultFrame;
Container contentPane;
JTextArea defaultDisplay;
public Simulator(){
defaultFrame = new JFrame();
defaultFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = defaultFrame.getContentPane();
JButton songButton = new JButton("Add Song!");
JButton playlistCreateButton = new JButton("Create Playlist");
JButton viewPlaylistsButton = new JButton("View Playlists");
songButton.setBackground(Color.CYAN);
songButton.setOpaque(true);
playlistCreateButton.setBackground(Color.blue);
playlistCreateButton.setOpaque(true);
viewPlaylistsButton.setBackground(Color.blue);
viewPlaylistsButton.setOpaque(true);
songButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addSong();
}
});
playlistCreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pList();
}
});
viewPlaylistsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
viewPlaylists();
}
});
defaultDisplay = new JTextArea(16, 58);
defaultDisplay.setText(getSongList());
defaultDisplay.setBackground(Color.gray);
defaultDisplay.setOpaque(false);
defaultDisplay.setEditable(false); // set textArea non-editable
JScrollPane scroll = new JScrollPane(defaultDisplay);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
songNameLabel = new JLabel("Your Songs: ");
Font font = new Font("Courier", Font.BOLD,25);
songNameLabel.setFont(font);
songNameLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
songNameLabel.setBackground(Color.LIGHT_GRAY);
songNameLabel.setOpaque(true);
JPanel p = new JPanel(new BorderLayout());
p.add(playlistCreateButton,BorderLayout.WEST);
p.add(songButton,BorderLayout.CENTER);
p.add(viewPlaylistsButton,BorderLayout.EAST);
scroll.setOpaque(true);
contentPane.add(scroll,BorderLayout.CENTER);
//contentPane.add(button1, BorderLayout.SOUTH);
//contentPane.add(button, BorderLayout.SOUTH);
//contentPane.add(button2,BorderLayout.SOUTH);
contentPane.add(songNameLabel,BorderLayout.PAGE_START);
contentPane.add(p,BorderLayout.PAGE_END);
defaultFrame.pack();
defaultFrame.setVisible(true);
defaultFrame.setLocationRelativeTo(null);
}
public void pList(){
if (media.getSongs().size() == 0){
JOptionPane.showMessageDialog(null,"Add Songs First!");
return;
}
JFrame frame = new JFrame(title);
JPanel panel = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Add Songs To Playlist:");
panel.setBorder(border);
final ArrayList<JCheckBox> boxes = new ArrayList<JCheckBox>();
for (int i = 0; i < media.getLength(); i++) {
JCheckBox check = new JCheckBox(media.getSongs().get(i).toString());
boxes.add(check);
panel.add(check);
}
JButton button = new JButton("Create");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Playlist list = new Playlist(JOptionPane.showInputDialog(null, "Name the playlist"));
for (int i = 0; i < boxes.size(); i++) {
if (boxes.get(i).isSelected()){
Song song;
song = media.getSongFromToString(boxes.get(i).getText());
list.addToPlaylist(song);
}
}
media.addPlaylist(list);
}
});
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public void addSong(){
final JFrame frame = new JFrame();
final JPanel panel = new JPanel(new GridLayout(5, 1));
setLayout(new GridBagLayout());
songNameLabel = new JLabel();
artistNameLabel = new JLabel();
addSongButton = new JButton("Add Song");
songName = new JTextField("Song Name", 15);
panel.add(songName);
artistName = new JTextField("Artist Name", 15);
panel.add(artistName);
genreName = new JTextField("Genre Name", 15);
panel.add(genreName);
songLength = new JTextField("Song Length", 15);
panel.add(songLength);
// add BUTTON
addSongButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = songName.getText();
String artist = artistName.getText();
String genre = genreName.getText();
int length = -1;
try {
length = Integer.parseInt(songLength.getText());
}
catch (NumberFormatException exc){
JOptionPane.showMessageDialog(null,"Please enter a number for length");
return;
}
songNameLabel.setText("Song Added: " + name);
panel.add(songNameLabel);
songNameLabel.updateUI();
Song s = new Song(name, artist,length,genre);
media.addSong(s);
defaultDisplay.setText(getSongList());
defaultDisplay.updateUI();
}
});
//making text go to "" in text boxes after clicked (not sure of a better solution :( )
songName.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
songName.setText("");
}
});
artistName.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
artistName.setText("");
}
});
genreName.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
genreName.setText("");
}
});
songLength.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
songLength.setText("");
}
});
Container contentPane = frame.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(addSongButton, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public String getTitle() {
return title;
}
public String getSongList(){
String out = "";
ArrayList<Song> songs = media.getSongs();
for (int i = 0; i < media.getSongs().size(); i++) {
out += songs.get(i).toString();
}
return out;
}
public String getPlaylists(){
String out = "";
for (int i = 0; i < media.getPlaylists().size(); i++) {
out += "Playlist: " + media.getPlaylists().get(i).getName() + "\n";
out += media.getPlaylists().get(i).toString() + "\n";
}
return out;
}
public void viewPlaylists(){
if (media.getSongs().size() == 0){
JOptionPane.showMessageDialog(null,"Make A Playlist First!");
return;
}
final JTextField textPane = new JTextField();
textPane.setColumns(1);
textPane.setText( getPlaylists() );
JButton nameButton = new JButton("Sort by name");
JButton lengthButton = new JButton("Sort by song length");
nameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < media.getPlaylists().size(); i++) {
media.getPlaylists().get(i).sortAlpha();
}
textPane.setText( getPlaylists() );
textPane.updateUI();
}
});
lengthButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < media.getPlaylists().size(); i++) {
media.getPlaylists().get(i).sortByDuration();
}
textPane.setText( getPlaylists() );
textPane.updateUI();
}
});
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.add(textPane, BorderLayout.PAGE_START);
contentPane.add(lengthButton,BorderLayout.EAST );
contentPane.add(nameButton, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args) {
Simulator g = new Simulator();
Song song1 = new Song("Arun Fun","konswela",166,"Rock n Pop!");
Song song2 = new Song("Marun Fun","konswela",167,"Rock n Pop!");
g.media.addSong(song1);
g.media.addSong(song2);
}
}
/**
* Created by Connor on 1/20/16.
*/
public class Song extends Media implements Information {
private String title;
private String artist;
private String genre;
private int length;
//constructors
public Song() {
this.title = genString();
this.artist = genString();
this.length = (int) ((Math.random() * 90) + 60);
this.genre = genString();
}
public Song(String title, String artist, int length, String genre) {
this.title = title;
this.artist = artist;
this.length = length;
this.genre = genre;
super.addSong(this);
}
public Song(String name1, String artist1) {
this.title = name1;
this.artist = artist1;
this.length = (int) ((Math.random() * 90) + 60);
this.genre = genString();
}
@Override
public String toString(){
return "Title: " + title + " Artist: " + artist + " Genre: " + genre + " Length: " + length + "\n";
}
//getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
@Override
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
@Override
public String getName(){
return title;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Song song = (Song) o;
if (length != song.length) return false;
if (title != null ? !title.equals(song.title) : song.title != null) return false;
if (artist != null ? !artist.equals(song.artist) : song.artist != null) return false;
return genre != null ? genre.equals(song.genre) : song.genre == null;
}
}
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import static org.junit.Assert.*;
/**
* The test class BlockUnitTest
*
* @author Bushell
* @version 1.0
*/
public class Tester
{
private static final double EPSILON = 1e-14;
/**
* Default constructor for test class BlockTester
*/
public Tester()
{
}
/**
* Sets up the test fixture.
*
*/
@Before
public void setUp()
{
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@After
public void tearDown()
{
}
//Song Class
@Test
public void testSongGettersAndSetters()
{
Song song1 = new Song();
song1.setTitle("Arun Fun");
assertEquals("Arun Fun",song1.getTitle());
song1.setArtist("konswela");
assertEquals("konswela",song1.getArtist());
song1.setGenre("Rock n Roll");
assertEquals("Rock n Roll",song1.getGenre());
song1.setLength(166);
assertEquals(166,song1.getLength());
//assertTrue(block1.getColor().equals("yellow"));
//assertEquals(10.2,block1.getMass(),EPSILON);
}
@Test
public void testSongEquals(){
Song song1 = new Song("Arun Fun","konswela",166,"Rock n Roll");
Song song2 = new Song("Arun Fun","konswela",166,"Rock n Roll");
assertTrue(song1.equals(song2));
Song song3 = new Song("Soem song","idk even",2342332,"Roll and Rock");
Song song4 = new Song("Soem song","idk even",2342332,"Roll and Rock");
assertTrue(song3.equals(song4));
assertFalse(song3.equals(song1));
}
@Test
public void testPlaylistEquals(){
Playlist p1 = new Playlist("hey");
Playlist p2 = new Playlist("hey");
Playlist p3 = new Playlist("hey");
Playlist p4 = new Playlist("hey");
Song song1 = new Song("Arun Fun","konswela",166,"Rock n Pop!");
Song song2 = new Song("Arun Fun","konswela",167,"Rock n Pop!");
//creating two sets og identical playlists
for (int i = 0; i < 50; i++) {
p1.addToPlaylist(song1);
p2.addToPlaylist(song1);
p3.addToPlaylist(song2);
p4.addToPlaylist(song2);
}
//turns of sout cuz this method is loud
System.setOut(new PrintStream(new OutputStream() {
@Override public void write(int b) throws IOException {}
}));
p4.removeSong(song2);
assertTrue(p1.equals(p2));
assertFalse(p1.equals(p3));
assertFalse(p2.equals(p4));
assertFalse(p3.equals(p4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment