java - 数据访问对象 IF 错误

标签 java if-statement netbeans dao

我正在创建一个餐厅预订系统,并且已经创建了客户类别,但作为该类别的一部分,您可以扩展它,以便公司可以添加联系人。

我有一个客户类、一个公司类和一个 TextCustomerDAO。 DAO 内部是我收到错误的地方。当我尝试说“如果‘Y’则添加公司联系人”时,如果否,则只需添加客户。我可以添加客户,但是当我尝试添加公司联系人时,出现错误

TextCustomerDAO:在 if 语句后面出现错误 联系人 = 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;
} 


} 

客户类别:

 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();

}      
 }

公司类别:

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();
}
 }

很抱歉,如果这是一个明显的错误,但我已经尝试了一段时间来解决该问题。如果有人可以帮助我或为我指明正确的方向,我将不胜感激。

错误信息:错误:找不到符号 联系= getContact(); 符号:方法 getContact() 位置:类 TextCustomerDAO

谢谢

最佳答案

   String companyContact = temp[5];
   contact = companyContact.getContact();

companyContactString 类型,并且 String 上没有 getContact() 方法。

关于java - 数据访问对象 IF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925604/

相关文章:

java - Jmap 无法连接进行转储

php - 如何在 PHP 函数中执行 if 语句?

c++ - 正确关闭 while(cin.good()) 循环

php - 在 Fedora 上使用 Xdebug 和 Netbeans 进行调试

java - 如何从字符串中解析日期并保持其格式?

java - 在 Web 应用程序中使用 Java 和 JSON 文件设计建议

java - 如何使用数组中存储的数据每 24 小时发送一次自动电子邮件 - Android studio

python - 字符串以特定文本开头

java - 使用 NetBeans 时在 tomcat 中出现错误

java - 资源 URI 在 tomcat 8 服务器上的 Java RESTful Web 服务中不起作用