Article management System using Java Console Application with Memory base

In this post I will to show you how to create Java Console  Application called article management. main feature of this project has write article read article, you can see with my capture screen..

Ok let's start all together in this posted with simple  source code project in under of this post I uploaded source code.



public class Article {
private int id;
 private String title;
private String author;
private String content;
private String date;
public Article(int id){
this.id = id;
}
public Article(int id, String title, String author, String content, String date){
this.id = id;
this.title = title;
this.author = author;
this.content = content;
this.date = date;
}
public int getId(){
return id;
}
public String getTitle(){
return title;
}
public String getAuthor(){
return author;
}
public String getContent(){
return content;
}
public String getDate(){
return date;
}
public void setId(int id) {
this.id = id;
}

public void setTitle(String title) {
this.title = title;
}

public void setAuthor(String author) {
this.author = author;
}

public void  setContent(String content) {
this.content = content;
}

public void setDate(String date) {
this.date = date;
}

}



Class data.java is class for store all data in memory we can read update delete on it.




Data.java


package article_management;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Data {

/* static variable */

// variable row use to store the display row
private static int displayRow = 5;
private static int currentPage = 1;
// variable autoNumber use to add the auto number key to the data when
// insert
private static int autoNumber = 0;

// collection for storing data
private static ArrayList<Article> data = new ArrayList<Article>();
//Function Display Record
public static void display(ArrayList<Article> arr){
Display.header();
for(int i=Pagination.startIndex; i<Pagination.endIndex;i++){
Display.row(arr.get(i).getId(), arr.get(i).getTitle(), arr.get(i).getAuthor(), arr.get(i).getDate());
}
Display.footer();

}
// method for processing data
/* Test Insert */
public static void testWrite() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String d = dateFormat.format(date);
for (int i = 0; i < 1E6; i++) {
data.add(new Article(++autoNumber,
"Java",
"Jonh Deny", "Free download", d));
}
Collections.reverse(data);
}

/**
 * Inserting new Data
 * */
public static void write() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String d = dateFormat.format(date);
// get input from keyboard
Scanner in = new Scanner(System.in);

String[] s = new String[3];
System.out.println();
System.out.println("\tID: " + ++autoNumber);
// title
System.out.print("\tTitle > ");
s[0] = in.nextLine();

// author
System.out.print("\tAuthor > ");
s[1] = in.nextLine();

// content
System.out.print("\tContent");
s[2] = getMiltiLineString("");

// add record to collection
data.add(0, new Article(autoNumber, s[0], s[1], s[2], d));
Display.enter(20);
display(data);

}

public static void write(String record){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String d = dateFormat.format(date);
String [] arr_split = record.split("-");
 
//check if input more then
if(arr_split.length != 3){
System.out.println("Invail input please follow rule" );
--autoNumber;
}else{
//check if content has ; will replace to \n for enter
String enterString="";
if(arr_split[2]!=null){
enterString=arr_split[2].replace(";", "\n\t");
}
// add new record to first index of collection
data.add(0, new Article(++autoNumber, arr_split[0], arr_split[1], enterString, d));
Display.enter(20);
display(data);
}
}
/* read data */
/**
 * Display Data based on input id.
 * 
 * @param id
 * is the input key. Use id when searching for specific article.
 * */
public static void read(int id) {

// display header
Display.header();
int index = searchIndex(id);
// display record
Display.row(data.get(index).getId(), data.get(index).getTitle(), data
.get(index).getAuthor(), data.get(index).getDate());

// display footer
Display.footer();

}

/**
 * Display Data by specific range.
 * 
 * @param firstIndex
 * is the start index of record.
 * @param lastIndex
 * is the stop index of record
 * */
public static void read(int firstIndex, int lastIndex) {

// display header
Display.header();

// set the rang of record to display
List<Article> list = data.subList(firstIndex, lastIndex);

// display the record
for (Article art : list) {
Display.row(art.getId(), art.getTitle(), art.getAuthor(),
art.getDate());
}

// display footer
Display.footer();

}

/**
 * Read Detail
 * */
public static void readDetail() {
// get input from keyboard
Scanner in = new Scanner(System.in);
// if the value input from keyboard is valid id
boolean isInputAgain = true;
// continue process the input if the input is invalid
while (isInputAgain) {
System.out.print("\n\tArticle ID > ");
if (in.hasNext()) {
// check the input value
String input = in.nextLine();
if (Validate.isNumber(input)) {
isInputAgain = false;
// search for index of record
int index = searchIndex(Integer.parseInt(input));
if (index >= 0) {
Display.enter(25);
System.out.println("\tArticle Detail");
System.out.println("\tLast update: " + data.get(index).getDate());
System.out.println("\tArticle ID: " + data.get(index).getId());
System.out.println("\tTitle: " + data.get(index).getTitle());
System.out.println("\tAuthor: " + data.get(index).getAuthor());
System.out.println("\tContent: " + data.get(index).getContent());
}else{
System.out.println("\tRecord not found...");
}

}
}
}
}

/**
 * Read Detail
 * 
 * @param id
 * is the id of record.
 * */
public static void readDetail(int id) {

// search for index of record
int index = searchIndex(id);
if (index >= 0) {
Display.enter(25);
System.out.println("\tArticle Detail");
System.out.println("\tLast update: " + data.get(index).getDate());
System.out.println("\tArticle ID: " + data.get(index).getId());
System.out.println("\tTitle: " + data.get(index).getTitle());
System.out.println("\tAuthor: " + data.get(index).getAuthor());
System.out.println("\tContent: " + data.get(index).getContent());
}else{
System.out.println("\tRecord not found...");
}
}

/* update */
/**
 * Update data
 * */
public static boolean update() {
// get input from keyboard
Scanner in = new Scanner(System.in);

boolean isUpdate = false;

// if the value input from keyboard is valid id
boolean isInputAgain = true;
// continue process the input if the input is invalid
while (isInputAgain) {
System.out.print("\n\tArticle ID to update >");
if (in.hasNext()) {

// check the input value
String input = in.nextLine();
if (Validate.isNumber(input)) {
isInputAgain = false;
if(update(Integer.parseInt(input))) isUpdate = true;
} else {

System.out.println("\n\tInput id is not valid!");
}
}
}

// return boolean value
return isUpdate;
}
/**
 * Update data
 * @param id is the id of record
 * */
public static boolean update(int id) {
// get input value from keyboard
Scanner in = new Scanner(System.in);
boolean isUpdate = false;

// if the value input from keyboard is valid id
boolean isInputAgain = true;

// get the record index of the inputed id
int index = Data.searchIndex(id);
// show old record user before update
readDetail(id);
// start updating process if the record found
if (index >= 0) {

// store oldValue
String[] s = { data.get(index).getTitle(),
data.get(index).getAuthor(),
data.get(index).getContent() };
// get new date
DateFormat dateFormat = new SimpleDateFormat(
"yyyy/MM/dd");
Date date = new Date();
String d = dateFormat.format(date);

isInputAgain = true;
System.out.print("\n\tWhat do you want to update?  ");

String op = "";
while (isInputAgain) {
System.out.print("\n\t(Al)All\t(T)Title\t(A)Author\t(C)Content\t(E)Exit\t> ");
if (in.hasNext()) {
op = in.nextLine();
isInputAgain = false;
switch (op.toLowerCase()) {
case "al":
// replace old record
System.out.print("\tTitle > ");
s[0] = in.nextLine();
// replace old author
System.out.print("\tAuthor > ");
s[1] = in.nextLine();
// replace old content
System.out.print("\tContent");
s[2] = getMiltiLineString("");
break;
case "t":
// replace old author
System.out.print("\tTitle > ");
s[0] = in.nextLine();
break;
case "a":
// replace old author
System.out.print("\tAuthor > ");
s[1] = in.nextLine();
break;
case "c":
// replace old content
System.out.print("\tContent");
s[2] = getMiltiLineString("");
break;
case "e":
display(data);
return isUpdate;
default:
System.out.println("\n\tInvalid option...");
isInputAgain = true;
break;
}

}

/* display data before update to make sure */
if(!isInputAgain){
// display record before save
Display.enter(25);
System.out.println("\tArticle Detail");
System.out.println("\tLast update: " + d);
System.out.println("\tArticle ID: " + id);
System.out.println("\tTitle: " + s[0]);
System.out.println("\tAuthor: " + s[1]);
System.out.println("\tContent: " + s[2]);
// confirm user before update
if (confirm("\n\tAre you sure want to update?")) {
// remove old data
data.remove(index);
// add record to collection
data.add(index, (new Article(id, s[0], s[1], s[2], d)));
display(data);
isUpdate = true;
}
}
}

} else {
// display message to inform user that the input id
// doesn't exist
System.out.println("\n\tRecord not found!");
}


// return boolean value
return isUpdate;
}

/**
 * Delete record
 * 
 * @param id
 * */
public static boolean delete() {
// get input from keyboard
Scanner in = new Scanner(System.in);

boolean isDeleted = false;
boolean isInputAgain = true;
// continue process the input if the input is invalid
while (isInputAgain) {
System.out.print("\tArticle ID to delete > ");
if (in.hasNext()) {
// check the input value is valid or not
String input = in.nextLine();
if (Validate.isNumber(input)) {
isInputAgain = false;
delete(Integer.parseInt(input));
} else {

System.out.println("\tInput id is not valid!");
}
}
}

// return true if delete successfully
return isDeleted;
}
/**
 * Delete record
 * 
 * @param id
 * */
public static boolean delete(int id) {
boolean isDeleted = false;
// get the record index of the inputed id
int index = Data.searchIndex(id);
// start to search if the collection contain key
if (index >= 0) {
// display record before save
Display.enter(25);
System.out.println("\tArticle Detail");
System.out.println("\tLast update: " + data.get(index).getDate());
System.out.println("\tArticle ID: " + id);
System.out.println("\tTitle: " + data.get(index).getTitle());
System.out.println("\tAuthor: " + data.get(index).getAuthor());
System.out.println("\tContent: " + data.get(index).getContent());
// remove data if id is found
System.out.println("\n\tRecord Found...");
if (confirm("Are you sure want to delete article #"
+ id + "?")) {
data.remove(index);
isDeleted = true;
display(data);
}

} else {
// display message to inform user that the input id
// doesn't exist
System.out.println("\tRecord not found!");
}


// return true if delete successfully
return isDeleted;
}

/* searching method */
/**
 * Search record. This method is use to search record from outside class
 * */
public static void search() {
// get input from keyboard
Scanner in = new Scanner(System.in);
// if the value input from keyboard is valid id
boolean isInputAgain = true;
// continue process the input if the input is invalid
while (isInputAgain) {
System.out.print("\n\tSearch > ");
if (in.hasNext()) {
// check the input value
String input = in.nextLine();

ArrayList<Article> temp_al = new ArrayList<Article>(
searchString(input));
if (temp_al.size() == 0) {
System.out.println("\n\tNo record found...");
return;
}
// display record based on the record found
Display.header();
for(Article art : temp_al){
Display.row(art.getId(), art.getTitle(), art.getAuthor(), art.getDate());
}
Display.footer();
isInputAgain = false;
}
}
}

/**
 * Search record. This method is use to search record from outside class
 *
     * @param keyword */
public static void search(String keyword) {

ArrayList<Article> temp_al = new ArrayList<Article>(
searchString(keyword));
if (temp_al.size() == 0) {
System.out.println("\n\tNo record found...");
return;
}
// display record based on the founded record
Display.header();
for(Article art : temp_al){
Display.row(art.getId(), art.getTitle(), art.getAuthor(), art.getDate());
}
Display.footer();
}

/**
 * Search index. use this method with local method for example method
 * delete, update need searching data before processing that operation
 * 
 * @param id
 *            is the key of each record
 * */
private static int searchIndex(int id) {

// search data based on id
for (Article art : data) {
if (art.getId() == id) {
// if data found return index on the collection
return data.indexOf(art);
}
}
// return -1 if data is not found
return -1;
}

/**
 * Searching everything
 * 
 * @return ArrayList<Article>
 * */
private static ArrayList<Article> searchString(String s) {

// store index
ArrayList<Article> al = new ArrayList<Article>();

// search data based on id
for (Article art : data) {

if (art.toString().contains(s.toLowerCase())) {
al.add(art);
}
}
// return null if data is not found
return al;
}


/* Get Multi-line String */
private static String getMiltiLineString(String msg) {
Scanner input = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
System.out.print(msg + " use ; for break point " + "> ");
while (true) {
String imsi = input.nextLine();
// if(imsi != null && imsi.trim().length()==1 &&
// imsi.trim().charAt(0)==';') break;
if (imsi != null && imsi.indexOf(";") > -1)
break;
if (imsi == null)
imsi = "";
sb.append(imsi + "\n");
}
return sb.toString();
}

/**
 * @return total records.
 * */
public static int records(){
return data.size();
}
/**
 * @return total pages.
 * */
public static int pages(){
return data.size()/displayRow;
}
// return displayRow
public static int getRow() {
return displayRow;
}

// set displayRow
public static void setRow(int row) {
if (row <= data.size()) {
displayRow = row;
}else{
// display error message
System.out.println("\tRow must be less that or equal total records.");
return;
}
}
/**
 * Get current display pages.
 * */

/**
 * Confirm
 * 
 * @Confirm before process operation
 * @return true if user press y otherwise return false.
 * */
private static boolean confirm(String msg) {
// get user input
Scanner in = new Scanner(System.in);
boolean isValid = false;
boolean confirm = false;

// process again if user input invalid option
while (!isValid) {
System.out.println("\t" + msg);
System.out.print("\tPress y to confirm, n to cancel => ");
if (in.hasNext()) {
String input = in.nextLine();
switch (input.toLowerCase()) {
case "y":
confirm = true;
isValid = true; // cancel input process is user input valid
// option
break;
case "n":
confirm = false;
isValid = true; // cancel input process is user input valid
// option
break;
default:
System.out.println("\tInvalid option...");
}
}

}
return confirm;
}

public static ArrayList<Article> getData() {
return data;
}
}

No comments

Powered by Blogger.