Member Avatar for TheStig01

Hi all,

Last night i started coding a database with java. The way i was going to do it was through netbeans with a database connection to an sql database.
Then i went to school and told my teacher how i was going to do it and he told me that i must strictly use the java language and no sql :/ He told me to use arraylists.

Now i have been reading arraylist from the Java for beginners book by Schilt and seem to have gotten the jist.... but i cannot see how i can build a database with it and use it through a gui.

Basically i want to be able to store for example names in an arraylist that is entered through a textfield and then is saved into the class.

If someone can give me an idea on how i can approach such a task it would be really helpfull.

Thanks for you time.

Form what I understood I would put the data entered in an object and once there add that object to the arraylist. This way you can simulate what a sql table would do, evey index in the arraylist would be a "row and every object property woulld be a "column"

Hope this helps.

Member Avatar for TheStig01

Thanks for your reply reallly appreciate it.

To get you in the picture im doing an insurance database. Now as you can imagine i have around 30 variables.

Now you are telling me that i should create an object per variable such as for name, surname address etc.. which uses the scanner class for input.

Now at school we learnt that each object is an instance of a class. So therefore i must create 30 different seperate classes for each variable. Am i right in saying this?

Then i create different arraylists per object.
Where the index of each arraylist will correspond to a row and each different type of object will indicate a column.

Thank you for your time and if you can please tell me if i understood you correct and reply to me about the objects so i can continue coding right away :)

I'm not sure of the best way to do this exactly, but I doubt you would actually have to create 30 separate classes. Think of ways you could simplify things. You probably don't need a "name" class, since you could just make the name a string. Since it's an insurance database, maybe you could just have 2 or 3 main classes, such as "client", or "claim" etc. I'd have to see the details of the assignment to give you more specific advice, but try and group information in classes, and use member variables for those specific pieces of information.

Think of an ArrayList as table. You'd make 1 class for each different table, not each variable. Let's say you need to store customer information, a typical table would look like this:

Name	Address		Phone		Email
--------------------------------------------------------
John	123 main st.	555-1234	john@gmail.com
Sue	654 4th st.	555-6547	sue@gmail.com

Create a class which stores those 4 fields (UserDetails). Every row added to this 'table' (the arrayList) would be an object of UserDetails.

You might have another table called Transactions with other various fields. Create a class for that and store it in another ArrayList.

To manage all these ArrayLists, I'd use a HashMap and associate each array list with a 'table' name. That would make it easier to call on the table you want.

HashMap tables = new HashMap();
tables.put("UserDetailsTable", new ArrayList<UserDetails>());

public getTable(String name){
    return (ArrayList)tables.get(name)
}

I agree with Phaelax and Mikeyp926. That's what I tried to explain in my first reply.

BTW awesome explanation Phelax.

Member Avatar for TheStig01

Ok so this is what i have done so far is create 2 classes. These are Class 1 has all the insurance information and Class 2 which inherits class 1 has all the client details. As you can see is i only started the basic getter and setter and constructors. Now i will do what Phaelax has told me.

These are the 2 Classes i coded so far.

Class 1:

class insurance {

// Insurance Details

private int policyno; 
private String typeofinsurance;
private int dsp; 
private int dep; 
private int ncbyears; 
private double discount; 
private String claim;
private String fault;

//Calculator Details

private double annualprem;
private double monthlyprem;

// Default Constructer for IDB

insurance  () {

discount = 0.0;
policyno = dsp = dep = ncbyears = 0;
typeofinsurance = claim = fault = "null";
}

// Parameterized Constructer

insurance  ( int pn, String typeofinsurance, int dsp, int dep, int ncbyears, double discount, String claim, String fault, double ap, double mp ) {

policyno = pn;
typeofinsurance = typeofinsurance;
dsp = dsp;
dep = dep;
ncbyears = ncbyears;
discount = discount;
claim = claim;
fault = fault;
annualprem = ap;
monthlyprem = mp;

}

// Getters and Setters

int getPolicyno() {
    return policyno;
}

void setPolicyno( int pn ) {
    policyno = pn;
}

String getTypeofinsurance() {
    return typeofinsurance;
}

void setTypeofinsurance ( String typeofinsurance ) {
    typeofinsurance = typeofinsurance;
}

int getDsp() {
    return dsp;
}

void setDsp( int dsp){
    dsp=dsp;
}

int getDep() {
    return dep;
}

void setDep( int dep){
    dep = dep;
}

int getNcbyears() {
    return ncbyears;
}

void setNcbyears ( int ncbyears ) {
    ncbyears = ncbyears;
}

double getDiscount() {
    return discount;
}

void setDiscount( double discount) {
    discount = discount;
}

String getClaim () {
    return claim;
}

void setClaim ( String Claim ) {
    claim = claim;
}

String getFault() {
    return fault;
}

void setFault() {
    fault = fault;
}

double getAnnualprem() {
    return annualprem;
}

void setAnnualprem ( double ap ) {
    annualprem = ap;
}

double getMonthlyprem() {
    return monthlyprem;
}

void setmonthlyprem ( double mp ) {
    monthlyprem = mp;
}
}

Class 2:

class client extends insurance {

private int id;
private int dob; 
private int age;
private String name;
private String surname;
private String address;
private String job;
private String status;
private String postcode;

client() {
super();
}

client ( int pn, String typeofinsurance, int dsp, int dep, int ncbyears, double discount, String claim, String fault, double ap, double mp ,  int id , int dob, int age, String name, String surname, String address, String job, String status, String postcode) {
super ( pn, typeofinsurance, dsp, dep, ncbyears, discount, claim, fault, ap, mp );
id = id;
dob = dob;
age = age;
name = name;
surname = surname;
address = address;
job = job;
status = status;
postcode = postcode;
}

int getId() {
    return id;
}

void setId (int id ){
id = id;
}

int getDob() {
    return dob;
}

void setDob ( int dob ) {
    dob = dob;
}

int getAge() {
    return age;
}

void setAge() {
    age = age;
}

String getName() {
    return name;
}

void setName ( String Name ) {
    name = name;
}

String getSurname() {
    return surname;
}

void setSurname( String surname ) {
   surname = surname;
}

String getAddress() {
    return address;
}

void setAddress( String address ) {   
    address = address;
}

String getJob() {
    return job;
}

void setJob ( String job){ 
    job = job;
}

String getStatus () {
    return status;
}

void setStatus( String Status ) {
    status = status;
}

String getPostcode () {
    return postcode;
}

void setPostcode ( String postcode ) {
    postcode = postcode;
}
}

Phaelax i read your post and i must say its very helpful. Today i will continue coding according to how you explainded to me. If i run into any problems i will just post it here.

Thanks

Nice. Just a tip use the this keyword on the classes member in your constructor and setters to differentiate them from the parameters.

Ex.

insurance  ( int pn, String typeofinsurance, int dsp, int dep, 
      int ncbyears, double discount, String claim, String fault, 
      double ap, double mp ) {
 
   this.policyno = pn;
   this.typeofinsurance = typeofinsurance;
   this.dsp = dsp;
   this.dep = dep;
   this.ncbyears = ncbyears;
   this.discount = discount;
   this.claim = claim;
   this.fault = fault;
   this.annualprem = ap;
   this.monthlyprem = mp;
}
Member Avatar for TheStig01

Thanks i just did that. This is the updated ClientDetails Class :

import java.util.Scanner;
class ClientDetails {

// Client Details

private int id;
private int dob; 
private int age;
private String name;
private String surname;
private String address;
private String job;
private String status;
private String postcode;

private static int selection;

// Constructor for Client Details
ClientDetails (int id , int dob, int age, String name, String surname, String address, String job, String status, String postcode) {
    this.id = id;
     this.dob = dob;
     this.age = age;
     this.name = name;
     this.surname = surname;
     this.address = address;
     this.job = job;
     this.status = status;
     this.postcode = postcode;
}

// Getters and Setters for Client Details

int getId() {
    return this.id;
}

void setId (int id ){
this.id = id;
}

int getDob() {
    return this.dob;
}

void setDob ( int dob ) {
    this.dob = dob;
}

int getAge() {
    return this.age;
}

void setAge() {
    this.age = age;
}

String getName() {
    return this.name;
}

void setName ( String Name ) {
    this.name = name;
}

String getSurname() {
    return this.surname;
}

void setSurname( String surname ) {
   this.surname = surname;
}

String getAddress() {
    return this.address;
}

void setAddress( String address ) {   
    this.address = address;
}

String getJob() {
    return this.job;
}

void setJob ( String job){ 
    this.job = job;
}

String getStatus () {
    return this.status;
}

void setStatus( String Status ) {
    this.status = status;
}

String getPostcode () {
    return this.postcode;
}

void setPostcode ( String postcode ) {
    this.postcode = postcode;
}

public String toString() {
        return  "" + id + "     " + dob + "     " + age + "     " + name + "       " + surname + "      " + address + "     " + job + "     " + status + "      " + postcode;
    }

public static void getClientMenu(){
    System.out.println("1.Enter Client Details \n 2.Print the Client Database \n 3.Exit the Database.");
}

static Scanner input= new Scanner(System.in);
    
public static Integer getSelection() {
    System.out.println("Please enter selection:");
    selection =input.nextInt();
    return selection;
}

public static void getMainTitle() {
        System.out.println("MAIN MENU\nWelcome to the Client Database\n");
}
}

And this is the use_Client Class that allows the user to the enter the data in an arraylist just like Phaelox said.

import java.util.*;

class use_ClientDetails 

{

    public static void main ( String[] Args ) 
    {
    ArrayList<ClientDetails> ClientDatabase = new ArrayList<ClientDetails>();

    Scanner InputClientDetails = new Scanner (System.in);
    ClientDetails.getMainTitle();
    ClientDetails.getClientMenu();
    
    int selection;
	System.out.println("Please enter selection\n");
	selection = InputClientDetails.nextInt();
	for(selection=0; selection <= 3; selection++)
	
	{	  
	   switch(selection){
	   case 1: do 
	               {System.out.println("Enter Id Number");
	                    int id = InputClientDetails.nextInt();    
	                System.out.println("Enter Date Of birth");
	                    int dob = InputClientDetails.nextInt();    
	                System.out.println("Enter Age");
	                   int age = InputClientDetails.nextInt();    
	                System.out.println("Enter Name");
	                   String name = InputClientDetails.next();    
	                System.out.println("Enter Surname");
	                   String surname = InputClientDetails.next();    
	                System.out.println("Enter Address");
	                   String address = InputClientDetails.next();    
	                System.out.println("Enter Job");
	                   String job = InputClientDetails.next();    
	                System.out.println("Enter Maritial Status");
	                   String status = InputClientDetails.next();    
	                System.out.println("Enter Postcode");
	                   String postcode = InputClientDetails.next();    
	                ClientDatabase.add(new ClientDetails(id, dob, age, name, surname, address, job, status, postcode));
                    ClientDetails.getClientMenu();}
                    while (ClientDetails.getSelection()==1);						
	 case 2: System.out.println("ID         DOB         Age          Name        Surname         Address         Job            Maritial Status         Postcode  ");
		     System.out.println("---------------------------------------------------------------------------------------------------------------------------------");
				for(int i =0; i<ClientDatabase.size();i++){
				System.out.println(ClientDatabase.get(i));

					}
					}
				}

}
}

So far the program allows me to enter the data but when i come to select the second choice to print the database everything is returning null and 0. Any solution.

Also i plan on doing this for every Mini database such as The Client Details, Car Details, Insurance Details. Then i add them up together using the maps. Can this be done?

Thanks again.

I'll think about the problem. Also I would put the setter and getters as public, unless you have a good reason not to of course. thats the way I do it at work at least. I'll check it out once I get out of here and let you know if I find anything.

Good job BTW :)

Always happy to help out a fellow top gear viewer ;)

Member Avatar for TheStig01

Thanks if i need any help i will ask :P Up till now im managing. Top Gear is simply the best :)

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.