File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Beginner Java Program Help/ txt database search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner Java Program Help/ txt database search" Watch "Beginner Java Program Help/ txt database search" New topic
Author

Beginner Java Program Help/ txt database search

Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
Hi all, I am new to java and what I have learned so far I thoroughly enjoy! I am currently stuck on direction for this code I have to write. I am not looking for someone to do the work for me, but I am truly having a problem even getting started. Any help would be awesome!

Here are the instructions:

Implement a program to read and search the text database for a furniture store.
1. Use the following classes:
o Store
o Furniture
Type
Style
o Manufacturer
Address
2. Use the ArrayList class to hold instances of Furniture and Manufacturer. Be sure to use generics appropriately.
3. Use a text data file with the following format - which you may extend, and may be extended in the later projects

All the
classes should be provided with appropriate:
o constructors
o toString implementations
o additional methods
o instance variables connecting the class to appropriate instances of other classes
4. A GUI that will display allow the user to minimally:
o search on furniture or manufacturer index
o search on type
o seach on style
5. The GUI class(es) should be distinct from the other classes in the program.


SO... I've written the GUI class, which I can provide (I've also created the other classes but they are practically empty), and here is the database sample text I have
F:<254>:<sofa>:<Victorian>:<399.99>:<32>
F:<250>:<chair>:<Victorian>:<799.99>:<32>
F:<255>:<chair>:<Jacobean>:<1899.99>:<37>
M:<32>:<Junior LLC>:<10 binary St>:<Binaryville>:<MD>:<20853>:<(301)-123-4567>
M:<37>:<Macy's Inc>:<8 Spring Lake>:<Old Town>:<MD>:<20906>:<(301)-193-4767>
M:<38>:<Java Corp>:<19 Duke Pl>:<O Town>:<SC>:<30210>:<(301)-945-3218>

I'm unclear on what information the classes (Store, Furniture, Manufacturer) should hold and how to link them to the database. Like I said, I'm new to this!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
£1899.99 for a Jacobean chair? You won't get many customers at that price. Unless the things really is >400 years old.

Actually, the instructions you have been given are very helpful. What you want is to create a Furniture class and test it with a FurnitureDemo class.
I shall give you an example of the FurnitureDemo class, to give you an idea what you want.If you have a UML diagram, you would have the starting methods in the classes in the middle, and you design your application from the inside out. But when you are coding, it is usually easier to go the other way, from the outside in. So get your Furniture working, then try the Store, or get something to read your text file.
Get your application working in little bits, and don't try too much at once.
I would have thought you should leave the GUI till later. You can leave it on your hard drive somewhere and retrieve it when you are ready for it.>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
. . . and welcome to the Ranch
Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
Ha Ha, yes the chair is expensive. Thanks for the response. I will do some coding and post it soon.
Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
ok... Here is what I have so far, but the problem is that I dont know what to do next. It's more an issue of not understanding how the program should work rather than how to code it.

import java.io.*;



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Afraid not, no.

A Furniture class should store its data, eg price, description, colour, size, as appropriate. It should also model any real behaviour of furniture with its methods, eg fallApartAfterTwentyMinutes(boolean b). Actually it would be better to write a fallApartAfter(TimeUnit t, int howMany), then you can have it fall apart after 20 minutes or 20 years
Then test the Furniture class with a class like that I showed you yesterday.
Leave out the reading from the file for the time being. You are making life difficult for yourself by trying to do too many things at once.
Why are you using StringTokenizer? Have you read its documentation?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
You are also making life difficult for yourself with inconsistent indentation. You need to get all your {} pairs correctly matched. If you don't match them and are lucky, you will get a compiler error. If you are unlucky, on the other hand, you will get code which isn't executed when you expect it. We have some suggestions about indentation. Don't use tab characters. Don't write from top to bottom as if you were writing a letter, but from outside in. Get a decent text editor, as mentioned in that last link.
Getting your indentation right and consistent makes it easier for people (yourself included) to read the code, and easier to see which block is which.
Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
Ok... Taking baby steps here... I have just barely started my furniture class



So I have declared my variables for this class and their data types, however I don't understand what methods this class needs to hold. What purpose does this class serve in the overall program besides just declaring variables? Also, before I was able to read your suggestion to hold off on the ReadFile class I gave it another go and here is what I have for both writing the file and reading it (two classes)


Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
And my file reader:

import java.io.File;
import java.util.Scanner;
public class FileReader {

/**
* @param args
*/
public static void main(String[] args)throws Exception {
java.io.File myfile = new java.io.File("FMData.txt");

Scanner myinput = new Scanner (myfile);

while (myinput.hasNext()){
String FandM = myinput.next();
System.out.println (FandM);
}
myinput.close();

}

}

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Ryan Daniel wrote: . . . ... I have just barely started my furniture class

. . .
So I have declared my variables for this class and their data types, . . .
Afraid you haven't. I went back and added code tags, which allow you better to see the structure of the class, and we see they aren't variables for the class at all. They are local variables inside the main method.
I'm afraid it's back to the drawing board.

You should not only look at your own thread, but at others. You find somebody else has designed a class to model a Bank Account. Here. Apart from the name field not being declared final, take that class as a model for the Furniture class. (You have specifically been told to call it Furniture. You ought to look at the instructions for the assignment, because they are very helpful.) I would have given the withdraw method a void return type myself, and missed out the return statement, but others might not agree with me.

And you are still not indenting correctly. That will lose you marks (at least it ought to), as well as making it easy to overlook errors in the code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Don't call a class FileWriter, because such a class already exists. I don't think you need that class at all, because you will write that information in a text file by hand, surely?
Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
Wow. Let's pray I did something right here! I created a class Furniture that will display furniture by type or style, however it does not read and search a text file, as is required in the final project. Getting desperate at this point

import java.util.Scanner;

public class Furniture
{
public static void main(String[] args)
{


String type = "Chair, Sofa";
String style = "Victorian, Jacobean";
int choice;
int choice2;
int choice3;

System.out.println ("Choose how you would like to search Furniture.");
System.out.println ("Press 1 for type, or 2 for style.");

Scanner input = new Scanner(System.in);

choice = input.nextInt();

if (choice == 1)
{

System.out.println ("The furniture types are:" + type);
System.out.println ("Press 1 to view chair(s), or 2 to view sofa(s)");
Scanner input2 = new Scanner(System.in);
choice2 = input2.nextInt();

if (choice2 == 1)
{

System.out.println ("<chair>:<Victorian>:<799.99>"+
"<chair>:<Jacobean>:<1899.99>}");
}

else if (choice2 == 2)
{

System.out.println ("<sofa>:<Victorian>:<399.99>");
}

else
{

System.out.println ("You entry is invalid");
}

}
else if (choice == 2)
{

System.out.println ("The furniture styles are:" + style);
System.out.println ("Press 1 to view Victorian, or 2 to view Jacobean");
Scanner input3 = new Scanner(System.in);
choice3 = input3.nextInt();

if (choice3 == 1)
{

System.out.println ("<Victorian>:<chair>:<799.99> " +
"<Victorian><sofa>::<399.99>");
}

else if (choice3 == 2)
{

System.out.println ("<Jacobean>:<chair>:<1899.99>");
}

else
{

System.out.println ("You entry is invalid");
}

}
else
{

System.out.println ("You entry is invalid");
}
}


}
Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
Also, my indentation matched that of the bank example you gave, until I posted it here...
Ryan Daniel
Greenhorn

Joined: Sep 20, 2011
Posts: 8
Anyway, I give up! I have run out of time for this project. Thanks for all your help and for trying to lead me to the answer. Hopefully our paths will cross again here when I am a little more Java smart!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner Java Program Help/ txt database search
 
Similar Threads
Suitable datastructure for classes...?
Why static variables can not be overridden, Any Example
HELP: Suitable datastructure for classes
Passing Database query results through a method to a JTable
passed SCJD with 360/400