I have a project which I have been working on for the past few days and I have recently reached a point where I don't understand what to do. My program must have a main menu with "Add, remove, List Record #, List All, Sort, Exit" functions.
I am supposed to also include a "Database" class which must implement:
addItem(), removeItem(), listItems(), listParticularItem(), sortItems().

My program is basically a horrible fail of a "game" which basically allows you to name a character, pick a race and class. I am also expected to allow the user to be able to create multiple characters and these characters have to be made in an "Array".

Main Class:

/**
 * @(#)Game.java
 *
 * Game application
 *
 * @author 
 * @version 1.00 2010/1/7
 */
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
 
public class Game
{
   String name;

public Game(String nm)
	{
		name = nm;
	//	age = yearsold;
	//	b1 = new Book("Comp Sci", 234, "Hard cover");
	//	bin1 = new Binder("Blue", 3, "Five Star");
		
	}
public String getName()
		{
			return name;
		}
	
public void setName(String lawl)
		{
			name = lawl;
		}	
			

    public static void main(String[] args) 
    {
	 Game[] Chars = new Game[5];
	 //Database[] charsDB = new Database[5];
	 Chars[0] = new Game("");
				
	new GUI1(Chars);
    }
}

GUI Class:

/**
 * @(#)GUI.java
 *
 *
 * @author 
 * @version 1.00 2010/1/7
 */

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;

public class GUI1 extends JFrame 
{
	JFrame mainwindow, namewindow, racewindow, classwindow;
	JPanel mainpanel, namepanel, racepanel, classpanel;
	JLabel imagelbl, nameBgLbl,nameFlLbl, racelbl, racewinLbl, scrollLbl, raceinfoLbl, nameShow;
	JButton start, exit, nextrace, toClass, show;
	JRadioButton human,orc,elf;
	JTextField nametext;
	String name;
	int guinum;
	Game[] newChar = new Game[5];

	public GUI1(Game[] gameChar) 
    {
		newChar= gameChar;
 		
    	
    	//Frames
    	mainwindow = new JFrame("Game");
    	mainwindow.setSize(500,500);
    
mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	namewindow = new JFrame("Create a new character");
    	namewindow.setSize(500,500);
    	namewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	racewindow = new JFrame("Pick a race");
    	racewindow.setSize(400,501);
    	racewindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		classwindow = new JFrame("Pick a class");
		classwindow.setSize(500,500);
		classwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
		//Panels
		mainpanel = new JPanel();
    	mainpanel.setBounds(0,0,500,500);
    	mainpanel.setBackground(Color.red);
    	mainpanel.setLayout(null);
    	mainwindow.getContentPane().add(mainpanel);
		
		namepanel = new JPanel();
    	namepanel.setBackground(Color.red);
    	namepanel.setBounds(0,0,500,500);
    	namepanel.setLayout(null);
    	namewindow.getContentPane().add(namepanel);
    	
    	racepanel = new JPanel();
    	racepanel.setBackground(Color.white);
    	racepanel.setBounds(0,0,500,501);
    	racepanel.setLayout(null);
    	racewindow.getContentPane().add(racepanel);
    	
    	classpanel = new JPanel();
    	classpanel.setBackground(Color.red);
    	classpanel.setBounds(0,0,400,400);
    	classpanel.setLayout(null);
    	classwindow.getContentPane().add(classpanel);
    	
    	//Buttons
    	ImageIcon startBt = new ImageIcon(getClass().getResource("create.png"));
    	start = new JButton("");
    	start.setIcon(startBt);
    	start.setBounds(0,0,101,35);
    	start.addActionListener(new MyAction());
    	mainpanel.add(start);
    	
    	ImageIcon exitBt = new ImageIcon(getClass().getResource("exit.png"));
    	exit = new JButton("");
    	exit.setIcon(exitBt);
    	exit.setBounds(411,0,84,38);
    	exit.addActionListener(new MyAction());
    	mainpanel.add(exit);
    	
    	ImageIcon nameok = new ImageIcon(getClass().getResource("nameok.png"));
    	nextrace = new JButton("");
    	nextrace.setBounds(223,316,66,29);
    	nextrace.setIcon(nameok);
    	nextrace.addActionListener(new MyAction());
    	namepanel.add(nextrace);
    	
   	 	toClass = new JButton("Next");
    	toClass.setBounds(200,240,100,20);
    	toClass.addActionListener(new MyAction());
    	racepanel.add(toClass);
    	
    	show = new JButton("show");
    	show.setBounds(0,0,100,30);
    	show.addActionListener(new MyAction());
    	classpanel.add(show);
    	
    	//Radio Buttons
    	
    	human = new JRadioButton("Human");
    	human.setBounds(0,0,100,15);
    	human.setOpaque(false);
    	human.addActionListener(new MyRace());
    	racepanel.add(human);
    	
    	orc = new JRadioButton("Orc");
    	orc.setBounds(0,15,100,15);
    	orc.setOpaque(false);
    	orc.addActionListener(new MyRace());
    	racepanel.add(orc);
    	
    	elf = new JRadioButton("Elf");
    	elf.setBounds(0,30,100,15);
    	elf.setOpaque(false);
    	elf.addActionListener(new MyRace());
    	racepanel.add(elf);
    	
    	ButtonGroup bg = new ButtonGroup();
    	bg.add(human);
    	bg.add(orc);
    	bg.add(elf);
    	
    	//TextFields    
    	nametext = new JTextField(15);
    	nametext.setBounds(180,239,147,23);
    	nametext.setFont(new Font("Arial", Font.PLAIN, 13));
    	nametext.setForeground(Color.white);
    	nametext.setDocument(new JTextFieldLimit(12));
    	nametext.setOpaque(false);
    	namepanel.add(nametext);
    	
    	//Images
     raceinfoLbl = new JLabel();
		raceinfoLbl.setBounds(145,-27,220,250);
		raceinfoLbl.setHorizontalAlignment(JLabel.CENTER);
		racepanel.add(raceinfoLbl);

    	
    	racelbl = new JLabel();
		racelbl.setBounds(120,225,300,300);
		racelbl.setHorizontalAlignment(JLabel.CENTER);
		racepanel.add(racelbl);

    	ImageIcon mainpic = new ImageIcon(getClass().getResource("Main.png"));
		imagelbl = new JLabel("",mainpic,JLabel.CENTER);
    	imagelbl.setBounds(0,-5,500,500);
    	mainpanel.add(imagelbl);
    	
    	ImageIcon nameBackground = new ImageIcon(getClass().getResource("namecharBg.png"));
		nameBgLbl = new JLabel("",nameBackground,JLabel.CENTER);
    	nameBgLbl.setBounds(-5,0,500,500);
    	namepanel.add(nameBgLbl);
    	
    	ImageIcon scrollInfo= new ImageIcon(getClass().getResource("Scroll.png"));
    	scrollLbl = new JLabel();
    	scrollLbl.setIcon(scrollInfo);
    	scrollLbl.setBounds(125,0,250,250);
    	racepanel.add(scrollLbl);


    	ImageIcon raceBg= new ImageIcon(getClass().getResource("racebg.png"));
    	racewinLbl = new JLabel();
    	racewinLbl.setIcon(raceBg);
    	racewinLbl.setBounds(0,-8,500,500);
   	 	racepanel.add(racewinLbl);
   	 	
   	 	nameShow = new JLabel();
    	nameShow.setBounds(125,0,250,250);
    	classpanel.add(nameShow);
    	
    	
    	//Visibility
    	mainwindow.setVisible(true);
    	mainwindow.setResizable(false);
    	racewindow.setResizable(false);
    	namewindow.setResizable(false);
   
    }
 
 //Limit text in JTextfield constructor    
public class JTextFieldLimit extends PlainDocument 
{
    private int limit;
    // optional uppercase conversion
    private boolean toUppercase = false;
    
    JTextFieldLimit(int limit) 
    {
        super();
        this.limit = limit;
    }
    
    JTextFieldLimit(int limit, boolean upper) 
    {
        super();
        this.limit = limit;
        toUppercase = upper;
    }
    
    public void insertString
            (int offset, String  str, AttributeSet attr)
            throws BadLocationException 
     {
        if (str == null) return;
        
        if ((getLength() + str.length()) <= limit) 
        {
            if (toUppercase) str = str.toUpperCase();
            super.insertString(offset, str, attr);
        }
    }
}// end of limit text constructors
    
    
    
    //Action Listener 
     public class MyAction implements ActionListener
     	{
    	public void actionPerformed (ActionEvent e)
    	{
    		if(e.getSource() == start)
    		{
				mainwindow.setVisible(false);
				namewindow.setVisible(true);
    		}	
    		if(e.getSource()==exit)
    		{
    			System.exit(0);
    		}
    		if(e.getSource()==nextrace)
    		{
    			String name = nametext.getText();
    			newChar[0].setName(name);
    			//d1.add(name);
	   			nametext.setText(" ");
    			mainwindow.setVisible(false);
				namewindow.setVisible(false);
				racewindow.setVisible(true);
    		}
    	if(e.getSource()==toClass)
    		{
    			mainwindow.setVisible(false);
				namewindow.setVisible(false);
				racewindow.setVisible(false);
				classwindow.setVisible(true);
    		}
    		if(e.getSource()==show)
    		{
    			nameShow.setText(newChar[0].getName());
    		}
    		}	
    	}
    	     
    	public class MyRace implements ActionListener
     	{
    	public void actionPerformed (ActionEvent e)
    	{
    	
    		if(e.getSource() == human)
				{
					racelbl.setIcon(new ImageIcon("Human.png"));
					raceinfoLbl.setText("<html><b>The noble warriors of humanity employ both a strong military and powerful magics in the defense of their shining kingdoms. Human's are best suited for players who prefer a balance in all areas.</b></html>");
					
    			
    			}	
    		if(e.getSource() == orc)
    			{
  					racelbl.setIcon(new ImageIcon("Orc.png"));
  					raceinfoLbl.setText("<html><b>The green skinned Orc is a race which is feared for their brute strength and raw melee power along with great accuracy, they can make a great ranged class. Orc's have amazing strength and are made for players who enjoy rushing into the front lines and dealing major hits.</b></html>");
   				}	
    		if(e.getSource() == elf)
    			{
    				racelbl.setIcon(new ImageIcon("Elf.png"));	
    				raceinfoLbl.setText("<html><b>The ancient and wise Elves were believed to be around since the beginning of time. Players who want to be outstanding in the control of their magic, or want to use their shadowy powers in conjunction with melee powers should attempt playing Elves.</b></html>");
    			}			
			}
    	}

    	
    	
}
//Junk Code

//setText(newChar[0].getName());

Database Class:

/**
 * @(#)Database.java
 *
 *
 * @author 
 * @version 1.00 2010/1/14
 */


public class Database {
 
 Game[] Chars;
   
   
public Database(int num) 
    {
		Chars = new Game[num];
    }
}

I honestly don't understand the database concept so my database class is really not completed and this is why I have come here for assistance. Some how I am supposed to allow the user to create a character, which has a name, race and class and then allow the user to create more characters if they prefer. After they are completed I am to let them have functions which can sort these characters or what not.

Help would greatly be appreciated.


Thanks,
GoldenDrago (Ray)

Recommended Answers

All 4 Replies

maybe you 're supposed to connect to a database and write/remove/edit the data about the character in there?

Well, I have to create some constructors in my own "Database" class and link them/connect them together to write/remove/sort the data about the character, but I don't know how to link my database class to the rest of the classes using an array..

I have not gone through your code, but from what you suggest about the task at hand I guess you would have to do something like this:

1. Print a menu/ or Show GUI about various operations/tasks a user can perform. Such as Add Char, Edit Char, Remove Char, Search Char etc
2. Then print a menu/ show GUI depending on the initial menu selection. Say for example in the Add Char menu you might first have to ask the name of the character, then the race class, other details etc. For the Edit Character menu you would first have to ask for a character name to be edited, then search the character in the existing db and allow the character to be modified if such a character exists and so on and so forth for the remove and search menus.
3. Once you gathered added/modified information for the character you need to bother yourself with the manipulation of such information in the database. This can be done by connecting with a database through a driver using JDBC. As you say if you aren't aware of what database connectivity and JDBC is you can find more information about it here.

I suggest you could write classes dedicated to each function such as one for showing the GUI/ or a menu class, one for implementation of the business logic say for example if you want to work upon the characters and their values in anyway and one for the database connection (as you are already going to make).

PS: Sorry I just overlooked your question in the above post. You do not need to connect the classes using an array. You could call methods in the database class that take the array as an input. To be able to do this you just need to import the class using the import keyword. If your classes are public they are available to you always as long as they fall in the classpath. If they are not then you would have to put them into the same package so that they could access each other (they still need to be in the classpath).

For example you could put all the classes into the package com.example.projectname by putting

package com.example.project;

as their first non-comment line.

Thanks for the help :P. Today, the teacher explained and showed us an example on how we are supposed to go about with the whole database thing so I somewhat understand it now. Also, your post helped clear somethings up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.