首页 > 解决方案 > 在 switch case 方法调用中预期的表达式

问题描述

我的 Java 代码是一个简单的 CRM,它使用用户输入来详细说明潜在客户的信息。但这并不重要。我的麻烦是我试图将 ArrayList<> 中的对象写入 .txt 文件。

我有编写对象的方法,但是这行代码:

case "20" : {
                saveCustomerToFile(leadfilepath,);
                break;
            }

错误说表达式是预期的,但我不知道该放什么。

这是我的代码:

主类:

public class Main {

private static int leadsId = 0;
private static int interactionId = 0;

private static LeadsManager leadsManager = new LeadsManager();
private static InteractionsManager interactionsManager = new InteractionsManager();

public static void main(String[] args) {

    String leadfilepath = "leads.txt";

    //Declare Scanner
    Scanner input = new Scanner(System.in);

    System.out.print("[MENU] \n"
            + "===[MANAGING LEADS]===  \n"
            + "1) View all Leads \n"
            + "2) Create new Leads \n"
            + "3) Update a lead \n"
            + "4) Delete a lead \n"
            + "===[MANAGING INTERACTIONS]=== \n"
            + "5) View all Interactions \n"
            + "6) Create new Interactions \n"
            + "7) Update an Interaction \n"
            + "8) Delete an Interaction \n"
            + "===[Reporting and Statistics]===\n"
            + "10) N/A\n"
            + "11) N/A\n"
            + "12) N/A\n"
            + "===[Save Progress]===\n"
            + "20) Save leads arrays data into file.\n"
            + "Please input the desired function:");

    while(true){
        String answer = input.nextLine();

        switch (answer){
            case "1" : {
                printLeads();
                break;
            }

            case "2" : {
                LeadMethod();
                break;
            }

            case "3": {
                String leadId = enterLeadIdPrompt();
                updateLeads(leadId);
                break;
            }

            case "4" : {
                String leadId = enterLeadIdPrompt();
                deleteLeads(leadId);
                break;
            }

            case "5" : {
                System.out.println("Printing all the sales people in the list ...");
                System.out.println(" ");
                printAllInteractions();
                break;
            }

            case "6" : {
                addInteractionInfo();
                break;
            }

            case "7" : {
                String interactionId = enterInteractionIdPrompt();
                updateInteractionInfo(interactionId);
                break;
            }

            case "8" : {
                String interactionId = enterInteractionIdPrompt();
                deleteInteractionInfo(interactionId);
                break;
            }

            case "20" : {
                saveCustomerToFile(leadfilepath,);
                break;
            }
        }
    }

 }




///////////////////////Methods for Lead///////////////////////////////////


////Main method for creating leads/////
public static void LeadMethod(){

    Leads leads = createLeadsObject();

    if(leadsManager.addLeads(leads)){
        System.out.println("Added Lead Successfully! \n" + leads);
    }

    else{
        System.out.println("Adding Lead failed!");
    }


}

public static void saveCustomerToFile(Leads lead, String leadfilepath){
    try{
        FileWriter leadfw = new FileWriter(leadfilepath, true);
        BufferedWriter leadbw = new BufferedWriter(leadfw);
        PrintWriter leadpw = new PrintWriter(leadbw);

        leadpw.println(lead.toString());
        leadpw.flush();
        leadpw.close();

        JOptionPane.showMessageDialog(null, "Leads saved!");

    }catch (Exception e){
        JOptionPane.showMessageDialog(null, "Leads not saved!");
    }
}



/////Creating the Lead Object from the Class and User input////////
private static Leads createLeadsObject(){
    Scanner leadsInfoInput = new Scanner(System.in);
    Leads leads = new Leads();

    System.out.println("Enter Leads Data:");
    leads.setLead_id(String.format("lead_%03d", leadsId++));

    System.out.print("Name: ");
    String Name = leadsInfoInput.nextLine();
    leads.setName(Name);

    System.out.print("Date of Birth (MM/DD/YYYY) : ");
    String DateOfBirth = leadsInfoInput.nextLine();
    leads.setDoB(DateOfBirth);


    System.out.print("Gender (Male - 0, Female - 1) : ");
    String Gender = leadsInfoInput.nextLine();
    leads.setGender(Gender);

    System.out.print("Phone Number : ");
    String PhoneNumber = leadsInfoInput.nextLine();
    leads.setPhone(PhoneNumber);

    System.out.print("Email : ");
    String Email = leadsInfoInput.nextLine();
    leads.setEmail(Email);

    System.out.print("Address : ");
    String Address = leadsInfoInput.nextLine();
    leads.setAddress(Address);

    return leads;
}


////Printing out the leads on the console/////
private static void printLeads() { leadsManager.printAllLeads(); }


////Deleting a lead by ID////
private static void deleteLeads(String lead_id){
    boolean isDeleted = leadsManager.deleteLeads(lead_id);
    if(isDeleted){System.out.println("Deleting lead from the list...");
        System.out.println("Deleted " + lead_id + " successfully!");
    }else{
        System.out.println("Delete lead failed!");
    }
}


////Method to enter desired Lead ID////
private static String enterLeadIdPrompt(){
    System.out.print("Enter customer id : ");
    Scanner del = new Scanner(System.in);
    return del.nextLine();
}


/////Method for updating a lead's data///////
private static void updateLeads(String lead_id) {

    boolean isUpdated = leadsManager.updateLeads(lead_id);

    if(isUpdated){
        System.out.println("Update customer successful!");
    }else{
        System.out.println("Update customer failed.");
    }
}



/////////////////////////////////////////////////////////////////


//////////////////////Interactions Section///////////////////////

///Method to call for adding Interaction
private static void addInteractionInfo() {

    Interactions interaction = createInteractionObject();

    boolean isAdded = interactionsManager.addInteraction(interaction);
    if(isAdded){
        System.out.println("Interaction added successfully!\n" + interaction);
    }else{
        System.out.println("Interaction add failed !");
    }
}


////Method for Asking the Interaction's ID////
private static String enterInteractionIdPrompt(){
    System.out.print("Enter model.Interaction Id : ");
    Scanner interactionIdInput = new Scanner(System.in);
    return interactionIdInput.nextLine();
}


//////Method to call for deleting an Interaction
private static void deleteInteractionInfo(String interactionId) {
    boolean isDeleted = interactionsManager.deleteInteraction(interactionId);
    if(isDeleted){
        System.out.println("Delete interaction information successful!");
    }else{
        System.out.println("Delete interaction information failed.");
    }
}


////Method to call for Updating Interaction's data
private static void updateInteractionInfo(String interactionId) {
    boolean isUpdated = interactionsManager.updateInteraction(interactionId);
    if(isUpdated){
        System.out.println("Update interaction information successful!");
    }else{
        System.out.println("Update interaction information failed.");
    }
}


///Method to call for printing interactions///
private static void printAllInteractions() {
    interactionsManager.printAllInteractions();
}



/////User Input to create data for the Interaction object
private static Interactions createInteractionObject(){
    Scanner interactionInfoInput = new Scanner(System.in);
    Interactions interaction = new Interactions();
    interaction.setInter_id(String.format("inter_%03d", interactionId++));

    System.out.print("Date of interaction (MM/DD//YYYY) : ");
    String dateOfInteraction = interactionInfoInput.nextLine();
    interaction.setDoI(dateOfInteraction);

    System.out.print("Lead ID : ");
    String leadId = interactionInfoInput.nextLine();
    Leads lead = leadsManager.getLeadsById(leadId);

    if(lead == null){
        while(lead == null){
            System.out.println("Wrong customer id, please try again!");
            leadId = interactionInfoInput.nextLine();
            lead = leadsManager.getLeadsById(leadId);
        }
    }
    interaction.setLead_id(lead);

    System.out.print("Interaction Method (SNS / email / telephone / face to face) : ");
    String method = interactionInfoInput.nextLine();
    interaction.setMeans(method);

    System.out.print("Potential (P - positive, NEG - negative, NEU - neutral) : ");
    String potential = interactionInfoInput.nextLine();
    interaction.setPotential(potential);

    return interaction;
    }
}

我的潜在客户类:

public class Leads {

private String lead_id;
private String Name;
private String DoB;
private String Gender;
private String Phone;
private String Email;
private String Address;


//The Lead constructor
public Leads(String lead_id, String Name, String DoB, String Gender, String Phone, String Email, String Address){
    this.lead_id = lead_id;
    this.Name = Name;
    this.DoB = DoB;
    this.Gender = Gender;
    this.Phone = Phone;
    this.Email = Email;
    this.Address = Address;
}

public Leads() {

}

///Mutators and Accessors////
public String getLead_id() {
    return lead_id;
}

public void setLead_id(String lead_id) {
    this.lead_id = lead_id;
}

public String getName(){
    return Name;
}

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

public String getDoB(){
    return DoB;
}

public void setDoB(String DoB){
    this.DoB = DoB;
}

public String getGender(){
    return Gender;
}

public void setGender(String Gender){
    this.Gender = Gender;
}

public String getPhone(){
    return Phone;
}

public void setPhone(String Phone){
    this.Phone = Phone;
}

public String getEmail(){
    return Email;
}

public void setEmail(String Email){
    this.Email = Email;
}

public String getAddress(){
    return Address;
}

public void setAddress (String Address){
    this.Address = Address;
}


@Override
public String toString() {
    return "ID: "+lead_id + ", Name: "  + Name +", DOB: " + DoB + ", Gender: " + Gender + ", Phone 
    Number: " + Phone + ", Email: " + Email + ", Address: " + Address;
}
}

还有我的 LeadsManager 课程:

private ArrayList<Leads> leads = new ArrayList<>();

public ArrayList<Leads> getLeads(){
    return leads;
}




///Actual method to call a Lead from its ID/////
public Leads getLeadsById(String lead_id){
    for (int i = 0; i < leads.size(); i++){
        if(lead_id.equals(leads.get(i).getLead_id())){
            return leads.get(i);
        }
    }
    return null;
}


///Adding a lead////
public boolean addLeads(Leads lead){
    return leads.add(lead);
}


////Actual printing////
public void printAllLeads(){
    for (int i = 0; i < leads.size(); i++) {
        System.out.println(leads.get(i));
    }
    if(leads.size()==0){
        System.out.println("The list is empty.");
    }
}


///Actual deleting////
public boolean deleteLeads(String lead_id){
    Leads lead = getLeadsById(lead_id);
    if(lead == null){
        return false;
    }else{
        return leads.remove(lead);
    }
}

////////Updating Data//////
/////Might not work!!!  Duan check xem co dung dc khong. Xong thi xoa line note nay/////
public boolean updateLeads(String lead_Id){

    Leads leads = getLeadsById(lead_Id);

    if(leads == null){
        return false;
    }

    printLeadUpdateManual();

    Scanner s = new Scanner(System.in);
    boolean isDone = false;
    String newInfo = "";

    Interactions inter = null;
    while(!isDone){
        String target = s.nextLine();

        switch (target){
            case "name" : {
                newInfo = updateInfoPrompt(target);
                // leads.setName(newInfo);
                boolean isValid = InputValidator.getInstance().validateName(newInfo);
                if(isValid){
                    JOptionPane.showMessageDialog(null,"valid form!");
                    leads.setName(newInfo);
                }else{
                    JOptionPane.showMessageDialog(null,"Invalid");
                }
                break;
            }

            case "dob" : {
                System.out.print("enter new date of birth(MM/DD/YYYY) : ");
                String newDate = new Scanner(System.in).nextLine();
                boolean isValid = InputValidator.getInstance().validateBirthDay(newDate);
                if(isValid){
                    leads.setDoB(newDate);
                }else{
                    System.out.println("Invalid birthday form!");
                }
                break;
            }
            case "gender" : {

                newInfo = updateInfoPrompt(target);
                // leads.setGender(newInfo);
                boolean isValid = InputValidator.getInstance().validateGender(newInfo);
                if(isValid){
                    JOptionPane.showMessageDialog(null,"valid form!");
                    leads.setGender(newInfo);
                }else{
                    JOptionPane.showMessageDialog(null,"Invalid");
                }
                break;

            }

            case "phone" : {
                newInfo = updateInfoPrompt(target);
                // leads.setName(newInfo);
                boolean isValid = InputValidator.getInstance().validatePhoneNumber(newInfo);
                if(isValid){
                    JOptionPane.showMessageDialog(null,"valid form!");
                    leads.setPhone(newInfo);
                }else{
                    JOptionPane.showMessageDialog(null,"Invalid");
                }
                break;

            }

            case "email" : {

                newInfo = updateInfoPrompt(target);
                // leads.setName(newInfo);
                boolean isValid = InputValidator.getInstance().validateEmail(newInfo);
                if(isValid){
                    JOptionPane.showMessageDialog(null,"valid form!");
                    leads.setEmail(newInfo);
                }else{
                    JOptionPane.showMessageDialog(null,"Invalid");
                }
                break;
            }

            case "address" : {

                newInfo = updateInfoPrompt(target);
                // leads.setName(newInfo);
                boolean isValid = InputValidator.getInstance().validateAddress(newInfo);
                if(isValid){
                    JOptionPane.showMessageDialog(null,"valid form!");
                    leads.setAddress(newInfo);
                }else{
                    JOptionPane.showMessageDialog(null,"Invalid");
                }
                break;
            }

            case "0" : {
                isDone = true;
                break;
            }
            default : {
                System.out.println("Wrong Input !");
                printLeadUpdateManual();
                break;
            }
        }
    }
    return true;
}

private void printLeadUpdateManual(){
    System.out.println("Which information would you like to update?");
    System.out.println("OPTIONS : [name, dob (MM/DD/YYYY), gender, phone, email, address]");
    System.out.println("Enter '0' when update is complete.");
}

private String updateInfoPrompt(String updateTarget){
    System.out.println("Type new "+ updateTarget+" to update: ");
    return new Scanner(System.in).nextLine();
}

标签: javaswitch-statement

解决方案


在“saveCustomerToFile”函数的开头,我们可以看到它需要两个对象:一个“Leads”对象和一个“String”对象:

公共静态无效 saveCustomerToFile(线索线索,字符串线索文件路径){

然而在你的 switch 函数中你只发送一个,在'Leads'对象位置和一个逗号:

    case "20" : {
        saveCustomerToFile(leadfilepath,);

您需要指定添加前导对象并将对象按函数期望的正确顺序放置,以使程序正常工作。像这样的东西:

    case "20" : {
        saveCustomerToFile(lead_object, leadfilepath);
        break;
    }

推荐阅读