Driving Academy Java Program and GUI
Java Code:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class StudentDataManager implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
studentRecord newStudent;
/*public StudentDataManager(int i, String n, String ins, String c, int m, int f, int d){
newStudent = new studentRecord(i, n, ins, c, m, f, d);
}*/
public StudentDataManager(){
}
public void add(int i, String n, String ins, String c, int m, int f, int d) throws IOException{
//System.out.println(newStudent.Instructor);
File file = new File("directory\\"+i+".dat");
if(!file.exists())
{
FileOutputStream fos = new FileOutputStream("directory\\"+i+".dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//newStudent.setIns(ins);
//oos.writeObject(newStudent = new studentRecord(i, n, ins, c, m, f, d));
newStudent = new studentRecord(i, n, ins, c, m, f, d);
oos.writeObject(newStudent);
oos.close();
System.out.println("File created successfully.");
}
}
public void update(int i, String n, String ins, String c, int m, int f, int d) throws IOException{
newStudent = new studentRecord(i, n, ins, c, m, f, d);
File file = new File("directory\\"+i+".dat");
if(file.exists())
{
FileOutputStream fos = new FileOutputStream("directory\\"+i+".dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//newStudent.setIns(ins);
oos.writeObject(newStudent = new studentRecord(i, n, ins, c, m, f, d));
newStudent = new studentRecord(i, n, ins, c, m, f, d);
oos.writeObject(newStudent);
oos.close();
System.out.println("File edited successfully.");
}
else{
System.out.println("file does not exists");
}
}
@SuppressWarnings("resource")
public studentRecord fetch(int i) throws IOException, ClassNotFoundException{
File file = null;
try{
file = new File("directory\\"+i+".dat");
if(file.exists()){
ObjectInputStream objectIn = null;
objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("directory\\"+i+".dat")));
newStudent = (studentRecord) objectIn.readObject();
objectIn.close();
//String text = newStudent.Instructor;
/*
System.out.println(newStudent.Instructor);
System.out.println(newStudent.comments);*/
//File file = new File("directory\\"+i+".dat");
return newStudent;
}
else{
System.out.println("file no found");
return null;
}
}
catch(Exception e){
System.out.println("file no found");
return null;
}
}
public void delete(int i){
try{
File file = new File("directory\\"+i+".dat");
boolean success = file.delete();
if (!success){
System.out.println("Deletion failed.");
}
else{
System.out.println("File deleted.");
}
}
catch(Exception e){
System.out.println("file not found");
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class StudentDataManager implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
studentRecord newStudent;
/*public StudentDataManager(int i, String n, String ins, String c, int m, int f, int d){
newStudent = new studentRecord(i, n, ins, c, m, f, d);
}*/
public StudentDataManager(){
}
public void add(int i, String n, String ins, String c, int m, int f, int d) throws IOException{
//System.out.println(newStudent.Instructor);
File file = new File("directory\\"+i+".dat");
if(!file.exists())
{
FileOutputStream fos = new FileOutputStream("directory\\"+i+".dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//newStudent.setIns(ins);
//oos.writeObject(newStudent = new studentRecord(i, n, ins, c, m, f, d));
newStudent = new studentRecord(i, n, ins, c, m, f, d);
oos.writeObject(newStudent);
oos.close();
System.out.println("File created successfully.");
}
}
public void update(int i, String n, String ins, String c, int m, int f, int d) throws IOException{
newStudent = new studentRecord(i, n, ins, c, m, f, d);
File file = new File("directory\\"+i+".dat");
if(file.exists())
{
FileOutputStream fos = new FileOutputStream("directory\\"+i+".dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//newStudent.setIns(ins);
oos.writeObject(newStudent = new studentRecord(i, n, ins, c, m, f, d));
newStudent = new studentRecord(i, n, ins, c, m, f, d);
oos.writeObject(newStudent);
oos.close();
System.out.println("File edited successfully.");
}
else{
System.out.println("file does not exists");
}
}
@SuppressWarnings("resource")
public studentRecord fetch(int i) throws IOException, ClassNotFoundException{
File file = null;
try{
file = new File("directory\\"+i+".dat");
if(file.exists()){
ObjectInputStream objectIn = null;
objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("directory\\"+i+".dat")));
newStudent = (studentRecord) objectIn.readObject();
objectIn.close();
//String text = newStudent.Instructor;
/*
System.out.println(newStudent.Instructor);
System.out.println(newStudent.comments);*/
//File file = new File("directory\\"+i+".dat");
return newStudent;
}
else{
System.out.println("file no found");
return null;
}
}
catch(Exception e){
System.out.println("file no found");
return null;
}
}
public void delete(int i){
try{
File file = new File("directory\\"+i+".dat");
boolean success = file.delete();
if (!success){
System.out.println("Deletion failed.");
}
else{
System.out.println("File deleted.");
}
}
catch(Exception e){
System.out.println("file not found");
}
}
}
-------------------------------------------------
import java.io.Serializable;
public class studentRecord implements Serializable {
int ID;
String Name;
String Instructor;
String comments;
int midTerm;
int finalTerm;
int driving;
int overall;
public studentRecord(){
}
public studentRecord(int i, String n, String ins, String c, int m, int f, int d){
ID=i;
Name=n;
Instructor=ins;
comments=c;
midTerm=m;
finalTerm=f;
driving=d;
overall=midTerm+finalTerm+driving;
}
public String getIns(){
return Instructor;
}
public void setIns(String ins){
Instructor=ins;
}
}
-------------------------------------------
public class studentRecord implements Serializable {
int ID;
String Name;
String Instructor;
String comments;
int midTerm;
int finalTerm;
int driving;
int overall;
public studentRecord(){
}
public studentRecord(int i, String n, String ins, String c, int m, int f, int d){
ID=i;
Name=n;
Instructor=ins;
comments=c;
midTerm=m;
finalTerm=f;
driving=d;
overall=midTerm+finalTerm+driving;
}
public String getIns(){
return Instructor;
}
public void setIns(String ins){
Instructor=ins;
}
}
-------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
public class UserInterface extends JFrame implements Serializable, ActionListener
{
private StudentDataManager dataManager = new StudentDataManager();
private JButton add, fetch, update, delete, reset;
private JLabel sidl, namel, instl;
private JTextField sidtf, nametf, insttf;
private JLabel commentl;
private JTextArea commentta;
private JScrollPane commentsp;
private JLabel midterml, finall, drivingl, overalll;
private JTextField midtermtf, finaltf, drivingtf, overalltf;
private JLabel statusl;
private JTextField statustf;
public UserInterface() throws ClassNotFoundException
{
super("Rick's Driving Academy");
buildTop();
buildBottom();
buildRight();
buildLeft();
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void buildTop()
{
Dimension dim = new Dimension(60,25);
JPanel tp = new JPanel();
sidl = new JLabel("Student ID");
namel = new JLabel("St. Name");
instl = new JLabel("Instructor");
sidtf = new JTextField(8);
nametf = new JTextField(8);
insttf = new JTextField(8);
sidl.setPreferredSize(dim);
namel.setPreferredSize(dim);
instl.setPreferredSize(dim);
sidl.setHorizontalAlignment(SwingConstants.RIGHT);
namel.setHorizontalAlignment(SwingConstants.RIGHT);
instl.setHorizontalAlignment(SwingConstants.RIGHT);
tp.add(sidl);
tp.add(sidtf);
tp.add(namel);
tp.add(nametf);
tp.add(instl);
tp.add(insttf);
add(tp, BorderLayout.NORTH);
}
private void buildBottom()
{
Dimension dim = new Dimension(90,25);
JPanel bp1 = new JPanel();
add = new JButton("Add");
add.addActionListener(this);
fetch = new JButton("Fetch");
fetch.addActionListener(this);
update = new JButton("Update");
update.addActionListener(this);
delete = new JButton("Delete");
delete.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);
add.setPreferredSize(dim);
fetch.setPreferredSize(dim);
update.setPreferredSize(dim);
delete.setPreferredSize(dim);
reset.setPreferredSize(dim);
bp1.add(add);
bp1.add(fetch);
bp1.add(update);
bp1.add(delete);
bp1.add(reset);
JPanel bp2 = new JPanel();
statusl = new JLabel("Command Status:");
statustf = new JTextField(32);
statustf.setEditable(false);
bp2.add(statusl);
bp2.add(statustf);
JPanel bp3 = new JPanel(new GridLayout(2,1));
bp3.add(bp1);
bp3.add(bp2);
add(bp3, BorderLayout.SOUTH);
}
private void buildRight()
{
JPanel rp = new JPanel();
rp.setLayout(new BorderLayout());
rp.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
commentl = new JLabel("Instructor Comments");
commentl.setHorizontalAlignment(SwingConstants.CENTER);
rp.add(commentl, BorderLayout.NORTH);
commentta = new JTextArea(6,30);
commentsp = new JScrollPane(commentta);
rp.add(commentsp, BorderLayout.CENTER);
add(rp, BorderLayout.CENTER);
}
private void buildLeft()
{
JPanel lp = new JPanel();
lp.setLayout(new GridLayout(4,2,5,5));
lp.setBorder(BorderFactory.createEmptyBorder(0,10,0,0));
midterml = new JLabel("Midterm");
finall = new JLabel("Final");
drivingl = new JLabel("Driving");
overalll = new JLabel("Overall");
midtermtf = new JTextField("0",3);
finaltf = new JTextField("0",3);
drivingtf = new JTextField("0",3);
overalltf = new JTextField("0",3);
overalltf.setEditable(false);
lp.add(midterml);
lp.add(midtermtf);
lp.add(finall);
lp.add(finaltf);
lp.add(drivingl);
lp.add(drivingtf);
lp.add(overalll);
lp.add(overalltf);
add(lp, BorderLayout.WEST);
}
public static void main(String[] args)
{
try {
UserInterface myApp = new UserInterface();
myApp.setVisible(true);
//myApp.actionPerformed(null);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// StudentDataManager dataManager = new StudentDataManager();
/*
try {
//dataManager.add(25, "a", "b", "c", 1, 2, 3);
//dataManager.add(26, "a", "b", "c", 1, 2, 3);
//dataManager.fetch(25);
//dataManager.delete(25);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//System.out.println("button clicked");
if(e.getSource()==add){
System.out.println("button clicked of add");
int id=0;
int mid=0;
int fnl=0;
int driving=0;
try{
id=Integer.parseInt(sidtf.getText());
mid=Integer.parseInt(midtermtf.getText());
fnl=Integer.parseInt(finaltf.getText());
driving=Integer.parseInt(drivingtf.getText());
try {
dataManager.add(id, nametf.getText(), insttf.getText(), commentta.getText(), mid, fnl, driving);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
dataManager.fetch(id);
} catch (ClassNotFoundException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
catch(Exception d){
System.out.println("please input integer in ID, midTem, FinalTerm and driving");
}
}
if(e.getSource()==delete){
System.out.println("button clicked of delete");
int id=Integer.parseInt(sidtf.getText());
dataManager.delete(id);
}
if(e.getSource()==fetch){
System.out.println("button clicked of fetch");
boolean test=false;
int id=0;
try{
id=Integer.parseInt(sidtf.getText());
dataManager.fetch(id);
test=true;
if(test){
String mid = "" + dataManager.fetch(id).midTerm;
String fnl = "" + dataManager.fetch(id).finalTerm;
String driving = "" + dataManager.fetch(id).driving;
String total = "" + dataManager.fetch(id).overall;
nametf.setText(dataManager.fetch(id).Name);
commentta.setText(dataManager.fetch(id).comments);
insttf.setText(dataManager.fetch(id).Instructor);
midtermtf.setText(mid);
finaltf.setText(fnl);
drivingtf.setText(driving);
overalltf.setText(total);
}
else{
System.out.println("pleae input corrext file name");
}
}
catch(Exception b){
System.out.println("please intput correct file name");
}/*
try {
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
}
if(e.getSource()==reset){
System.out.println("button clicked of reset");
sidtf.setText("");
nametf.setText("");
commentta.setText("");
insttf.setText("");
midtermtf.setText("");
finaltf.setText("");
drivingtf.setText("");
overalltf.setText("");
}
if(e.getSource()==update){
System.out.println("button clicked of update");
int id=Integer.parseInt(sidtf.getText());
int mid=Integer.parseInt(midtermtf.getText());
int fnl=Integer.parseInt(finaltf.getText());
int driving=Integer.parseInt(drivingtf.getText());
try {
dataManager.update(id, nametf.getText(), insttf.getText(), commentta.getText(), mid, fnl, driving);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(e.getSource()==delete){
System.out.println("button clicked of delete");
try{
int id=Integer.parseInt(sidtf.getText());
dataManager.delete(id);
}
catch(Exception e2){
System.out.println("please input correct file name");
}
}
}
}
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
public class UserInterface extends JFrame implements Serializable, ActionListener
{
private StudentDataManager dataManager = new StudentDataManager();
private JButton add, fetch, update, delete, reset;
private JLabel sidl, namel, instl;
private JTextField sidtf, nametf, insttf;
private JLabel commentl;
private JTextArea commentta;
private JScrollPane commentsp;
private JLabel midterml, finall, drivingl, overalll;
private JTextField midtermtf, finaltf, drivingtf, overalltf;
private JLabel statusl;
private JTextField statustf;
public UserInterface() throws ClassNotFoundException
{
super("Rick's Driving Academy");
buildTop();
buildBottom();
buildRight();
buildLeft();
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void buildTop()
{
Dimension dim = new Dimension(60,25);
JPanel tp = new JPanel();
sidl = new JLabel("Student ID");
namel = new JLabel("St. Name");
instl = new JLabel("Instructor");
sidtf = new JTextField(8);
nametf = new JTextField(8);
insttf = new JTextField(8);
sidl.setPreferredSize(dim);
namel.setPreferredSize(dim);
instl.setPreferredSize(dim);
sidl.setHorizontalAlignment(SwingConstants.RIGHT);
namel.setHorizontalAlignment(SwingConstants.RIGHT);
instl.setHorizontalAlignment(SwingConstants.RIGHT);
tp.add(sidl);
tp.add(sidtf);
tp.add(namel);
tp.add(nametf);
tp.add(instl);
tp.add(insttf);
add(tp, BorderLayout.NORTH);
}
private void buildBottom()
{
Dimension dim = new Dimension(90,25);
JPanel bp1 = new JPanel();
add = new JButton("Add");
add.addActionListener(this);
fetch = new JButton("Fetch");
fetch.addActionListener(this);
update = new JButton("Update");
update.addActionListener(this);
delete = new JButton("Delete");
delete.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);
add.setPreferredSize(dim);
fetch.setPreferredSize(dim);
update.setPreferredSize(dim);
delete.setPreferredSize(dim);
reset.setPreferredSize(dim);
bp1.add(add);
bp1.add(fetch);
bp1.add(update);
bp1.add(delete);
bp1.add(reset);
JPanel bp2 = new JPanel();
statusl = new JLabel("Command Status:");
statustf = new JTextField(32);
statustf.setEditable(false);
bp2.add(statusl);
bp2.add(statustf);
JPanel bp3 = new JPanel(new GridLayout(2,1));
bp3.add(bp1);
bp3.add(bp2);
add(bp3, BorderLayout.SOUTH);
}
private void buildRight()
{
JPanel rp = new JPanel();
rp.setLayout(new BorderLayout());
rp.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
commentl = new JLabel("Instructor Comments");
commentl.setHorizontalAlignment(SwingConstants.CENTER);
rp.add(commentl, BorderLayout.NORTH);
commentta = new JTextArea(6,30);
commentsp = new JScrollPane(commentta);
rp.add(commentsp, BorderLayout.CENTER);
add(rp, BorderLayout.CENTER);
}
private void buildLeft()
{
JPanel lp = new JPanel();
lp.setLayout(new GridLayout(4,2,5,5));
lp.setBorder(BorderFactory.createEmptyBorder(0,10,0,0));
midterml = new JLabel("Midterm");
finall = new JLabel("Final");
drivingl = new JLabel("Driving");
overalll = new JLabel("Overall");
midtermtf = new JTextField("0",3);
finaltf = new JTextField("0",3);
drivingtf = new JTextField("0",3);
overalltf = new JTextField("0",3);
overalltf.setEditable(false);
lp.add(midterml);
lp.add(midtermtf);
lp.add(finall);
lp.add(finaltf);
lp.add(drivingl);
lp.add(drivingtf);
lp.add(overalll);
lp.add(overalltf);
add(lp, BorderLayout.WEST);
}
public static void main(String[] args)
{
try {
UserInterface myApp = new UserInterface();
myApp.setVisible(true);
//myApp.actionPerformed(null);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// StudentDataManager dataManager = new StudentDataManager();
/*
try {
//dataManager.add(25, "a", "b", "c", 1, 2, 3);
//dataManager.add(26, "a", "b", "c", 1, 2, 3);
//dataManager.fetch(25);
//dataManager.delete(25);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//System.out.println("button clicked");
if(e.getSource()==add){
System.out.println("button clicked of add");
int id=0;
int mid=0;
int fnl=0;
int driving=0;
try{
id=Integer.parseInt(sidtf.getText());
mid=Integer.parseInt(midtermtf.getText());
fnl=Integer.parseInt(finaltf.getText());
driving=Integer.parseInt(drivingtf.getText());
try {
dataManager.add(id, nametf.getText(), insttf.getText(), commentta.getText(), mid, fnl, driving);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
dataManager.fetch(id);
} catch (ClassNotFoundException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
catch(Exception d){
System.out.println("please input integer in ID, midTem, FinalTerm and driving");
}
}
if(e.getSource()==delete){
System.out.println("button clicked of delete");
int id=Integer.parseInt(sidtf.getText());
dataManager.delete(id);
}
if(e.getSource()==fetch){
System.out.println("button clicked of fetch");
boolean test=false;
int id=0;
try{
id=Integer.parseInt(sidtf.getText());
dataManager.fetch(id);
test=true;
if(test){
String mid = "" + dataManager.fetch(id).midTerm;
String fnl = "" + dataManager.fetch(id).finalTerm;
String driving = "" + dataManager.fetch(id).driving;
String total = "" + dataManager.fetch(id).overall;
nametf.setText(dataManager.fetch(id).Name);
commentta.setText(dataManager.fetch(id).comments);
insttf.setText(dataManager.fetch(id).Instructor);
midtermtf.setText(mid);
finaltf.setText(fnl);
drivingtf.setText(driving);
overalltf.setText(total);
}
else{
System.out.println("pleae input corrext file name");
}
}
catch(Exception b){
System.out.println("please intput correct file name");
}/*
try {
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
}
if(e.getSource()==reset){
System.out.println("button clicked of reset");
sidtf.setText("");
nametf.setText("");
commentta.setText("");
insttf.setText("");
midtermtf.setText("");
finaltf.setText("");
drivingtf.setText("");
overalltf.setText("");
}
if(e.getSource()==update){
System.out.println("button clicked of update");
int id=Integer.parseInt(sidtf.getText());
int mid=Integer.parseInt(midtermtf.getText());
int fnl=Integer.parseInt(finaltf.getText());
int driving=Integer.parseInt(drivingtf.getText());
try {
dataManager.update(id, nametf.getText(), insttf.getText(), commentta.getText(), mid, fnl, driving);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(e.getSource()==delete){
System.out.println("button clicked of delete");
try{
int id=Integer.parseInt(sidtf.getText());
dataManager.delete(id);
}
catch(Exception e2){
System.out.println("please input correct file name");
}
}
}
}
-----------------------------------------
No comments:
Post a Comment