Skip to content

Commit c0f0702

Browse files
authored
Submit the Design Patterns Course Assignments (#1)
* Submit the Ungraded Assignment on Adapter Pattern * Submit the Ungraded Assignment on Composite Pattern * Submit the Capstone Assignment on Command Pattern * Submit the Ungraded Assignment on Observer Pattern * Submit the Ungraded Assignment on MVC Pattern * Submit the Capstone Assignment on MVC Pattern * Submit the Capstone Assignment on Code Smells [Part 1] * Submit the Capstone Assignment on Code Smells [Part 2]
1 parent f1f822b commit c0f0702

30 files changed

+1741
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package course.week1.ungraded.adapter;
2+
3+
public interface CoffeeMachineInterface {
4+
5+
void chooseFirstSelection();
6+
7+
void chooseSecondSelection();
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package course.week1.ungraded.adapter;
2+
3+
public class CoffeeTouchscreenAdapter implements CoffeeMachineInterface {
4+
5+
private final OldCoffeeMachine adaptedMachine;
6+
7+
public CoffeeTouchscreenAdapter(final OldCoffeeMachine vendingMachine) {
8+
9+
adaptedMachine = vendingMachine;
10+
}
11+
12+
@Override
13+
public void chooseFirstSelection() {
14+
15+
adaptedMachine.pressFirstButton();
16+
}
17+
18+
@Override
19+
public void chooseSecondSelection() {
20+
21+
adaptedMachine.pressSecondButton();
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package course.week1.ungraded.adapter;
2+
3+
public class OldCoffeeMachine {
4+
5+
public void pressFirstButton() {
6+
7+
// Original Machine - Button `1` Logic
8+
}
9+
10+
public void pressSecondButton() {
11+
12+
// Original Machine - Button `2` Logic
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package course.week1.ungraded.composite;
2+
3+
public interface IComponent {
4+
5+
void setSpeed(final float speed);
6+
7+
void play();
8+
9+
String getName();
10+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package course.week1.ungraded.composite;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Playlist implements IComponent {
7+
8+
private final String name;
9+
10+
private final List<IComponent> songs = new ArrayList<>();
11+
12+
public Playlist(final String name) {
13+
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public void setSpeed(final float speed) {
19+
20+
for (final IComponent song : songs) {
21+
22+
song.setSpeed(speed);
23+
}
24+
}
25+
26+
@Override
27+
public void play() {
28+
29+
for (final IComponent song : songs) {
30+
31+
song.play();
32+
}
33+
}
34+
35+
@Override
36+
public String getName() {
37+
38+
return name;
39+
}
40+
41+
public void insert(final IComponent song) {
42+
43+
songs.add(song);
44+
}
45+
46+
public void delete(final IComponent song) {
47+
48+
songs.remove(song);
49+
}
50+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package course.week1.ungraded.composite;
2+
3+
public class Program {
4+
5+
public static void main(String[] args) {
6+
7+
final Playlist studyPlaylist = new Playlist("Study");
8+
9+
final Playlist synthPopPlaylist = new Playlist("Synth Pop");
10+
11+
final Song synthPopSong1 = new Song("Girl Like You", "Toro Y Moi");
12+
final Song synthPopSong2 = new Song("Outside", "TOPS");
13+
14+
synthPopPlaylist.insert(synthPopSong1);
15+
synthPopPlaylist.insert(synthPopSong2);
16+
17+
final Playlist experimentalPlaylist = new Playlist("Experimental");
18+
19+
final Song experimentalSong1 = new Song("About you", "XXYYXX");
20+
final Song experimentalSong2 = new Song("Motivation", "Clams Casino");
21+
final Song experimentalSong3 = new Song("Computer Vision", "Oneohtrix Point Never");
22+
23+
experimentalPlaylist.insert(experimentalSong1);
24+
experimentalPlaylist.insert(experimentalSong2);
25+
experimentalPlaylist.insert(experimentalSong3);
26+
27+
experimentalPlaylist.setSpeed(0.5f);
28+
experimentalPlaylist.insert(synthPopPlaylist);
29+
studyPlaylist.insert(experimentalPlaylist);
30+
31+
final Song glitchSong = new Song("Textuell", "Oval");
32+
33+
glitchSong.setSpeed(1.25f);
34+
glitchSong.play();
35+
36+
System.out.printf("Song Name = [%s].%n", glitchSong.getName());
37+
System.out.printf("Song Artist = [%s.]%n", glitchSong.getArtist());
38+
39+
studyPlaylist.insert(glitchSong);
40+
studyPlaylist.play();
41+
42+
System.out.printf("Playlist Name = [%s].%n", studyPlaylist.getName());
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package course.week1.ungraded.composite;
2+
3+
public class Song implements IComponent {
4+
5+
private final String name;
6+
private final String artist;
7+
8+
private float speed = 1f;
9+
10+
public Song(final String name, final String artist) {
11+
12+
this.name = name;
13+
this.artist = artist;
14+
}
15+
16+
@Override
17+
public void setSpeed(final float speed) {
18+
19+
this.speed = speed;
20+
}
21+
22+
@Override
23+
public void play() {
24+
25+
System.out.printf("Playing `%s` by `%s`, at %.2fx Speed...%n", name, artist, speed);
26+
}
27+
28+
@Override
29+
public String getName() {
30+
31+
return name;
32+
}
33+
34+
public String getArtist() {
35+
36+
return artist;
37+
}
38+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.example.sharingapp;
2+
3+
import android.content.Context;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.os.Bundle;
6+
import android.view.View;
7+
import android.widget.EditText;
8+
import javafx.scene.web.HTMLEditorSkin.Command;
9+
10+
public class AddContactActivity extends AppCompatActivity {
11+
12+
private Context context;
13+
14+
private EditText username;
15+
private EditText email;
16+
17+
private ContactList contactList = new ContactList();
18+
19+
@Override
20+
protected void onCreate(final Bundle savedInstanceState) {
21+
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_add_contact);
24+
25+
username = (EditText) findViewById(R.id.username);
26+
email = (EditText) findViewById(R.id.email);
27+
28+
context = getApplicationContext();
29+
contactList.loadContacts(context);
30+
}
31+
32+
public void saveContact(final View view) {
33+
34+
final String usernameStr = username.getText().toString();
35+
final String emailStr = email.getText().toString();
36+
37+
if (usernameStr.equals("")) {
38+
39+
username.setError("Empty field!");
40+
return;
41+
}
42+
43+
if (emailStr.equals("")) {
44+
45+
email.setError("Empty field!");
46+
return;
47+
}
48+
49+
if (!emailStr.contains("@")) {
50+
51+
email.setError("Must be an email address!");
52+
return;
53+
}
54+
55+
if (!contactList.isUsernameAvailable(usernameStr)) {
56+
57+
username.setError("Username already taken!");
58+
return;
59+
}
60+
61+
final Contact contact = new Contact(usernameStr, emailStr, null);
62+
final Command command = new AddContactCommand(contactList, contact, context);
63+
64+
command.execute();
65+
66+
if (!command.isExecuted()) {
67+
68+
return;
69+
}
70+
71+
finish();
72+
}
73+
}
74+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.sharingapp;
2+
3+
import android.content.Context;
4+
5+
public class AddContactCommand extends Command {
6+
7+
private final ContactList contactList;
8+
9+
private final Contact contact;
10+
private final Context context;
11+
12+
public AddContactCommand(final ContactList contactList,
13+
final Contact contact, final Context context) {
14+
15+
this.contactList = contactList;
16+
17+
this.contact = contact;
18+
this.context = context;
19+
}
20+
21+
@Override
22+
public void execute() {
23+
24+
contactList.addContact(contact);
25+
setIsExecuted(contactList.saveContacts(context));
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.sharingapp;
2+
3+
import android.content.Context;
4+
5+
public class DeleteContactCommand extends Command {
6+
7+
private final ContactList contactList;
8+
9+
private final Contact contact;
10+
private final Context context;
11+
12+
public DeleteContactCommand(final ContactList contactList,
13+
final Contact contact, final Context context) {
14+
15+
this.contactList = contactList;
16+
17+
this.contact = contact;
18+
this.context = context;
19+
}
20+
21+
@Override
22+
public void execute() {
23+
24+
contactList.deleteContact(contact);
25+
setIsExecuted(contactList.saveContacts(context));
26+
}
27+
}

0 commit comments

Comments
 (0)