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.

In a month, I have to hand in a Java project. I have to make an e-shop program which has two types of users (admin and normal users). The admin can add/delete/restock stuff in the shop while the normal user can watch products and buy them. In order to buy a product the customer has to make an account in that e-shop. I'm not the best Java student but I've made a plan of what classes I might use and this kind of stuff.

But what I can't really figure out is how to:

  1. make the users register to the e-shop
  2. make different menus without doing a GUI, we are allowed to make a GUI but it is only a bonus and is not really required. By the way I haven't ever tried making a GUI.
  3. how to make the program save things like the username and the products on the hard drive.

I don't want anyone to do this for me, I just need some guidance, just to find out where to start and what I should look for.

share|improve this question

migrated from stackoverflow.com Aug 3 '11 at 13:50

This question came from our site for professional and enthusiast programmers.

closed as off topic by Yannis Rizos, gnat, Matthieu, Thomas Owens, Steve Evers Jan 25 '12 at 14:07

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Don't do a GUI. In order to learn Swing you need to understand multi-threading. –  toto Aug 3 '11 at 12:49
2  
There's a lot of useful tips and tools below. Since you are a beginner, I recommend sticking with the basics. Use simple solutions that just work, don't try to use all the big and complicated frameworks. (e.g. use CSV files to save data instead of JDBC if you never worked with databases) –  mort Aug 3 '11 at 12:59
    
@toto -- you only need to understand multithreading and concurrency from the standpoint of a few simple guidelines: 1) if all your actions are quick, trigger them from Swing (which runs on a single event dispatch thread) and don't worry about it; 2) if your actions are slower, use Executors.newSingleThreadExecutor() and let the swing actions add a task to that executor. Then Swing is single-threaded and so is your program's logic. –  Jason S Aug 3 '11 at 12:59
1  
@Jason Those rules are simple when you know them and when you have some basic concepts of multithreading. I don't think captain will have time to untangle those things since is project will keep him fully occupied. –  toto Aug 3 '11 at 13:09

7 Answers 7

Create some methods that will handle user addition, deletion etc.

I feel that you should tackle the GUI. I believe that programming with a GUI would be a little easier for a big project like this.

Use netbeans for your IDE and look into the swing components. This will offer you a drag and drop setup of a GUI that you can tweak the way you want it. Simply drag out your components and give em a click to code away at each section of your program.

Swing tutorial

To make the users register you could look at simple options(if you are short on time) such as popping up a window at the start of your program. (maybe a JOptionPane) That will request a Username/Password and then check that against a "user file" that is saved as a txt file on your HDD.

Something like this will allow you to write text to a file.

public static void main(String[] args) {

    // TODO code application logic here
    String filename = "C:\\UsersFile.txt";
    FileWriter fstream;

    try {
        fstream = new FileWriter(filename);
        BufferedWriter out = new BufferedWriter(fstream);
        //get the input from your textbox, optionpane etc.
        //and write it out to the file.  
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        //handle your exception the way you'd like
    }
}

As for saving things to the HDD you can tweak the code above to write the information you want to a text file and then use that to populate the lists etc.

With the GUI you could then seperate the controls and if username is not an admin the buttons for adding/deleting/restocking items etc would be disabled. (see pics below)

Tabs

To sum up:

1.Registration can be done with popup windows in the beginning or a few textboxes on the main GUI with a submit button that writes them to a "user" file

2.IMHO making a large menu with a bunch of options is much easier with a GUI. And if you keep it simple all that threading stuff shouldn't be a problem. Plus GUIs are cool.

3.Using the code above you can write your lists to text files. Your items can populate a listbox, and the users can be checked before "login" from the user file.

Good luck, and if you have a month and access to SO you will do just fine!

share|improve this answer
2  
Oh god...why would you recommend NetBeans? :[ –  mre Aug 3 '11 at 12:41
    
thanks, this might help. –  captain Aug 3 '11 at 12:54
    
@captain GL, I added some example GUIs(you can make em up pretty quick. –  sealz Aug 3 '11 at 13:06
2  
I would not recommend learning Swing for a project like this, especially as the first thought. UI should be done last. It won't make things easier. It's more likely to muddy the waters and confuse a new developer about where to put things. Layering will suffer. –  duffymo Aug 3 '11 at 13:18
    
GUIs make things click for me. Its his project so he makes the choice. :) –  sealz Aug 3 '11 at 13:21

Java's an object-oriented language: start by modeling the problem as objects. I see Product, Inventory, Order, OrderItem, etc. Start with those.

List the functions that your app has to implement and start checking them off:

  1. Admin add, delete, update products
  2. Buyers search and buy products

Layer your application: persistence, service, model, view.

Forget about the UI for now. Get the objects and services right. You can always add a more elaborate UI later. Command line is sufficient.

Unit test everything.

share|improve this answer
2  
++ to unit test. That is key to ANY program one writes. @captain - look into a unit test framework like JUnit or TestNG. Will save you a LOT of headache and can be the driver to help you prove out how your GUI should work as well. –  Chris Aldrich Aug 3 '11 at 12:40

Get some books on Java. My recommendation is to get the OReilly books "Learning Java" and "Java Swing" (for GUIs).

Or look at the Java tutorials for Swing.

Otherwise your question is too vague and undefined for us to answer.

make different menus without doing a GUI

IMHO it's much easier to make a menu using a GUI than a text-based menu, but that's just my opinion.

A sample program:

package com.example.test.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MenuExample extends JFrame
{
    public MenuExample(String title)
    {
        super(title);

        JPanel panel = new JPanel(new BorderLayout());
        setJMenuBar(createMenuBar());
        Action button1Action = new AbstractAction("button1") {
            @Override public void actionPerformed(ActionEvent e) {
                onButton1();
            }           
        };
        Action button2Action = new AbstractAction("button2") {
            @Override public void actionPerformed(ActionEvent e) {
                onButton2();
            }           
        };
        panel.add(new JButton(button1Action), BorderLayout.WEST);
        panel.add(new JButton(button2Action), BorderLayout.EAST);
        setContentPane(panel);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // in center of screen
        setVisible(true);
    }

    private JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        Action fileSaveAction = new AbstractAction("Save") {
            @Override public void actionPerformed(ActionEvent e) {
                saveFile();             
            }           
        };
        Action fileOpenAction = new AbstractAction("Open") {
            @Override public void actionPerformed(ActionEvent e) {
                openFile();             
            }           
        };
        Action aboutAction = new AbstractAction("About") { 
            @Override public void actionPerformed(ActionEvent e) {
                showAboutBox();             
            }           
        };
        JMenu menu;
        menu = new JMenu("File");
        menu.add(new JMenuItem(fileSaveAction));
        menu.add(new JMenuItem(fileOpenAction));
        menuBar.add(menu);

        menu = new JMenu("Help");
        menu.add(new JMenuItem(aboutAction));       
        menuBar.add(menu);

        return menuBar;
    }

    // here's where your program would actually do stuff
    protected void saveFile() { note("save file"); }
    protected void openFile() { note("open file"); }
    protected void showAboutBox() { note("show about box"); }
    protected void onButton2() { note("button2"); }
    protected void onButton1() { note("button1"); }

    private void note(String string) { System.out.println(string); }

    public static void main(String[] args)
    {
        new MenuExample("Menu example");
    }
}
share|improve this answer
    
Wouldn't learning Swing distract me from the problem? I think I will mostly be using time to learn Swing rather than trying to finish the project. I don't wan't to be one week before the deadline without having done anything project related. –  captain Aug 3 '11 at 12:37
    
If you don't need a GUI, you don't need to look into Swing at all. You should probably take care about all the logic first and than - if you have time left - start with Swing. –  mort Aug 3 '11 at 12:48
    
@captain -- true (I agree w/ Mort), but a simple GUI is pretty easy, and it makes testing much easier than trying to use stdin or command-line arguments to control your program, IMHO. I've posted a quick example above. This took me 10 minutes to put together, without having to glance at any javadoc or literature -- granted, I've had some experience with Swing, but it's much easier than putting a GUI together with MFC/WTL/whatever your favorite C++ GUI for Windows is. –  Jason S Aug 3 '11 at 12:52
    
@Jason: I agree with you in principle, but looking at all the abstract classes, layout manager and action listener stuff in your sample, it could get pretty difficult for a beginner to find a bug. –  mort Aug 3 '11 at 12:57

I think first you should start by creating a data model for your application, find different types of entities you need (eg. User, Product, Customer)

Second you would probably need to identify different layers needed (repositories, managers, etc..), and also how do you want to unit test/integration test them.

Decide how do you want to handle dependency injection.

Decide the menu structure. I believe using a simple GUI would be easier than using text based structure.

share|improve this answer
1  
Someone learning Java should not be worried about DI yet. Calling new will be sufficient. –  duffymo Aug 3 '11 at 13:02

I'll try to give you some hints:

1+3) Usually a user will have a user name and a password. A new user may register by entering a user name and his password. Make sure that the user name is unique! This pair is can than be stored somewhere (a database usually, or just a plain text file: http://www.javapractices.com/topic/TopicAction.do?Id=42). You could use a CSV (comma separated values) file: All the data for one user is stored in one line, where different fields are separated by a comma or semi colon. (Just google for CSV file to find out more about that).

2) I'm not sure what would qualify as being a menu without using a GUI. Maybe it's enough that a user can enter stuff like "buy item1" in the command line? If so, have a look at: http://www.devdaily.com/java/edu/pj/pj010005. To start with a Java-GUI: http://download.oracle.com/javase/tutorial/uiswing/

share|improve this answer
    
Thanks. I'm thinking of showing a basic non GUI menu, where the user types something like "goPC" and it would take him to the PC menu... –  captain Aug 3 '11 at 12:47
    
That won't be too much work. Just make a view simple commands like go, buy, register, add,... Make sure to also handle invalid commands, like "go nonexistingMenuPoint" or "buy nonexistingItem". –  mort Aug 3 '11 at 12:54

Why not just store registered users in a HashMap, indexed by username?

For different menus, you want to process command line input. Often used is the Scanner class.

You can save java objects to the hard drive. Look up "java object serialization" for one way to save your structures and read them back in. Another one would be to output the users in a csv format and read/write it when necessary.

share|improve this answer

Well, usually you would save things off using JDBC, although to simplify things I suppose you could try to just write to a file using the file I/O or the NIO API's. Read up on those tutorials at the Java tutorials area (that's where I read up on how to do what I want a lot of times).

Also, I'd look into the MVC (Model View Controller) pattern for doing work. That may help you a LOT. You could google it or even search this forum. There is a LOT out there for the MVC pattern (keeps you logic and your UI separate and more easy to maintain).

share|improve this answer
    
+1 for MVC. Treat the UI as something that manipulates the non-UI portion of your program. –  Jason S Aug 3 '11 at 13:05

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