数据访问对象 IF 错误
Data Access Object IF Error
我正在创建一个餐厅预订系统,我已经创建了客户 class 但作为 class 的一部分,您可以扩展它以便公司可以添加联系人。
我有一个客户 class、一个公司 class 和一个 TextCustomerDAO。在 DAO 内部是我遇到错误的地方。当我尝试说 "If 'Y' then add a company contact" 时,如果没有,则只需添加客户。我可以添加客户,但是当我尝试添加公司联系人时出现错误
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;
}
}
客户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();
}
}
公司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();
}
}
抱歉,如果这是一个明显的错误,但我已经尝试了一段时间来解决这个问题。如果有人可以帮助或指出正确的方向,将不胜感激。
错误信息:error: cannot find symbol
接触 = getContact();
符号:方法 getContact()
位置:class TextCustomerDAO
谢谢
String companyContact = temp[5];
contact = companyContact.getContact();
companyContact
是 String
类型,String
上没有 getContact()
方法。
我正在创建一个餐厅预订系统,我已经创建了客户 class 但作为 class 的一部分,您可以扩展它以便公司可以添加联系人。
我有一个客户 class、一个公司 class 和一个 TextCustomerDAO。在 DAO 内部是我遇到错误的地方。当我尝试说 "If 'Y' then add a company contact" 时,如果没有,则只需添加客户。我可以添加客户,但是当我尝试添加公司联系人时出现错误
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;
}
}
客户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();
}
}
公司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();
}
}
抱歉,如果这是一个明显的错误,但我已经尝试了一段时间来解决这个问题。如果有人可以帮助或指出正确的方向,将不胜感激。
错误信息:error: cannot find symbol 接触 = getContact(); 符号:方法 getContact() 位置:class TextCustomerDAO
谢谢
String companyContact = temp[5];
contact = companyContact.getContact();
companyContact
是 String
类型,String
上没有 getContact()
方法。