Last active
August 29, 2015 14:06
-
-
Save DarkLotus/08d62e6908886dba0078 to your computer and use it in GitHub Desktop.
Week 2 Exericse
This file contains hidden or 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
| // https://www.dropbox.com/s/ya77lpafdjvp8pq/week2-uml2.PNG?dl=0 | |
| // My final UML which didnt quite match up with the actual implementation. | |
| import java.io.IOException; | |
| import java.sql.Time; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.Random; | |
| import java.util.Scanner; | |
| import java.util.Timer; | |
| /** | |
| * James Kidd | |
| */ | |
| public class Week2Exercise | |
| { | |
| /** | |
| * @param args | |
| * @throws InterruptedException | |
| * @throws IOException | |
| */ | |
| public static void main(String[] args) throws InterruptedException, | |
| IOException | |
| { | |
| new Week2Exercise().Run(); | |
| ; | |
| } | |
| // CDPlayer Input and setup | |
| public void Run() throws InterruptedException, IOException | |
| { | |
| CDPlayer player = new CDPlayer(); | |
| player.InsertDisc(new Disc(new Track[] { new Track("Who Can It Be Now?" | |
| .getBytes()), new Track("Fight for your Right".getBytes()) })); | |
| player.InsertDisc(new Disc(new Track[] { new Track("Who Can It Be Now?2" | |
| .getBytes()), new Track("Fight for your Right2".getBytes()) })); | |
| Scanner scanner = new Scanner(System.in); | |
| while (true) | |
| { | |
| Thread.sleep(10); | |
| if (scanner.hasNext()) | |
| switch(scanner.nextLine().charAt(0)){ | |
| case '1': | |
| player.SetPlayMode(PlayMode.Default); | |
| break; | |
| case '2': | |
| player.SetPlayMode(PlayMode.RepeatDisc); | |
| break; | |
| case '3': | |
| player.SetPlayMode(PlayMode.RepeatTrack); | |
| break; | |
| case 'p': | |
| if (player.isPlaying()) | |
| player.Pause(); | |
| else | |
| player.Play(); | |
| break; | |
| case 'q': | |
| player.PreviousTrack(); | |
| break; | |
| case 'w': | |
| player.NextTrack(); | |
| break; | |
| case 'e': | |
| player.NextDisc(); | |
| break; | |
| case 'r': | |
| player.PreviousDisc(); | |
| break; | |
| case 'a': | |
| player.EjectCurrentDisc(); | |
| break; | |
| case 's': | |
| player.InsertDisc(new Disc(new Track[]{new Track("Dont Stop Me Now".getBytes()),new Track("Everybody wants to rule the world".getBytes())} )); | |
| break; | |
| } | |
| } | |
| } | |
| public class Track | |
| { | |
| private final byte[] _data; | |
| private long _length; | |
| public Track(byte[] data) | |
| { | |
| this._data = data; | |
| } | |
| /* | |
| * Lets pretend that data can be parsed and holds a title along with audio | |
| * data | |
| */ | |
| public String GetTitle() | |
| { | |
| return new String(_data); | |
| } | |
| // Fake the Length | |
| public long getLength() | |
| { | |
| if (_length == 0) | |
| _length = new Random().nextInt(10000); | |
| return _length; | |
| } | |
| } | |
| public class Disc | |
| { | |
| private final Track[] _tracks; | |
| public Disc(Track[] tracks) | |
| { | |
| this._tracks = tracks; | |
| } | |
| public Track getTrack(int trackNum) throws InvalidTrackException | |
| { | |
| if (trackNum > _tracks.length - 1) | |
| throw new InvalidTrackException("Invalid Track : " + trackNum); | |
| return _tracks[trackNum]; | |
| } | |
| } | |
| public class DiscCassette | |
| { | |
| private ArrayList<Disc> _discs = new ArrayList<>(); | |
| public Disc getDisc(int disc) throws InvalidDiscException | |
| { | |
| if (disc > _discs.size() - 1) | |
| throw new InvalidDiscException("Invalid Disc : " + disc); | |
| return _discs.get(disc); | |
| } | |
| public void InsertDisc(Disc disc) | |
| { | |
| _discs.add(disc); | |
| } | |
| public void RemoveDisc(int disc) throws InvalidDiscException | |
| { | |
| if (disc > _discs.size() - 1) | |
| throw new InvalidDiscException("Invalid Disc"); | |
| _discs.remove(disc); | |
| } | |
| } | |
| public class CDPlayer | |
| { | |
| private DiscCassette _cassette; | |
| private int _currentDisc = 0; | |
| private long _currentTrackTime; | |
| private int _currentTrack = 0; | |
| private PlayMode _playMode = PlayMode.Default; | |
| private Thread _player; | |
| public boolean isPlaying() { | |
| if(_player != null && _player.isAlive() && !_player.isInterrupted()) | |
| return true; | |
| return false; | |
| } | |
| public CDPlayer() | |
| { | |
| _cassette = new DiscCassette(); | |
| } | |
| private void Display(String text) | |
| { | |
| System.out.println(text); | |
| } | |
| public void InsertDisc(Disc disc) | |
| { | |
| _cassette.InsertDisc(disc); | |
| } | |
| public void EjectCurrentDisc() | |
| { | |
| try | |
| { | |
| _cassette.RemoveDisc(_currentDisc); | |
| if (_player != null && _player.isAlive() && !_player.isInterrupted()) | |
| _player.interrupt(); | |
| } | |
| catch (InvalidDiscException e) | |
| { | |
| Display(e.getMessage()); | |
| } | |
| } | |
| public void NextTrack() | |
| { | |
| if (_playMode != PlayMode.RepeatTrack) | |
| _currentTrack++; | |
| Play(); | |
| } | |
| public void NextDisc() | |
| { | |
| _currentDisc++; | |
| _currentTrack = 0; | |
| } | |
| public void Pause() throws InterruptedException | |
| { | |
| if (_player != null && _player.isAlive() && !_player.isInterrupted()) | |
| _player.interrupt(); | |
| //Sleep to let interupt handler fire and set playtime... this is getting more hacky as the day goes on | |
| Thread.sleep(100); | |
| // Fake the amount of time played because my thread idea wasnt great | |
| Track curTrack; | |
| try | |
| { | |
| curTrack = getTrack(_currentTrack); | |
| } | |
| catch (InvalidTrackException | InvalidDiscException e) | |
| { | |
| Display(e.getMessage()); | |
| return; | |
| } | |
| Display("Paused on: " + curTrack.GetTitle() + " Length: " + | |
| curTrack.getLength() + " Remaining: " + | |
| (curTrack.getLength() - _currentTrackTime)); | |
| } | |
| public void Play() | |
| { | |
| _currentTrackTime = 0; | |
| final Track curTrack; | |
| try | |
| { | |
| curTrack = getTrack(_currentTrack); | |
| } | |
| catch (InvalidTrackException e1) | |
| { | |
| switch (_playMode) | |
| { | |
| case Default: | |
| _currentTrack = 0; | |
| _currentDisc++; | |
| Play(); | |
| break; | |
| case RepeatDisc: | |
| _currentTrack = 0; | |
| Play(); | |
| break; | |
| default: | |
| break; | |
| } | |
| return; | |
| } | |
| catch (InvalidDiscException e1) | |
| { | |
| _currentDisc = 0; | |
| Play(); | |
| return; | |
| } | |
| Display("Playing: " + curTrack.GetTitle() + " Length: " + | |
| curTrack.getLength()); | |
| //interupt old thread just incase. | |
| if(_player != null && _player.isAlive() && !_player.isInterrupted()) | |
| _player.interrupt(); | |
| //Background thread so tracks advance automatically. | |
| _player = new Thread(new Runnable() | |
| { | |
| @Override | |
| public void run() | |
| { | |
| long startTime = System.currentTimeMillis(); | |
| try | |
| { | |
| Thread.sleep(curTrack.getLength() - _currentTrackTime); | |
| } | |
| catch (InterruptedException e) | |
| { | |
| _currentTrackTime = System.currentTimeMillis() - startTime; | |
| Display("Play Interupted: " + _currentTrackTime + " / " + curTrack.getLength()); | |
| return; | |
| } | |
| switch (_playMode) | |
| { | |
| case RepeatTrack: | |
| Play(); | |
| break; | |
| case RepeatDisc: | |
| case Default: | |
| default: | |
| NextTrack(); | |
| break; | |
| } | |
| } | |
| }); | |
| _player.start(); | |
| } | |
| private Track getTrack(int tracknum) throws InvalidTrackException, | |
| InvalidDiscException | |
| { | |
| return _cassette.getDisc(_currentDisc).getTrack(_currentTrack); | |
| } | |
| public void PreviousTrack() | |
| { | |
| if(_currentTrack > 0) | |
| _currentTrack--; | |
| } | |
| public void PreviousDisc() | |
| { | |
| if(_currentDisc == 0) | |
| return; | |
| _currentDisc--; | |
| _currentTrack = 0; | |
| } | |
| public void SetPlayMode(PlayMode mode) | |
| { | |
| _playMode = mode; | |
| } | |
| public void Stop() | |
| { | |
| if (_player != null && _player.isAlive() && !_player.isInterrupted()) | |
| _player.interrupt(); | |
| } | |
| } | |
| enum PlayMode | |
| { | |
| Default, RepeatDisc, RepeatTrack | |
| } | |
| class InvalidTrackException extends Exception | |
| { | |
| public InvalidTrackException(String string) | |
| { | |
| super(string); | |
| } | |
| } | |
| class InvalidDiscException extends Exception | |
| { | |
| public InvalidDiscException(String string) | |
| { | |
| super(string); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment