Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

A week ago I asked the question; First big project, how to get started. Make menus, save to harddrive etc. I tried to do everything without doing a GUI with Swing but I realized that for every view I wanted to present to the user I had to make a new class. Right now I have 3 menu classes (the mainMenu, computerCategoryMenu,laptopCategoryMenu) I stopped because I would need to make another 6 menu classes and then another 6 if the user registered and another 6 for the administrator of the site. It doesn't look right to me. Furthermore everything looks too static for me. I don't know how to make the administrator add or delete products.

Should I store all my products in a hashmap and then get them to show in different menus? Do you think that doing a GUI with Swing will make things much easier?

share|improve this question
    
You have menu classes but are not using Swing? so what platform are you using? GWT? –  Jonas Aug 10 '11 at 13:32
    
I tried to make menus with System.out commands... because I don't have any experience with Swing and our professor said that it isn't necessary to make a GUI with buttons and all that stuff. –  captain Aug 10 '11 at 13:36
    
What all is going into these menu classes? If it's just a list of options and nothing else, you'd probably be better served just putting them into methods, not separate classes. –  Eric Hydrick Aug 10 '11 at 13:54
    
the menu classes show menus for different products the laptopmenu shows all the laptops the store has, the pcmenu shows all the pc the store has. The problem is that when the admin adds some new product I don't know how to make it be shown in the menu. –  captain Aug 10 '11 at 15:05

4 Answers 4

Sounds like you've gone a bit off the rails. You should have a single menu class. Period, done.

In your application you'll have some sort of menu bar/container that holds all the menus you need. Here's some rough pseudo-code

Menu mainMenu = new Menu();
Menu computerCategoryMenu = new Menu();
mainMenu.setTitle("Main");
computerCategoryMenu.setTitle("Computer Category");
mainMenu.addItem("Exit");

And whatever else you need for the menus. If you need to read the names from a file, it'd be more dynamic, but you should get the idea.

share|improve this answer

Yes, doing a complex user-interface will be easier in Swing compared to the command line. But you need to show more code if you want help with the design.

share|improve this answer

By all means, yes, for this type of application you should write a GUI. From your problem description, it seems that you should have only one menu called "Products" with two menu entries, one for browsing the list of products and the other for adding a new product. Now, you don't know to duplicate menus for each "user role" in your application. You should enable/disable your menu, menu items and windows and buttons, and all "action" graphical elements depending on the role(s) of the user. For example only if he's an administrator would you enable the action consisting in "adding/modifying/removing" products. However, all users are able to browse the list of products. But, really, this is the very tip of your iceberg here...

Regardless of your GUI problems I suspect you have another major problem in that you don't have a database. You wrote about storing your products in a HashMap, that's why I am being suspicious. Now you could get away with just a HashMap if you serialize/deserialize it somewhere in a file but that's just a (very) quick and dirty solution for you to concentrate on coding your GUI.

Also, an even bigger problem... Since you are talking about different user roles, I also suspect that different people should be using the application at the same time. If that is the case you will need to have a database to connect to and store/retrieve user information. You will not be able to achieve this with simple hashmaps. Take a look at how a multi-tiered application should be done theoretically. Even very simple and quickly written applications take this approach.

Finally, check out the following link where they given an example of an implementation of the MVC architecture, using Java SE: http://www.oracle.com/technetwork/articles/javase/mvc-136693.html. All the source code is available for you to quickly understand how it works.

ps: you can do all of that in a desktop application, but also in a web application... Maybe you first have another decision to make here ?

share|improve this answer

It doesn't look right to me.

First of all, usually when it doesn't feel right is because it isn't. You should always try to make your code intuitive for others. Ever tried to change someone else´s code and complained everything is a mess? Well, that's because the coder was only coding for himslef. When you code you should try to make clear for everyone else what youre trying to accomplish. And this usually means choosing the right level of granularity, and choosing the right names for classes, methods, variables, packages and so on.

Should I store all my products in a hashmap and then get them to show in different menus?

If you store all your products on a hashmap you can find them easily with some key. That's good. But is that all you need? You may need to find them by category, by name, order them, specify relations between them and so on. So my guess would be that you should have a Products class with all this common operations as a public interface. I bet you will find this a lot easier to implement because everything is encapsulated in a single place, and you only need to think about a single -hopefully- simple operation at a time. Remember:clases should ALWAYS be specialists. Keep it short, and simple. I personally like the spartan programming principle.

Do you think that doing a GUI with Swing will make things much easier?

Nope, whatever problems you're having right now may disappear by changing the way you read user input, but you will find new ones. My guess is that you're doing too much in each of your menu classes. Instead of creating a new class for each use case, and stuffing everything inside. I think, you should maybe have a single class which references business objects, and delegates the real job to them depending on the users input. Remember:clases should ALWAYS be specialists. Ask yourself, whats the point of this menu class? Well if it's a menu then it's just about choosing what i want, and that's it. Simple as that. Let someone else do the real work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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