0

I have created a functioning console menu and I will choose an option to add a new customer, as you can see below I have created the scanner to enter the details, what I dont know is how I would save the user input out to a text file called CustomerInformation. I can either do this with a function on this method or if there is some way to store the information and create a save function before I quit out of the console menu.

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

the whole customer class is:

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

}
Mr E
  • 33
  • 9
  • 1
    Something in the heirarchy seems wrong. Do you have an `addCustomer()` in the `Customer` class? Why? Why not use a constructor? – Reut Sharabani Apr 18 '15 at 19:51
  • Can you maybe show us the full implementation? So we know where you are going with this. Thanks – TejjD Apr 18 '15 at 19:54
  • To write a file see [here](http://stackoverflow.com/a/2885224/2631402). – Sesame Apr 18 '15 at 19:55
  • I have added the whole class, I have another class created called Main Menu but it only has the menu using switch and case to create it – Mr E Apr 18 '15 at 20:01
  • I assume I need a readFromFile and WriteToFile but I'm not entirely sure. I have code ready to use and it works I'm just not sure of where to put the code and how to run it through the console menu using the user input. I will apologise as I am fairly new to all this – Mr E Apr 18 '15 at 20:07

1 Answers1

0

This is Test class.read writeCustomerToFile method.this is not difficult.i hope you will figure out:

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

  }

Your customer class is the same not changed

Sarkhan
  • 1,281
  • 1
  • 11
  • 33
  • thanks for that and I do apologise as this is fairly new to me, the only thing I am experiencing trouble with in your code is the Logger, This is the first I have seen these and have no idea of how to fix them or how to use them, I have researched them now and understand exactly what it is they do but Im getting a cannot find symbol at Logger and Level. – Mr E Apr 19 '15 at 00:30