我已经创建了一个正常运行的控制台菜单,我将选择添加新客户的选项,如下所示我创建了扫描仪以输入详细信息,我不知道如何保存用户输入到名为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();
}
整个客户类是:
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;
}
}
答案 0 :(得分:0)
这是Test class.read writeCustomerToFile方法。这不难。我希望你能弄明白:
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();
}
}
}
您的客户类别未更改