mercredi 25 novembre 2015

Data Access Object IF Error

I'm creating a restaurant booking system and I have created the customer class but as part of the class you have be able to extend it so a company can add a contact.

I have a customer class, a company class and a TextCustomerDAO. inside the DAO is where i'm getting the error. When I try to say "If 'Y' then add a company contact" if no then just add customer. I can just add customer but when I try to add in the company contact i get an error

TextCustomerDAO: error just after the if statement where it says contact = companyContact.getContact();

 package projects.dao.textdao;


 import java.io.BufferedReader;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.nio.file.Path;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 import java.util.Scanner;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import projects.Customer;
 import restaurant.dao.CustomerDAO;
 import projects.Company;

public class TextCustomerDAO extends CustomerDAO {
static final char DELIMITER=':';

@Override
public List<Customer> loadCustomers(Path path) {
    List<Customer> customers = new ArrayList<>();   
   try (BufferedReader br = new BufferedReader(new FileReader(path.toString()))) {
        Customer c = null;
        Company co = null;
        int customerId;
        String customerName, customerPhone, customerAddress, customerEmail, contact;

         String[] temp;
         String line = br.readLine();

        while(line!=null){
            temp=line.split(Character.toString(DELIMITER));
            customerId = Integer.parseInt(temp[0]);
            customerName = temp[1];
            customerPhone = temp[2];
            customerAddress = temp[3];
            customerEmail = temp[4];

            if (temp.length==6){ 
               String companyContact = temp[5];
               contact = companyContact.getContact();
                co = new Company(customerId, customerName, customerPhone, customerAddress, customerEmail, contact);
                customers.add(co);
            }
            else {
                c = new Customer(customerId,customerName, customerPhone, customerAddress, customerEmail);
                customers.add(c);  
            }
            line = br.readLine();
        }  
        br.close();
    } catch (IOException ex) {
        Logger.getLogger(TextCustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
    }               
    return customers;
}

@Override
public void storeCustomers(Path path, List<Customer> customers) {
    try (PrintWriter output = new PrintWriter(path.toFile())) {
        for (Customer c:customers) {
            output.println(toFileString(c));
        }
        output.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(TextCustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
    }        
}

public String toFileString(Customer c) {
    return  Integer.toString(c.getCustomerId()) + DELIMITER +
            c.getCustomerName() + DELIMITER +
            c.getCustomerPhone() + DELIMITER +
            c.getCustomerAddress() + DELIMITER +
            c.getCustomerEmail() + DELIMITER;
} 

    public String toFileString(Company co) {
    return  Integer.toString(co.getCustomerId()) + DELIMITER +
            co.getCustomerName() + DELIMITER +
            co.getCustomerPhone() + DELIMITER +
            co.getCustomerAddress() + DELIMITER +
            co.getCustomerEmail() + DELIMITER +
            co.getContact() + DELIMITER;
} 


} 

Customer Class:

 public class Customer{
private int customerId;
private String customerName;
private String customerPhone;
private String customerAddress;
private String customerEmail;

private static int numberOfCustomers=0;

public Customer()
{
    this.customerId = 0;
    this.customerName = null;
    this.customerPhone = null;
    this.customerAddress = null;
    this.customerEmail = null;
    numberOfCustomers++;
}   

public Customer(int customerId, String customerName, String customerPhone,
        String customerAddress, String customerEmail)
{
    this.customerId = customerId;
    this.customerName = customerName;
    this.customerPhone = customerPhone;
    this.customerAddress = customerAddress;
    this.customerEmail = customerEmail;

    numberOfCustomers++;
}

public static int getNumberOfCustomers() {
    return numberOfCustomers;
}



public int getCustomerId()
{
    return customerId;
}    

public void setCustomerId(int customerId)
{
    this.customerId = customerId;
}      



public String getCustomerName()
{
    return customerName;
}     
public void setCustomerName(String customerName)
{
    this.customerName = customerName;
}     


public String getCustomerPhone()
{
    return customerPhone;
}        
public void setCustomerPhone(String customerPhone)
{
    this.customerPhone = customerPhone;
}


public String getCustomerAddress()
{
    return customerAddress;
}

public void setCustomerAddress(String customerAddress)
{
    this.customerAddress = customerAddress;
}    



public String getCustomerEmail()
{
    return customerEmail;
}

public void setCustomerEmail(String customerEmail)
{
    this.customerEmail = customerEmail;
}              

@Override
public String toString() {
    return  "customer id: " + getCustomerId() + ", " +
            "customer name: " + getCustomerName() + ", " +
            "customer phone: " + getCustomerPhone() + ", " +
            "customer address: " + getCustomerAddress() + ", " +    
            "customer email: " + getCustomerEmail();

}      
 }

Company Class:

package projects;

public class Company extends Customer {
private String contact;

public Company() {
    super();
    this.contact = null;
}

public Company(int customerId, String customerName, String customerPhone, String customerAddress, String customerEmail, String contact) {
    super(customerId, customerName, customerPhone, customerAddress, customerEmail);
    this.contact = contact;
}    


public String getContact() {
    return this.contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

@Override
public String toString() {
    return super.toString() + "Company Contact" + getContact();
}
 }

Sorry if this is an obvious error but i have been trying for some time to fix the problem. If someone could assist or point me in the right direction it would be greatly appreciated.

The error message: error: cannot find symbol contact = getContact(); symbol: method getContact() location: class TextCustomerDAO

Thanks

Aucun commentaire:

Enregistrer un commentaire