Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit 6fe4675

Browse files
committed
Initial commit
0 parents commit 6fe4675

19 files changed

+877
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/thoughtworks_xstream.xml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rykerzhu.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="thoughtworks.xstream" level="project" />
11+
</component>
12+
</module>

src/controllers/AppStoreAPI.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package controllers;
2+
3+
import com.thoughtworks.xstream.XStream;
4+
import com.thoughtworks.xstream.io.xml.DomDriver;
5+
import models.*;
6+
import utils.*;
7+
8+
import java.io.FileReader;
9+
import java.io.FileWriter;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutputStream;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
import static java.lang.Math.random;
16+
import static utils.RatingUtility.generateRandomRating;
17+
18+
/**
19+
* The responsibility for this class is to store and manage a list of Apps. Note that App is the super class in the hierarchy pictured below, so any subclass objects can be added to this list of Apps e.g. an object of GameApp can be added to it.
20+
* @author Ryker Zhu
21+
* @author Mairead Meagher
22+
*/
23+
public class AppStoreAPI {
24+
25+
//TODO refer to the spec and add in the required methods here (make note of which methods are given to you first!)
26+
27+
//---------------------
28+
// Method to simulate ratings (using the RatingUtility).
29+
// This will be called from the Driver (see skeleton code)
30+
//---------------------
31+
// TODO UNCOMMENT THIS COMPLETED method as you start working through this class
32+
//---------------------
33+
/*
34+
public void simulateRatings(){
35+
for (App app :apps) {
36+
app.addRating(generateRandomRating());
37+
}
38+
}*/
39+
40+
//---------------------
41+
// Validation methods
42+
//---------------------
43+
// TODO UNCOMMENT THIS COMPlETED method as you start working through this class
44+
//---------------------
45+
/*
46+
public boolean isValidIndex(int index) {
47+
return (index >= 0) && (index < apps.size());
48+
}*/
49+
50+
//---------------------
51+
// Persistence methods
52+
//---------------------
53+
// TODO UNCOMMENT THIS COMPLETED CODE block as you start working through this class
54+
//---------------------
55+
/*
56+
@SuppressWarnings("unchecked")
57+
public void load() throws Exception {
58+
//list of classes that you wish to include in the serialisation, separated by a comma
59+
Class<?>[] classes = new Class[]{App.class, EducationApp.class, GameApp.class, ProductivityApp.class, Rating.class};
60+
61+
//setting up the xstream object with default security and the above classes
62+
XStream xstream = new XStream(new DomDriver());
63+
XStream.setupDefaultSecurity(xstream);
64+
xstream.allowTypes(classes);
65+
66+
//doing the actual serialisation to an XML file
67+
ObjectInputStream in = xstream.createObjectInputStream(new FileReader(fileName()));
68+
apps = (List<App>) in.readObject();
69+
in.close();
70+
}
71+
72+
public void save() throws Exception {
73+
XStream xstream = new XStream(new DomDriver());
74+
ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter(fileName()));
75+
out.writeObject(apps);
76+
out.close();
77+
}
78+
79+
public String fileName(){
80+
return "apps.xml";
81+
}
82+
*/
83+
}

src/controllers/DeveloperAPI.java

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package controllers;
2+
3+
import com.thoughtworks.xstream.XStream;
4+
import com.thoughtworks.xstream.io.xml.DomDriver;
5+
import models.*;
6+
import utils.Utilities;
7+
8+
import java.io.FileReader;
9+
import java.io.FileWriter;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutputStream;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
public class DeveloperAPI {
16+
17+
//TODO Nothing! The DeveloperAPI class is completed! All you have to do here is implemment the ISerializer interface
18+
19+
private List<Developer> developers = new ArrayList<>();
20+
21+
//---------------------
22+
// Create methods
23+
//---------------------
24+
public boolean addDeveloper(Developer developer) {
25+
if (isValidDeveloper(developer.getDeveloperName())){
26+
return false;
27+
}
28+
return developers.add(developer);
29+
}
30+
31+
//---------------------
32+
// Read methods
33+
//---------------------
34+
public Developer getDeveloperByIndex(int index){
35+
if (Utilities.isValidIndex(developers, index)){
36+
return developers.get(index);
37+
}
38+
else{
39+
return null;
40+
}
41+
}
42+
43+
public Developer getDeveloperByName (String developerName){
44+
int index = retrieveDeveloperIndex(developerName);
45+
if (index != -1){
46+
return developers.get(index);
47+
}
48+
return null;
49+
}
50+
51+
52+
public String listDevelopers(){
53+
String listDevelopers = "";
54+
for (Developer developer : developers){
55+
listDevelopers += developers.indexOf(developer) + ": " + developer + "\n";
56+
}
57+
if (listDevelopers.equals("")){
58+
return "No developers";
59+
}
60+
else {
61+
return listDevelopers;
62+
}
63+
}
64+
65+
//---------------------
66+
// Update methods
67+
//---------------------
68+
public boolean updateDeveloperWebsite(String developerName, String developerWebsite){
69+
if (isValidDeveloper(developerName)){
70+
Developer developerToUpdate = getDeveloperByName(developerName);
71+
developerToUpdate.setDeveloperWebsite(developerWebsite);
72+
return true;
73+
}
74+
return false;
75+
}
76+
77+
//---------------------
78+
// Delete methods
79+
//---------------------
80+
public Developer removeDeveloper(String developerName){
81+
int index = retrieveDeveloperIndex(developerName);
82+
if (index != -1) {
83+
return developers.remove(index);
84+
}
85+
return null;
86+
}
87+
88+
//---------------------
89+
// Validation Methods
90+
//---------------------
91+
public boolean isValidDeveloper(String developerName){
92+
for (Developer developer : developers){
93+
if (developer.getDeveloperName().equalsIgnoreCase(developerName)){
94+
return true;
95+
}
96+
}
97+
return false;
98+
}
99+
100+
public int retrieveDeveloperIndex(String developerName){
101+
for (Developer developer : developers){
102+
if (developer.getDeveloperName().equalsIgnoreCase(developerName)){
103+
return developers.indexOf(developer);
104+
}
105+
}
106+
return -1;
107+
}
108+
109+
//---------------------
110+
// Getters/Setters
111+
//---------------------
112+
public List<Developer> getDevelopers() {
113+
return developers;
114+
}
115+
116+
//---------------------
117+
// Persistence Methods
118+
//---------------------
119+
/**
120+
* The load method uses the XStream component to read all the objects from the xml
121+
* file stored on the hard disk. The read objects are loaded into the associated ArrayList
122+
*
123+
* @throws Exception An exception is thrown if an error occurred during the load e.g. a missing file.
124+
*/
125+
@SuppressWarnings("unchecked")
126+
public void load() throws Exception {
127+
//list of classes that you wish to include in the serialisation, separated by a comma
128+
Class<?>[] classes = new Class[]{Developer.class};
129+
130+
//setting up the xstream object with default security and the above classes
131+
XStream xstream = new XStream(new DomDriver());
132+
XStream.setupDefaultSecurity(xstream);
133+
xstream.allowTypes(classes);
134+
135+
//doing the actual serialisation to an XML file
136+
ObjectInputStream in = xstream.createObjectInputStream(new FileReader(fileName()));
137+
developers = (List<Developer>) in.readObject();
138+
in.close();
139+
}
140+
141+
/**
142+
* The save method uses the XStream component to write all the objects in the ArrayList
143+
* to the xml file stored on the hard disk.
144+
*
145+
* @throws Exception An exception is thrown if an error occurred during the save e.g. drive is full.
146+
*/
147+
public void save() throws Exception {
148+
XStream xstream = new XStream(new DomDriver());
149+
ObjectOutputStream out = xstream.createObjectOutputStream(new FileWriter(fileName()));
150+
out.writeObject(developers);
151+
out.close();
152+
}
153+
154+
public String fileName(){
155+
return "developers.xml";
156+
}
157+
158+
159+
}

0 commit comments

Comments
 (0)