percent in list view working, moving to util class
This commit is contained in:
parent
9119e30335
commit
122b91ead3
@ -1,16 +1,25 @@
|
||||
package sarsoo.fmframework.drive;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import sarsoo.fmframework.gui.MainMenu;
|
||||
import sarsoo.fmframework.util.Getter;
|
||||
import sarsoo.fmframework.util.Reference;
|
||||
|
||||
public class Driver {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Reference.setUserName("sarsoo");
|
||||
|
||||
MainMenu main = new MainMenu();
|
||||
main.setVisible(true);
|
||||
// String username = JOptionPane.showInputDialog(null, "Enter User-Name");
|
||||
// System.out.println(username);
|
||||
// if (username != null) {
|
||||
Reference.setUserName("sarsoo");
|
||||
// Reference.setUserName(username);
|
||||
|
||||
// System.out.println(Getter.getScrobbles(Reference.getUserName()));
|
||||
|
||||
MainMenu main = new MainMenu();
|
||||
main.setVisible(true);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package sarsoo.fmframework.gui;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
@ -13,23 +14,26 @@ import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingConstants;
|
||||
|
||||
import sarsoo.fmframework.music.Album;
|
||||
import sarsoo.fmframework.music.FMObj;
|
||||
import sarsoo.fmframework.net.Network;
|
||||
import sarsoo.fmframework.util.FMObjList;
|
||||
import sarsoo.fmframework.util.GetObject;
|
||||
import sarsoo.fmframework.util.Getter;
|
||||
import sarsoo.fmframework.util.Reference;
|
||||
|
||||
public class FMObjListView extends JFrame{
|
||||
public class FMObjListView extends JFrame {
|
||||
|
||||
public FMObjListView(FMObjList objects, String title) {
|
||||
super(title);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
setLayout(new GridLayout(objects.size() + 2,0));
|
||||
setLayout(new GridLayout(objects.size() + 2, 0));
|
||||
setResizable(false);
|
||||
// createMenu();
|
||||
|
||||
// createMenu();
|
||||
|
||||
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
|
||||
Font font = new Font("Arial", Font.BOLD, 24);
|
||||
|
||||
JPanel headerLabels = new JPanel();
|
||||
headerLabels.setLayout(new GridLayout(1, 5));
|
||||
@ -38,22 +42,22 @@ public class FMObjListView extends JFrame{
|
||||
headerLabels.add(new JLabel("Total Scrobbles"));
|
||||
headerLabels.add(new JLabel(""));
|
||||
headerLabels.add(new JLabel(""));
|
||||
|
||||
|
||||
add(headerLabels);
|
||||
|
||||
|
||||
int counter;
|
||||
for(counter = 0; counter < objects.size(); counter++) {
|
||||
for (counter = 0; counter < objects.size(); counter++) {
|
||||
FMObj fmObj = objects.get(counter);
|
||||
JLabel name = new JLabel(fmObj.getName());
|
||||
|
||||
|
||||
int playCountString = fmObj.getUserPlayCount();
|
||||
|
||||
|
||||
JLabel userPlays;
|
||||
if(playCountString == 0)
|
||||
userPlays = new JLabel("0");
|
||||
if (playCountString == 0)
|
||||
userPlays = new JLabel("0");
|
||||
else
|
||||
userPlays = new JLabel(Integer.toString(fmObj.getUserPlayCount()));
|
||||
|
||||
|
||||
JLabel plays = new JLabel(numberFormat.format(fmObj.getPlayCount()));
|
||||
JButton openExternal = new JButton("Open Online");
|
||||
openExternal.addActionListener(new ActionListener() {
|
||||
@ -74,19 +78,44 @@ public class FMObjListView extends JFrame{
|
||||
panel.add(plays);
|
||||
panel.add(openInternal);
|
||||
panel.add(openExternal);
|
||||
|
||||
|
||||
add(panel);
|
||||
}
|
||||
|
||||
JPanel info = new JPanel();
|
||||
info.setLayout(new GridLayout(1, 2));
|
||||
|
||||
JLabel totalScrobbles = new JLabel(numberFormat.format(objects.getTotalUserScrobbles()) + " Total Plays");
|
||||
add(totalScrobbles);
|
||||
totalScrobbles.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
totalScrobbles.setFont(font);
|
||||
info.add(totalScrobbles);
|
||||
|
||||
int userScrobbles = Getter.getScrobbles(Reference.getUserName());
|
||||
if (userScrobbles > 0) {
|
||||
JLabel percent = new JLabel();
|
||||
percent.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
|
||||
double plays = (double) objects.getTotalUserScrobbles();
|
||||
double userScrobblesDouble = (double) userScrobbles;
|
||||
|
||||
double percentage = (plays / userScrobblesDouble) * 100;
|
||||
|
||||
if (percentage > 1) {
|
||||
percent.setText(String.format("%.2f%%", percentage));
|
||||
percent.setFont(font);
|
||||
info.add(percent);
|
||||
}
|
||||
}
|
||||
|
||||
add(info);
|
||||
pack();
|
||||
}
|
||||
|
||||
|
||||
private void createMenu() {
|
||||
JMenuBar menuBar = new JMenuBar();
|
||||
|
||||
JMenu editMenu = new JMenu("Edit");
|
||||
|
||||
|
||||
JMenu addMenu = new JMenu("Add");
|
||||
|
||||
// create menu items
|
||||
@ -94,25 +123,24 @@ public class FMObjListView extends JFrame{
|
||||
addAlbum.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
Album album = GetObject.getAlbum();
|
||||
if(album != null) {
|
||||
|
||||
Album album = Getter.getAlbum();
|
||||
if (album != null) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
JMenuItem addTrack = new JMenuItem("Track");
|
||||
|
||||
|
||||
JMenuItem addArtist = new JMenuItem("Artist");
|
||||
|
||||
|
||||
addMenu.add(addAlbum);
|
||||
addMenu.add(addTrack);
|
||||
addMenu.add(addArtist);
|
||||
|
||||
|
||||
editMenu.add(addMenu);
|
||||
menuBar.add(editMenu);
|
||||
|
||||
setJMenuBar(menuBar);
|
||||
setJMenuBar(menuBar);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,8 @@ import javax.swing.JOptionPane;
|
||||
import sarsoo.fmframework.music.Album;
|
||||
import sarsoo.fmframework.music.Artist;
|
||||
import sarsoo.fmframework.music.Track;
|
||||
import sarsoo.fmframework.util.GetObject;
|
||||
import sarsoo.fmframework.util.Getter;
|
||||
import sarsoo.fmframework.util.Reference;
|
||||
|
||||
public class MainMenu extends JFrame {
|
||||
|
||||
@ -21,7 +22,7 @@ public class MainMenu extends JFrame {
|
||||
JButton viewList = new JButton("View List");
|
||||
|
||||
public MainMenu() {
|
||||
super("fmframework");
|
||||
super("fmframework - " + Reference.getUserName());
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLayout(new GridLayout(2, 2));
|
||||
setSize(300, 300);
|
||||
@ -29,7 +30,7 @@ public class MainMenu extends JFrame {
|
||||
|
||||
getAlbum.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
Album album = GetObject.getAlbum();
|
||||
Album album = Getter.getAlbum();
|
||||
if (album != null) {
|
||||
album.view();
|
||||
} else {
|
||||
@ -39,7 +40,7 @@ public class MainMenu extends JFrame {
|
||||
});
|
||||
getArtist.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
Artist artist = GetObject.getArtist();
|
||||
Artist artist = Getter.getArtist();
|
||||
if (artist != null) {
|
||||
artist.view();
|
||||
} else {
|
||||
@ -49,7 +50,7 @@ public class MainMenu extends JFrame {
|
||||
});
|
||||
viewLastTrack.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
Track track = GetObject.getLastTrack();
|
||||
Track track = Getter.getLastTrack();
|
||||
if (track != null) {
|
||||
track.view();
|
||||
} else {
|
||||
|
@ -15,7 +15,7 @@ public class RefListsView extends JFrame {
|
||||
public RefListsView() {
|
||||
super("fmframework");
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
setLayout(new GridLayout(5, 5));
|
||||
setLayout(new GridLayout(3, 2));
|
||||
setSize(500, 500);
|
||||
setResizable(false);
|
||||
|
||||
|
@ -95,8 +95,17 @@ public class Network {
|
||||
|
||||
public static String getLastTrackUrl(String username) {
|
||||
String urlString = String.format(
|
||||
"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s", username,
|
||||
Key.getKey());
|
||||
"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s",
|
||||
username, Key.getKey());
|
||||
System.out.println(urlString);
|
||||
return urlString;
|
||||
}
|
||||
|
||||
public static String getUserInfoUrl(String username) {
|
||||
String urlString = String.format(
|
||||
"http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=%s&api_key=%s",
|
||||
username, Key.getKey());
|
||||
System.out.println(urlString);
|
||||
return urlString;
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ import sarsoo.fmframework.net.Network;
|
||||
import sarsoo.fmframework.net.TestCall;
|
||||
import sarsoo.fmframework.parser.Parser;
|
||||
|
||||
public class GetObject {
|
||||
public class Getter {
|
||||
public static Album getAlbum() {
|
||||
String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name");
|
||||
if (artistName != null) {
|
||||
@ -48,5 +48,16 @@ public class GetObject {
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public static int getScrobbles(String username) {
|
||||
String url = Network.getUserInfoUrl(username);
|
||||
Document doc = Network.getResponse(url);
|
||||
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
|
||||
String scrobbles = doc.getElementsByTagName("playcount").item(0).getTextContent();
|
||||
if(scrobbles != null)
|
||||
return Integer.parseInt(scrobbles);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
24
fmframework/src/sarsoo/fmframework/util/Maths.java
Normal file
24
fmframework/src/sarsoo/fmframework/util/Maths.java
Normal file
@ -0,0 +1,24 @@
|
||||
package sarsoo.fmframework.util;
|
||||
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.SwingConstants;
|
||||
|
||||
import sarsoo.fmframework.music.FMObj;
|
||||
|
||||
public class Maths {
|
||||
public static double getPercentListening(FMObj obj, String username) {
|
||||
|
||||
int userScrobbles = Getter.getScrobbles(Reference.getUserName());
|
||||
double plays = (double) obj.getUserPlayCount();
|
||||
if (userScrobbles > 0 && plays > 0) {
|
||||
|
||||
double userScrobblesDouble = (double) userScrobbles;
|
||||
|
||||
double percentage = (plays / userScrobblesDouble) * 100;
|
||||
|
||||
return percentage;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@ public class Reference {
|
||||
groups.add(getTDE());
|
||||
groups.add(getBPHQ());
|
||||
groups.add(getDre());
|
||||
groups.add(getWu());
|
||||
groups.add(getHopeless());
|
||||
groups.add(getSaturation());
|
||||
}
|
||||
@ -108,5 +109,18 @@ public class Reference {
|
||||
|
||||
return dre;
|
||||
}
|
||||
|
||||
public static FMObjList getWu() {
|
||||
FMObjList wu = new FMObjList("Wu-Tang Clan");
|
||||
|
||||
wu.add(Artist.getArtist("Wu-Tang Clan", Reference.getUserName()));
|
||||
wu.add(Artist.getArtist("GZA/Genius", Reference.getUserName()));
|
||||
wu.add(Artist.getArtist("Ol' Dirty Bastard", Reference.getUserName()));
|
||||
wu.add(Artist.getArtist("Ghostface Killah", Reference.getUserName()));
|
||||
wu.add(Artist.getArtist("Method Man", Reference.getUserName()));
|
||||
wu.add(Artist.getArtist("Raekwon", Reference.getUserName()));
|
||||
|
||||
return wu;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user