Java代写:CPSC210 Json App

代写一个简易的JSON对象转换器工具。

JSON

Learning Goals

  • override hashcode and equals to handle the case where duplicate objects exist in the application
  • add data persistence to the application so that tasks added to the application can be stored and reloaded from file when the application is stopped and restarted
  • represent the state of objects in a Java program using JSON format
  • parse JSON data and create the corresponding objects in a Java program

Setup

We have provided a new starter project for Phase 3 that contains a solution to Phase 1 and Phase 2. Most of the solution has been obfuscated in a jar file, however the Task and Tag classes are visible in the model package, as you will need to modify them.

Note that we have made the following changes to the code that were not included in the requirements for Phase 1 and 2:

  • overridden hashCode and equals for DueDate , so that two DueDate objects are considered equal if they have the same year, month, day, hour and minute.
  • overridden hashCode and equals for Priority, so that two Priority objects are considered equal if they have the same important and urgent status.
  • overridden hashCode and equals for Project, so that two Project objects are considered equal if they have the same description.

Note that there is an 8 hour wait between successive requests for feedback from AutoTest on this phase of the term project.

Task 0

Clone the repository PomoTODO_phase3_xxxx from GitHub, where xxxx is your CS login ID.

Task 1

Override equals and hashCode for the Task and Tag classes so that:

  • two Tag objects are considered equal if they have the same name
  • two Task objects are considered equal if they have the same description, due date, priority and status.

Test the implementation you have added by adding appropriate test methods to the TestTag and TestTask classes.

Task 2

Note that we have added the following methods to the Task and Tag classes:

  • Task.addTag(Tag t)
  • Task.removeTag(Tag t)
  • Tag.addTask(Task t)
  • Tag.removeTask(Task t)

You must now test and implement these methods so that a bi-directional association is established between Task and Tag as depicted in the following UML class diagram.

When you are done, push your code to GitHub and request a grade from AutoTest: @autobot #phase3 If you see any hints related to the Task or Tag classes, you have an error in Task 1 or Task 2, so debug before moving on.

Task 3

In this task we will focus on representing the state of a list of Task objects in JSON format. Once in this format, it will be an easy matter to store a list of Task objects in a file, so that their state can be preserved from one execution of our PomoTODO application to the next.

A library for representing data in JSON format has been linked to the project that you cloned from GitHub. You must not use any other library! Documentation for the library provided to you can be found here: http://stleary.github.io/JSON-java/index.html

Before you do anything else, clone this repository and study the code carefully. Note that this sample application is not intended to illustrate all the features of JSON parsing that will be needed in this project. However, it will give you a very good start. Note that course staff will first ensure that you understand this sample application before helping you with any of the remaining tasks -so don’t be tempted to skip this step!

Task 4

Test and implement the methods in the 3sonifier class so that:

  • A single Tag object having the name cpsc210 , for example, is represented as the JSON object {"name":"cpsc210"} . Note that you must not attempt to represent the tasks that are associated with a tag object!
  • A Priority object that is important but not urgent, for example, is represented as the JSON object {"important": true,"urgent": false}
  • A DueDate object corresponding to the date 16th Jan 2019 at 23:59 is represented by the JSON object {"year":2019,"month":0,"day":16,"hour":23,"minute":59} . Keep in mind that due dates can be null .
  • A Task object that has description “Register for the course.”, two tags named “cpsc210” and “cpsc310”, priority that is important and urgent, and status in progress, is represented by the JSON object.
    • note that the collection of tags belonging to a task, is represented as a JSON array named tags r.
    • take note of how the status is represented: IN_PROGRESS rather than IN_PROGRESS . Similarly, for all other values of Status , a space is replaced with an underscore.
  • A List<Task> object is represented as a JSON array of JSON objects each of which represents a single Task object. A sample of such a JSON array is as follows.

When you are done, submit your code to AutoTest and request a grade. If you see any hints related to the Jsonifier class, debug before moving on.

Note: to obtain full code coverage of the Jsonifier class, you will have to include a test that makes a call to the 3sonifier constructor (but does nothing else).

Task 5

In the previous task we focussed on representing the state of a list of Task objects using JSON format. Once in this format, tasks can easily be written to a file.

In this task we focus on the reverse operation of reading JSON data such as that presented above and parsing it to create the corresponding list of Task objects.

Test and implement the TaskParser.parse method. It is recommended that you first implement your parser without considering any of the exceptional situations described below. However, don’t forget that the due date could be null. Once you can parse data that contains no errors, it will be relatively easy to modify your implementation to handle the following situations.

When you are done, submit your code to GitHub and request a grade.

Task 6

Note that you are not allowed to ask for help in the lab or on the discussion board or from anywhere else about this task. Note that it is possible to score a grade of approximately 80% on this phase of the term project and not meet any of the remaining requirements.

You parser must be able to handle the following situations:

  • Missing or incorrectly represented:
    • description
    • tags array
    • individual tag within the tags array
    • due date object
    • year, month, day, hour or minute elements within a due date object
    • priority
    • important or urgent elements within a priority object
    • status

in all of the above cases, the TaskParser.parse method must not attempt to add the corresponding Task to the list of tasks that it returns. However, any data already added to the Task object in the process of being parsed can remain in the task. Note that data is considered incorrectly represented if it has the wrong identifier (e.g. “description” instead of “description”) or the wrong type of data (e.g. year is the string “2019” instead of the integer value 2e19 ).

When you are done, submit your code to GitHub and request a grade.

Note that we will also run tests (with very low weight) to check that you do not break the functionality provided in Phase 1 and Phase 2. Hints for these tests are provided after the tests that check the code that you have added in Phase 3.

Challenge

Before attempting the challenge, make sure to read What are challenge exercises on Project Homepage

Consider the following code segment

1
2
3
4
5
6
7
Task task1 = new Task("Buy milk ## Grocery shopping");
Task task2 = new Task("Buy bread ## Grocery shopping");

List<Tag> tags = new ArrayList<>(task1.getTags());
Tag groceryShopping = tags.get(0);

System.out.println(groceryShopping.getTasks().size());

What will be printed out?

The output will be 1 ; there is only one task associated to groceryShopping tag (and that is task1 ). In phase-3, we decided two tags that have the same name are equal and since the same tag name was given to both task1 and task2, we may have hoped to see both task1 and task2 are associated with groceryShopping . Why this is not the case?

In this challenge exercise, you will find a way to fix the issue discussed above. That is, you will find a way to assure both tasks are associated with a tag that has the name “Grocery shopping.”

To illustrate the idea further, consider the code below:

1
2
3
4
5
6
7
8
Task task1 = new Task("Buy milk");
Task task2 = new Task("Buy bread");

Tag tag1 = new Tag("Grocery shopping");
Tag tag2 = new Tag("Grocery shopping");

task1.addTag(tag1);
task2.addTag(tag2);

Moreover, assume we have the following method:

1
2
3
4
5
6
public static void printTagInfo(Tag tag) {
System.out.println("Tasks that are tagged with " + tag.getName() + " are:");
for (Task t: tag.getTasks()) {
System.out.println("\t" + t.getDescription());
}
}