I am trying to write code for my latest class assignment where we create a Harness Record System, the code needs to keep track of Harness's in a system, be able to create new Harnesses and edit/loan/check old ones. I have gone down the route of using a GUI rather than the console as it is easier for the user to use. Although, I'm having trouble coming up with ideas on how to write code that differentiates old Harnesses written into the code, with new Harnesses that overwrite the previous input of the user.
So my question is, is there any way to write a code that can create objects once the user clicks a certain button, so that when that button is clicked the information inputted by the user into that object comes up, rather than just the most recent input of the user.
I know this is a silly and badly worded question, but my brain is just fried at the moment, I'm in need of help.
My code is below, I'm using two different classes and the SWT kit, if anyone could have a look at my code and help me out I would be so grateful, really struggling to find a way to overcome this:
Harness Class: http://pastebin.com/HqJqGfTN
HarnessSelection Class: http://pastebin.com/EE4C2WCs
The 'new' keyword is how you create new object instances. I haven't read all of your code but make sure you understand what the 'static' keyword does - it makes all instances of that class share the same value for that field. Perhaps all of your harness instances are sharing the same static variables.
Related
Is there a way to make a collection of class files/objects and then have them used in an interactive main file? So let's say I want to make a program to store information interactively where different classes are designed to hold different information. Then I would have an interactive main file where I made instances of these classes which would collectively hold the information I want stored. And then any changes or anything I do in this interactive main file is then saved.
I understand that this might be a very odd inquiry and maybe some other program might be useful for this. If so, feel free to point me in the right direction.
Here are two solutions that are good for the purpose you mentioned in your comment.
The first one is called Serialization. This let's you save your java object to your hard drive, and retrieve it later.
The second, (and in this case, more preferable option in my opinion), is using a Database.
A database is a compliment to your program, that stores data. You can then use "Queries" to access this data, and update it. Almost every database software is compatible with java.
I would look into MySQL
The reason I think a database would be better for your purpose is that they are already highly optimized, and are designed to have multiple people accessing and writing to them at once. If you wanted just want one person to use this program at a time however, serialization might be easier to implement.
Absolutely! Your main class would use the standard input (perhaps Scanner input = new Scanner(System.in);) and output (System.out.println()). To interact with your other classes, most simply, just put them in the same folder (if you are interested take a look at Java packages). If you have a Dog class in the same folder as your main class, you can freely create Dog objects in your main class. I hope this helps!
As a side note, because you mentioned storing information with different classes, you might be interested in the Java Collections Framework.
I have been learning java and data structures in java lately. I am dealing with this problem , this would be the first sort of real program i will be coding up . Up to this point i have only wrote code for short algorithms and data structures. So i was kind of looking for guidance on how i can approach this step by step. I am not looking for any code as i don't learn by looking at other peoples codes rather i needed guidance in how i should approach such a problem ..ex. do i first determine which classes i need etc? So after surveying the problem i realize this problem can be made as detailed or complicated as we like but i am trying to keep it simple do the basic that is required. So the following is what i have gathered so far.
I need a Customer class that will include name, age, number of grocery items bought, and method for determining which queue to join(join the shortest queue).
I need a several Customer queues that are associated with "checkers" who take random time to process each customer.
I would need a supermarket class where all the interaction between the customer, the queue and the checker would take place.
I am confused as i don't know where to start at the moment and also right now i don't have a clear idea on how everything will come together. I would really appreciate if someone can provide me step by step guidance as to what i should do in which order. By doing so it will help me in the future when i write more code for other object oriented languages.
I highly appreciate all inputs and apologize if have asked anything inappropriate.
In general, you need a main loop that repeats over and over and handles input and all entities you have (customers, groceries, etc.)
Since you want the supermarket class to manage your program, I would start with that.
You will then realize that you need a customer in your supermarket.
So create that class when you need it.
Then you will realize you need groceries.
So create that class when you need it.
Then you will realize you need to create the instances for these classes.
That would happen inside the supermarket in your case.
So far, nothing has been made 'viewable' and no interaction with the program is possible.
It creates customers themselves. Adds groceries to them (e.g. in an ArrayList property of the customer).
Then you will realize you need Ques.
So create them.
Time for each customer could depend on the number of items times a random factor.
Now you have your supermarket running. You should add debug logging that you can maybe turn off so you see what's happening.
Now, if you want, you can create something where you can view the supermarket's statistics or however you want to visualize.
Then, create the input for the user. Remember that you are running an infinite loop. You can ask for user input on each iteration. When input is present, react accordingly.
Now: fix bugs, add features, test, fix bugs, add features, test, ...
I hope this is a high-level overview that will help you get started.
Don't be afraid that your classes are to simple at the start. You can always add functionality to them at a later stage when the rough program is running.
I'm working on a moderate-sized Java project and trying to stick to the best possible practices, so I thought I'd run a few questions by you guys. Since I currently have time, I want to do it right. My apologies in advance if this sort of question isn't appropriate for StackOverflow. Perhaps others can refer to it for stylistic advice.
I'm writing a class called LinkOpener which has one public, static method: openAgencyWindows. You feed it an (oil) well serial number and, based on the serial number, a opens regulatory website for any one of the 50 US states. I'd be doing quite a bit of scraping, and due to the labyrinthine nature of these websites the code can get pretty extensive. Should I:
Include all of my scraping code in a LinkOpener class, including methods to handle serial numbers that correspond to each state in the US (sorted alphabetically).
Give each state its own class, which would extend a Scraper class that contains a few common website scraping/regex methods. Each state class would have one to three methods to assist with scraping.
Do something else?
Any assistance would be much appreciated.
Your second alternative will be more readable and a more object-oriented approach, which is good. It is also possible to call methods in the specific classes without knowing what state it is through abstract methods in the implemented class.
I'm having a bit of trouble figuring this out so if you can help that would be great.
I have written a bit of code that gets a list of URLs from a sitemap. I then go each page, scrape all the links, and then test their status(200,404, etc).
I am using HttpClient. I have it all working OK but as I am new to Java I reckon my code is a complete hack/maze and I could most likely get far better performance if it was organised correctly. So what I have is
Main class - This builds the gui
Parse the sitemap class - This parses the sitemap and get a list of the urls.
Class called PingURl - I'm sure my above is poor but this is the bit I reckon is worst. This class opens all the urls, scrapes them for links, then tests the links for their status and returns it. I presume this class should be broken down? Most importantly I think I should be isolating the testing of the links in it's own class, so it would be easy to implement threads later on?
Basically I'm looking for advice. If someone could help me with laying out the project a bit better. Secondly I believe this is my weakest area so to improve I need to learn more about this, I don't even know what to call this(design/layout problem?). Can you also recommend resources to learn more about this?
Java is a language which IMO, embodies good OO design. Designing with OO in mind is very effective.
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/oo.html
In terms of your problem, I think it works pretty well. You are kind of following a Model-View-Controller pattern: http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html
I can't tell you how to design your code, but I will tell you how I would do it if I were presented with the same problem:
I would get a class to represent a hyperlink. In that class there would be the hyperlink that has been scraped, and a getter and setter. As well as a boolean value and a ping function. That means that if I create a "hyperlink" object, then I can invoke ("ping") on that object because it is part of that class. That means that the ping function simply sets the boolean that represents connectivity.
That way, your parser basically gets a page, and for each link it finds, it creates a new "hyperlink" object based on your own "hyperlink" class. And puts it in an array. So once your parser executes you get an array of pointers to hyperlinks.
Then all you have to do is invoke the ping function on each one to see if it is there.
I think this design is the best because it scales from doing one hyperlink test to doing 1000 quite easily.
There is soooo much stuff out there about design, there are countless principals as well. There is never one absolute way to solve a problem. But the more experience you get, and the more you read up on design patterns and models the better you will get at it :)
The goal with my program is to have it save when closed, and reloaded when opened.
I have a driver (which contains all of the graphics) and I want it to create and save a file. I've seen numerous tutorials on the internet, but what they fail to explain is how to correctly implement this system in an actual program.
Do I create the OutputStrema in my main method? Do I need to check if a file has been created and if not create one, and if so read from it? How do I accomplish all of this? Should I have a WindowListener for quitting so that it can save all of the data?
Thanks
Yes, a WindowListener sounds like a good idea. One way to save the state of a program is to use Properties. Please have a look at the tutorial linked to above. I'm not sure what to recommend for your "graphics". Please tell us more details about just what you mean here.
Edit
Do I create the OutputStrema in my main method?
you're question re creating the OutputStream in the main method is asking about program structure that we know nothing about. My gut tells me that i wouldn't create anything like this in the main method, even if the JFrame is created there (or in a static method called from main). The OutputStream would be created in the class that implements the WindowListener, and this can be its own stand alone class. You want to make the main method as small as possible.
Do I need to check if a file has been created and if not create one, and if so read from it?
Possibly. This question revolves around what you're going to do with the stored information once created. Perhaps you'll search for it and read it on program start up? I don't know as it all depends on your needs, something you'll have to figure out.
How do I accomplish all of this? Should I have a WindowListener for quitting so that it can save all of the data?
If this is a Swing application, then yes, a WindowListener would help you control your application's closing.
Do I create the OutputStrema in my main method?
It would be better to create the stream at the point where you are saving the state.
When my program runs it is going to take in the saved data file with its ObjectInputStream. Do I put the code to accomplish this in my Main method?
Sounds like a good choice. Basically, you need to do this before you attempt to do something that needs that data. Anything that achieves this will work (though doing it using static initialization is a bad idea ...)
Do I need to check if a file has been created and if not create one, and if so read from it?
This question is confusing writing the state file and reading it. They occur at different points in the lifecycle, and use different code to do the task.
There is no point checking to see if a save file exists before creating one ... unless you propose to rename the existing save file.
Conversely, if a save file doesn't exist when you start the application, then the constructor that attempts to open it will throw a FileNotFoundException. Just catch the exception and skip the code that reads the saved state.
... if there has not been a file created yet, will this cause an error?
Yes. A FileNotFoundException; see above. So your code has to deal with that, or test to see if the file exists before attempting to open in; see File.exists() and related methods.
Should I have a WindowListener for quitting so that it can save all of the data?
That sounds like part of the solution. (But what about the case where the program crashes or is killed? Do you want to save state in those cases ... or not?)
A couple of things to beware of:
Many GUI-related objects do not implement Serializable, and therefore cannot be saved using an ObjectOutputStream. Threads, streams, sockets and a few other things can't be serialized either.
When you save instances of classes using ObjectOutputStream, change the classes or their names, and then attempt to reload them using ObjectInputStream, you are liable to run into trouble with saved instances not matching the new versions of the classes. If you have to be able to read that serialized state you are in trouble.