├── 2. Design Patterns ├── Week 4 │ └── 1. Capstone Assignment 2.3 │ │ ├── Code Smells.pdf │ │ ├── Code Smells.docx │ │ └── Sharing App - Code Smell │ │ ├── AddContactActivity.java │ │ ├── EditContactActivity.java │ │ ├── AddItemActivity.java │ │ └── EditItemActivity.java ├── Week 1 │ ├── 1. Ungraded Assignment - Adapter Pattern │ │ ├── OldCoffeeMachine.java │ │ ├── CoffeeMachineInterface.java │ │ └── CoffeeTouchscreenAdapter.java │ └── 2. Ungraded Assignment - Composite Pattern │ │ ├── IComponent.java │ │ ├── Song.java │ │ └── Playlist.java ├── Week 3 │ ├── 1. Ungraded Assignment - MVC Pattern │ │ ├── MVC Pattern - UML Diagram - UML Class.pdf │ │ └── MVC Pattern - UML Diagram - UML Class.png │ └── 2. Capstone Assignment 2.2 │ │ ├── ContactController.java │ │ ├── Contact.java │ │ ├── AddContactActivity.java │ │ ├── ContactListController.java │ │ ├── EditContactActivity.java │ │ └── ContactList.java └── Week 2 │ ├── 2. Ungraded Assignment - Observer Pattern │ ├── Subject.java │ ├── Follower.java │ └── Channel.java │ └── 1. Capstone Assignment 2.1 │ ├── AddContactCommand.java │ ├── DeleteContactCommand.java │ ├── EditContactCommand.java │ ├── AddContactActivity.java │ └── EditContactActivity.java ├── 1. Object Oriented Design ├── Week 4 │ └── 1. Capstone Assignment 1.4 │ │ ├── Part-1-submission.zip │ │ ├── Coursera object oriented design.mp4 │ │ ├── Contact.java │ │ └── ContactList.java ├── Week 3 │ ├── 3. Ungraded Assignment - UML State Diagram │ │ └── Activity diagram.pdf │ ├── 4. Capstone Assignment 1.3 │ │ ├── Capstone Assignment 1.3 - UML State Diagram - SharingApp.pdf │ │ └── Capstone Assignment 1.3 - UML State Diagram - SharingApp.png │ ├── 1. Ungraded Assignment - UML Sequence Diagram │ │ └── Airport Flight Booking - Sequence Diagram.pdf │ └── 2. Capstone Assignment 1.2 │ │ └── Capstone Assignment 1.2 - Sequence Diagram - Sequence diagram (1).pdf └── Week 2 │ ├── 1. Ungraded Assignment │ └── Car Rental Company - UML Class Diagram.png │ └── 2. Capstone Assignment 1.1 │ ├── Capstone Assignment 1.1 - UML Class Diagram.pdf │ └── Capstone Assignment 1.1 - UML Class Diagram.png ├── 3. Software Architecture ├── Week 1 │ ├── 1. Capstone Assignment 3.1 │ │ ├── Component diagram.png │ │ └── ComponentDiagram.pdf │ └── 2. Capstone Assignment 3.2 │ │ ├── Deployment diagram.png │ │ └── DeploymentDiagram.pdf └── Week 4 │ └── 1. Capstone Assignment 3.3 │ ├── Capstone Assignment 3.3 - Analyze & Evaluate Architecture.docx │ └── Capstone Assignment 3.3 - Analyze & Evaluate Architecture.pdf ├── 4. Service Oriented Architecture ├── Week 2 │ └── 1. Capstone Assignment 4.1 │ │ ├── Blank diagram.pdf │ │ └── Blank diagram.png ├── Week 3 │ └── 1. Peer-graded Assignment - REST Services │ │ └── Trello - A case study.docx └── Week 4 │ └── 1. Capstone Assignment 4.2 │ ├── DeleteBidCommand.java │ ├── AddBidCommand.java │ ├── BidListController.java │ ├── ItemFragmentAdapter.java │ ├── BidList.java │ ├── ViewItemBidsActivity.java │ ├── ViewItemActivity.java │ └── ElasticSearchManager.java └── README.md /2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Code Smells.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Code Smells.pdf -------------------------------------------------------------------------------- /2. Design Patterns/Week 1/1. Ungraded Assignment - Adapter Pattern/OldCoffeeMachine.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | @author Bishwa on 15/10/2020 4 | */ 5 | public class OldCoffeeMachine { 6 | public void selectA() {} 7 | public void selectB() {} 8 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Code Smells.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Code Smells.docx -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 4/1. Capstone Assignment 1.4/Part-1-submission.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 4/1. Capstone Assignment 1.4/Part-1-submission.zip -------------------------------------------------------------------------------- /3. Software Architecture/Week 1/1. Capstone Assignment 3.1/Component diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/3. Software Architecture/Week 1/1. Capstone Assignment 3.1/Component diagram.png -------------------------------------------------------------------------------- /3. Software Architecture/Week 1/1. Capstone Assignment 3.1/ComponentDiagram.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/3. Software Architecture/Week 1/1. Capstone Assignment 3.1/ComponentDiagram.pdf -------------------------------------------------------------------------------- /3. Software Architecture/Week 1/2. Capstone Assignment 3.2/Deployment diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/3. Software Architecture/Week 1/2. Capstone Assignment 3.2/Deployment diagram.png -------------------------------------------------------------------------------- /3. Software Architecture/Week 1/2. Capstone Assignment 3.2/DeploymentDiagram.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/3. Software Architecture/Week 1/2. Capstone Assignment 3.2/DeploymentDiagram.pdf -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 2/1. Capstone Assignment 4.1/Blank diagram.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/4. Service Oriented Architecture/Week 2/1. Capstone Assignment 4.1/Blank diagram.pdf -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 2/1. Capstone Assignment 4.1/Blank diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/4. Service Oriented Architecture/Week 2/1. Capstone Assignment 4.1/Blank diagram.png -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 3/3. Ungraded Assignment - UML State Diagram/Activity diagram.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 3/3. Ungraded Assignment - UML State Diagram/Activity diagram.pdf -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 4/1. Capstone Assignment 1.4/Coursera object oriented design.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 4/1. Capstone Assignment 1.4/Coursera object oriented design.mp4 -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 2/1. Ungraded Assignment/Car Rental Company - UML Class Diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 2/1. Ungraded Assignment/Car Rental Company - UML Class Diagram.png -------------------------------------------------------------------------------- /2. Design Patterns/Week 1/1. Ungraded Assignment - Adapter Pattern/CoffeeMachineInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | @author Bishwa on 15/10/2020 4 | */ 5 | 6 | public interface CoffeeMachineInterface { 7 | 8 | public void chooseFirstSelection(); 9 | 10 | public void chooseSecondSelection(); 11 | 12 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/1. Ungraded Assignment - MVC Pattern/MVC Pattern - UML Diagram - UML Class.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/2. Design Patterns/Week 3/1. Ungraded Assignment - MVC Pattern/MVC Pattern - UML Diagram - UML Class.pdf -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/1. Ungraded Assignment - MVC Pattern/MVC Pattern - UML Diagram - UML Class.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/2. Design Patterns/Week 3/1. Ungraded Assignment - MVC Pattern/MVC Pattern - UML Diagram - UML Class.png -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 2/2. Capstone Assignment 1.1/Capstone Assignment 1.1 - UML Class Diagram.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 2/2. Capstone Assignment 1.1/Capstone Assignment 1.1 - UML Class Diagram.pdf -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 2/2. Capstone Assignment 1.1/Capstone Assignment 1.1 - UML Class Diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 2/2. Capstone Assignment 1.1/Capstone Assignment 1.1 - UML Class Diagram.png -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 3/1. Peer-graded Assignment - REST Services/Trello - A case study.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/4. Service Oriented Architecture/Week 3/1. Peer-graded Assignment - REST Services/Trello - A case study.docx -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/2. Ungraded Assignment - Observer Pattern/Subject.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Bishwa on 05/11/2020 3 | */ 4 | 5 | public interface Subject { 6 | 7 | void registerObserver(Observer observer); 8 | 9 | void removeObserver(Observer observer); 10 | 11 | void notifyObservers(); 12 | 13 | } -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 3/4. Capstone Assignment 1.3/Capstone Assignment 1.3 - UML State Diagram - SharingApp.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 3/4. Capstone Assignment 1.3/Capstone Assignment 1.3 - UML State Diagram - SharingApp.pdf -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 3/4. Capstone Assignment 1.3/Capstone Assignment 1.3 - UML State Diagram - SharingApp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 3/4. Capstone Assignment 1.3/Capstone Assignment 1.3 - UML State Diagram - SharingApp.png -------------------------------------------------------------------------------- /3. Software Architecture/Week 4/1. Capstone Assignment 3.3/Capstone Assignment 3.3 - Analyze & Evaluate Architecture.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/3. Software Architecture/Week 4/1. Capstone Assignment 3.3/Capstone Assignment 3.3 - Analyze & Evaluate Architecture.docx -------------------------------------------------------------------------------- /3. Software Architecture/Week 4/1. Capstone Assignment 3.3/Capstone Assignment 3.3 - Analyze & Evaluate Architecture.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/3. Software Architecture/Week 4/1. Capstone Assignment 3.3/Capstone Assignment 3.3 - Analyze & Evaluate Architecture.pdf -------------------------------------------------------------------------------- /2. Design Patterns/Week 1/2. Ungraded Assignment - Composite Pattern/IComponent.java: -------------------------------------------------------------------------------- 1 | package Structural.CompositePattern.Assignment; 2 | 3 | /** 4 | * @author Bishwa on 15/10/2020 5 | */ 6 | public interface IComponent { 7 | public void play(); 8 | public void setPlaybackSpeed(float speed); 9 | public String getName(); 10 | } -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 3/1. Ungraded Assignment - UML Sequence Diagram/Airport Flight Booking - Sequence Diagram.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 3/1. Ungraded Assignment - UML Sequence Diagram/Airport Flight Booking - Sequence Diagram.pdf -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 3/2. Capstone Assignment 1.2/Capstone Assignment 1.2 - Sequence Diagram - Sequence diagram (1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bishwa-poudel/Software-Design-and-Architecture-Specialization-Coursera/HEAD/1. Object Oriented Design/Week 3/2. Capstone Assignment 1.2/Capstone Assignment 1.2 - Sequence Diagram - Sequence diagram (1).pdf -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/2. Ungraded Assignment - Observer Pattern/Follower.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Bishwa on 05/11/2020 3 | */ 4 | public class Follower implements Observer { 5 | private String followerName; 6 | 7 | @Override 8 | public void update(String string) { 9 | 10 | } 11 | 12 | public void play() { 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 1/1. Ungraded Assignment - Adapter Pattern/CoffeeTouchscreenAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Bishwa on 15/10/2020 3 | */ 4 | public class CoffeeTouchscreenAdapter implements CoffeeMachineInterface { 5 | private OldCoffeeMachine oldVendingMachine; 6 | 7 | public void connect(OldCoffeeMachine oldVendingMachine) { 8 | this.oldVendingMachine = oldVendingMachine; 9 | } 10 | 11 | @Override 12 | public void chooseFirstSelection() { 13 | oldVendingMachine.selectA(); 14 | } 15 | 16 | @Override 17 | public void chooseSecondSelection() { 18 | oldVendingMachine.selectB(); 19 | } 20 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/1. Capstone Assignment 2.1/AddContactCommand.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * @author Bishwa on 04/11/2020 7 | */ 8 | public class AddContactCommand extends Command { 9 | private ContactList contactList; 10 | private Contact contact; 11 | private Context context; 12 | 13 | public AddContactCommand(ContactList contactList, Contact contact, Context context) { 14 | this.contactList = contactList; 15 | this.contact = contact; 16 | this.context = context; 17 | } 18 | 19 | @Override 20 | public void execute() { 21 | contactList.addContact(contact); 22 | setIsExecuted(contactList.saveContacts(context)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/1. Capstone Assignment 2.1/DeleteContactCommand.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * @author Bishwa on 04/11/2020 7 | */ 8 | public class DeleteContactCommand implements Command { 9 | private ContactList contactList; 10 | private Contact contact; 11 | private Context context; 12 | 13 | public DeleteContactCommand(ContactList contactList, Contact contact, Context context) { 14 | this.contactList = contactList; 15 | this.contact = contact; 16 | this.context = context; 17 | } 18 | 19 | @Override 20 | public void execute() { 21 | contactList.deleteContact(contact); 22 | setIsExecuted(contactList.saveContacts(context)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 1/2. Ungraded Assignment - Composite Pattern/Song.java: -------------------------------------------------------------------------------- 1 | package Structural.CompositePattern.Assignment; 2 | 3 | /** 4 | * @author Bishwa on 15/10/2020 5 | */ 6 | public class Song implements IComponent { 7 | private String songName; 8 | private String artist; 9 | private float speed = 1; 10 | 11 | public Song(String songName, String artist) { 12 | this.songName = songName; 13 | this.artist = artist; 14 | } 15 | 16 | @Override 17 | public void play() { 18 | 19 | } 20 | 21 | @Override 22 | public void setPlaybackSpeed(float speed) { 23 | 24 | } 25 | 26 | @Override 27 | public String getName() { 28 | return this.songName; 29 | } 30 | 31 | public String getArtist() { 32 | return artist; 33 | } 34 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 1/2. Ungraded Assignment - Composite Pattern/Playlist.java: -------------------------------------------------------------------------------- 1 | package Structural.CompositePattern.Assignment; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * @author Bishwa on 15/10/2020 7 | */ 8 | public class Playlist implements IComponent { 9 | private String playlistName; 10 | private ArrayList playlist = new ArrayList<>(); 11 | 12 | public Playlist(String playlistName) { 13 | this.playlistName = playlistName; 14 | } 15 | 16 | public void add(IComponent component) { 17 | this.playlist.add(component); 18 | } 19 | 20 | @Override 21 | public void play() { 22 | 23 | } 24 | 25 | @Override 26 | public void setPlaybackSpeed(float speed) { 27 | 28 | } 29 | 30 | @Override 31 | public String getName() { 32 | return null; 33 | } 34 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/2. Ungraded Assignment - Observer Pattern/Channel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Bishwa on 05/11/2020 3 | */ 4 | public class Channel implements Subject { 5 | private List observers; 6 | private String channelName; 7 | private String status; 8 | 9 | public String getStatus() { 10 | return status; 11 | } 12 | 13 | public void setStatus(String status) { 14 | this.status = status; 15 | } 16 | 17 | @Override 18 | public void registerObserver(Observer observer) { 19 | observers.add(observer); 20 | } 21 | 22 | @Override 23 | public void removeObserver(Observer observer) { 24 | observers.remove(observer); 25 | } 26 | 27 | @Override 28 | public void notifyObservers() { 29 | observers.forEach(observer -> observer.update(status)); 30 | } 31 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/1. Capstone Assignment 2.1/EditContactCommand.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * @author Bishwa on 04/11/2020 7 | */ 8 | public class EditContactCommand extends Command { 9 | private ContactList contactList; 10 | private Contact oldContact; 11 | private Contact newContact; 12 | private Context context; 13 | 14 | public EditContactCommand(ContactList contactList, Contact oldContact, Contact newContact, Context context) { 15 | this.contactList = contactList; 16 | this.oldContact = oldContact; 17 | this.newContact = newContact; 18 | this.context = context; 19 | } 20 | 21 | @Override 22 | public void execute() { 23 | contactList.deleteContact(oldContact); 24 | contactList.addContact(newContact); 25 | setIsExecuted(contactList.saveContacts(context)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/2. Capstone Assignment 2.2/ContactController.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import java.util.Observer; 4 | 5 | /** 6 | * @author Bishwa on 05/11/2020 7 | */ 8 | public class ContactController { 9 | private Contact contact; 10 | 11 | public ContactController(Contact contact) { 12 | this.contact = contact; 13 | } 14 | 15 | public String getId(){ 16 | return contact.getId(); 17 | } 18 | 19 | public void setId() { 20 | contact.setId(); 21 | } 22 | 23 | public String getUsername() { 24 | return contact.getUsername(); 25 | } 26 | 27 | public void setUsername(String username) { 28 | contact.setUsername(username); 29 | } 30 | 31 | public String getEmail() { 32 | return contact.getEmail(); 33 | } 34 | 35 | public void setEmail(String email) { 36 | contact.setEmail(email); 37 | } 38 | 39 | public void addObserver(Observer observer) { 40 | contact.addObserver(observer); 41 | } 42 | 43 | public void removeObserver(Observer observer) { 44 | contact.deleteObserver(observer); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 4/1. Capstone Assignment 1.4/Contact.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import java.util.UUID; 4 | 5 | /** 6 | * Created by bishwa on 10/08/2020. 7 | */ 8 | 9 | public class Contact { 10 | private String username; 11 | private String email; 12 | private String id; 13 | 14 | public Contact(String name, String email, String id) { 15 | this.username = name; 16 | this.email = email; 17 | 18 | if(id == null) { 19 | setId(); 20 | } 21 | else { 22 | updateId(id); 23 | } 24 | } 25 | 26 | public void setId() { 27 | this.id = UUID.randomUUID().toString(); 28 | } 29 | 30 | public String getId() { 31 | return this.id; 32 | } 33 | 34 | public void updateId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public void setUsername(String username) { 39 | this.username = username; 40 | } 41 | 42 | public String getUsername() { 43 | return this.username; 44 | } 45 | 46 | public void setEmail(String email) { 47 | this.email = email; 48 | } 49 | 50 | public String getEmail() { 51 | return this.email; 52 | } 53 | } -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/DeleteBidCommand.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import java.util.concurrent.ExecutionException; 5 | 6 | /** 7 | * Command to delete a bid 8 | */ 9 | public class DeleteBidCommand extends Command { 10 | 11 | private BidList bid_list; 12 | private Bid bid; 13 | private Context context; 14 | 15 | public DeleteBidCommand(Bid bid) { this.bid = bid; } 16 | 17 | // Delete the bid remotely from server 18 | public void execute() { 19 | ElasticSearchManager.RemoveBidTask remove_bid_task = new ElasticSearchManager.RemoveBidTask(); 20 | remove_bid_task.execute(bid); 21 | 22 | // use get() to access the return of RemoveBidTask. i.e. RemoveBidTask returns a Boolean to 23 | // indicate if the bid was successfully deleted from the remote server 24 | try { 25 | if(remove_bid_task.get()) { 26 | bid_list.removeBid(bid); 27 | super.setIsExecuted(true); 28 | } 29 | } catch (InterruptedException | ExecutionException e) { 30 | e.printStackTrace(); 31 | super.setIsExecuted(false); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/2. Capstone Assignment 2.2/Contact.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import java.util.Observable; 4 | import java.util.UUID; 5 | 6 | /** 7 | * Contact class 8 | */ 9 | public class Contact extends Observable { 10 | private String username; 11 | private String email; 12 | private String id; 13 | 14 | Contact(String username, String email, String id) { 15 | this.username = username; 16 | this.email = email; 17 | 18 | if (id == null){ 19 | setId(); 20 | } else { 21 | updateId(id); 22 | } 23 | } 24 | 25 | public String getId(){ 26 | return this.id; 27 | } 28 | 29 | public void setId() { 30 | this.id = UUID.randomUUID().toString(); 31 | notifyObservers(); 32 | } 33 | 34 | public void updateId(String id){ 35 | this.id = id; 36 | notifyObservers(); 37 | } 38 | 39 | public String getUsername() { 40 | return username; 41 | } 42 | 43 | public void setUsername(String username) { 44 | this.username = username; 45 | notifyObservers(); 46 | } 47 | 48 | public String getEmail() { 49 | return email; 50 | } 51 | 52 | public void setEmail(String email) { 53 | this.email = email; 54 | notifyObservers(); 55 | } 56 | 57 | 58 | 59 | } 60 | 61 | -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/AddBidCommand.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import java.util.concurrent.ExecutionException; 5 | 6 | /** 7 | * Command to add a bid 8 | */ 9 | public class AddBidCommand extends Command { 10 | 11 | private BidList bid_list; 12 | private Bid bid; 13 | private Context context; 14 | 15 | public AddBidCommand(Bid bid) { this.bid = bid; } 16 | 17 | // Save bid locally 18 | /*public void execute(){ 19 | bid_list.addBid(bid); 20 | super.setIsExecuted(bid_list.saveBids(context)); 21 | }*/ 22 | 23 | // Save the item remotely to server 24 | public void execute(){ 25 | ElasticSearchManager.AddBidTask add_bid_task = new ElasticSearchManager.AddBidTask(); 26 | add_bid_task.execute(bid); 27 | 28 | // use get() to access the return of AddBidTask. i.e. AddBidTask returns a Boolean to 29 | // indicate if the bid was successfully saved to the remote server 30 | try { 31 | if(add_bid_task.get()) { 32 | bid_list.addBid(bid); 33 | super.setIsExecuted(true); 34 | } 35 | } catch (InterruptedException | ExecutionException e) { 36 | e.printStackTrace(); 37 | super.setIsExecuted(false); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/1. Capstone Assignment 2.1/AddContactActivity.java: -------------------------------------------------------------------------------- 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 | 9 | /** 10 | * Add a new contact 11 | */ 12 | public class AddContactActivity extends AppCompatActivity { 13 | 14 | private ContactList contact_list = new ContactList(); 15 | private Context context; 16 | 17 | private EditText username; 18 | private EditText email; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 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 | contact_list.loadContacts(context); 30 | } 31 | 32 | public void saveContact(View view) { 33 | 34 | String username_str = username.getText().toString(); 35 | String email_str = email.getText().toString(); 36 | 37 | if (username_str.equals("")) { 38 | username.setError("Empty field!"); 39 | return; 40 | } 41 | 42 | if (email_str.equals("")) { 43 | email.setError("Empty field!"); 44 | return; 45 | } 46 | 47 | if (!email_str.contains("@")){ 48 | email.setError("Must be an email address!"); 49 | return; 50 | } 51 | 52 | if (!contact_list.isUsernameAvailable(username_str)){ 53 | username.setError("Username already taken!"); 54 | return; 55 | } 56 | 57 | Contact contact = new Contact(username_str, email_str, null); 58 | 59 | Command command = new AddContactCommand(contact_list, contact, context); 60 | command.execute(); 61 | 62 | if(!command.isExecuted()) return; 63 | 64 | // End AddContactActivity 65 | finish(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/2. Capstone Assignment 2.2/AddContactActivity.java: -------------------------------------------------------------------------------- 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 | 9 | /** 10 | * Add a new contact 11 | */ 12 | public class AddContactActivity extends AppCompatActivity { 13 | 14 | private ContactList contact_list = new ContactList(); 15 | private Context context; 16 | 17 | private EditText username; 18 | private EditText email; 19 | 20 | private ContactListController contactListController = new ContactListController(contact_list); 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_add_contact); 26 | 27 | username = (EditText) findViewById(R.id.username); 28 | email = (EditText) findViewById(R.id.email); 29 | 30 | context = getApplicationContext(); 31 | contactListController.loadContacts(context); 32 | } 33 | 34 | public void saveContact(View view) { 35 | 36 | String username_str = username.getText().toString(); 37 | String email_str = email.getText().toString(); 38 | 39 | if (username_str.equals("")) { 40 | username.setError("Empty field!"); 41 | return; 42 | } 43 | 44 | if (email_str.equals("")) { 45 | email.setError("Empty field!"); 46 | return; 47 | } 48 | 49 | if (!email_str.contains("@")){ 50 | email.setError("Must be an email address!"); 51 | return; 52 | } 53 | 54 | if (!contactListController.isUsernameAvailable(username_str)){ 55 | username.setError("Username already taken!"); 56 | return; 57 | } 58 | 59 | Contact contact = new Contact(username_str, email_str, null); 60 | 61 | if(!contactListController.addContact(contact, context)) return; 62 | 63 | // End AddContactActivity 64 | finish(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Sharing App - Code Smell/AddContactActivity.java: -------------------------------------------------------------------------------- 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 | 9 | /** 10 | * Add a new contact 11 | */ 12 | public class AddContactActivity extends AppCompatActivity { 13 | 14 | private ContactList contact_list = new ContactList(); 15 | private ContactListController contact_list_controller = new ContactListController(contact_list); 16 | 17 | private Context context; 18 | 19 | private EditText username; 20 | private EditText email; 21 | 22 | private String username_str; 23 | private String email_str; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_add_contact); 29 | 30 | username = (EditText) findViewById(R.id.username); 31 | email = (EditText) findViewById(R.id.email); 32 | 33 | context = getApplicationContext(); 34 | contact_list_controller.loadContacts(context); 35 | } 36 | 37 | public void saveContact(View view) { 38 | 39 | username_str = username.getText().toString(); 40 | email_str = email.getText().toString(); 41 | 42 | if(!validateInput()) return; 43 | 44 | Contact contact = new Contact(username_str, email_str, null); 45 | 46 | // Add contact 47 | boolean success = contact_list_controller.addContact(contact, context); 48 | if (!success) { 49 | return; 50 | } 51 | 52 | // End AddContactActivity 53 | finish(); 54 | } 55 | 56 | public boolean validateInput() { 57 | if (username_str.equals("")) { 58 | username.setError("Empty field!"); 59 | return false; 60 | } 61 | 62 | if (!email_str.contains("@")) { 63 | email.setError("Must be an email address!"); 64 | return false; 65 | } 66 | 67 | if (!contact_list_controller.isUsernameAvailable(username_str)){ 68 | username.setError("Username already taken!"); 69 | return false; 70 | } 71 | 72 | return true; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/2. Capstone Assignment 2.2/ContactListController.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Observer; 7 | 8 | /** 9 | * @author Bishwa on 05/11/2020 10 | */ 11 | public class ContactListController { 12 | private ContactList contactList; 13 | 14 | public ContactListController(ContactList contactList) { 15 | this.contactList = contactList; 16 | } 17 | 18 | public void setContacts(ArrayList contact_list) { 19 | contactList.setContacts(contact_list); 20 | } 21 | 22 | public ArrayList getContacts() { 23 | return contactList.getContacts(); 24 | } 25 | 26 | public boolean addContact(Contact contact, Context context){ 27 | AddContactCommand addContactCommand = new AddContactCommand(contactList, contact, context); 28 | addContactCommand.execute(); 29 | return addContactCommand.isExecuted(); 30 | } 31 | 32 | public boolean deleteContact(Contact contact, Context context) { 33 | DeleteContactCommand deleteContactCommand = new DeleteContactCommand(contactList, contact, context); 34 | deleteContactCommand.execute(); 35 | return deleteContactCommand.isExecuted(); 36 | } 37 | 38 | public boolean editContact(Contact contact, Contact updatedContact, Context context){ 39 | DeleteContactCommand deleteContactCommand = new DeleteContactCommand(contactList, contact, context); 40 | deleteContactCommand.execute(); 41 | return deleteContactCommand.isExecuted(); 42 | } 43 | 44 | public void addObserver(Observer observer) { 45 | contactList.addObserver(observer); 46 | } 47 | 48 | public void removeObserver(Observer observer) { 49 | contactList.deleteObserver(observer); 50 | } 51 | 52 | public void loadContacts(Context context) { 53 | contactList.loadContacts(context); 54 | } 55 | 56 | public boolean isUsernameAvailable(String username) { 57 | return contactList.isUsernameAvailable(username); 58 | } 59 | 60 | public Contact getContact(int index) { 61 | return contactList.getContact(index); 62 | } 63 | 64 | public Contact getContactByUsername(String username) { 65 | return contactList.getContactByUsername(username); 66 | } 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/BidListController.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | import java.util.ArrayList; 6 | 7 | /** 8 | * BidListController is responsible for all communication between views and BidList model 9 | */ 10 | public class BidListController { 11 | private BidList bid_list; 12 | 13 | public BidListController(BidList bid_list) { 14 | this.bid_list = bid_list; 15 | } 16 | 17 | public void setBids(ArrayList bid_list) { 18 | this.bid_list.setBids(bid_list); 19 | } 20 | 21 | public ArrayList getBids() { 22 | return bid_list.getBids(); 23 | } 24 | 25 | public boolean addBid(Bid bid){ 26 | AddBidCommand add_bid_command = new AddBidCommand(bid); 27 | add_bid_command.execute(); 28 | return add_bid_command.isExecuted(); 29 | } 30 | 31 | public boolean removeBid(Bid bid) { 32 | DeleteBidCommand delete_bid_command = new DeleteBidCommand(bid); 33 | delete_bid_command.execute(); 34 | return delete_bid_command.isExecuted(); 35 | } 36 | 37 | public boolean removeItemBids(String id) { 38 | DeleteBidCommand delete_bid_command; 39 | ArrayList old_bids = bid_list.getItemBids(id); 40 | 41 | for (Bid b : old_bids) { 42 | delete_bid_command = new DeleteBidCommand(b); 43 | delete_bid_command.execute(); 44 | if (!delete_bid_command.isExecuted()){ 45 | return false; 46 | } 47 | } 48 | return true; 49 | } 50 | 51 | public Bid getBid(int index) { 52 | return bid_list.getBid(index); 53 | } 54 | 55 | public boolean isEmpty() { 56 | return bid_list.isEmpty(); 57 | } 58 | 59 | public int getIndex(Bid bid) { 60 | return bid_list.getIndex(bid); 61 | } 62 | 63 | public int getSize() { 64 | return bid_list.getSize(); 65 | } 66 | 67 | public ArrayList getItemBids(String id) { 68 | return bid_list.getItemBids(id); 69 | } 70 | 71 | public Float getHighestBid(String id) { 72 | return bid_list.getHighestBid(id); 73 | } 74 | 75 | public String getHighestBidder(String id) { 76 | return bid_list.getHighestBidder(id); 77 | } 78 | 79 | public void getRemoteBids(Context context) { bid_list.getRemoteBids(context); } 80 | 81 | public boolean saveBids(Context context) { 82 | return bid_list.saveBids(context); 83 | } 84 | 85 | public void addObserver(Observer observer) { 86 | bid_list.addObserver(observer); 87 | } 88 | 89 | public void removeObserver(Observer observer) { 90 | bid_list.removeObserver(observer); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 2/1. Capstone Assignment 2.1/EditContactActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.widget.EditText; 9 | 10 | /** 11 | * Editing a pre-existing contact consists of deleting the old contact and adding a new contact with the old 12 | * contact's id. 13 | * Note: You will not be able contacts which are "active" borrowers 14 | */ 15 | public class EditContactActivity extends AppCompatActivity { 16 | 17 | private ContactList contact_list = new ContactList(); 18 | private Contact contact; 19 | private EditText email; 20 | private EditText username; 21 | private Context context; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_edit_contact); 27 | 28 | context = getApplicationContext(); 29 | contact_list.loadContacts(context); 30 | 31 | Intent intent = getIntent(); 32 | int pos = intent.getIntExtra("position", 0); 33 | 34 | contact = contact_list.getContact(pos); 35 | 36 | username = (EditText) findViewById(R.id.username); 37 | email = (EditText) findViewById(R.id.email); 38 | 39 | username.setText(contact.getUsername()); 40 | email.setText(contact.getEmail()); 41 | } 42 | 43 | public void saveContact(View view) { 44 | 45 | String email_str = email.getText().toString(); 46 | 47 | if (email_str.equals("")) { 48 | email.setError("Empty field!"); 49 | return; 50 | } 51 | 52 | if (!email_str.contains("@")){ 53 | email.setError("Must be an email address!"); 54 | return; 55 | } 56 | 57 | String username_str = username.getText().toString(); 58 | String id = contact.getId(); // Reuse the contact id 59 | 60 | // Check that username is unique AND username is changed (Note: if username was not changed 61 | // then this should be fine, because it was already unique.) 62 | if (!contact_list.isUsernameAvailable(username_str) && !(contact.getUsername().equals(username_str))) { 63 | username.setError("Username already taken!"); 64 | return; 65 | } 66 | 67 | Contact updated_contact = new Contact(username_str, email_str, id); 68 | 69 | Command command = new EditContactCommand(contact_list, contact, updated_contact, context); 70 | command.execute(); 71 | 72 | if(!command.isExecuted()) return; 73 | 74 | // End EditContactActivity 75 | finish(); 76 | } 77 | 78 | public void deleteContact(View view) { 79 | 80 | Command command = new DeleteContactCommand(contact_list, contact, context); 81 | command.execute(); 82 | 83 | if(!command.isExecuted()) return; 84 | 85 | // End EditContactActivity 86 | finish(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Buy Me A Coffee 2 | 3 | # Software Design and Architecture Specialization Assignments 4 | This repo contains all my assignment work while going through [Software Design and Architecture Specialization](https://www.coursera.org/specializations/software-design-architecture) in Coursera. 5 | 6 | #### Offered By 7 | [![N|Solid](https://d3njjcbhbojbot.cloudfront.net/api/utilities/v1/imageproxy/https://coursera-university-assets.s3.amazonaws.com/35/23d929219e4489c2d11215009dc699/coursera_180x180.png?auto=format%2Ccompress&dpr=1&w=120&h=120&q=40)](https://d3njjcbhbojbot.cloudfront.net/api/utilities/v1/imageproxy/https://coursera-university-assets.s3.amazonaws.com/35/23d929219e4489c2d11215009dc699/coursera_180x180.png?auto=format%2Ccompress&dpr=1&w=120&h=120&q=40) 8 | 9 | ## About this Specialization 10 | In the Software Design and Architecture Specialization, you will learn how to apply design principles, patterns, and architectures to create reusable and flexible software applications and systems. You will learn how to express and document the design and architecture of a software system using a visual notation. 11 | 12 | ### There are 4 Courses in this Specialization: 13 | 14 | ### [Object-Oriented Design](https://www.coursera.org/learn/object-oriented-design) 15 | This course takes Java beginners to the next level by covering object-oriented analysis and design. You will discover how to create modular, flexible, and reusable software, by applying object-oriented design principles and guidelines. And, you will be able to communicate these designs in a visual notation known as Unified Modelling Language (UML). 16 | 17 | ### [Design Patterns](https://www.coursera.org/learn/design-patterns) 18 | This course extends object-oriented analysis and design by incorporating design patterns to create interactive applications. Through a survey of established design patterns, you will gain a foundation for more complex software applications. Finally, you will identify problematic software designs by referencing a catalog of code smells. 19 | 20 | ### [Software Architecture](https://www.coursera.org/learn/software-architecture) 21 | The way that software components — subroutines, classes, functions, etc. — are arranged, and the interactions between them, is called architecture. In this course you will study the ways these architectures are represented, both in UML and other visual tools. We will introduce the most common architectures, their qualities, and tradeoffs. We will talk about how architectures are evaluated, what makes a good architecture, and an architecture can be improved. We'll also talk about how the architecture touches on the process of software development. 22 | 23 | ### [Service-Oriented Architecture](https://www.coursera.org/learn/service-oriented-architecture) 24 | Based on an understanding of architectural styles, you will review architectures for web applications, then explore the basics of Service-Oriented Architecture (SOA) in two approaches: Web Services (WS*) and Representational State Transfer (REST) architecture. 25 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/2. Capstone Assignment 2.2/EditContactActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.widget.EditText; 9 | 10 | import java.util.Observer; 11 | 12 | /** 13 | * Editing a pre-existing contact consists of deleting the old contact and adding a new contact with the old 14 | * contact's id. 15 | * Note: You will not be able contacts which are "active" borrowers 16 | */ 17 | public class EditContactActivity extends AppCompatActivity implements Observer { 18 | 19 | private ContactList contact_list = new ContactList(); 20 | private Contact contact; 21 | private EditText email; 22 | private EditText username; 23 | private Context context; 24 | 25 | private ContactController contactController = new ContactController(contact); 26 | private ContactListController contactListController = new ContactListController(contact_list); 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_edit_contact); 32 | 33 | context = getApplicationContext(); 34 | contactListController.loadContacts(context); 35 | 36 | Intent intent = getIntent(); 37 | int pos = intent.getIntExtra("position", 0); 38 | 39 | contact = contactListController.getContact(pos); 40 | 41 | username = (EditText) findViewById(R.id.username); 42 | email = (EditText) findViewById(R.id.email); 43 | 44 | username.setText(contactController.getUsername()); 45 | email.setText(contactController.getEmail()); 46 | } 47 | 48 | public void saveContact(View view) { 49 | 50 | String email_str = email.getText().toString(); 51 | 52 | if (email_str.equals("")) { 53 | email.setError("Empty field!"); 54 | return; 55 | } 56 | 57 | if (!email_str.contains("@")){ 58 | email.setError("Must be an email address!"); 59 | return; 60 | } 61 | 62 | String username_str = username.getText().toString(); 63 | String id = contactController.getId(); // Reuse the contact id 64 | 65 | // Check that username is unique AND username is changed (Note: if username was not changed 66 | // then this should be fine, because it was already unique.) 67 | if (!contactListController.isUsernameAvailable(username_str) && !(contactController.getUsername().equals(username_str))) { 68 | username.setError("Username already taken!"); 69 | return; 70 | } 71 | 72 | Contact updated_contact = new Contact(username_str, email_str, id); 73 | 74 | if(!contactListController.editContact(contact, updated_contact, context)) return; 75 | 76 | // End EditContactActivity 77 | finish(); 78 | } 79 | 80 | public void deleteContact(View view) { 81 | if(!contactListController.deleteContact(contact, context)) 82 | 83 | // End EditContactActivity 84 | finish(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/ItemFragmentAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.ArrayAdapter; 10 | import android.widget.ImageView; 11 | import android.widget.TextView; 12 | 13 | import java.util.ArrayList; 14 | 15 | /** 16 | * ItemFragmentAdapter is responsible for what information is displayed in ListView entries. 17 | */ 18 | public class ItemFragmentAdapter extends ArrayAdapter { 19 | private LayoutInflater inflater; 20 | private Fragment fragment; 21 | private Context context; 22 | 23 | public ItemFragmentAdapter(Context context, ArrayList items, Fragment fragment) { 24 | super(context, 0, items); 25 | inflater = LayoutInflater.from(context); 26 | this.fragment = fragment; 27 | this.context = context; 28 | } 29 | 30 | @Override 31 | public View getView(int position, View convertView, ViewGroup parent) { 32 | Item item = getItem(position); 33 | ItemController item_controller = new ItemController(item); 34 | 35 | String title = "Title: " + item_controller.getTitle(); 36 | String description = "Description: " + item_controller.getDescription(); 37 | Bitmap thumbnail = item_controller.getImage(); 38 | String status = "Status: " + item_controller.getStatus(); 39 | 40 | // Check if an existing view is being reused, otherwise inflate the view. 41 | if (convertView == null) { 42 | convertView = inflater.from(context).inflate(R.layout.itemlist_item, parent, false); 43 | } 44 | 45 | TextView title_tv = (TextView) convertView.findViewById(R.id.title_tv); 46 | TextView status_tv = (TextView) convertView.findViewById(R.id.status_tv); 47 | TextView description_tv = (TextView) convertView.findViewById(R.id.description_tv); 48 | ImageView photo = (ImageView) convertView.findViewById(R.id.image_view); 49 | TextView bidder_tv = (TextView) convertView.findViewById(R.id.bidder_tv); 50 | TextView highest_bid_tv = (TextView) convertView.findViewById(R.id.highest_bid_tv); 51 | 52 | if (thumbnail != null) { 53 | photo.setImageBitmap(thumbnail); 54 | } else { 55 | photo.setImageResource(android.R.drawable.ic_menu_gallery); 56 | } 57 | 58 | title_tv.setText(title); 59 | description_tv.setText(description); 60 | 61 | // AllItemFragments: itemlist_item shows title, description and status 62 | if (fragment instanceof AllItemsFragment ) { 63 | status_tv.setText(status); 64 | } 65 | 66 | // BorrowedItemsFragment/AvailableItemsFragment: itemlist_item shows title and description only 67 | if (fragment instanceof BorrowedItemsFragment || fragment instanceof AvailableItemsFragment 68 | || fragment instanceof BiddedItemsFragment) { 69 | status_tv.setVisibility(View.GONE); 70 | } 71 | 72 | // BiddedItemsFragment: itemlist_item shows bidder and highest bid 73 | if (fragment instanceof BiddedItemsFragment) { 74 | BidList bid_list = new BidList(); 75 | BidListController bid_list_controller = new BidListController(bid_list); 76 | bid_list_controller.getRemoteBids(parent.getContext()); 77 | 78 | String bidder = "Bidder: " + bid_list_controller.getHighestBidder(item_controller.getId()); 79 | String highest_bid = "Highest Bid: " + bid_list_controller.getHighestBid(item_controller.getId()); 80 | 81 | bidder_tv.setVisibility(View.VISIBLE); 82 | highest_bid_tv.setVisibility(View.VISIBLE); 83 | bidder_tv.setText(bidder); 84 | highest_bid_tv.setText(highest_bid); 85 | } 86 | 87 | return convertView; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /1. Object Oriented Design/Week 4/1. Capstone Assignment 1.4/ContactList.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | import com.google.gson.Gson; 6 | import com.google.gson.reflect.TypeToken; 7 | 8 | import java.io.FileInputStream; 9 | import java.io.FileNotFoundException; 10 | import java.io.FileOutputStream; 11 | import java.io.IOException; 12 | import java.io.InputStreamReader; 13 | import java.io.OutputStreamWriter; 14 | import java.lang.reflect.Type; 15 | import java.util.ArrayList; 16 | 17 | /** 18 | * Created by bishwa on 10/08/2020. 19 | */ 20 | 21 | public class ContactList { 22 | 23 | private ArrayList contacts; 24 | private String FILENAME = "contacts.sav"; 25 | 26 | public ContactList() { 27 | contacts = new ArrayList(); 28 | } 29 | 30 | public void setContacts(ArrayList contact_list) { 31 | contacts = contact_list; 32 | } 33 | 34 | public ArrayList getContacts() { 35 | return contacts; 36 | } 37 | 38 | public ArrayList getAllUsernames() { 39 | 40 | ArrayList allUsernames = new ArrayList(); 41 | 42 | for(Contact contact : contacts) { 43 | allUsernames.add(contact.getUsername()); 44 | } 45 | 46 | return allUsernames; 47 | } 48 | 49 | public void addContact(Contact contact) { 50 | contacts.add(contact); 51 | } 52 | 53 | public void deleteContact(Contact contact) { 54 | contacts.remove(contact); 55 | } 56 | 57 | public Contact getContact(int index) { 58 | return contacts.get(index); 59 | } 60 | 61 | public int getSize() { 62 | return contacts.size(); 63 | } 64 | 65 | public int getIndex(Contact contact) { 66 | int pos = 0; 67 | for (Contact c : contacts) { 68 | if(c.getId().equals(contact.getId())) { 69 | return pos; 70 | } 71 | pos += 1; 72 | } 73 | return -1; 74 | } 75 | 76 | public boolean hasContact(Contact contact) { 77 | return contacts.contains(contact); 78 | } 79 | 80 | public Contact getContactByUsername(String username) { 81 | 82 | for (Contact contact : contacts) { 83 | if(contact.getUsername().equals(username)) { 84 | return contact; 85 | } 86 | } 87 | return null; 88 | } 89 | 90 | public void loadContacts(Context context) { 91 | try { 92 | FileInputStream fis = context.openFileInput(FILENAME); 93 | InputStreamReader isr = new InputStreamReader(fis); 94 | Gson gson = new Gson(); 95 | Type listType = new TypeToken>() {}.getType(); 96 | contacts = gson.fromJson(isr, listType); // temporary 97 | fis.close(); 98 | } catch (FileNotFoundException e) { 99 | contacts = new ArrayList(); 100 | } catch (IOException e) { 101 | contacts = new ArrayList(); 102 | } 103 | } 104 | 105 | public void saveContacts(Context context) { 106 | try { 107 | FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_APPEND); 108 | OutputStreamWriter osw = new OutputStreamWriter(fos); 109 | Gson gson = new Gson(); 110 | gson.toJson(contacts, osw); 111 | osw.flush(); 112 | fos.close(); 113 | } catch (FileNotFoundException e) { 114 | e.printStackTrace(); 115 | } catch (IOException e) { 116 | e.printStackTrace(); 117 | } 118 | } 119 | 120 | public boolean isUsernameAvailable(String username) { 121 | 122 | for (Contact contact : contacts) { 123 | if(contact.getUsername().equals(username)) { 124 | return false; 125 | } 126 | } 127 | return true; 128 | } 129 | } -------------------------------------------------------------------------------- /2. Design Patterns/Week 3/2. Capstone Assignment 2.2/ContactList.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | import com.google.gson.Gson; 6 | import com.google.gson.reflect.TypeToken; 7 | 8 | import java.io.FileInputStream; 9 | import java.io.FileNotFoundException; 10 | import java.io.FileOutputStream; 11 | import java.io.IOException; 12 | import java.io.InputStreamReader; 13 | import java.io.OutputStreamWriter; 14 | import java.lang.reflect.Type; 15 | import java.util.ArrayList; 16 | import java.util.Observable; 17 | 18 | /** 19 | * ContactList class 20 | */ 21 | public class ContactList extends Observable { 22 | 23 | private static ArrayList contacts; 24 | private String FILENAME = "contacts.sav"; 25 | 26 | public ContactList() { 27 | contacts = new ArrayList(); 28 | } 29 | 30 | public void setContacts(ArrayList contact_list) { 31 | contacts = contact_list; 32 | notifyObservers(); 33 | } 34 | 35 | public ArrayList getContacts() { 36 | return contacts; 37 | } 38 | 39 | public ArrayList getAllUsernames(){ 40 | ArrayList username_list = new ArrayList(); 41 | for (Contact u : contacts){ 42 | username_list.add(u.getUsername()); 43 | } 44 | return username_list; 45 | } 46 | 47 | public void addContact(Contact contact) { 48 | contacts.add(contact); 49 | notifyObservers(); 50 | } 51 | 52 | public void deleteContact(Contact contact) { 53 | contacts.remove(contact); 54 | notifyObservers(); 55 | } 56 | 57 | public Contact getContact(int index) { 58 | return contacts.get(index); 59 | } 60 | 61 | public int getSize() { 62 | return contacts.size(); 63 | } 64 | 65 | public Contact getContactByUsername(String username){ 66 | for (Contact c : contacts){ 67 | if (c.getUsername().equals(username)){ 68 | return c; 69 | } 70 | } 71 | return null; 72 | } 73 | 74 | public boolean hasContact(Contact contact) { 75 | for (Contact c : contacts) { 76 | if (contact.getId().equals(c.getId())) { 77 | return true; 78 | } 79 | } 80 | return false; 81 | } 82 | 83 | public int getIndex(Contact contact) { 84 | int pos = 0; 85 | for (Contact c : contacts) { 86 | if (contact.getId().equals(c.getId())) { 87 | return pos; 88 | } 89 | pos = pos+1; 90 | } 91 | return -1; 92 | } 93 | 94 | public boolean isUsernameAvailable(String username){ 95 | for (Contact c : contacts) { 96 | if (c.getUsername().equals(username)) { 97 | return false; 98 | } 99 | } 100 | return true; 101 | } 102 | 103 | public void loadContacts(Context context) { 104 | 105 | try { 106 | FileInputStream fis = context.openFileInput(FILENAME); 107 | InputStreamReader isr = new InputStreamReader(fis); 108 | Gson gson = new Gson(); 109 | Type listType = new TypeToken>() {}.getType(); 110 | contacts = gson.fromJson(isr, listType); // temporary 111 | fis.close(); 112 | } catch (FileNotFoundException e) { 113 | contacts = new ArrayList(); 114 | } catch (IOException e) { 115 | contacts = new ArrayList(); 116 | } 117 | 118 | notifyObservers(); 119 | } 120 | 121 | public boolean saveContacts(Context context) { 122 | try { 123 | FileOutputStream fos = context.openFileOutput(FILENAME, 0); 124 | OutputStreamWriter osw = new OutputStreamWriter(fos); 125 | Gson gson = new Gson(); 126 | gson.toJson(contacts, osw); 127 | osw.flush(); 128 | fos.close(); 129 | } catch (FileNotFoundException e) { 130 | e.printStackTrace(); 131 | return false; 132 | } catch (IOException e) { 133 | e.printStackTrace(); 134 | return false; 135 | } 136 | 137 | return true; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Sharing App - Code Smell/EditContactActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.widget.EditText; 9 | 10 | /** 11 | * Editing a pre-existing contact consists of deleting the old contact and adding a new contact with the old 12 | * contact's id. 13 | * Note: You will not be able to edit contacts which are "active" borrowers 14 | */ 15 | public class EditContactActivity extends AppCompatActivity implements Observer { 16 | 17 | private ContactList contact_list = new ContactList(); 18 | private ContactListController contact_list_controller = new ContactListController(contact_list); 19 | 20 | private Contact contact; 21 | private ContactController contact_controller; 22 | 23 | private EditText email; 24 | private EditText username; 25 | 26 | private String email_str; 27 | private String username_str; 28 | 29 | private Context context; 30 | private int pos; 31 | private boolean on_create_update = true; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_edit_contact); 37 | 38 | Intent intent = getIntent(); 39 | pos = intent.getIntExtra("position", 0); 40 | 41 | context = getApplicationContext(); 42 | contact_list_controller.addObserver(this); 43 | contact_list_controller.loadContacts(context); 44 | on_create_update = false; 45 | } 46 | 47 | public void saveContact(View view) { 48 | 49 | email_str = email.getText().toString(); 50 | username_str = username.getText().toString(); 51 | 52 | if(!validateInput()) return; 53 | 54 | // Reuse the contact id 55 | String id_str = contact_controller.getId(); 56 | Contact updated_contact = new Contact(username_str, email_str, id_str); 57 | 58 | // Edit Contact: replace contact with updated contact 59 | boolean success = contact_list_controller.editContact(contact, updated_contact, context); 60 | if (!success) { 61 | return; 62 | } 63 | 64 | // End EditContactActivity 65 | finish(); 66 | } 67 | 68 | public boolean validateInput() { 69 | if (username_str.equals("")) { 70 | username.setError("Empty field!"); 71 | return false; 72 | } 73 | 74 | if (!email_str.contains("@")) { 75 | email.setError("Must be an email address!"); 76 | return false; 77 | } 78 | 79 | // Check that username is unique AND username is changed (Note: if username was not changed 80 | // then this should be fine, because it was already unique.) 81 | if (!contact_list_controller.isUsernameAvailable(username_str) && 82 | !(contact.getUsername().equals(username_str))){ 83 | username.setError("Username already taken!"); 84 | return false; 85 | } 86 | 87 | return true; 88 | } 89 | 90 | public void deleteContact(View view) { 91 | 92 | // Delete contact 93 | boolean success = contact_list_controller.deleteContact(contact, context); 94 | if (!success) { 95 | return; 96 | } 97 | 98 | // End EditContactActivity 99 | finish(); 100 | } 101 | 102 | /** 103 | * Called when the activity is destroyed, thus we remove this activity as a listener 104 | */ 105 | @Override 106 | protected void onDestroy() { 107 | super.onDestroy(); 108 | contact_list_controller.removeObserver(this); 109 | } 110 | 111 | /** 112 | * Only need to update the view from the onCreate method 113 | */ 114 | public void update(){ 115 | 116 | if (on_create_update) { 117 | 118 | contact = contact_list_controller.getContact(pos); 119 | contact_controller = new ContactController(contact); 120 | 121 | username = (EditText) findViewById(R.id.username); 122 | email = (EditText) findViewById(R.id.email); 123 | 124 | // Update the view 125 | username.setText(contact_controller.getUsername()); 126 | email.setText(contact_controller.getEmail()); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/BidList.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | 5 | import com.google.gson.Gson; 6 | import com.google.gson.reflect.TypeToken; 7 | 8 | import java.io.FileInputStream; 9 | import java.io.FileNotFoundException; 10 | import java.io.FileOutputStream; 11 | import java.io.IOException; 12 | import java.io.InputStreamReader; 13 | import java.io.OutputStreamWriter; 14 | import java.lang.reflect.Type; 15 | import java.util.ArrayList; 16 | import java.util.concurrent.ExecutionException; 17 | 18 | /** 19 | * BidList Class 20 | */ 21 | public class BidList extends Observable { 22 | private static ArrayList bids; 23 | private String FILENAME = "bids.sav"; 24 | 25 | public BidList() { 26 | bids = new ArrayList(); 27 | } 28 | 29 | public void setBids(ArrayList bid_list) { 30 | bids = bid_list; 31 | notifyObservers(); 32 | } 33 | 34 | public ArrayList getBids() { 35 | return bids; 36 | } 37 | 38 | public void addBid(Bid bid){ 39 | bids.add(bid); 40 | notifyObservers(); 41 | } 42 | 43 | public void removeBid(Bid bid){ 44 | bids.remove(bid); 45 | notifyObservers(); 46 | } 47 | 48 | public Bid getBid(int index) { 49 | return bids.get(index); 50 | } 51 | 52 | public boolean isEmpty() { 53 | if (bids.isEmpty()) { 54 | return true; 55 | } 56 | return false; 57 | } 58 | 59 | public int getIndex(Bid bid) { 60 | int pos = 0; 61 | for (Bid b : bids) { 62 | if (bid.getBidId().equals(b.getBidId())) { 63 | return pos; 64 | } 65 | pos = pos + 1; 66 | } 67 | return -1; 68 | } 69 | 70 | public int getSize() { 71 | return bids.size(); 72 | } 73 | 74 | // Used by getHighestBid and BidListController 75 | public ArrayList getItemBids(String id){ 76 | ArrayList item_bids = new ArrayList(); 77 | for (Bid b : bids) { 78 | if (b.getItemId().equals(id)) { 79 | item_bids.add(b); 80 | } 81 | } 82 | return item_bids; 83 | } 84 | 85 | // Get highest bid_amount of all bids 86 | public Float getHighestBid(String id) { 87 | 88 | ArrayList item_bids = getItemBids(id); 89 | 90 | if (item_bids.isEmpty()){ 91 | return null; 92 | } 93 | 94 | Float highest_bid_amount = item_bids.get(0).getBidAmount(); // Initialize 95 | for (Bid b : item_bids) { 96 | if (b.getBidAmount() > highest_bid_amount) { 97 | highest_bid_amount = b.getBidAmount(); 98 | } 99 | } 100 | 101 | return highest_bid_amount; 102 | } 103 | 104 | public String getHighestBidder(String id) { 105 | ArrayList item_bids = getItemBids(id); 106 | 107 | if (item_bids.isEmpty()){ 108 | return null; 109 | } 110 | 111 | Float highest_bid_amount = item_bids.get(0).getBidAmount(); // Initialize 112 | String highest_bidder = item_bids.get(0).getBidderUsername(); 113 | for (Bid b : item_bids) { 114 | if (b.getBidAmount() > highest_bid_amount) { 115 | highest_bid_amount = b.getBidAmount(); 116 | highest_bidder = b.getBidderUsername(); 117 | } 118 | } 119 | 120 | return highest_bidder; 121 | } 122 | 123 | public void getRemoteBids(Context context) { 124 | ElasticSearchManager.GetBidListTask get_bid_list_task = new ElasticSearchManager.GetBidListTask(); 125 | get_bid_list_task.execute(); 126 | 127 | try { 128 | bids = get_bid_list_task.get(); 129 | } catch (InterruptedException | ExecutionException e) { 130 | e.printStackTrace(); 131 | } 132 | notifyObservers(); 133 | } 134 | 135 | 136 | public boolean saveBids(Context context) { 137 | try { 138 | FileOutputStream fos = context.openFileOutput(FILENAME, 0); 139 | OutputStreamWriter osw = new OutputStreamWriter(fos); 140 | Gson gson = new Gson(); 141 | gson.toJson(bids, osw); 142 | osw.flush(); 143 | fos.close(); 144 | } catch (FileNotFoundException e) { 145 | e.printStackTrace(); 146 | return false; 147 | } catch (IOException e) { 148 | e.printStackTrace(); 149 | return false; 150 | } 151 | return true; 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Sharing App - Code Smell/AddItemActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.provider.MediaStore; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.os.Bundle; 9 | import android.view.View; 10 | import android.widget.EditText; 11 | import android.widget.ImageView; 12 | 13 | /** 14 | * Add a new item 15 | */ 16 | public class AddItemActivity extends AppCompatActivity { 17 | 18 | private EditText title; 19 | private EditText maker; 20 | private EditText description; 21 | private EditText length; 22 | private EditText width; 23 | private EditText height; 24 | 25 | private String title_str; 26 | private String maker_str; 27 | private String description_str; 28 | private String length_str; 29 | private String width_str; 30 | private String height_str; 31 | 32 | private ImageView photo; 33 | private Bitmap image; 34 | private int REQUEST_CODE = 1; 35 | 36 | private ItemList item_list = new ItemList(); 37 | private ItemListController item_list_controller = new ItemListController(item_list); 38 | 39 | private Context context; 40 | 41 | @Override 42 | protected void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | 45 | setContentView(R.layout.activity_add_item); 46 | 47 | title = (EditText) findViewById(R.id.title); 48 | maker = (EditText) findViewById(R.id.maker); 49 | description = (EditText) findViewById(R.id.description); 50 | length = (EditText) findViewById(R.id.length); 51 | width = (EditText) findViewById(R.id.width); 52 | height = (EditText) findViewById(R.id.height); 53 | photo = (ImageView) findViewById(R.id.image_view); 54 | 55 | photo.setImageResource(android.R.drawable.ic_menu_gallery); 56 | 57 | context = getApplicationContext(); 58 | item_list_controller.loadItems(context); 59 | } 60 | 61 | public void saveItem (View view) { 62 | 63 | title_str = title.getText().toString(); 64 | maker_str = maker.getText().toString(); 65 | description_str = description.getText().toString(); 66 | length_str = length.getText().toString(); 67 | width_str = width.getText().toString(); 68 | height_str = height.getText().toString(); 69 | 70 | if(!validateInput()) return; 71 | 72 | Item item = new Item(title_str, maker_str, description_str, image, null); 73 | ItemController item_controller = new ItemController(item); 74 | item_controller.setDimensions(length_str, width_str, height_str); 75 | 76 | // Add item 77 | boolean success = item_list_controller.addItem(item, context); 78 | if (!success) { 79 | return; 80 | } 81 | 82 | // End AddItemActivity 83 | Intent intent = new Intent(this, MainActivity.class); 84 | startActivity(intent); 85 | } 86 | 87 | public boolean validateInput() { 88 | if (title_str.equals("")) { 89 | title.setError("Empty field!"); 90 | return false; 91 | } 92 | 93 | if (maker_str.equals("")) { 94 | maker.setError("Empty field!"); 95 | return false; 96 | } 97 | 98 | if (description_str.equals("")) { 99 | description.setError("Empty field!"); 100 | return false; 101 | } 102 | 103 | if (length_str.equals("")) { 104 | length.setError("Empty field!"); 105 | return false; 106 | } 107 | 108 | if (width_str.equals("")) { 109 | width.setError("Empty field!"); 110 | return false; 111 | } 112 | 113 | if (height_str.equals("")) { 114 | height.setError("Empty field!"); 115 | return false; 116 | } 117 | 118 | return true; 119 | } 120 | 121 | public void addPhoto(View view) { 122 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 123 | if (intent.resolveActivity(getPackageManager()) != null) { 124 | startActivityForResult(intent, REQUEST_CODE); 125 | } 126 | } 127 | 128 | public void deletePhoto(View view) { 129 | image = null; 130 | photo.setImageResource(android.R.drawable.ic_menu_gallery); 131 | } 132 | 133 | @Override 134 | protected void onActivityResult(int request_code, int result_code, Intent intent){ 135 | if (request_code == REQUEST_CODE && result_code == RESULT_OK){ 136 | Bundle extras = intent.getExtras(); 137 | image = (Bitmap) extras.get("data"); 138 | photo.setImageBitmap(image); 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/ViewItemBidsActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.os.Bundle; 7 | import android.os.Handler; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.view.View; 10 | import android.widget.ArrayAdapter; 11 | import android.widget.ListView; 12 | import android.widget.Toast; 13 | 14 | import java.util.ArrayList; 15 | 16 | public class ViewItemBidsActivity extends AppCompatActivity implements Observer { 17 | 18 | private BidList bid_list = new BidList(); 19 | private BidListController bid_list_controller = new BidListController(bid_list); 20 | 21 | private ArrayList item_bid_list; // Bids placed on the item 22 | 23 | private ItemList item_list = new ItemList(); 24 | private ItemListController item_list_controller = new ItemListController(item_list); 25 | 26 | private UserList user_list = new UserList(); 27 | private UserListController user_list_controller = new UserListController(user_list); 28 | 29 | private Context context; 30 | 31 | private ListView item_bids; 32 | private ArrayAdapter adapter; 33 | 34 | private String user_id; 35 | private String item_id; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_view_item_bids); 41 | 42 | Intent intent = getIntent(); // Get intent from EditItemActivity 43 | user_id = intent.getStringExtra("user_id"); 44 | item_id = intent.getStringExtra("item_id"); 45 | 46 | context = getApplicationContext(); 47 | 48 | bid_list_controller.getRemoteBids(context); 49 | bid_list_controller.addObserver(this); 50 | item_bid_list = bid_list_controller.getItemBids(item_id); 51 | 52 | item_list_controller.addObserver(this); 53 | item_list_controller.getRemoteItems(); 54 | user_list_controller.getRemoteUsers(); 55 | } 56 | 57 | public void acceptBid(View view) { 58 | int pos = item_bids.getPositionForView(view); 59 | 60 | Bid bid = adapter.getItem(pos); 61 | BidController bid_controller = new BidController(bid); 62 | 63 | Item item = item_list_controller.getItemById(item_id); 64 | ItemController item_controller = new ItemController(item); 65 | 66 | String borrower_username = bid_controller.getBidderUsername(); 67 | User borrower = user_list_controller.getUserByUsername(borrower_username); 68 | 69 | String title = item_controller.getTitle(); 70 | String maker = item_controller.getMaker(); 71 | String description = item_controller.getDescription(); 72 | String owner_id = item_controller.getOwnerId(); 73 | String minimum_bid = item_controller.getMinBid().toString(); 74 | Bitmap image = item_controller.getImage(); 75 | String length = item_controller.getLength(); 76 | String width = item_controller.getWidth(); 77 | String height = item_controller.getHeight(); 78 | String status = "Borrowed"; 79 | 80 | Item updated_item = new Item(title, maker, description, owner_id, minimum_bid, image, item_id); 81 | ItemController updated_item_controller = new ItemController(updated_item); 82 | updated_item_controller.setDimensions(length, width, height); 83 | updated_item_controller.setStatus(status); 84 | updated_item_controller.setBorrower(borrower); 85 | 86 | boolean success = item_list_controller.editItem(item, updated_item); 87 | if (!success){ 88 | return; 89 | } 90 | 91 | // Delete all bids related to that item. 92 | success = bid_list_controller.removeItemBids(item_id); 93 | if (!success){ 94 | return; 95 | } 96 | 97 | item_list_controller.removeObserver(this); 98 | bid_list_controller.removeObserver(this); 99 | 100 | // End ViewItemBidsActivity 101 | final Intent intent = new Intent(this, MainActivity.class); 102 | intent.putExtra("user_id", user_id); 103 | 104 | // Delay launch of MainActivity to allow server enough time to process request 105 | new Handler().postDelayed(new Runnable() { 106 | @Override 107 | public void run() { 108 | Toast.makeText(context, "Bid accepted.", Toast.LENGTH_SHORT).show(); 109 | startActivity(intent); 110 | } 111 | }, 750); 112 | } 113 | 114 | public void declineBid(View view){ 115 | int pos = item_bids.getPositionForView(view); 116 | 117 | Bid bid = adapter.getItem(pos); 118 | 119 | Item item = item_list_controller.getItemById(item_id); 120 | ItemController item_controller = new ItemController(item); 121 | 122 | String title = item_controller.getTitle(); 123 | String maker = item_controller.getMaker(); 124 | String description = item_controller.getDescription(); 125 | String owner_id = item_controller.getOwnerId(); 126 | String minimum_bid = item_controller.getMinBid().toString(); 127 | Bitmap image = item_controller.getImage(); 128 | String length = item_controller.getLength(); 129 | String width = item_controller.getWidth(); 130 | String height = item_controller.getHeight(); 131 | String status = item_controller.getStatus(); 132 | 133 | // Delete selected bid. 134 | Boolean success = bid_list_controller.removeBid(bid); 135 | if (!success){ 136 | return; 137 | } 138 | 139 | item_bid_list.remove(bid); 140 | bid_list_controller.saveBids(context); // Save the changes, call to update 141 | 142 | if (item_bid_list.isEmpty()) { 143 | status = "Available"; 144 | } 145 | 146 | Item updated_item = new Item(title, maker, description, owner_id, minimum_bid, image, item_id); 147 | ItemController updated_item_controller = new ItemController(updated_item); 148 | updated_item_controller.setDimensions(length, width, height); 149 | updated_item_controller.setStatus(status); 150 | 151 | success = item_list_controller.editItem(item, updated_item); 152 | if (!success){ 153 | return; 154 | } 155 | 156 | item_list_controller.removeObserver(this); 157 | bid_list_controller.removeObserver(this); 158 | 159 | if (status.equals("Available")){ // No bids remain 160 | Toast.makeText(context, "All bids declined.", Toast.LENGTH_SHORT).show(); 161 | 162 | } else { // Some bids remain 163 | Toast.makeText(context, "Bid declined.", Toast.LENGTH_SHORT).show(); 164 | } 165 | 166 | // End ViewItemBidsActivity 167 | final Intent intent = new Intent(this, MainActivity.class); 168 | intent.putExtra("user_id", user_id); 169 | 170 | // Delay launch of MainActivity to allow server enough time to process request 171 | new Handler().postDelayed(new Runnable() { 172 | @Override 173 | public void run() { 174 | startActivity(intent); 175 | } 176 | }, 750); 177 | 178 | } 179 | 180 | public void declineAllBids(View view){ 181 | Item item = item_list_controller.getItemById(item_id); 182 | ItemController item_controller = new ItemController(item); 183 | 184 | String title = item_controller.getTitle(); 185 | String maker = item_controller.getMaker(); 186 | String description = item_controller.getDescription(); 187 | String owner_id = item_controller.getOwnerId(); 188 | String minimum_bid = item_controller.getMinBid().toString(); 189 | Bitmap image = item_controller.getImage(); 190 | String length = item_controller.getLength(); 191 | String width = item_controller.getWidth(); 192 | String height = item_controller.getHeight(); 193 | String status = "Available"; 194 | 195 | Item updated_item = new Item(title, maker, description, owner_id, minimum_bid, image, item_id); 196 | ItemController updated_item_controller = new ItemController(updated_item); 197 | updated_item_controller.setDimensions(length, width, height); 198 | updated_item_controller.setStatus(status); 199 | 200 | boolean success = item_list_controller.editItem(item, updated_item); 201 | if (!success){ 202 | return; 203 | } 204 | 205 | // Delete all bids related to that item. 206 | success = bid_list_controller.removeItemBids(item_id); 207 | if (!success){ 208 | return; 209 | } 210 | 211 | item_list_controller.removeObserver(this); 212 | bid_list_controller.removeObserver(this); 213 | 214 | // End ViewItemBidsActivity 215 | final Intent intent = new Intent(this, MainActivity.class); 216 | intent.putExtra("user_id", user_id); 217 | 218 | // Delay launch of MainActivity to allow server enough time to process request 219 | new Handler().postDelayed(new Runnable() { 220 | @Override 221 | public void run() { 222 | Toast.makeText(context, "All bids declined.", Toast.LENGTH_SHORT).show(); 223 | startActivity(intent); 224 | } 225 | }, 750); 226 | } 227 | 228 | /** 229 | * Update the view 230 | */ 231 | public void update() { 232 | item_bids = (ListView) findViewById(R.id.item_bids); 233 | adapter = new BidAdapter(this, item_bid_list); 234 | item_bids.setAdapter(adapter); 235 | adapter.notifyDataSetChanged(); 236 | } 237 | } 238 | -------------------------------------------------------------------------------- /2. Design Patterns/Week 4/1. Capstone Assignment 2.3/Sharing App - Code Smell/EditItemActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.provider.MediaStore; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.os.Bundle; 9 | import android.view.View; 10 | import android.widget.ArrayAdapter; 11 | import android.widget.EditText; 12 | import android.widget.ImageView; 13 | import android.widget.Spinner; 14 | import android.widget.Switch; 15 | import android.widget.TextView; 16 | 17 | /** 18 | * Editing a pre-existing item consists of deleting the old item and adding a new item with the old 19 | * item's id. 20 | * Note: invisible EditText is used to setError for status. For whatever reason we cannot .setError to 21 | * the status Switch so instead an error is set to an "invisible" EditText. 22 | */ 23 | public class EditItemActivity extends AppCompatActivity implements Observer { 24 | 25 | private ItemList item_list = new ItemList(); 26 | private ItemListController item_list_controller = new ItemListController(item_list); 27 | 28 | private Item item; 29 | private ItemController item_controller; 30 | 31 | private Context context; 32 | 33 | private ContactList contact_list = new ContactList(); 34 | private ContactListController contact_list_controller = new ContactListController(contact_list); 35 | 36 | private Bitmap image; 37 | private int REQUEST_CODE = 1; 38 | private ImageView photo; 39 | 40 | private EditText title; 41 | private EditText maker; 42 | private EditText description; 43 | private EditText length; 44 | private EditText width; 45 | private EditText height; 46 | private Spinner borrower_spinner; 47 | private TextView borrower_tv; 48 | private Switch status; 49 | private EditText invisible; 50 | 51 | private String title_str; 52 | private String maker_str; 53 | private String description_str; 54 | private String length_str; 55 | private String width_str; 56 | private String height_str; 57 | 58 | 59 | private ArrayAdapter adapter; 60 | private boolean on_create_update = false; 61 | private int pos; 62 | 63 | @Override 64 | protected void onCreate(Bundle savedInstanceState) { 65 | super.onCreate(savedInstanceState); 66 | setContentView(R.layout.activity_edit_item); 67 | 68 | title = (EditText) findViewById(R.id.title); 69 | maker = (EditText) findViewById(R.id.maker); 70 | description = (EditText) findViewById(R.id.description); 71 | length = (EditText) findViewById(R.id.length); 72 | width = (EditText) findViewById(R.id.width); 73 | height = (EditText) findViewById(R.id.height); 74 | borrower_spinner = (Spinner) findViewById(R.id.borrower_spinner); 75 | borrower_tv = (TextView) findViewById(R.id.borrower_tv); 76 | photo = (ImageView) findViewById(R.id.image_view); 77 | status = (Switch) findViewById(R.id.available_switch); 78 | invisible = (EditText) findViewById(R.id.invisible); 79 | 80 | invisible.setVisibility(View.GONE); 81 | 82 | Intent intent = getIntent(); // Get intent from ItemsFragment 83 | pos = intent.getIntExtra("position", 0); 84 | 85 | context = getApplicationContext(); 86 | 87 | item_list_controller.addObserver(this); 88 | item_list_controller.loadItems(context); 89 | 90 | on_create_update = true; 91 | 92 | contact_list_controller.addObserver(this); 93 | contact_list_controller.loadContacts(context); 94 | 95 | on_create_update = false; 96 | } 97 | 98 | public void addPhoto(View view) { 99 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 100 | if (intent.resolveActivity(getPackageManager()) != null) { 101 | startActivityForResult(intent, REQUEST_CODE); 102 | } 103 | } 104 | 105 | public void deletePhoto(View view) { 106 | image = null; 107 | photo.setImageResource(android.R.drawable.ic_menu_gallery); 108 | } 109 | 110 | @Override 111 | protected void onActivityResult(int request_code, int result_code, Intent intent){ 112 | if (request_code == REQUEST_CODE && result_code == RESULT_OK){ 113 | Bundle extras = intent.getExtras(); 114 | image = (Bitmap) extras.get("data"); 115 | photo.setImageBitmap(image); 116 | } 117 | } 118 | 119 | public void deleteItem(View view) { 120 | 121 | // Delete item 122 | boolean success = item_list_controller.deleteItem(item, context); 123 | if (!success) { 124 | return; 125 | } 126 | 127 | // End EditItemActivity 128 | item_list_controller.removeObserver(this); 129 | 130 | Intent intent = new Intent(this, MainActivity.class); 131 | startActivity(intent); 132 | } 133 | 134 | public void saveItem(View view) { 135 | 136 | title_str = title.getText().toString(); 137 | maker_str = maker.getText().toString(); 138 | description_str = description.getText().toString(); 139 | length_str = length.getText().toString(); 140 | width_str = width.getText().toString(); 141 | height_str = height.getText().toString(); 142 | 143 | Contact contact = null; 144 | if (!status.isChecked()) { 145 | String borrower_str = borrower_spinner.getSelectedItem().toString(); 146 | contact = contact_list_controller.getContactByUsername(borrower_str); 147 | } 148 | 149 | if(!validateInput()) return; 150 | 151 | String id = item_controller.getId(); // Reuse the item id 152 | Item updated_item = new Item(title_str, maker_str, description_str, image, id); 153 | ItemController updated_item_controller = new ItemController(updated_item); 154 | updated_item_controller.setDimensions(length_str, width_str, height_str); 155 | 156 | boolean checked = status.isChecked(); 157 | if (!checked) { 158 | updated_item_controller.setStatus("Borrowed"); 159 | updated_item_controller.setBorrower(contact); 160 | } 161 | 162 | // Edit item 163 | boolean success = item_list_controller.editItem(item, updated_item, context); 164 | if (!success) { 165 | return; 166 | } 167 | 168 | // End EditItemActivity 169 | item_list_controller.removeObserver(this); 170 | 171 | Intent intent = new Intent(this, MainActivity.class); 172 | startActivity(intent); 173 | } 174 | 175 | public boolean validateInput() { 176 | if (title_str.equals("")) { 177 | title.setError("Empty field!"); 178 | return false; 179 | } 180 | 181 | if (maker_str.equals("")) { 182 | maker.setError("Empty field!"); 183 | return false; 184 | } 185 | 186 | if (description_str.equals("")) { 187 | description.setError("Empty field!"); 188 | return false; 189 | } 190 | 191 | if (length_str.equals("")) { 192 | length.setError("Empty field!"); 193 | return false; 194 | } 195 | 196 | if (width_str.equals("")) { 197 | width.setError("Empty field!"); 198 | return false; 199 | } 200 | 201 | if (height_str.equals("")) { 202 | height.setError("Empty field!"); 203 | return false; 204 | } 205 | 206 | return true; 207 | } 208 | 209 | /** 210 | * Checked == "Available" 211 | * Unchecked == "Borrowed" 212 | */ 213 | public void toggleSwitch(View view){ 214 | if (status.isChecked()) { 215 | // Means was previously borrowed, switch was toggled to available 216 | borrower_spinner.setVisibility(View.GONE); 217 | borrower_tv.setVisibility(View.GONE); 218 | item_controller.setBorrower(null); 219 | item_controller.setStatus("Available"); 220 | 221 | } else { 222 | // Means not borrowed 223 | if (contact_list.getSize()==0){ 224 | // No contacts, need to add contacts to be able to add a borrower 225 | invisible.setEnabled(false); 226 | invisible.setVisibility(View.VISIBLE); 227 | invisible.requestFocus(); 228 | invisible.setError("No contacts available! Must add borrower to contacts."); 229 | status.setChecked(true); // Set switch to available 230 | 231 | } else { 232 | // Means was previously available 233 | borrower_spinner.setVisibility(View.VISIBLE); 234 | borrower_tv.setVisibility(View.VISIBLE); 235 | } 236 | } 237 | } 238 | 239 | /** 240 | * Only need to update the view from the onCreate method 241 | */ 242 | public void update() { 243 | if (on_create_update){ 244 | adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, 245 | contact_list_controller.getAllUsernames()); 246 | borrower_spinner.setAdapter(adapter); 247 | 248 | item = item_list_controller.getItem(pos); 249 | item_controller = new ItemController(item); 250 | 251 | Contact contact = item_controller.getBorrower(); 252 | if (contact != null){ 253 | int contact_pos = contact_list_controller.getIndex(contact); 254 | borrower_spinner.setSelection(contact_pos); 255 | } 256 | 257 | title.setText(item_controller.getTitle()); 258 | maker.setText(item_controller.getMaker()); 259 | description.setText(item_controller.getDescription()); 260 | 261 | length.setText(item_controller.getLength()); 262 | width.setText(item_controller.getWidth()); 263 | height.setText(item_controller.getHeight()); 264 | 265 | String status_str = item_controller.getStatus(); 266 | if (status_str.equals("Borrowed")) { 267 | status.setChecked(false); 268 | } else { 269 | borrower_tv.setVisibility(View.GONE); 270 | borrower_spinner.setVisibility(View.GONE); 271 | } 272 | 273 | image = item_controller.getImage(); 274 | if (image != null) { 275 | photo.setImageBitmap(image); 276 | } else { 277 | photo.setImageResource(android.R.drawable.ic_menu_gallery); 278 | } 279 | } 280 | } 281 | } 282 | -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/ViewItemActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.graphics.Bitmap; 6 | import android.os.Handler; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.os.Bundle; 9 | import android.view.View; 10 | import android.widget.Button; 11 | import android.widget.EditText; 12 | import android.widget.ImageView; 13 | import android.widget.TextView; 14 | import android.widget.Toast; 15 | 16 | public class ViewItemActivity extends AppCompatActivity implements Observer { 17 | 18 | private ItemList item_list = new ItemList(); 19 | private ItemListController item_list_controller = new ItemListController(item_list); 20 | 21 | private Item item; 22 | private ItemController item_controller; 23 | 24 | private BidList bid_list = new BidList(); 25 | private BidListController bid_list_controller = new BidListController(bid_list); 26 | 27 | private Context context; 28 | 29 | private UserList user_list = new UserList(); 30 | private UserListController user_list_controller = new UserListController(user_list); 31 | 32 | private Bitmap image; 33 | private ImageView photo; 34 | 35 | private TextView title_tv; 36 | private TextView maker_tv; 37 | private TextView description_tv; 38 | private TextView length_tv; 39 | private TextView width_tv; 40 | private TextView height_tv; 41 | private TextView current_bid_right_tv; 42 | private TextView current_bid_left_tv; 43 | private EditText bid_amount; 44 | private Button save_bid_button; 45 | private Button return_item_button; 46 | 47 | private boolean on_create_update; 48 | 49 | private String title_str; 50 | private String maker_str; 51 | private String description_str; 52 | private String length_str; 53 | private String width_str; 54 | private String height_str; 55 | private String user_id; 56 | private String current_bid_amount_str; 57 | private String new_bid_amount_str; 58 | private String item_id; 59 | 60 | private Float new_bid_amount; 61 | 62 | @Override 63 | protected void onCreate(Bundle savedInstanceState) { 64 | super.onCreate(savedInstanceState); 65 | setContentView(R.layout.activity_view_item); 66 | 67 | title_tv = (TextView) findViewById(R.id.title_right_tv); 68 | maker_tv = (TextView) findViewById(R.id.maker_right_tv); 69 | description_tv = (TextView) findViewById(R.id.description_right_tv); 70 | length_tv = (TextView) findViewById(R.id.length_tv); 71 | width_tv = (TextView) findViewById(R.id.width_tv); 72 | height_tv = (TextView) findViewById(R.id.height_tv); 73 | photo = (ImageView) findViewById(R.id.image_view); 74 | current_bid_right_tv = (TextView) findViewById(R.id.current_bid_right_tv); 75 | current_bid_left_tv = (TextView) findViewById(R.id.current_bid_left_tv); 76 | 77 | bid_amount = (EditText) findViewById(R.id.bid_amount); 78 | 79 | save_bid_button = (Button) findViewById(R.id.save_bid_button); 80 | return_item_button = (Button) findViewById(R.id.return_item_button); 81 | 82 | Intent intent = getIntent(); // Get intent from BorrowedItemsActivity/SearchActivity 83 | item_id = intent.getStringExtra("item_id"); 84 | user_id = intent.getStringExtra("user_id"); 85 | 86 | context = getApplicationContext(); 87 | 88 | on_create_update = false; // Suppress first call to update() 89 | user_list_controller.getRemoteUsers(); 90 | 91 | on_create_update = true; // First call to update occurs now 92 | bid_list_controller.getRemoteBids(context); 93 | bid_list_controller.addObserver(this); 94 | item_list_controller.addObserver(this); 95 | item_list_controller.getRemoteItems(); 96 | 97 | on_create_update = false; // Suppress any further calls to update() 98 | } 99 | 100 | @Override 101 | public void onBackPressed() { 102 | Intent borrow_intent = new Intent(this, BorrowedItemsActivity.class); 103 | borrow_intent.putExtra("user_id", user_id); 104 | startActivity(borrow_intent); 105 | } 106 | 107 | public void saveBid(View view) { 108 | title_str = title_tv.getText().toString(); 109 | maker_str = maker_tv.getText().toString(); 110 | description_str = description_tv.getText().toString(); 111 | current_bid_amount_str = current_bid_right_tv.getText().toString(); 112 | new_bid_amount_str = bid_amount.getText().toString(); 113 | length_str = length_tv.getText().toString(); 114 | width_str = width_tv.getText().toString(); 115 | height_str = height_tv.getText().toString(); 116 | 117 | if(!validateInput()){ // Current bid amount must be higher than the previous bid 118 | return; 119 | } 120 | 121 | String owner_id_str = item_controller.getOwnerId(); 122 | String status_str = "Bidded"; 123 | String minimum_bid_amount_str = item_controller.getMinBid().toString(); 124 | String username = user_list_controller.getUsernameByUserId(user_id); 125 | 126 | Bid bid = new Bid(item_id, new_bid_amount, username); 127 | 128 | boolean success = bid_list_controller.addBid(bid); 129 | if (!success){ 130 | return; 131 | } 132 | 133 | // Reuse the item id 134 | Item updated_item = new Item(title_str, maker_str, description_str,owner_id_str, 135 | minimum_bid_amount_str, image, item_id); 136 | ItemController updated_item_controller = new ItemController(updated_item); 137 | 138 | updated_item_controller.setStatus(status_str); 139 | updated_item_controller.setDimensions(length_str, width_str, height_str); 140 | 141 | success = item_list_controller.editItem(item, updated_item); 142 | if (!success){ 143 | return; 144 | } 145 | 146 | // End ViewItemActivity 147 | item_list_controller.removeObserver(this); 148 | bid_list_controller.removeObserver(this); 149 | 150 | final Intent intent = new Intent(this, SearchActivity.class); 151 | intent.putExtra("user_id", user_id); 152 | 153 | // Delay launch of SearchActivity to allow server enough time to process request 154 | new Handler().postDelayed(new Runnable() { 155 | @Override 156 | public void run() { 157 | Toast.makeText(context, "Bid placed.", Toast.LENGTH_SHORT).show(); 158 | startActivity(intent); 159 | } 160 | }, 750); 161 | 162 | } 163 | 164 | public void update() { 165 | if (on_create_update){ 166 | // For all status options we do the following 167 | item = item_list_controller.getItemById(item_id); 168 | item_controller = new ItemController(item); 169 | 170 | title_tv.setText(item_controller.getTitle()); 171 | maker_tv.setText(item_controller.getMaker()); 172 | description_tv.setText(item_controller.getDescription()); 173 | 174 | length_tv.setText(item_controller.getLength()); 175 | width_tv.setText(item_controller.getWidth()); 176 | height_tv.setText(item_controller.getHeight()); 177 | 178 | image = item_controller.getImage(); 179 | if (image != null) { 180 | photo.setImageBitmap(image); 181 | } else { 182 | photo.setImageResource(android.R.drawable.ic_menu_gallery); 183 | } 184 | 185 | String status = item_controller.getStatus(); 186 | 187 | if (status.equals("Available") || status.equals("Bidded")) { 188 | Float highest_bid = bid_list_controller.getHighestBid(item_id); 189 | 190 | if (highest_bid == null) { 191 | current_bid_right_tv.setText(item_controller.getMinBid().toString()); 192 | } else { 193 | current_bid_right_tv.setText(highest_bid.toString()); 194 | } 195 | } else { // Borrowed 196 | current_bid_right_tv.setVisibility(View.GONE); 197 | current_bid_left_tv.setVisibility(View.GONE); 198 | bid_amount.setVisibility(View.GONE); 199 | save_bid_button.setVisibility(View.GONE); 200 | return_item_button.setVisibility(View.VISIBLE); 201 | 202 | } 203 | } 204 | } 205 | 206 | public void returnItem(View view) { 207 | 208 | title_str = title_tv.getText().toString(); 209 | maker_str = maker_tv.getText().toString(); 210 | description_str = description_tv.getText().toString(); 211 | length_str = length_tv.getText().toString(); 212 | width_str = width_tv.getText().toString(); 213 | height_str = height_tv.getText().toString(); 214 | String status = "Available"; 215 | String owner_id = item_controller.getOwnerId(); 216 | String minimum_bid = String.valueOf(item_controller.getMinBid()); 217 | 218 | Item updated_item = new Item(title_str, maker_str, description_str, owner_id, minimum_bid, image, item_id); 219 | ItemController updated_item_controller = new ItemController(updated_item); 220 | updated_item_controller.setDimensions(length_str, width_str, height_str); 221 | updated_item_controller.setStatus(status); 222 | 223 | boolean success = item_list_controller.editItem(item, updated_item); 224 | if (!success){ 225 | return; 226 | } 227 | 228 | // End ViewItemActivity 229 | item_list_controller.removeObserver(this); 230 | bid_list_controller.removeObserver(this); 231 | 232 | final Intent intent = new Intent(this, MainActivity.class); 233 | intent.putExtra("user_id", user_id); 234 | 235 | // Delay launch of MainActivity to allow server enough time to process request 236 | new Handler().postDelayed(new Runnable() { 237 | @Override 238 | public void run() { 239 | Toast.makeText(context, "Item returned.", Toast.LENGTH_SHORT).show(); 240 | startActivity(intent); 241 | } 242 | }, 750); 243 | } 244 | 245 | public boolean validateInput(){ 246 | if (new_bid_amount_str.equals("")) { 247 | bid_amount.setError("Enter Bid!"); 248 | return false; 249 | } 250 | 251 | Float current_bid_amount = Float.valueOf(current_bid_amount_str); 252 | new_bid_amount = Float.valueOf(new_bid_amount_str); 253 | 254 | if (new_bid_amount <= current_bid_amount){ 255 | bid_amount.setError("New bid amount must be higher than current bid amount!"); 256 | return false; 257 | } 258 | 259 | return true; 260 | } 261 | } 262 | -------------------------------------------------------------------------------- /4. Service Oriented Architecture/Week 4/1. Capstone Assignment 4.2/ElasticSearchManager.java: -------------------------------------------------------------------------------- 1 | package com.example.sharingapp; 2 | 3 | import android.os.AsyncTask; 4 | import android.util.Log; 5 | 6 | import com.searchly.jestdroid.DroidClientConfig; 7 | import com.searchly.jestdroid.JestClientFactory; 8 | import com.searchly.jestdroid.JestDroidClient; 9 | 10 | import java.io.IOException; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | import io.searchbox.core.Delete; 15 | import io.searchbox.core.DocumentResult; 16 | import io.searchbox.core.Index; 17 | import io.searchbox.core.Search; 18 | import io.searchbox.core.SearchResult; 19 | 20 | /** 21 | * For remote machine: SERVER = "http://34.202.206.222:8080" 22 | * ------------------------------------------------------------------------------------------------- 23 | * curl -XDELETE 'http://34.202.206.222:8080/INDEX' - can be used to delete ALL objects on the server 24 | * (items, users, and bids) at that index 25 | * view an item at: http://34.202.206.222:8080/INDEX/items/item_id 26 | * view a user at: http://34.202.206.222:8080/INDEX/users/user_id 27 | * view a bid at: http://34.202.206.222:8080/INDEX/bids/bid_id 28 | * Where INDEX is replaced with the random number string you generate as per the assignment 29 | * instructions. Note: item_ids and user_ids are printed to the log (See the Android Monitor) 30 | * as each user/item is added. 31 | */ 32 | 33 | public class ElasticSearchManager { 34 | private static final String SERVER = "http://34.202.206.222:8080"; 35 | private static final String INDEX = "127113579"; // TODO: MUST CHANGE THIS to random number string 36 | private static final String ITEM_TYPE = "items"; 37 | private static final String USER_TYPE = "users"; 38 | private static JestDroidClient client; 39 | 40 | /** 41 | * Returns all remote items from server 42 | */ 43 | public static class GetItemListTask extends AsyncTask> { 44 | 45 | @Override 46 | protected ArrayList doInBackground(Void... params) { 47 | 48 | verifyConfig(); 49 | ArrayList items = new ArrayList<>(); 50 | String search_string = "{\"from\":0,\"size\":10000}"; 51 | 52 | Search search = new Search.Builder(search_string).addIndex(INDEX).addType(ITEM_TYPE).build(); 53 | try { 54 | SearchResult execute = client.execute(search); 55 | if (execute.isSucceeded()) { 56 | items = (ArrayList) execute.getSourceAsObjectList(Item.class); 57 | Log.i("ELASTICSEARCH","Item search was successful"); 58 | } else { 59 | Log.i("ELASTICSEARCH", "No items found"); 60 | } 61 | } catch (IOException e) { 62 | Log.i("ELASTICSEARCH", "Item search failed"); 63 | e.printStackTrace(); 64 | } 65 | 66 | return items; 67 | } 68 | } 69 | 70 | /** 71 | * Add item to remote server 72 | */ 73 | public static class AddItemTask extends AsyncTask { 74 | 75 | @Override 76 | protected Boolean doInBackground(Item... params) { 77 | 78 | verifyConfig(); 79 | Boolean success = false; 80 | Item item = params[0]; 81 | 82 | String id = item.getId(); // Explicitly set the id to match the locally generated id 83 | Index index = new Index.Builder(item).index(INDEX).type(ITEM_TYPE).id(id).build(); 84 | try { 85 | DocumentResult execute = client.execute(index); 86 | if(execute.isSucceeded()) { 87 | Log.i("ELASTICSEARCH", "Add item was successful"); 88 | Log.i("ADDED ITEM", id); 89 | success = true; 90 | } else { 91 | Log.e("ELASTICSEARCH", "Add item failed"); 92 | } 93 | } catch (IOException e) { 94 | e.printStackTrace(); 95 | } 96 | 97 | return success; 98 | } 99 | } 100 | 101 | 102 | /** 103 | * Delete item from remote server using item_id 104 | */ 105 | public static class RemoveItemTask extends AsyncTask { 106 | 107 | @Override 108 | protected Boolean doInBackground(Item... params) { 109 | 110 | verifyConfig(); 111 | Boolean success = false; 112 | Item item_to_delete = params[0]; 113 | try { 114 | DocumentResult execute = client.execute(new Delete.Builder(item_to_delete.getId()).index(INDEX).type(ITEM_TYPE).build()); 115 | if(execute.isSucceeded()) { 116 | Log.i("ELASTICSEARCH", "Delete item was successful"); 117 | success = true; 118 | } else { 119 | Log.e("ELASTICSEARCH", "Delete item failed"); 120 | } 121 | } catch (IOException e) { 122 | e.printStackTrace(); 123 | } 124 | 125 | return success; 126 | } 127 | } 128 | 129 | 130 | /** 131 | * Returns all remote users from server 132 | */ 133 | public static class GetUserListTask extends AsyncTask> { 134 | 135 | @Override 136 | protected ArrayList doInBackground(Void... params) { 137 | verifyConfig(); 138 | 139 | ArrayList users = new ArrayList<>(); 140 | String search_string = "{\"from\":0,\"size\":10000}"; 141 | 142 | Search search = new Search.Builder(search_string).addIndex(INDEX).addType(USER_TYPE).build(); 143 | try { 144 | SearchResult execute = client.execute(search); 145 | if (execute.isSucceeded()) { 146 | List remote_users = execute.getSourceAsObjectList(User.class); 147 | users.addAll(remote_users); 148 | Log.i("ELASTICSEARCH","User search was successful"); 149 | } else { 150 | Log.i("ELASTICSEARCH", "No users found"); 151 | } 152 | } catch (IOException e) { 153 | Log.i("ELASTICSEARCH", "User search failed"); 154 | e.printStackTrace(); 155 | } 156 | 157 | return users; 158 | } 159 | } 160 | 161 | /** 162 | * Add user to remote server 163 | */ 164 | public static class AddUserTask extends AsyncTask { 165 | 166 | @Override 167 | protected Boolean doInBackground(User... params) { 168 | 169 | verifyConfig(); 170 | Boolean success = false; 171 | User user = params[0]; 172 | 173 | String id = user.getId(); // Explicitly set the id to match the locally generated id 174 | Index index = new Index.Builder(user).index(INDEX).type(USER_TYPE).id(id).build(); 175 | try { 176 | DocumentResult execute = client.execute(index); 177 | if(execute.isSucceeded()) { 178 | Log.i("ELASTICSEARCH", "User was successfully added"); 179 | Log.i("ADDED USER", id); 180 | success = true; 181 | } else { 182 | Log.e("ELASTICSEARCH", "User failed to be added"); 183 | } 184 | } catch (IOException e) { 185 | e.printStackTrace(); 186 | } 187 | 188 | return success; 189 | } 190 | } 191 | 192 | 193 | /** 194 | * Delete user from remote server using user_id 195 | */ 196 | public static class RemoveUserTask extends AsyncTask { 197 | 198 | @Override 199 | protected Boolean doInBackground(User... params) { 200 | 201 | verifyConfig(); 202 | Boolean success = false; 203 | User user = params[0]; 204 | try { 205 | DocumentResult execute = client.execute(new Delete.Builder(user.getId()).index(INDEX).type(USER_TYPE).build()); 206 | if(execute.isSucceeded()) { 207 | Log.i("ELASTICSEARCH", "User was successfully deleted"); 208 | success = true; 209 | } else { 210 | Log.e("ELASTICSEARCH", "User delete failed"); 211 | } 212 | } catch (IOException e) { 213 | e.printStackTrace(); 214 | } 215 | 216 | return success; 217 | } 218 | } 219 | 220 | /** 221 | * Delete bid from remote server using user_id 222 | */ 223 | 224 | public static class RemoveBidTask extends AsyncTask { 225 | @Override 226 | protected Boolean doInBackground(Bid... params) { 227 | 228 | verifyConfig(); 229 | Boolean success = false; 230 | Bid bid = params[0]; 231 | try { 232 | DocumentResult execute = client.execute(new Delete.Builder(bid.getBidId()).index(INDEX).type(USER_TYPE).build()); 233 | if(execute.isSucceeded()) { 234 | Log.i("ELASTICSEARCH", "Bid was successfully deleted"); 235 | success = true; 236 | } else { 237 | Log.e("ELASTICSEARCH", "Bid Delete failed"); 238 | } 239 | } catch (IOException e) { 240 | e.printStackTrace(); 241 | } 242 | 243 | return success; 244 | } 245 | } 246 | 247 | /** 248 | * Add bid to remote server 249 | */ 250 | public static class AddBidTask extends AsyncTask { 251 | 252 | @Override 253 | protected Boolean doInBackground(Bid... params) { 254 | 255 | verifyConfig(); 256 | Boolean success = false; 257 | Bid bid = params[0]; 258 | 259 | String id = bid.getBidId(); // Explicitly set the id to match the locally generated id 260 | Index index = new Index.Builder(bid).index(INDEX).type(USER_TYPE).id(id).build(); 261 | try { 262 | DocumentResult execute = client.execute(index); 263 | if(execute.isSucceeded()) { 264 | Log.i("ELASTICSEARCH", "Bid was successfully added"); 265 | Log.i("ADDED Bid", id); 266 | success = true; 267 | } else { 268 | Log.e("ELASTICSEARCH", "Bid failed to be added"); 269 | } 270 | } catch (IOException e) { 271 | e.printStackTrace(); 272 | } 273 | 274 | return success; 275 | } 276 | } 277 | 278 | /** 279 | * Returns all remote bids from server 280 | */ 281 | public static class GetBidListTask extends AsyncTask> { 282 | 283 | @Override 284 | protected ArrayList doInBackground(Void... params) { 285 | 286 | verifyConfig(); 287 | ArrayList bids = new ArrayList<>(); 288 | String search_string = "{\"from\":0,\"size\":10000}"; 289 | 290 | Search search = new Search.Builder(search_string).addIndex(INDEX).addType(USER_TYPE).build(); 291 | try { 292 | SearchResult execute = client.execute(search); 293 | if (execute.isSucceeded()) { 294 | bids = (ArrayList) execute.getSourceAsObjectList(Bid.class); 295 | Log.i("ELASTICSEARCH","Bid search was successful"); 296 | } else { 297 | Log.i("ELASTICSEARCH", "No Bids found"); 298 | } 299 | } catch (IOException e) { 300 | Log.i("ELASTICSEARCH", "Bid search failed"); 301 | e.printStackTrace(); 302 | } 303 | 304 | return bids; 305 | } 306 | } 307 | 308 | // If no client, add one 309 | private static void verifyConfig() { 310 | if(client == null) { 311 | DroidClientConfig.Builder builder = new DroidClientConfig.Builder(SERVER); 312 | DroidClientConfig config = builder.build(); 313 | JestClientFactory factory = new JestClientFactory(); 314 | factory.setDroidClientConfig(config); 315 | client = (JestDroidClient) factory.getObject(); 316 | } 317 | } 318 | } 319 | --------------------------------------------------------------------------------