percent in list view working, moving to util class

This commit is contained in:
aj 2018-04-02 16:51:45 -07:00
parent 9119e30335
commit 122b91ead3
8 changed files with 137 additions and 41 deletions

View File

@ -1,16 +1,25 @@
package sarsoo.fmframework.drive; package sarsoo.fmframework.drive;
import javax.swing.JOptionPane;
import sarsoo.fmframework.gui.MainMenu; import sarsoo.fmframework.gui.MainMenu;
import sarsoo.fmframework.util.Getter;
import sarsoo.fmframework.util.Reference; import sarsoo.fmframework.util.Reference;
public class Driver { public class Driver {
public static void main(String[] args) { public static void main(String[] args) {
Reference.setUserName("sarsoo"); // String username = JOptionPane.showInputDialog(null, "Enter User-Name");
// System.out.println(username);
// if (username != null) {
Reference.setUserName("sarsoo");
// Reference.setUserName(username);
MainMenu main = new MainMenu(); // System.out.println(Getter.getScrobbles(Reference.getUserName()));
main.setVisible(true);
MainMenu main = new MainMenu();
main.setVisible(true);
// }
} }
} }

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.gui; package sarsoo.fmframework.gui;
import java.awt.Font;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -13,23 +14,26 @@ import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.net.Network; import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.util.FMObjList; 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) { public FMObjListView(FMObjList objects, String title) {
super(title); super(title);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(objects.size() + 2,0)); setLayout(new GridLayout(objects.size() + 2, 0));
setResizable(false); setResizable(false);
// createMenu(); // createMenu();
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
Font font = new Font("Arial", Font.BOLD, 24);
JPanel headerLabels = new JPanel(); JPanel headerLabels = new JPanel();
headerLabels.setLayout(new GridLayout(1, 5)); headerLabels.setLayout(new GridLayout(1, 5));
@ -42,15 +46,15 @@ public class FMObjListView extends JFrame{
add(headerLabels); add(headerLabels);
int counter; int counter;
for(counter = 0; counter < objects.size(); counter++) { for (counter = 0; counter < objects.size(); counter++) {
FMObj fmObj = objects.get(counter); FMObj fmObj = objects.get(counter);
JLabel name = new JLabel(fmObj.getName()); JLabel name = new JLabel(fmObj.getName());
int playCountString = fmObj.getUserPlayCount(); int playCountString = fmObj.getUserPlayCount();
JLabel userPlays; JLabel userPlays;
if(playCountString == 0) if (playCountString == 0)
userPlays = new JLabel("0"); userPlays = new JLabel("0");
else else
userPlays = new JLabel(Integer.toString(fmObj.getUserPlayCount())); userPlays = new JLabel(Integer.toString(fmObj.getUserPlayCount()));
@ -77,8 +81,33 @@ public class FMObjListView extends JFrame{
add(panel); add(panel);
} }
JPanel info = new JPanel();
info.setLayout(new GridLayout(1, 2));
JLabel totalScrobbles = new JLabel(numberFormat.format(objects.getTotalUserScrobbles()) + " Total Plays"); 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(); pack();
} }
@ -94,8 +123,8 @@ public class FMObjListView extends JFrame{
addAlbum.addActionListener(new ActionListener() { addAlbum.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Album album = GetObject.getAlbum(); Album album = Getter.getAlbum();
if(album != null) { if (album != null) {
} }
} }
@ -109,7 +138,6 @@ public class FMObjListView extends JFrame{
addMenu.add(addTrack); addMenu.add(addTrack);
addMenu.add(addArtist); addMenu.add(addArtist);
editMenu.add(addMenu); editMenu.add(addMenu);
menuBar.add(editMenu); menuBar.add(editMenu);

View File

@ -11,7 +11,8 @@ import javax.swing.JOptionPane;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Track; 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 { public class MainMenu extends JFrame {
@ -21,7 +22,7 @@ public class MainMenu extends JFrame {
JButton viewList = new JButton("View List"); JButton viewList = new JButton("View List");
public MainMenu() { public MainMenu() {
super("fmframework"); super("fmframework - " + Reference.getUserName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 2)); setLayout(new GridLayout(2, 2));
setSize(300, 300); setSize(300, 300);
@ -29,7 +30,7 @@ public class MainMenu extends JFrame {
getAlbum.addActionListener(new ActionListener() { getAlbum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Album album = GetObject.getAlbum(); Album album = Getter.getAlbum();
if (album != null) { if (album != null) {
album.view(); album.view();
} else { } else {
@ -39,7 +40,7 @@ public class MainMenu extends JFrame {
}); });
getArtist.addActionListener(new ActionListener() { getArtist.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Artist artist = GetObject.getArtist(); Artist artist = Getter.getArtist();
if (artist != null) { if (artist != null) {
artist.view(); artist.view();
} else { } else {
@ -49,7 +50,7 @@ public class MainMenu extends JFrame {
}); });
viewLastTrack.addActionListener(new ActionListener() { viewLastTrack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
Track track = GetObject.getLastTrack(); Track track = Getter.getLastTrack();
if (track != null) { if (track != null) {
track.view(); track.view();
} else { } else {

View File

@ -15,7 +15,7 @@ public class RefListsView extends JFrame {
public RefListsView() { public RefListsView() {
super("fmframework"); super("fmframework");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(5, 5)); setLayout(new GridLayout(3, 2));
setSize(500, 500); setSize(500, 500);
setResizable(false); setResizable(false);

View File

@ -95,8 +95,17 @@ public class Network {
public static String getLastTrackUrl(String username) { public static String getLastTrackUrl(String username) {
String urlString = String.format( String urlString = String.format(
"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s", username, "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=%s&limit=1&api_key=%s",
Key.getKey()); 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; return urlString;
} }

View File

@ -11,7 +11,7 @@ import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall; import sarsoo.fmframework.net.TestCall;
import sarsoo.fmframework.parser.Parser; import sarsoo.fmframework.parser.Parser;
public class GetObject { public class Getter {
public static Album getAlbum() { public static Album getAlbum() {
String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name"); String artistName = JOptionPane.showInputDialog(null, "Enter Artist Name");
if (artistName != null) { if (artistName != null) {
@ -49,4 +49,15 @@ public class GetObject {
} }
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;
}
} }

View 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;
}
}

View File

@ -31,6 +31,7 @@ public class Reference {
groups.add(getTDE()); groups.add(getTDE());
groups.add(getBPHQ()); groups.add(getBPHQ());
groups.add(getDre()); groups.add(getDre());
groups.add(getWu());
groups.add(getHopeless()); groups.add(getHopeless());
groups.add(getSaturation()); groups.add(getSaturation());
} }
@ -109,4 +110,17 @@ public class Reference {
return dre; 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;
}
} }