Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions java/src/main/java/com/google/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void main(String[] args){
return;
}
parser.executeCommand(Arrays.asList(input.split("\\s+")));
scanner.close();
}
}
}
12 changes: 12 additions & 0 deletions java/src/main/java/com/google/SortByTitle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.google;

import java.util.Comparator;

// This class can be used by a Collection of Videos like a List of Videos. By using this,
// I can sort a list of Videos by their title easily whenever we need to display videos in a sorted way.
public class SortByTitle implements Comparator<Video> {
@Override
public int compare(Video o1, Video o2) {
return o1.getTitle().compareTo(o2.getTitle());
}
}
2 changes: 2 additions & 0 deletions java/src/main/java/com/google/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ String getVideoId() {
List<String> getTags() {
return tags;
}


}
44 changes: 38 additions & 6 deletions java/src/main/java/com/google/VideoLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -16,8 +12,12 @@ class VideoLibrary {

private final HashMap<String, Video> videos;

// Keep track of flagged videos with mapping from VideoId to the corresponding flagged reason.
private Map<String, String> flaggedVideos;

VideoLibrary() {
this.videos = new HashMap<>();
flaggedVideos = new HashMap<>();
try {
File file = new File(this.getClass().getResource("/videos.txt").getFile());

Expand Down Expand Up @@ -46,10 +46,42 @@ List<Video> getVideos() {
return new ArrayList<>(this.videos.values());
}

// Return a list videos in the library that is not flagged and hence playable.
// Note this method is not scalable for a large amount of videos because we need to go
// through all the video each time this method is called. One way to improve it is that
// we can store the list of playable videos in a field and update it when a video is flagged
// and un-flagged. However, we will need to store another field in this way. It's a tradeoff
// between space and runtime complexity.
List<Video> getPlayableVideos() {
List<Video> playableVideo = new ArrayList<>();
// We go through all the videos and only add non-flagged videos to the list.
for (String videoId: this.videos.keySet()) {
if (!this.flaggedVideos.containsKey(videoId)) {
playableVideo.add(this.videos.get(videoId));
}
}
return playableVideo;
}
/**
* Get a video by id. Returns null if the video is not found.
*/
Video getVideo(String videoId) {
return this.videos.get(videoId);
}
}

String getFlaggedReason(String videoId) {
return this.flaggedVideos.get(videoId);
}

void flagVideo(String videoId, String reason) {
this.flaggedVideos.put(videoId, reason);
}

boolean isFlagged(String videoId) {
return this.flaggedVideos.containsKey(videoId);
}

void allowVideo(String videoId) {
this.flaggedVideos.remove(videoId);
}
}
64 changes: 64 additions & 0 deletions java/src/main/java/com/google/VideoLibrary1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.google;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
* A class used to represent a Video Library. ewww
*/
class prevVideoLibrary {

private final HashMap<String, Video> videos;

prevVideoLibrary() {
this.videos = new HashMap<>();
try {
File file = new File(this.getClass().getResource("/videos.txt").getFile());

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] split = line.split("\\|");
String title = split[0].strip();
String id = split[1].strip();
List<String> tags;
if (split.length > 2) {
tags = Arrays.stream(split[2].split(",")).map(String::strip).collect(
Collectors.toList());
} else {
tags = new ArrayList<>();
}
this.videos.put(id, new Video(title, id, tags));
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Couldn't find videos.txt");
e.printStackTrace();
}
}

LinkedList<Video> getVideos() {
return new LinkedList<>(this.videos.values());
}

/**
* Get a video by id. Returns null if the video is not found.
*/
Video getVideo(String videoId) {
return this.videos.get(videoId);
}

public String getFlaggedReason(String videoId) {
return null;
}

public boolean isFlagged(String videoId) {
return false;
}
}
129 changes: 119 additions & 10 deletions java/src/main/java/com/google/VideoPlayer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.google;
import java.util.*;

public class VideoPlayer {

private final VideoLibrary videoLibrary;


public boolean vidPlaying = false;
public boolean vidPaused = false;
String prevVid = "";
Video currentVid=null;
LinkedList<String> playlists = new LinkedList<String>();

public VideoPlayer() {
this.videoLibrary = new VideoLibrary();
}
Expand All @@ -13,47 +20,149 @@ public void numberOfVideos() {
}

public void showAllVideos() {
System.out.println("showAllVideos needs implementation");
}
System.out.println("Here's a list of all available videos:");
List<Video> vids = this.videoLibrary.getVideos();
// Use a self-implemented comparator to sort videos by their titles.
Collections.sort(vids, new SortByTitle());
for (int i=0; i<vids.size();i++){
Video cVid = vids.get(i);

String name = cVid.getTitle();
String ident = cVid.getVideoId();
List<String> tags = cVid.getTags();
String tagNem = tags.toString().replaceAll(",","");

System.out.println(name+" ("+ident+") "+tagNem);

}


}

public void playVideo(String videoId) {
System.out.println("playVideo needs implementation");
Video realVid = this.videoLibrary.getVideo(videoId);
String nem="";

if (realVid!=null){
while (vidPlaying){
stopVideo();
vidPlaying = false;
}

prevVid = realVid.getTitle();
currentVid = realVid;
nem = "Playing video: "+ prevVid;
vidPlaying = true;
}
else{
nem ="Cannot play video: Video does not exist";

}

System.out.println(nem);
}

public void stopVideo() {
System.out.println("stopVideo needs implementation");

if(!vidPlaying){
System.out.println("Cannot stop video: No video is currently playing");
}
else{
if(!prevVid.equals("")){
System.out.println("Stopping video: "+prevVid);
vidPlaying=false;
vidPaused=false;
}
}

}

public void playRandomVideo() {
System.out.println("playRandomVideo needs implementation");
//i
List<Video> vids = this.videoLibrary.getVideos();
Random r = new Random();
int rndmInt = r.nextInt(vids.size()-1);
String selectedV = vids.get(rndmInt).getVideoId();
//get length of vid system and select a random vid tween there and send that to the videoId!!
playVideo(selectedV);
}

public void pauseVideo() {
System.out.println("pauseVideo needs implementation");
if(vidPaused){
System.out.println("Video already paused: "+prevVid);
}
else{
if(!vidPlaying){
System.out.println("Cannot pause video: No video is currently playing");
}
else{
vidPaused = true;
if(!prevVid.equals("")){
System.out.println("Pausing video: "+prevVid);
}
}

}
}

public void continueVideo() {
System.out.println("continueVideo needs implementation");
if(!vidPlaying){
System.out.println("Cannot continue video: No video is currently playing");
}
else{
if(vidPaused){
System.out.println("Continuing video: "+prevVid);
}
else{
System.out.println("Cannot continue video: Video is not paused");
}
}


}

public void showPlaying() {
System.out.println("showPlaying needs implementation");

if (vidPlaying){
String name = currentVid.getTitle();
String ident = currentVid.getVideoId();
List<String> tags = currentVid.getTags();
String tagNem = tags.toString().replaceAll(",","");

if(vidPaused){
System.out.println("Currently playing: "+name+" ("+ident+") "+tagNem+" - PAUSED");
}
else{
System.out.println("Currently playing: "+name+" ("+ident+") "+tagNem);
}

}
else{
System.out.println("No video is currently playing");

}


}

public void createPlaylist(String playlistName) {
System.out.println("createPlaylist needs implementation");
VideoPlaylist v = new VideoPlaylist();
System.out.println("Successfully created new playlist: "+ playlistName);
}

public void addVideoToPlaylist(String playlistName, String videoId) {
System.out.println("addVideoToPlaylist needs implementation");
//add it deya....loaed the playist and check if it's there already tbh
}

public void showAllPlaylists() {
System.out.println("showAllPlaylists needs implementation");
// just implement a list of playlists and their names no?
}

public void showPlaylist(String playlistName) {
System.out.println("showPlaylist needs implementation");
//a list must be assigned to each playlist...to display the videos
}

public void removeFromPlaylist(String playlistName, String videoId) {
Expand Down
5 changes: 5 additions & 0 deletions java/src/main/java/com/google/VideoPlaylist.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

/** A class used to represent a Playlist */
class VideoPlaylist {
//you want the names of the videos in here
//and the playlist name sha

//name=....get name,, add to list
//show videos, display list of videos

}
3 changes: 2 additions & 1 deletion java/src/test/java/com/google/Part1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void testNumberOfVideos() {
@Test
public void testShowAllVideos() {
videoPlayer.showAllVideos();

//BITCH I DID ITOIHSDLGJSDL;JGL;SDJG;SDJG
String[] lines = getOutputLines();
assertEquals(6, lines.length, outputStream.toString());
assertThat(lines[0], containsString("Here's a list of all available videos:"));
Expand All @@ -40,6 +40,7 @@ public void testShowAllVideos() {
assertThat(lines[5],
containsString("Video about nothing (nothing_video_id) []"));
}
//so you print a line of each.....so like each object kinda on a new line

@Test
public void testPlayVideo() {
Expand Down
4 changes: 2 additions & 2 deletions java/src/test/java/com/google/VideoLibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

public class VideoLibraryTest {

private VideoLibrary videoLibrary;
private prevVideoLibrary videoLibrary;

@BeforeEach
public void setUp() {
videoLibrary = new VideoLibrary();
videoLibrary = new prevVideoLibrary();
}

@Test
Expand Down