Hi, So i'm trying to write a program that will work like a credit card database. The instructions say to add a file with 10,000 names that was given to us and add it to our project file. a scanner is suppossed to read through that file and add each line to an ArrayList I'm calling database. I've written a class called Customer() and a class called CustomerDatabase(), I need to call methods from Customer() to use in CustomerDatabase and the program is saying that I cannot call a nonstatic method into a static one. So I made an instance of Customer(), and now its saying that it can't find the cunstructor for Customer(). If anyone has any ideas, that would be great, thanks. The method i'm working on now where it wont let me call a method is
"public int countCustomers(int zip)"

if anyone can see a way for me to use this method that would be wonderful, Thank you!

CUSTOMER CLASS:

/**
 * Maintains minimum information about customers  such as, gender, name, and date of birth, address, 
 * city, state, zipcode, credit card brand and current balance. 
 * 
 * @LindseyScribner 
 * @3/16/13
 */

public class Customer
{
    private boolean gender, female;
    private String firstName, lastName, address, city, state, cardBrand;
    private int birthDay, birthYear, birthMonth, zipcode;
    private double currentBalance;

    //initializes all instand variables and sets them to appropriate values. 
    public Customer(String info)
    {
        // Split the info string into multiple pieces separated by ‘,’ or ‘/’

        String [] tokens = info.split(",|/");

        if (tokens[0].trim() == "female")
        {
            gender = true;
        }
        else
        {
            gender = false;
        }

        firstName = tokens[1].trim();
        lastName = tokens[2].trim(); 

        birthDay = Integer.parseInt(tokens[3].trim());
        birthMonth = Integer.parseInt(tokens[4].trim());
        birthYear = Integer.parseInt(tokens[5].trim()); 

        address = tokens[6].trim(); 
        city = tokens[7].trim(); 
        state = tokens[8].trim(); 
        zipcode = Integer.parseInt(tokens[9].trim()); 

        cardBrand = tokens[10].trim(); 
        currentBalance = Double.parseDouble(tokens[11].trim());


    }

    //returns true if gender is female and false if gender is male.
    public boolean isFemale()
    {

        if (gender == true)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    //returns first name of customer
    public String getFirst()
    {
        return firstName;
    }

    //returns last name of customer
    public String getLast()
    {
        return lastName;
    } 

    //
    public String getAddress()
    {
        return address;
    } 

    public String getCity()
    {
        return city;
    } 

    public String getState()
    {
        return state;
    } 

    public String getCreditCard()
    {
        return cardBrand;
    } 

    public int getZip()
    {
        return zipcode;
    } 

    public int getDay()
    {
        return birthDay;
    } 

    public int getMonth()
    {
        return birthMonth;
    } 

    public int getYear()
    {
        return birthYear;
    } 

    public double getBalance()
    {
        return currentBalance;
    }

    public String toString ()
    {
        String result = getFirst() + getLast() + getAddress() + getCity() + getState()+ getZip();
        return result;


    }

CUSTOMERDATABASE CLASS

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.util.Date;
/**
 * Write a description of class CustomerDatabase here.
 * 
 * @Lindsey Scribner
 * @March 28, 2013
 */
public class CustomerDatabase
{
    // instance variables
    private ArrayList <Customer> database;
    private Customer c;

    public CustomerDatabase()
    {
        database = new ArrayList<Customer>();
        c = new Customer();

    }

    public void readCustomerData(String CustomerNames) 
    {
        // Attempt to read the complete set of data from file.

        try
        { 

            File f = new File("CustomerNames.txt");
            Scanner sc = new Scanner(f);
            String logline;

            // read header information first
            logline = sc.nextLine();

            while(sc.hasNext()) {
                logline = sc.nextLine();

                // remove this print statement after method works
                System.out.println(logline);

                Customer c = new Customer(logline);

                database.add(c);

            }

            sc.close();
        }
        catch (IOException e) 
        {
            System.out.println("Failed to read the data file: " +
                "CustomerNames.txt");
        }
    }

    public void addCustomer(Customer c)
    {
        database.add(c);
    }

    public int countCustomers()
    {
        return database.size();
    }  

   public int countCustomers(int zip)
    {
        int count;

        for (count = 0; count < database.size(); count++)
        {
            if (c.getZip() == zip)
            {
                count ++;
            }
        }

        return count;
    }

    public int countDebtFree()
    {
        int count;

        for (count = 0; count <database.size(); count++)
        {
            if (c.getBalance() == 0)
            {
                count ++;
            }

        }

        return count;
    }

    public Customer getHighestDebt ( ) 
    {
        int count;
        for (Customer c: database)
        {

        }
        return c;
    }

    public Customer getYoungestCardholder ( ) 
    {

        for (Customer c: database)
        {
            Date Todaysdate = new Date();
        }
        return c;
    }


}

Recommended Answers

All 4 Replies

it can't find the default constructor for Customer, because there isn't one. you'll need to pass a String as parameter.

public CustomerDatabase()
    {
        database = new ArrayList<Customer>();
        c = new Customer();
    }

here, you try to instantiate a Customer object without passing a String as a parameter.

in your other methods:

logline = sc.nextLine();
                // remove this print statement after method works
                System.out.println(logline);
                Customer c = new Customer(logline);

here you pass the String logline as parameter.

It is true that the compiler will add a default non-argument constructor in each of your classes, but this only happens if you do not explicitly add a constructor yourself, which you did do:

public Customer(String info)
    {

so, either ad a default constructor, or pass a String in each instantiation.

what exactly do you mean by add a default construstor? what's there now is what my professor told us to do in the instructions. Thanks for the help by the way!!

@stultuske

Hi let me give you an example about the default constructor. Suppose you have this:

class MyClass {

   public String a = null;
}

You can instantiate it like this:

MyClass cl = new MyClass();

You haven't declared a constructor so it will use the "default". A no-argument constructor.
If you had:

class MyClass {

 public MyClass(String a) {
 }

}

Then the code: MyClass cl = new MyClass(); would not compile. Since you declared a constructor (like in your case) it will use that.

In your case you have a constructor declared in the Customer:
public Customer(String info)
So when you call the c = new Customer() it doesn't compile because such constructor doesn't exist in your Customer class.
In my opinion you don't need one. Just delete the Customer c that you have in the CustomerDatabase. Use what you have in your other post.

Also don't do that: tokens[0].trim() == "female" . For Strings use the equals method:
gender = tokens[0].trim().equals("female")

as for the default constructor, it is basically a constructor which takes no parameters.
if for some reason you don't provide one in your code, the compiler will add the default parameter for you, to make sure you'll be able to instantiate that class.

so, basically: after compilation, the next two classes are equal to each other:

public class MyClass{

}

and

public class MyClass{

  public MyClass(){
  // this is a default constructor. the compiler will add this automatically if you 
  // don't provide a constructor yourself
  }
}

and, indeed, checking String objects, as JavaAddict remarks, should be done with the equals method.
if you keep it with the == operator, well ... it will compile, it will run, but the chances of you getting the results you want are very slim.

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.