Java 控制台菜单问题

Java issues with console menu

我已经创建了一个正常运行的控制台菜单,我将选择一个选项来添加新客户,如下所示我已经创建了扫描仪来输入详细信息,我不知道如何保存用户输入一个名为 CustomerInformation 的文本文件。我可以使用此方法的功能来执行此操作,或者如果有某种方法可以存储信息并在我退出控制台菜单之前创建保存功能。

    public void addCustomer()
    {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Name: ");
    this.name = sc.nextLine();
    System.out.print("Enter Address: ");
    this.address = sc.nextLine();
    System.out.print("Enter Gender(M or F): ");
    this.gender = sc.nextLine();
    System.out.print("Enter Date of Birth: ");
    this.dob = sc.nextLine();
    }

整个客户class是:

import java.util.*;
import java.io.*;

public class Customer {

private String name;
private String address;
private String gender;
private String dob;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}

Customer (){

    setName("Name");
    setAddress("Address");
    setGender("Gender");
    setDob("dDob");
}

    Customer (String strName, String strAddress, String strGender, String strDob, String strReport){

    setName(strName);
    setAddress(strName);
    setGender(strGender);
    setDob(strDob);
}

//Add Customer Function-------------------------------------------------------------------
public void addCustomer()
    {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Name: ");
    this.name = sc.nextLine();
    System.out.print("Enter Address: ");
    this.address = sc.nextLine();
    System.out.print("Enter Gender(M or F): ");
    this.gender = sc.nextLine();
    System.out.print("Enter Date of Birth: ");
    this.dob = sc.nextLine();
    }
//End Add Customer-------------------------------------------------------------------------

//Search Function-------------------------------------------------------------------------

//End Search Function-------------------------------------------------------------------

//Delete Customer Function ------------------------------------------------------
//End Delete Customer Function---------------------------------------------------------

//Save Progress Function--------------------------------------------------------------  

//End Save Progress Function----------------------------------------------------------

    public static int nextInt()
    {
        Scanner keyb = new Scanner(System.in);
        int i = keyb.nextInt();
        return i;
    }

}

这是测试 class.read writeCustomerToFile method.this 不是 difficult.i 希望你能弄清楚:

   public final class Test {

   public static void main(String[] args) {
       Customer customer = new Customer();

       Scanner sc = new Scanner(System.in);
       System.out.print("Enter Name: ");
       customer.setName(sc.nextLine());
       System.out.print("Enter Address: ");
       customer.setAddress(sc.nextLine());

       System.out.print("Enter Gender(M or F): ");
       customer.setGender(sc.nextLine());
       System.out.print("Enter Date of Birth: ");
       customer.setDob(sc.nextLine());

       writeCustomerToFile(customer,"D:/yourfilename.txt");

   }

   public static void writeCustomerToFile(Customer customer, String fileName) {

       PrintWriter writer = null;
    try {
        writer = new PrintWriter(fileName, "UTF-8");
        writer.println("name:" + customer.getName());
           writer.println("address:" + customer.getAddress());
           writer.println("gender:" + customer.getGender());
           writer.println("birth date:" + customer.getDob());

           writer.close();

       } catch (FileNotFoundException ex) {
           Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
       } catch (UnsupportedEncodingException ex) {
           Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
       } finally {
           writer.close();
       }
    }

  }

你的客户class不变