package project;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class DirectoryBook{
private finalFantasyVII list[] = new finalFantasyVII[20];
private int top = 0;
public static void main(String[] args) {
DirectoryBook book = new DirectoryBook();
book.showMenu();
}
public void readFromFile(){
BufferedReader br;
FileReader fr;
JFileChooser file = new JFileChooser();
String name, sex, skill, weapon;
int index=0;
finalFantasyVII list[] = new finalFantasyVII[20];
try{
file.showOpenDialog(null);
fr = new FileReader(file.getSelectedFile().getPath());
br = new BufferedReader(fr);
name = br.readLine();
while(name!=null){
sex = br.readLine();
skill = br.readLine();
weapon = br.readLine();
finalFantasyVII f = new finalFantasyVII(name,sex,skill,weapon);
list[index]= f;
index++;
name = br.readLine();
}
top=index;
br.close();
this.list=list;
}catch(Exception e){
System.err.println(e.getMessage());
}
}
public void writeToFile(){
BufferedWriter bw;
FileWriter fw;
JFileChooser file = new JFileChooser();
try{
file.showSaveDialog(null);
fw = new FileWriter(file.getSelectedFile().getPath());
bw = new BufferedWriter(fw);
for (finalFantasyVII f : list) {
if (f !=null){
bw.write(f.getName());
bw.newLine();
bw.write(f.getSex());
bw.newLine();
bw.write(f.getSkill());
bw.newLine();
bw.write(f.getWeapon());
bw.newLine();
}
}
bw.close();
}catch (Exception e){
System.err.println(e.getMessage());
}
}
public void showMenu() {
String menu = "";
menu += " Record Information Systrem";
menu += "\n [A] - Add a record";
menu += "\n [E] - Edit a record";
menu += "\n [D] - Delete a record";
menu += "\n [V] - View all record";
menu += "\n [S] - Search a record";
menu += "\n [O] - Open records from file";
menu += "\n [W] - Write records to file";
menu += "\n [Q] - Quit";
menu += "\n Please Enter your choice";
String name;
String sex;
String skill;
String weapon;
boolean isQuit = false;
char choice = 'u';
do {
choice = JOptionPane.showInputDialog(menu).charAt(0);
switch (choice) {
case 'a':
case 'A':
name = JOptionPane.showInputDialog("Enter name:");
sex = JOptionPane.showInputDialog("Enter sex:");
skill = JOptionPane.showInputDialog("Enter skill:");
weapon = JOptionPane.showInputDialog("Enter weapon:");
if( addAfinalFantasyVII(name,sex,skill,weapon)){
JOptionPane.showMessageDialog(null,"Added a finalFantasyVII");
} else {
JOptionPane.showMessageDialog(null, "Cannot add another finalFantasyVII");
}
break;
case 'v':
case 'V':
viewAll();
break;
case 'o':
case 'O':
readFromFile();
break;
case 'w':
case 'W':
writeToFile();
break;
case 'q':
case 'Q':
isQuit = true;
break;
}
}while (isQuit == false);
}
public boolean addAfinalFantasyVII(String name, String sex, String skill, String weapon) {
if (top == list.length){
return false;
}
finalFantasyVII aNewfinalFantasyVII = new finalFantasyVII(name,sex,skill,weapon);
list[top] = aNewfinalFantasyVII;
top++;
return true;
}
public void viewAll(){
int recordNumber = 0;
for(finalFantasyVII f: list){
if(f !=null){
System.out.println("Record Number:" + (recordNumber + 1));
f.displayInformation();
recordNumber++;
}else {
System.out.println("No more record to be viewed.");
break;
}
}
}
}