class Program{
public static void main(String[]args){
MovieRentalModel m=new MovieRentalModel("d:\\movies.xml");
MovieRentalView v=new MovieRentalView(m);
MovieRentalController c=new MovieRentalController(m,v);
}
}
---------------------------------------------------------------------------------------------------------
class MovieRentalController{
MovieRentalModel m;
MovieRentalView v;
public MovieRentalController(){
}
public MovieRentalController(MovieRentalModel m,MovieRentalView v){
this.m=m;
this.v=v;
menu();
}
private void menu(){
int option;
do{
option=v.getMenu();
switch(option){
case 1:
m.insert(v.getData(v.getId()));
v.InsertedSuccessfully();
break;
case 2:
int id=v.getId();
if(m.isFound(id)){
m.update(v.getData(id));
v.SuccessfullyUpdated();
}else{
v.NotFound();
}
break;
case 3:
v.display();
break;
case 4:
id=v.getId();
if(m.isFound(id)){
m.delete(id);
v.SuccessfulyDeleted();
}else{
v.NotFound();
}
break;
}
}while(option!=5);
}
}
---------------------------------------------------------------------------------------------------------
import java.util.*;
class MovieRentalView{
MovieRentalModel m;
public MovieRentalView(){
}
public MovieRentalView(MovieRentalModel m){
this.m=m;
}
public int getMenu(){
System.out.println("---Select Menu---");
System.out.println("[1]Insert");
System.out.println("[2]Update");
System.out.println("[3]Display");
System.out.println("[4]Delete");
System.out.println("[5]Exit");
return Console.In.getIntegerInput("Select[1-5]");
}
public MovieRentalModel getData(int id){
return new MovieRentalModel(id,
Console.In.getStringInput("Title:"),
Console.In.getStringInput("Genre:"),
Console.In.getStringInput("ReleaseDate:"),
Console.In.getStringInput("MainCast:")
);
}
public void InsertedSuccessfully(){
System.out.println("Inserted Successfully.");
}
public void display(){
ArrayList<MovieRentalModel> data=m.getData();
for(MovieRentalModel movie:data){
System.out.println(movie.toString());
}
}
public int getId(){
return Console.In.getIntegerInput("ID:");
}
public void NotFound(){
System.out.println("Not Found.");
}
public void SuccessfullyUpdated(){
System.out.println("Successfully Updated.");
}
public void SuccessfulyDeleted(){
System.out.println("Successfully Deleted.");
}
}
------------------------------------------------------------------------------------------------------------
import java.util.*;
class MovieRentalModel{
private int movieId;
private String title;
private String genre;
private String releaseDate;
private String mainCast;
public int getMovieId(){
//validating...
return movieId;}
public String getTitle(){return title;}
public String getGenre(){return genre;}
public String getReleaseDate(){return releaseDate;}
public String getMainCast(){return mainCast;}
public void setMovieId(int movieId){
//validation...
this.movieId=movieId;}
public void setTitle(String title){this.title=title;}
public void setGenre(String genre){this.genre=genre;}
public void setReleaseDate(String releaseDate){this.releaseDate=releaseDate;}
public void setMainCast(String mainCast){this.mainCast=mainCast;}
private MovieRentalModel[] movies;
private int ctr;
public MovieRentalModel(){
}
public MovieRentalModel(int movieId,String title,String genre,String releaseDate,String mainCast){
this.movieId=movieId;
this.title=title;
this.genre=genre;
this.releaseDate=releaseDate;
this.mainCast=mainCast;
}
private XMLProcessor xmlProcessor;
public MovieRentalModel(String XMLFile){
xmlProcessor=new XMLProcessor(XMLFile);
}
public MovieRentalModel(int size){
movies=new MovieRentalModel[size];
ctr=0;
}
public void insert(MovieRentalModel movie){
xmlProcessor.insert(movie);
}
public void update(MovieRentalModel data){
xmlProcessor.update(data);
}
public void delete(int id){
xmlProcessor.delete(id);
}
public boolean isFound(int id){
return xmlProcessor.isFound(id);
}
public ArrayList<MovieRentalModel>getData(){
return xmlProcessor.getData();
}
public String toString(){
return "\nID:"+movieId+
"\nTitle:"+title+
"\nGenre:"+genre+
"\nReleaseDate:"+releaseDate+
"\nMainCast:"+mainCast;
}
}
--------------------------------------------------------------------------------------------------------
import java.util.*;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
public class XMLProcessor{
File file;
DocumentBuilder docBuilder;
Document doc;
ArrayList<MovieRentalModel> data;
MovieRentalModel movie;
public XMLProcessor(){
}
public XMLProcessor(String XMLFile){
setDocument(new File(XMLFile));
}
public void insert(MovieRentalModel data){
Node movies=doc.getFirstChild();
Element movie = doc.createElement("movie");
movies.appendChild(movie);
Attr attr=doc.createAttribute("id");
attr.setValue(Integer.toString(data.getMovieId()));
movie.setAttributeNode(attr);
Element title =doc.createElement("title");
title.appendChild(doc.createTextNode(data.getTitle()));
movie.appendChild(title);
Element genre=doc.createElement("genre");
genre.appendChild(doc.createTextNode(data.getGenre()));
movie.appendChild(genre);
Element releaseDate=doc.createElement("releaseDate");
releaseDate.appendChild(doc.createTextNode(data.getReleaseDate()));
movie.appendChild(releaseDate);
Element mainCast=doc.createElement("mainCast");
mainCast.appendChild(doc.createTextNode(data.getMainCast()));
movie.appendChild(mainCast);
writeContentToXML();
}
public void update(MovieRentalModel data){
update(doc.getChildNodes(),data);
writeContentToXML();
}
private void update(NodeList nodeList,MovieRentalModel data){
for(int ndx=0;ndx<nodeList.getLength();ndx++){
Node node=nodeList.item(ndx);
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("movie")){
NamedNodeMap attr=node.getAttributes();
Node nodeAttr=attr.getNamedItem("id");
int movieId=Integer.parseInt(nodeAttr.getTextContent());
if(movieId==data.getMovieId()){
SetDataXML(node,data);
break;
}
}
if(node.hasChildNodes())
update(node.getChildNodes(),data);
}
}
}
public void delete(int id){
delete(doc.getChildNodes(),id,doc.getFirstChild());
writeContentToXML();
}
private void delete(NodeList nodeList,int id,Node parent){
for(int ndx=0;ndx<nodeList.getLength();ndx++){
Node node=nodeList.item(ndx);
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("movie")){
NamedNodeMap attr=node.getAttributes();
Node nodeAttr=attr.getNamedItem("id");
int movieId=Integer.parseInt(nodeAttr.getTextContent());
if(movieId==id){
parent.removeChild(node);
break;
}
}
if(node.hasChildNodes())
delete(node.getChildNodes(),id,parent);
}
}
}
private void SetDataXML(Node nodeParent,MovieRentalModel data){
NodeList nodeList=nodeParent.getChildNodes();
for(int ndx=0;ndx<nodeList.getLength();ndx++){
Node node=nodeList.item(ndx);
String name=node.getNodeName();
if("title".equals(name))
node.setTextContent(data.getTitle());
else if("genre".equals(name))
node.setTextContent(data.getGenre());
else if("releaseDate".equals(name))
node.setTextContent(data.getReleaseDate());
else if("mainCast".equals(name))
node.setTextContent(data.getMainCast());
}
}
private void writeContentToXML(){
try{
Transformer transformer=TransformerFactory.newInstance().newTransformer();
DOMSource source=new DOMSource(doc);
StreamResult result=new StreamResult(file);
transformer.transform(source,result);
}catch(TransformerException tfe){
tfe.printStackTrace();
}
}
private void setDocument(File file){
this.file=file;
try{
docBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc=docBuilder.parse(file);
}catch(ParserConfigurationException pfe){
pfe.printStackTrace();
}catch(SAXException sxe){
sxe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
public ArrayList<MovieRentalModel> getData(){
data=new ArrayList<MovieRentalModel>();
if(!doc.hasChildNodes())return null;
return extractData(doc.getChildNodes());
}
public boolean isFound(int id){
if(!doc.hasChildNodes()) return false;
return searchData(doc.getChildNodes(),id);
}
boolean found;
private boolean searchData(NodeList nodeList,int id){
found=false;
for(int ndx=0;ndx<nodeList.getLength();ndx++){
Node node=nodeList.item(ndx);
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("movie")){
NamedNodeMap attr=node.getAttributes();
Node nodeAttr=attr.getNamedItem("id");
int movieId=Integer.parseInt(nodeAttr.getTextContent());
if(movieId==id){found=true; break;}
}
if(node.hasChildNodes())
searchData(node.getChildNodes(),id);
}
}
return found;
}
private ArrayList<MovieRentalModel> extractData(NodeList nodeList){
for(int ndx=0;ndx<nodeList.getLength();ndx++){
Node node=nodeList.item(ndx);
if(node.getNodeType()==Node.ELEMENT_NODE){
setData(node);
if(node.getNodeName().equals("mainCast"))
data.add(movie);
if(node.hasChildNodes())
extractData(node.getChildNodes());
}
}
return data;
}
private void setData(Node node){
String name=node.getNodeName();
String value=node.getTextContent();
if("movie".equals(name)){
movie=new MovieRentalModel();
NamedNodeMap attr=node.getAttributes();
Node nodeAttr=attr.getNamedItem("id");
movie.setMovieId(Integer.parseInt(nodeAttr.getTextContent()));
}else if("title".equals(name))
movie.setTitle(value);
else if("genre".equals(name))
movie.setGenre(value);
else if("releaseDate".equals(name))
movie.setReleaseDate(value);
else if("mainCast".equals(name))
movie.setMainCast(value);
}
}
No comments:
Post a Comment