Created
February 2, 2016 03:03
-
-
Save connlark/4e1614e175ba991bd543 to your computer and use it in GitHub Desktop.
Updated 2/1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by 20160118 on 1/25/2016. | |
*/ | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
public class gui1 extends JPanel { | |
String title; | |
JButton addSongButton, deleteSongButton; | |
JTextField songName, artistName; | |
JLabel songNameLabel, artistNameLabel; | |
GridBagConstraints gbc = new GridBagConstraints(); | |
public gui1(String title){ | |
this.title = title; | |
setLayout(new GridBagLayout()); | |
songNameLabel = new JLabel(); | |
artistNameLabel = new JLabel(); | |
addSongButton = new JButton("Add Song"); | |
gbc.gridx = 1; | |
gbc.gridy = 6; | |
gbc.gridwidth = 3; | |
add(addSongButton, gbc); | |
songName = new JTextField("Song Name", 15); | |
gbc.gridx = 0; | |
gbc.gridy = 2; | |
add(songName,gbc); | |
artistName = new JTextField("Artist Name", 15); | |
gbc.gridx = 6; | |
gbc.gridy = 2; | |
songName.addMouseListener(new MouseAdapter(){ | |
@Override | |
public void mouseClicked(MouseEvent e){ | |
songName.setText(""); | |
} | |
}); | |
add(artistName,gbc); | |
// add BUTTON | |
addSongButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String name1 = songName.getText(); | |
songNameLabel.setText("Song Added " + name1); | |
String artist1 = artistName.getText(); | |
artistNameLabel.setText("Artist Added " + artist1); | |
Song s = new Song(name1, artist1); | |
System.out.println(s.getTitle()); | |
System.out.println(s.getArtist()); | |
} | |
}); | |
//delete button | |
} | |
public String getTitle() { | |
return title; | |
} | |
public static void main(String[] args) { | |
gui1 g = new gui1("dtitle"); | |
JFrame jf = new JFrame(); | |
jf.setTitle(g.getTitle()); | |
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
jf.add(g); | |
jf.pack(); //instead of jf.setSize() --> it automatically sizes the frame | |
jf.setVisible(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Connor on 1/21/16. | |
*/ | |
public interface Information { | |
public int getLength(); | |
public String getName(); | |
} | |
//TODO: this less than other == -1 this greater than other == 1 else 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(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; | |
} | |
//overriding interface methods | |
@Override | |
public int getLength() { | |
return songs.size() + playlists.size(); | |
} | |
@Override | |
public String getName() { | |
return 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) { | |
for (int i = 0; i < 20; i++) { | |
System.out.println(genString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.getName() + " " + s.getLength() + "\n"; | |
} | |
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 + "\nArtist: " + artist + "\nGenre: " + genre + "\nLength: " + length; | |
} | |
//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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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