0

I'm trying to write a program that reads operations from a file. these operators look like below :

CREATE TABLE student(id:integer, gpa:decimal, name:string, family:string,isMale:Boolean) 
INSERT INTO student(id,gpa,name,fami 
SELECT id, name FROM student WHERE id=0 

then the program will have a parser that translates these lines and makes a database to do the operations. after parsing and execution of each line ;a log will be made in a another file:

ERROR: Duplicate column name 
family Table student created
ERROR: Table student already exists 
ERROR: Invalid table name st 

as for the assignment I shouldn't use prepared packages for storing data like sql. so I've chosen JSON(JSON - simple) for making a more organized database and I will use regular expression for parser.

my first question is if I'm using the right tools for this task.

and my second question is how should I design the program so it will have a good OOP quality in respect to how classes and methods are written.

for example I'm writing a prototype code that reads simple lines from input file then simply splits them to an array and writes them into an output text file(see this question for the code I've written) . how should I design the objects.? should write and read parts be methods in a same class? should they be static methods?

closed as too broad by user40980, GlenH7, durron597, Kilian Foth, Bart van Ingen Schenau Apr 3 '15 at 12:45

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

Should write and read parts be methods in a same class?

I would disagree with this because reading and writing are two separate responsibilities and if you want to follow good practices such as Single Responsibility Principle, I would separate them out into two classes. It might seem like an overkill but you will see that with your code growing in the future it will be easier to maintain.

If your code won't be growing and this is just a personal project then it is still a good idea to sharpen up your skills by following good practices.

Also, if you are dealing with databases I would change the name from Reader to Repository and from Writer to Persistence (you can append or prepend nouns to make them more specific of course).

Should write and read parts be methods in a same class? should they be static methods?

No. You should only use static methods, variables in an object-oriented world when you absolutely need to and you have a good argument. Bottom line is:

Don't use statics unless you really need to.

I would instead make the method nonstatic and new up the classes in your main method and call the read and write from the instance of the object instead.

Above is more general information for the future reference, however below you can find some more specific suggestions for changing your code:

1) Don't use global variables for jsonObject and output instead create them in your main method. As a matter of fact you don't even need jsonObject and output.

2) Let your reader method create jsonObject itself and populate it with data. Once data is populated on the newly created object you can then return it back.

3) Your writer method will also take a jsonObject and use that to write to the file.

4) The writer method doesn't really need to return anything.

Hope that makes sense.

  • thank you. part of my priblem is that I'm confused about using methods and changing and accessing variables. you can see that in that prototype code in the link of the question. so I'm not sure how can use those two read and write methods in the main. should they jave a return? and must they be the filenamIn/Out in this example? do I have to use setter getter for variables (and how?) @AvetisG – Vicarious Apr 1 '15 at 14:14
  • @Vicarious edited my response. Hope it helps! – AvetisG Apr 1 '15 at 15:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.