Data Access Object (DAO)
Data Transfer Object (DTO).
-------------------------------
DAO is a class that usually has operations like save, update, delete. Whereas the DTO is just an object that holds data.
-------------------------------
The advantage of the DAO layer is that if you need to change the underlying persistence mechanism you only have to change the DAO layer, and not all the places in the domain logic where the DAO layer is used from.
The advantage of DTO layer is that it adds a good deal of flexibility to the service layer and subsequently to the design of the entire application
Wednesday, August 28, 2013
Friday, August 23, 2013
List Demo in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ListDemonstration extends Frame implements ActionListener{
private Label l = new Label("Enter Data");
private Button b1 = new Button("Add");
private TextField txt = new TextField(10);
private Button b2 = new Button("Close");
private List l1 = new List();
private List l2 = new List();
private Panel p = new Panel();
private Panel p1 = new Panel();
public ListDemonstration(String title){
super(title);
setSize(400, 400);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fr = getSize();
int xpos = screen.width/2 - fr.width/2;
int ypos = screen.height/2 - fr.height/2;
setLocation(xpos,ypos);
setResizable(false);
setVisible(true);
init();
start();
}
public void start(){
b1.addActionListener(this);
b2.addActionListener(this);
txt.setFocusable(true);
l1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
list1Click(e);
}
});
l2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
list2Click(e);
}
});
WindowListener window =new WindowAdapter() {
public void windowClosing(WindowEvent evt){
System.exit(0);
}
};
addWindowListener(window);
}
public void init(){
this.setLayout(new BorderLayout());
p.setLayout(new GridLayout(1,4));
p.add(l);
p.add(txt);
p.add(b1);
p.add(b2);
this.add(BorderLayout.NORTH,p);
p1.setLayout(new GridLayout(1,2));
p1.add(l1);
p1.add(l2);
this.add(BorderLayout.CENTER,p1);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()== b1){
if(txt.getText().equals("")||txt.getText().equals(null)){
JOptionPane.showMessageDialog(this, "Your have to input "
+ "data into textfield!!!!",
"Confirm", JOptionPane.CANCEL_OPTION);
txt.setFocusable(true);
}else{
l1.add(txt.getText());
txt.setText("");
txt.setFocusable(true);
}
}if(e.getSource()== b2){
System.exit(0);
}
}
public void list1Click(MouseEvent e){
if(l1.getItemCount()==0)
return;
if(e.getClickCount()==2){
l2.add(l1.getSelectedItem().toString());
l1.remove(l1.getSelectedItem());
}
}
public void list2Click(MouseEvent e){
if(e.getClickCount()==2){
l1.add(l2.getSelectedItem().toString());
l2.remove(l2.getSelectedItem());
}
}
public static void main(String[] args) {
new ListDemonstration("List Demo");
}
}
import java.awt.event.*;
import javax.swing.*;
public class ListDemonstration extends Frame implements ActionListener{
private Label l = new Label("Enter Data");
private Button b1 = new Button("Add");
private TextField txt = new TextField(10);
private Button b2 = new Button("Close");
private List l1 = new List();
private List l2 = new List();
private Panel p = new Panel();
private Panel p1 = new Panel();
public ListDemonstration(String title){
super(title);
setSize(400, 400);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension fr = getSize();
int xpos = screen.width/2 - fr.width/2;
int ypos = screen.height/2 - fr.height/2;
setLocation(xpos,ypos);
setResizable(false);
setVisible(true);
init();
start();
}
public void start(){
b1.addActionListener(this);
b2.addActionListener(this);
txt.setFocusable(true);
l1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
list1Click(e);
}
});
l2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
list2Click(e);
}
});
WindowListener window =new WindowAdapter() {
public void windowClosing(WindowEvent evt){
System.exit(0);
}
};
addWindowListener(window);
}
public void init(){
this.setLayout(new BorderLayout());
p.setLayout(new GridLayout(1,4));
p.add(l);
p.add(txt);
p.add(b1);
p.add(b2);
this.add(BorderLayout.NORTH,p);
p1.setLayout(new GridLayout(1,2));
p1.add(l1);
p1.add(l2);
this.add(BorderLayout.CENTER,p1);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()== b1){
if(txt.getText().equals("")||txt.getText().equals(null)){
JOptionPane.showMessageDialog(this, "Your have to input "
+ "data into textfield!!!!",
"Confirm", JOptionPane.CANCEL_OPTION);
txt.setFocusable(true);
}else{
l1.add(txt.getText());
txt.setText("");
txt.setFocusable(true);
}
}if(e.getSource()== b2){
System.exit(0);
}
}
public void list1Click(MouseEvent e){
if(l1.getItemCount()==0)
return;
if(e.getClickCount()==2){
l2.add(l1.getSelectedItem().toString());
l1.remove(l1.getSelectedItem());
}
}
public void list2Click(MouseEvent e){
if(e.getClickCount()==2){
l1.add(l2.getSelectedItem().toString());
l2.remove(l2.getSelectedItem());
}
}
public static void main(String[] args) {
new ListDemonstration("List Demo");
}
}
Thursday, August 22, 2013
File management using Serialize Object in java
1. Create Interface
package interfaces;
public interface InterBoard {
/*This method is used to list file in directory */
public void Listing();
/*This method is used to write data into file*/
public void Writing();
/*This method is used to read article */
public void Reading();
/*This method is used to find article*/
public void Searching();
/*This method is used to update*/
public void Editing();
/*This method is used to delete file*/
public void Deleting();
/*This is method is used quit application*/
public void Exiting();
}
package interfaces;
public interface InterBoard {
/*This method is used to list file in directory */
public void Listing();
/*This method is used to write data into file*/
public void Writing();
/*This method is used to read article */
public void Reading();
/*This method is used to find article*/
public void Searching();
/*This method is used to update*/
public void Editing();
/*This method is used to delete file*/
public void Deleting();
/*This is method is used quit application*/
public void Exiting();
}
2. Create Class:
package manageclass;
import java.io.*;
import java.util.*;
public class clsArticles implements Serializable{
private static final long serialVersionUID = 1L;
private String title;
private String author;
private Date date;
private String content;
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return title;
}
public void setAuthor(String author){
this.author = author;
}
public String getAuthor(){
return author;
}
public void setDate(Date date){
this.date = date;
}
public Date getDate(){
return date;
}
public void setContent(String content){
this.content = content;
}
public String getContent(){
return content;
}
}
package manageclass;
import java.io.Serializable;
import java.util.*;
public class clsUsers implements Serializable{
private static final long serialVersionUID = 1L;
private String userID;
private String passWord;
private Date dlogin;
private Date dlogout;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public Date getDlogin() {
return dlogin;
}
public void setDlogin(Date dlogin) {
this.dlogin = dlogin;
}
public Date getDlogout() {
return dlogout;
}
public void setDlogout(Date dlogout) {
this.dlogout = dlogout;
}
}
package manageclass;
import java.io.*;
import java.util.*;
import application.Login;
import interfaces.InterBoard;
public class clsBoard implements InterBoard{
private static File dir;
private static BufferedReader in;
private static Vector<Integer> vc;
private static clsArticles article;
private static clsSerialize writeObj;
private static clsDeserialize readObj;
private static clsMenu menu;
private static int current = 0;
private final int article_num=5;
private static boolean check = false;
static{
dir = new File("C:\\HRDSOUTSARETFMJAVASUN\\Board");
if(!dir.exists()) dir.mkdirs();
in = new BufferedReader(new InputStreamReader(System.in));
vc = new Vector<Integer>();
article = new clsArticles();
writeObj = new clsSerialize();
menu = new clsMenu();
readObj = new clsDeserialize();
}
public void NoFileMenu(){
menu.NoFile();
while(true){
try {
String con = String.valueOf(in.readLine());
if(con.equals("W") || con.equals("w") || con.equals("1")){
Writing();Listing();
}else if(con.equals("E") || con.equals("e") || con.equals("2")){
System.out.println();
System.out.println("You are logout!!!!");
Login.main(null);
}
} catch (IOException e) {
}
}
}
// Listing files in directory
public void Listing() {
if(dir.listFiles().length <= 0){
menu.Header();
System.out.println();
System.out.println("No Articles in the system!!!!!");
NoFileMenu();
}
if(dir.listFiles().length > 0){
try {
ShowList(0);
while(true){
menu.MenuMover();
String con = String.valueOf(in.readLine());
if(con.equals("F") || con.equals("f") || con.equals("1")){ // First
ShowList(4);
System.out.println();
System.out.println("It is standing at the first of Article!!!!!!!!!");
System.out.println();
}else if(con.equals("P") || con.equals("p") || con.equals("2")){ // Previous
if(current <vc.size() || current > 1){
current -=5;
ShowList(3);
}else{
ShowList(4);
}
}else if(con.equals("N") || con.equals("n") || con.equals("3")){ // Next
if(current < vc.size()-1 || current > 0){
current +=5;
ShowList(2);
}else{
ShowList(1);
}
}else if(con.equals("L") || con.equals("l") || con.equals("4")){ // End
ShowList(1);
System.out.println();
System.out.println("It is standing at the last of Article!!!!!!!!!");
System.out.println();
}else if(con.equals("R") || con.equals("r") || con.equals("5")){ // Read
Reading(); ShowList(0);
}else if(con.equals("W") || con.equals("w") || con.equals("6")){ // Write
Writing();ShowList(0);
}else if(con.equals("S") || con.equals("s") || con.equals("7")){ // Search
Searching();ShowList(0);
}else if(con.equals("U") || con.equals("u") || con.equals("8")){ // Update
Editing();ShowList(0);
}else if(con.equals("D") || con.equals("d") || con.equals("9")){ // Delete
Deleting();ShowList(0);
}else if(con.equals("L") || con.equals("l") || con.equals("10")){ // Log Out
System.out.println("You are logout from application!!!");
Login.main(null);
}
}
}catch(FileNotFoundException e){
} catch (IOException e) {
e.printStackTrace();
}
System.out.println();
}
}
// Writing
public void Writing(){
try {
System.out.println("Please Input Info!!!");
System.out.print("Title : ");
article.setTitle(in.readLine());
if(ManageUser.login !=null){
article.setAuthor(ManageUser.login);
}else{
System.out.print("Writer : ");
article.setAuthor(in.readLine());
}
Date date = new Date();
article.setDate(date);
System.out.print("Content(use '.' to finish writing!) : ");
StringBuffer sb = new StringBuffer();
while(true){
String imsi = in.readLine();
if(imsi !=null && imsi.trim().length()==1
&& imsi.trim().charAt(0)=='.') break;
if(imsi==null) imsi="";
sb.append(imsi+"\n");
}
article.setContent(sb.toString());
File file = null;
if(getArtID()==0)
file = new File(dir+"\\"+(getArtID()+1)+ ".ser");
else
file = new File(dir+"\\"+getArtID()+ ".ser");
writeObj.Writer(file,article);
System.out.println("Your data is saved to be successfully!!!!!!");
} catch (IOException e) {
e.printStackTrace();
}
}
// Read
public void Reading() {
try{
while(true){
menu.SubMenu();
String con =String.valueOf(in.readLine());
if(con.equals("A") || con.equals("a") || con.equals("1")){ // Article
System.out.print("Input Article : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart);
if(check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(check == true){
System.out.print("Read Article No: ");
String farticle = in.readLine();
readObj.Reader(new File(dir+"\\"+farticle+".ser"));
System.out.println("You was red "+farticle+".ser file!!!!!!");
System.out.println();
check = false;
}
}else if(con.equals("T") || con.equals("t") || con.equals("2")){ // Title
System.out.print("Input Title : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart, 1);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(clsDeserialize.check == true){
System.out.print("Read Article No: ");
String farticle = in.readLine();
readObj.Reader(new File(dir+"\\"+farticle+".ser"));
System.out.println("You was red "+farticle+".ser file!!!!!!");
System.out.println();
clsDeserialize.check =false;
}
}else if(con.equals("W") || con.equals("w") || con.equals("3")){ // Writer
System.out.print("Input Writer : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart, 2);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(clsDeserialize.check == true){
System.out.print("Read Article No: ");
String farticle = in.readLine();
readObj.Reader(new File(dir+"\\"+farticle+".ser"));
System.out.println("You was red "+farticle+".ser file!!!!!!");
System.out.println();
clsDeserialize.check = false;
}
}else if(con.equals("E") || con.equals("e") || con.equals("4")){ // Exit
return;
}
}
}catch(IOException e){}
}
// Search
public void Searching() {
try{
while(true){
menu.SubMenu();
String con =String.valueOf(in.readLine());
if(con.equals("A") || con.equals("a") || con.equals("1")){ // Article
System.out.print("Input Article : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart);
if(check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
}else if(con.equals("T") || con.equals("t") || con.equals("2")){ // Title
System.out.print("Input Title : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart, 1);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
}else if(con.equals("W") || con.equals("w") || con.equals("3")){ // Writer
System.out.print("Input Writer : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart, 2);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
}else if(con.equals("E") || con.equals("e") || con.equals("4")){ // Exit
return;
}
}
}catch(IOException e){}
}
// Update Info
public void Editing(){
try{
while(true){
menu.SubMenu();
String con =String.valueOf(in.readLine());
if(con.equals("A") || con.equals("a") || con.equals("1")){ // Article
System.out.print("Input Article : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart);
if(check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(check == true){
Updated(fart);
check = false;
}
}else if(con.equals("T") || con.equals("t") || con.equals("2")){ // Title
System.out.print("Input Title : ");
String fart =in.readLine();
menu.HeaderDetail();
SubSearch(fart,1,0);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(clsDeserialize.check == true){
System.out.print("Input Article: ");
String far = in.readLine();
File ff = new File(dir+"\\"+far+".ser");
if(ff.exists()){
Updated(far);
}else{
System.out.println("You input something!!!");
}
clsDeserialize.check =false;
}
}else if(con.equals("W") || con.equals("w") || con.equals("3")){ // Writer(W)
System.out.print("Input Writer : ");
String fart =in.readLine();
menu.HeaderDetail();
SubSearch(fart,2,0);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(clsDeserialize.check == true){
System.out.print("Input Article: ");
String far = in.readLine();
File ff = new File(dir+"\\"+far+".ser");
if(ff.exists()){
Updated(far);
}else{
System.out.println("You input something!!!");
}
clsDeserialize.check = false;
}
}else if(con.equals("E") || con.equals("e") || con.equals("4")){ // Exit
return;
}
}
}catch(IOException e){}
}
// Delete file
public void Deleting() {
try{
while(true){
menu.SubMenu();
String con = String.valueOf(in.readLine());
if(con.equals("A") || con.equals("a") || con.equals("1")){ // Article
System.out.print("Input Article : ");
String fart =in.readLine();
menu.Header();
SubSearch(fart);
if(check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(check == true){
check = false;
menu.SubMenuDelete();
while(true){
String del = String.valueOf(in.readLine());
if(del.equals("D") || del.equals("d") || del.equals("1")){ // delete
File ff= new File(dir+"\\"+fart+".ser");
if(ff.exists())
ff.delete();
System.out.println("Your Article was deleted!!!");
System.out.println();
break;
}else if(del.equals("C") || del.equals("c") || del.equals("2")){ // Cancel
System.out.println("Your Article wasn't deleted!!!");
System.out.println();
break;
}
}
}
}
else if(con.equals("T") || con.equals("t") || con.equals("2")){ // Title
System.out.print("Input Title : ");
String tt = in.readLine();
menu.Header();
SubSearch(tt, 1);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(clsDeserialize.check == true){
clsDeserialize.check = false;
menu.SubMenuDelete();
while(true){
String del = String.valueOf(in.readLine());
if(del.equals("D") || del.equals("d") || del.equals("1")){ // delete
System.out.print("Input Article No: ");
String ac = in.readLine();
File ff= new File(dir+"\\"+ac+".ser");
if(ff.exists())
ff.delete();
System.out.println("Your Article was deleted!!!");
System.out.println();
break;
}else if(del.equals("C") || del.equals("c") || del.equals("2")){ // cancel
System.out.println("Your Article wasn't deleted!!!");
System.out.println();
break;
}
}
}
}
else if(con.equals("W") || con.equals("w") || con.equals("3")){ // Writer(W)
System.out.print("Input Writer : ");
String tt = in.readLine();
menu.Header();
SubSearch(tt, 2);
if(clsDeserialize.check == false){
System.out.println();
System.out.println("No Article in system!!!");
System.out.println();
}
if(clsDeserialize.check == true){
clsDeserialize.check = false;
menu.SubMenuDelete();
while(true){
String del = String.valueOf(in.readLine());
if(del.equals("D") || del.equals("d") || del.equals("1")){ // Delete
System.out.print("Input Article No: ");
String ac = in.readLine();
File ff= new File(dir+"\\"+ac+".ser");
if(ff.exists())
ff.delete();
System.out.println("Your Article was deleted!!!");
System.out.println();
break;
}else if(del.equals("C") || del.equals("c") || del.equals("2")){ // Cancel
System.out.println("Your Article wasn't deleted!!!");
System.out.println();
break;
}
}
}
}
else if(con.equals("E") || con.equals("e") || con.equals("4")){ // Exit
return;
}
}
}catch(IOException e){}
}
public void Exiting() {
System.out.println();
System.out.println(" Thank you !!!!!!!!");
System.exit(0);
}
public void SubSearch(String arg) throws IOException{
for (File name : dir.listFiles()) {
if(name.getName().toString().equals(arg+".ser")){
readObj.Reader(new File(dir+"\\"+name.getName()), 0);
check = true;
}
}
}
public void SubSearch(String arg,int node) throws IOException{
for (File name : dir.listFiles()) {
vc.add(Integer.parseInt(name.getName().substring(0, name.getName().length()-4)));
}
for(int i=vc.size()-1;i>=0;i--){
readObj.Reader(new File(dir+"\\"+vc.get(i).toString()+".ser"),article, arg, node);
}
vc.clear();
}
/*
* Search Detail (Editing)
* */
public void SubSearch(String arg,int option,int node) throws IOException{
for (File name : dir.listFiles()) {
vc.add(Integer.parseInt(name.getName().substring(0, name.getName().length()-4)));
}
Collections.sort(vc);
for(int i=vc.size()-1;i>=0;i--){
readObj.Reader(new File(dir+"\\"+vc.get(i).toString()+".ser"), article, option, arg);
}
vc.clear();
}
public void SubSearch(int content,String fart)throws IOException{
for (File name : dir.listFiles()) {
if(name.getName().equals(fart+".ser"))
if(content == 0 ){
readObj.Reader(new File(dir+"\\"+fart+".ser"));
}
}
}
public void ShowList(int move)throws IOException{
// Add File to Vector
for (File name : dir.listFiles()) {
vc.add(Integer.parseInt(name.getName().substring(0, name.getName().length()-4)));
}
Collections.sort(vc); // Sort Data in collection
int c=0;
menu.Header();
if(move == 0){ // Start list
for(int i=vc.size()-1;i>=0;i--){
try {
if(c == article_num) break;
readObj.Reader(new File(dir+"\\"+vc.get(i)+".ser"),0);
c++;
}catch(ArrayIndexOutOfBoundsException e){
} catch (IOException e) {
}
}
System.out.println(" Total: "+c+"/"+(dir.listFiles().length)+" rows");
c = 0;
current = dir.listFiles().length -1;
}
if(move == 1){ // First
try {
current = dir.listFiles().length -1;
readObj.Reader(new File(dir+"\\"+vc.get(current)+".ser"),0);
c ++;
}catch(ArrayIndexOutOfBoundsException e){
} catch (IOException e) {
}
System.out.println(" Total: "+c+"/"+(dir.listFiles().length)+" rows");
}
if(move == 2){ // Previous
for(int i=current;;i--){
try {
if(i<0) break;
if(c ==article_num) break;
readObj.Reader(new File(dir+"\\"+vc.get((i))+".ser"),0);
c ++;
}catch(ArrayIndexOutOfBoundsException e){
} catch (IOException e) {
}
}
System.out.println(" Total: "+c+"/"+(dir.listFiles().length)+" rows");
c =0;
}
if(move == 3){ // Next
for(int i=(current);;i--){
try {
if(i <=0 ){
readObj.Reader(new File(dir+"\\"+vc.get(0)+".ser"),0);
break;
}
if(c ==article_num) break;
readObj.Reader(new File(dir+"\\"+vc.get(i)+".ser"),0);
c ++;
}catch(ArrayIndexOutOfBoundsException e){
} catch (IOException e) {
}
}
System.out.println(" Total: "+c+"/"+(dir.listFiles().length)+" rows");
}
if(move == 4){ // Last
try {
readObj.Reader(new File(dir+"\\"+vc.get(0)+".ser"),0);
c++;
current = 1;
}catch(ArrayIndexOutOfBoundsException e){
} catch (IOException e) {
}
System.out.println(" Total: "+c+"/"+(dir.listFiles().length)+" rows");
}
// Clear Vector
vc.clear();
c =0;
}
/**
* Used to get Auto Article ID
*
*/
public int getArtID(){
int value = dir.listFiles().length + 1;
for (File name : dir.listFiles()) {
int fname=Integer.valueOf(name.getName().substring(0, name.getName().length()-4));
if(value != fname)
value = value + 0;
else
value = value + 1;
}
return value;
}
// Update
public void Updated(String fart){
try{
while(true){
menu.MenuUpdate();
String up =String.valueOf(in.readLine());
if(up.equals("T") || up.equals("t") || up.equals("1")){ // Title
System.out.print("Input Title: ");
String title = in.readLine();
article.setTitle(title);
article.setAuthor(ManageUser.login);
article.setDate(new Date());
article.setContent(readObj.returnValue(new File(dir+"\\"+fart+".ser"), 3));
writeObj.Writer(new File(dir+"\\"+fart+".ser"),article);
System.out.println();
readObj.Reader(new File(dir+"\\"+fart+".ser"));
System.out.println("You was updated to be successfull!!!");
System.out.println();
}else if(up.equals("C") || up.equals("c") || up.equals("2")){ // Content
System.out.print("Input Content(use '.' to finish writing!) : ");
StringBuffer contents = new StringBuffer();
while(true){
String imsi = in.readLine();
if(imsi !=null && imsi.trim().length()==1
&& imsi.trim().charAt(0)=='.') break;
if(imsi==null) imsi="";
contents.append(imsi+"\n");
}
article.setTitle(readObj.returnValue(new File(dir+"\\"+fart+".ser"), 1));
article.setAuthor(ManageUser.login);
article.setDate(new Date());
article.setContent(contents.toString());
writeObj.Writer(new File(dir+"\\"+fart+".ser"),article);
System.out.println();
readObj.Reader(new File(dir+"\\"+fart+".ser"));
System.out.println("You was updated to be successfull!!!");
System.out.println();
}else if(up.equals("E") || up.equals("e") || up.equals("3")){ // Exit
return;
}
}
}catch(FileNotFoundException e){}
catch (IOException e) {
}
}
}
package manageclass;
import java.io.*;
import java.text.*;
import java.util.Date;
public class clsDeserialize {
private FileInputStream fis;
private BufferedInputStream bis;
private ObjectInputStream ois;
private clsArticles article;
private clsUsers user;
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
public static boolean check = false;
public void Reader(File f) throws IOException{
article = new clsArticles();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
article = (clsArticles)obj;
System.out.println();
System.out.println("========================================================");
System.out.println(" Title : "+article.getTitle());
System.out.println("--------------------------------------------------------");
System.out.println(" Writer : " + article.getAuthor());
System.out.println("--------------------------------------------------------");
try{
System.out.println(" Date : " + format.format((article.getDate())));
}catch(NullPointerException e){}
System.out.println("--------------------------------------------------------");
System.out.println(" Content: " + article.getContent());
System.out.println("========================================================");
System.out.println();
ois.close();
bis.close();
fis.close();
}
public void Reader(File f,int a) throws IOException{
article = new clsArticles();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
article = (clsArticles)obj;
System.out.print(" "+f.getName().substring(0, f.getName().length()-4));
if(article.getTitle().toString().length()>7){
System.out.print("\t"+article.getTitle().substring(0, 4)+"..");
}else{
System.out.print("\t"+article.getTitle());
}
if(article.getAuthor().toString().length()>7){
System.out.print("\t\t" + article.getAuthor().substring(0, 4)+"..");
}else{
System.out.print("\t\t" + article.getAuthor());
}
try{
System.out.print("\t\t" + format.format(article.getDate()));
}catch(NullPointerException e){}
System.out.println("\n--------------------------------------------------------");
if(article.getTitle() != null) check = true;
ois.close();
bis.close();
fis.close();
}
public void Reader(File f,clsArticles articles,String search,int node) throws IOException{
articles = new clsArticles();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
articles = (clsArticles)obj;
if(articles.getTitle().equalsIgnoreCase(search) && node == 1){
check = true;
System.out.print(" "+f.getName().substring(0, f.getName().length()-4));
if(articles.getTitle().length()>7){
System.out.print("\t"+articles.getTitle().substring(0, 4));
}else{
System.out.print("\t"+articles.getTitle());
}
if(articles.getAuthor().length()>7){
System.out.print("\t\t" + articles.getAuthor().substring(0, 4)+"..");
}else{
System.out.print("\t\t" + articles.getAuthor());
}
try{
System.out.print("\t\t" + format.format(articles.getDate()));
}catch(NullPointerException e){}
System.out.println("\n--------------------------------------------------------");
}else if(articles.getAuthor().equalsIgnoreCase(search) && node == 2){
check = true;
System.out.print(" "+f.getName().substring(0, f.getName().length()-4));
if(articles.getTitle().length()>7){
System.out.print("\t"+articles.getTitle().substring(0, 4));
}else{
System.out.print("\t"+articles.getTitle());
}
if(articles.getAuthor().length()>7){
System.out.print("\t\t" + articles.getAuthor().substring(0, 4)+"..");
}else{
System.out.print("\t\t" + articles.getAuthor());
}
try{
System.out.print("\t\t" + format.format(articles.getDate()));
}catch(NullPointerException e){}
System.out.println("\n--------------------------------------------------------");
}else {
check =false;
}
ois.close();
bis.close();
fis.close();
}
public void Reader(File f,clsArticles articles,int option,String search) throws IOException{
articles = new clsArticles();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
articles = (clsArticles)obj;
if(articles.getTitle().equalsIgnoreCase(search) && option == 1){
check = true;
System.out.print(" "+f.getName().substring(0, f.getName().length()-4));
if(articles.getTitle().length()>7){
System.out.print("\t"+articles.getTitle().substring(0, 4));
}else{
System.out.print("\t"+articles.getTitle());
}
if(articles.getAuthor().length()>7){
System.out.print("\t\t" + articles.getAuthor().substring(0, 4)+"..");
}else{
System.out.print("\t\t" + articles.getAuthor());
}
try{
System.out.print("\t\t" + format.format(articles.getDate()));
}catch(NullPointerException e){}
if(articles.getContent().length()>8){
System.out.print("\t\t"+articles.getContent().substring(0, 8)+"...");
}else{
System.out.print("\t\t"+articles.getContent());
}
System.out.println("\n------------------------------------------------------------------------------------------");
}else if(articles.getAuthor().equalsIgnoreCase(search) && option == 2){
check = true;
System.out.print(" "+f.getName().substring(0, f.getName().length()-4));
if(articles.getTitle().length()>7){
System.out.print("\t"+articles.getTitle().substring(0, 4));
}else{
System.out.print("\t"+articles.getTitle());
}
if(articles.getAuthor().length()>7){
System.out.print("\t\t" + articles.getAuthor().substring(0, 4)+"..");
}else{
System.out.print("\t\t" + articles.getAuthor());
}
try{
System.out.print("\t\t" + format.format(articles.getDate()));
}catch(NullPointerException e){}
if(articles.getContent().length()>8){
System.out.print("\t\t"+articles.getContent().substring(0, 8)+"...");
}else{
System.out.print("\t\t"+articles.getContent());
}
System.out.println("\n------------------------------------------------------------------------------------------");
}else{
check = false;
}
ois.close();
bis.close();
fis.close();
}
public String returnValue(File f,int option) throws IOException{
article = new clsArticles();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
String value="";
article = (clsArticles)obj;
if(option == 1) // Title
{
value = article.getTitle();
}else if(option == 2){ // Writer
value = article.getAuthor();
}else if(option == 3){ // Content
value = article.getContent();
}
ois.close();
bis.close();
fis.close();
return value;
}
public Date returnDate(File f) throws IOException{
article = new clsArticles();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
article = (clsArticles)obj;
ois.close();
bis.close();
fis.close();
return article.getDate();
}
public String returnUser(File f)throws IOException{
user = new clsUsers();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
user = (clsUsers)obj;
ois.close();
bis.close();
fis.close();
return user.getUserID().toString();
}
public String returnPassword(File f)throws IOException{
user = new clsUsers();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
user = (clsUsers)obj;
ois.close();
bis.close();
fis.close();
return user.getPassWord().toString();
}
public void ReaderUser(File f) throws IOException{
user = new clsUsers();
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis,1024);
ois = new ObjectInputStream(bis);
Object obj=null;
try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}
user = (clsUsers)obj;
System.out.print(user.getUserID());
System.out.print("\t\t"+user.getPassWord());
ois.close();
bis.close();
fis.close();
}
}
package manageclass;
public class clsMenu {
public void SubMenu(){
System.out.println();
System.out.println("Menu>>");
System.out.print(" 1.Article 2.Title 3.Writer 4.Exit(Main Menu): ");
}
public void SubMenuDelete(){
System.out.println();
System.out.println("Choose>>");
System.out.print(" 1.Delete 2.Cancel: ");
}
public void Header(){
System.out.println();
System.out.println("========================================================");
System.out.println(" No\tTitle\t\tWriter\t\tDate");
System.out.println("========================================================");
}
public void HeaderDetail(){
System.out.println();
System.out.println("==========================================================================================");
System.out.println(" No\tTitle\t\tWriter\t\tDate\t\t\tContents ");
System.out.println("==========================================================================================");
}
public void MenuUpdate(){
System.out.println();
System.out.println("Choose option to update>>");
System.out.print(" 1.Title 2.Content 3.Exit(Main Update): ");
}
public void NoFile(){
System.out.println();
System.out.println("Menu>>");
System.out.print("1.Write 2.Exit(Log Out): ");
}
public void MenuMover(){
System.out.println();
System.out.println("Choose>>");
System.out.println("==== 1.First 2.Previous 3.Next 4.End 5.Read ============");
System.out.print("==== 6.Write 7.Search 8.Update 9.Delete 10.Log Out: ");
}
public void LoginMenu(){
System.out.println();
String st1="H======================================================H";
String st2="H Korean HRD Software Center H";
String st3="H Simple Mini Java Project H";
String st4="H Article Management H";
String st5="H H";
String st6="H Tutor: Suon Yannchhuong H";
String st7="H Group: 1 H";
String st8="H 1:Sout Saret H";
String st9="H 2.Try Vithou H";
String st10="H======================================================H";
String st11="H============Welcome to System Login===================H";
String st12="H Note: Default UserName: Admin and Password: Admin H";
String st13="H======================================================H";
String st14=" Menu>>";
String st15=" 1.Login 2.Sign Up 3.Quit: ";
print(st1);System.out.println();print(st2);System.out.println();print(st3);
System.out.println();print(st4);System.out.println();print(st5);
System.out.println();print(st6);System.out.println();print(st7);
System.out.println();print(st8);System.out.println();print(st9);
System.out.println();print(st10);System.out.println("\n");print(st11);
System.out.println();print(st12);System.out.println();print(st13);
System.out.println();print(st14);System.out.println();print(st15);
}
void print(String st){
char []ch=st.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.print(ch[i]);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
}
}
public void MainScreen(){
System.out.println();
System.out.println("Choose Menu>>");
System.out.println(" 1.User Management 2. System Management: ");
}
public void MainUsers(){
System.out.println();
System.out.println("Menu>>");
System.out.println("1.List User 2.Update User 3.Delete User 4.Exit: ");
}
public void UserMover(){
System.out.println();
System.out.println("Menu>>");
System.out.println(" 1.Previous 2.Next 4.Exit: ");
}
}
package manageclass;
import java.io.*;
public class clsSerialize {
private FileOutputStream fos;
private BufferedOutputStream bos;
private ObjectOutputStream oos;
// Write Article
public void Writer(File f, clsArticles article)throws IOException{
fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
oos.writeObject(article);
oos.close();
bos.close();
fos.close();
}
// Write User
public void Writer(File f, clsUsers user)throws IOException{
fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
oos.writeObject(user);
oos.close();
bos.close();
fos.close();
}
}
package manageclass;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.TreeMap;
import interfaces.InterBoard;
public class ManageUser implements InterBoard{
private static clsUsers user;
private static TreeMap<Integer, File> lArc;
private static TreeMap<Integer, File> currentArticle;
private static clsSerialize writeObj;
private static clsDeserialize readObj;
private static BufferedReader in;
private static File dir;
private static int currentPage = 0;
private final int article_num=5;
private static clsMenu menu;
public static String login;
static{
dir = new File("C:\\HRDSOUTSARETFMJAVASUN\\Users");
user = new clsUsers();
in = new BufferedReader(new InputStreamReader(System.in));
lArc = new TreeMap<Integer, File>();
currentArticle = new TreeMap<Integer, File>();
writeObj = new clsSerialize();
readObj = new clsDeserialize();
menu = new clsMenu();
}
public void refreshListFile() throws Exception{
try{
File listFile[]=dir.listFiles();
if(listFile.length<=0){
return;
}
lArc=new TreeMap<Integer, File>();
for(int i=0;i<listFile.length;i++){
lArc.put(i, listFile[i]);
}
}catch(NullPointerException e){
e.printStackTrace();
}
}
public void SubListing() throws Exception{
try{
refreshListFile();
//check exist file or not
if(lArc.size()<=0){
System.out.println("\tArticle not found!");
return;
}
//check valid current
if(currentPage<0){
currentPage=0;
}
//define num_page
int num_page=(int)Math.ceil(((float)lArc.size())/((float)article_num));
if(currentPage>=num_page-1){
currentPage=num_page-1;
}
//create current article
currentArticle=new TreeMap<Integer, File>();
int count=0;
System.out.println("");
System.out.println("````````````````````````````````````````````````````````");
System.out.println("` Nº\t\tUserName\t\tPassword `");
System.out.println("````````````````````````````````````````````````````````");
for(int i=((lArc.size()-1)-(article_num*currentPage));i>=0;i--){
//check end loop
if(count>=article_num){
break;
}
count++;
currentArticle.put(i, lArc.get(i));
File listFile[]=dir.listFiles();
String files;
files = listFile[i].getName();
if (files.endsWith(".ser")) {
int n = i+1 ;
System.out.print(" "+ n + "\t\t ");
File file = new File(dir+"\\" + files);
readObj.ReaderUser(file);
}
System.out.println("");
}
System.out.println("--------------------------------------------------------");
System.out.println("Total Users:"+lArc.size()+"\t\t\t\t\tPage:"+(currentPage+1)+"/"+num_page);
System.out.println("--------------------------------------------------------");
System.out.println("");
}catch(IOException e){
e.printStackTrace();
}
}
public void Listing() {
try {
SubListing();
while(true){
menu.MainUsers();
int con = System.in.read() - 48;
System.in.read();
System.in.read();
if(con == 1){ // List users
}else if(con == 2){ // Update users
}else if(con == 3){ // Delete users
}else if(con == 4){ // Exit Manage user
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void Writing() {
try{
System.out.print("Input User Name: ");
String usr= in.readLine();
System.out.print("Input Password : ");
String pwd= in.readLine();
user.setUserID(usr);user.setPassWord(pwd);
while(true){
System.out.print("Use(S or s) to save: ");
String con = String.valueOf(in.readLine());
if(con.equalsIgnoreCase("s")){
File file = null;
if(getID()==0)
file = new File(dir,(getID()+1)+ ".ser");
else
file = new File(dir,getID()+ ".ser");
writeObj.Writer(file,user);
System.out.println("Your data is saved to be successfully!!!!!!");
return;
}
}
}catch(IOException e){
e.printStackTrace();
}
}
public void Reading() {
}
public void Searching() {
}
public void Editing() {
}
public void Deleting() {
}
public void Exiting() {
System.out.println("You are quit from application!!!!");
System.exit(0);
}
/**
* Used to get Auto Article ID
*
*/
public int getID(){
int value = dir.listFiles().length + 1;
for (File name : dir.listFiles()) {
int fname=Integer.valueOf(name.getName().substring(0, name.getName().length()-4));
if(value != fname)
value = value + 0;
else
value = value + 1;
}
return value;
}
public boolean Verify(String usr,String pwd) throws IOException{
boolean bool = false;
for(File ff : dir.listFiles()){
String rusr = readObj.returnUser(ff.getAbsoluteFile());
String rpwd = readObj.returnPassword(ff.getAbsoluteFile());
if(usr.equals(rusr) && pwd.equals(rpwd)){
bool = true;
login = rusr;
}
}
return bool;
}
public void AddUser(){
try {
user.setUserID("Admin");
user.setPassWord("Admin");
File file = new File(dir+"\\"+1+ ".ser");
writeObj.Writer(file,user);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Subscribe to:
Comments (Atom)