Dạ đúng rồi đó anh. Hiện tại em đang làm Linked List nên không biết sử dụng nó như thế nào. Em gặp vấn đề đọc và ghi dữ liệu nên em đang tìm ra vấn đề.
Đây là code của em:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;
/**
*
* @author magen
*/
public class Customer implements ICusPro, Serializable { //inheriting interfaces ICusPro,Serializable
public String ccode;
public String cus_name;
Customer next; // reference to the subsequent node in the list
Customer head; // head node of the list
Customer tail; // tail node of the list
Customer cus; //Create a variable
transient Scanner sa = new Scanner(System.in);
public String getCcode() {
return ccode;
}
public void setCcode(String ccode) {
this.ccode = ccode;
}
public String getCus_name() {
return cus_name;
}
public void setCus_name(String cus_name) {
this.cus_name = cus_name;
}
public Customer() {
head = null;
tail = null;
}
public Customer(String id, String name) {
this.ccode = id;
this.cus_name = name;
}
public boolean isEmpty() {
return (head == null);
}
//Create new Customer
@Override
public void addNew() {
cus = new Customer(); //create a new element
System.out.print("Enter code: ");
this.ccode = sa.nextLine();
cus.setCcode(ccode);
System.out.print("Enter name: ");
this.cus_name = sa.nextLine();
cus.setCus_name(cus_name);
cus.next = head;
head = cus;
}
@Override
public void displayData() {
if (isEmpty()) { //List no elements
System.out.println("No elements on the list!");
} else {
Customer c = head;
while (c != null) {
System.out.print("-->" + "[" + c.ccode + "," + c.cus_name + "]");
c = c.next;
}
System.out.println("");
}
}
@Override
public void deleteByCode() { //Delete Customer by code
System.out.println("Enter code: ");
String code = sa.nextLine();
head = deleteByCode(head, code);
System.out.println("Delete successful!");
}
public Customer deleteByCode(Customer node, Object code) {
if (node == null) { //if node is null
return node;
} else if (node.ccode.equals(code)) { //test code equal node.code
return node.next;
} else {
node.next = deleteByCode(node.next, code);
}
return node;
}
@Override
public boolean searchByCode() {
if (head == null) {
return false;
} else {
System.out.println("Enter code: ");
Object code = sa.nextLine();
cus = head;
while (cus != null) {
if (cus.getCcode().equals(code)) {
System.out.println("Found: " + "-->" + "[" + cus.ccode + "," + cus.cus_name + "]");
return true;
}
cus = cus.next;
}
}
return false;
}
@Override
public void writeToFile() {
Customer c = new Customer(ccode, cus_name);
try {
FileOutputStream fos = new FileOutputStream("A:\\HK5\\CSDL\\test.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(c);
oos.close();
fos.close();
System.out.println("Save file successful!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void readFromFile() {
try {
FileInputStream fis = new FileInputStream("A:\\HK5\\CSDL\\test.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
cus = (Customer) ois.readObject();
System.out.println("-->" + "[" + cus.ccode + "," + cus.cus_name + "]");
ois.close();
} catch (Exception ex) {
System.out.println(ex);
}
}