首页 > > 详细

data留学生编程辅导、讲解Java程序、Java语言编程调试讲解数据库SQL|辅导Python编程

Exercise 1
Before starting this exercise session, make sure you have read the problem statement document for the
course’s assignment. Some information required for this exercise, such as the format for saving user and
message data in text files, is described there and is missing in this document. In this exercise session, you will
implement the first two versions of the chat system: ChatSys 0.1 and ChatSys 0.2.
Configuring IntelliJ
Before starting to write any code, let’s configure IntelliJ. Start by creating a new Maven project using Java 8. (If
Java 8 is not installed, you can click “Download JDK” and select Java 8 from AdoptOpenJDK OpenJ9.) Then click
next and name your project ChatSys. In the “artifact coordinates”, you can change the groupid with
“programming3”, the artifactid to “ChatSys” and the version to “0.1”.
Before starting, create a package “programming3.chatsys”. You should put all your classes inside that package
or a sub-package.
Also initialize a git repository inside your IntelliJ project. You can do this either using the command line or using
IntelliJ’s GUI. Ideally, you should regularly commit your changes so that you save your progress and can go
backward if needed.
ChatSys 0.1 - Sending messages
Chat messages
Let’s first create the class ChatMessage inside the package “programming3.chatsys.data” that will be able to
read and save chat messages. Create the class with its attributes, getter methods and constructors to initialize
them. Also implement the toString, equals and hashCode methods. You can use IntelliJ to generate those for
you.
Once done, add two empty methods:
• format(): String
• parse(String formatted): void
The format method will return a formatted String representing the object while the parse method will set the
attributes to the values of the formatted String given as argument. For the time being, return null for the
format method and leave the parse method empty.
Create a test class in order to write unit tests for ChatMessage: ChatMessageTest. Create a test method for the
format and parse methods. These can be created automatically with IntelliJ.
Implement the test methods created. Run the tests. These should fail as you haven’t implemented the
methods yet.
Now implement the format and parse methods. Run your tests. If they don’t pass, it means there is a bug in
your methods and you should fix them. Continue until all the tests pass and you are confident the methods
work properly.
Now create a method save. The method takes a file as an argument and will save the object into that file. Make
sure that the file is opened in “append” mode. This prevents the file from being completely erased every time
we write inside! You can do this by creating first a FileWriter like this: “new FileWriter(filename, true)”. Then
you can pass the FileWriter to a PrintWriter.
Don’t forget to write unit tests for the save method. Inside the test, save your results in a file
“messages_test.txt”. After your test are executed, don’t forget to remove the temporary file. You can do this
by defining a method “clean” inside your test class and use the annotations @AfterAll or @AfterEach.
Create a first commit with git to save your progress.
Database
Create a class “Database”. For the time being, the class will have one attributes referencing the file for the
message database. This class has one public method: readMessages and returns a List of ChatMessages. It will
be obtained by reading the database files. For the time being, simply return null.
Create a new test class “DatabaseTest” and one test method for readMessages method. Implement the test
method. Run the test class and the test should fail.
Now implement the method. Use a BufferedReader to read each line of text and parse them using the parse
method of ChatMessage.
Add to the Database a method addMessage that takes a ChatMessage as argument and adds it to the database
file. The chat message added should have an id greater than all the ids of all the chat messages already in the
database. If it doesn’t, the method should throw an exception.
One issue with our message text file is that the parsing won’t work if there is a line feed (i.e. “\n”) in any of the
text fields. Moreover, the username can only contain letters, numbers and the underscore (_) character. Add
the appropriate code in the parse and format methods to throw an exception if the String to parse or any of
the String class attributes are not formatted properly. Add a unit test to check that the parse and format
methods fail (i.e. throw an exception) when given an invalid input.
Make a new commit to save your progress.
Command Line Interface (CLI)
Now create two classes inside the package “programming3.chatsys.cli” with a static method “main” that will be
used as a command line interface to read and save messages.
SendMessages do the following operations:
• Ask for a username.
• Check if the database exists and is not empty.
• If it exists and isn’t empty, read all the messages and find the chat message with the biggest id and save
that id as the “last id”.
• If the DB doesn’t exist or is empty, save 0 as the last id.
• Then, use a Scanner in Java to ask the user to enter text.
• For each line of text entered, increment the last id by 1. Create a ChatMessage using the incremented
last id as id, the current time for timestamp and the username of the authenticated user. Save it in the
message database.
ReadMessages:
• Ask for a number n.
• Read the message database and display the n most recent messages. If n is 0, shows all the messages.
Note: asking for the number of messages to read (for ReadMessages) or the user information (for
SendMessages) can be done either with a Scanner or using the command line arguments.
Make a new commit with git. Also create a tag with git and name it “0.1”
ChatSys 0.2 – Registering and authenticating users
User
Now create the class User (in programming3.chatsys.data) in a similar way that you created ChatMessage. Add
the getter methods, toString, equals, hashCode, format, parse and save. Create a test class UserTest and add
unit tests for format, parse and save. By default, the lastReadId of a User is 0. In the current version of ChatSys,
it won’t be changed.
Add to the Database a method readUsers and its associated test in DatabaseTest. The readUsers method
returns a Map

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!