/
CmdBook.java
155 lines (121 loc) · 4.05 KB
/
CmdBook.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Book
*this program reads n books from standard input and print them on standard output.
*it gets value of n through commaandline.
*/
import org.apache.commons.cli.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.util.*;
/*this is the this class Book
*
*/
public class CmdBook {
String title;
String publisher_name;
String published_date;
String content="";
ArrayList<String> author_name=new ArrayList<String>();
/*
*main function
*/
public static void main(String args[]) {
String title1=" ";
String publisher_name1=" ";
String published_date1="";
String content1="";
int n=0;
String str="";
//try block starts here
try{
Options option=new Options();
option.addOption("hp",false,"help");
option.addOption("hlp",false,"help");
CommandLineParser parser = new DefaultParser();
CommandLine cmd= parser.parse(option,args);
// str=cmd.getArgs()[0];
/*prints the help message here*/
if((cmd.hasOption("hp"))||(cmd.hasOption("hlp"))) {
System.out.println("this program gets an argument through commandline 'n' number of books. then reads n books from standard input"
+"and prints them on standard output");
if(args.length<=1){
// exit the program
System.exit(0);
}
} // close outer if block
else{
if(args.length<1){
//if user not gives option and value of n then prints this message.
System.out.println("this program gets an argument through commandline 'n' number of books. then reads n books from standard input"
+"and prints them on standard output");
System.exit(0);
}
} //close the outer else block
str=cmd.getArgs()[0];
n=Integer.parseInt(str);
}catch(Exception e){
//System.exit(0);
//if any erroy occurs it catches here
System.out.println(e);
}
int num=0;
Scanner sc1=new Scanner(System.in); /*create an instance of scanner object*/
Scanner sc=new Scanner(System.in);
CmdBook b[]=new CmdBook[n]; /* create arraylist of objects of class Book*/
for(int i=0;i<n;i++){
//initialise the array of objects
b[i]=new CmdBook();
}
for(int i=0;i<n;i++){
/* take input from standard input*/
System.out.println("enter the title of the book:");
title1=sc.next();
System.out.println("enter the name of publisher");
publisher_name1=sc.next();
System.out.println("enter published date");
published_date1=sc.next();
System.out.println("how many authors are");
num=sc1.nextInt();
ArrayList<String> author_name1=new ArrayList<String>(); /*create an ArrayList author_name1 */
for(int j=0;j<num;j++) {
/*take input */
System.out.println("enter authorname"+(j+1));
author_name1.add(sc.next());
}
System.out.println("enter contents of book:");
content1=sc.next();
/* call the method bookdetail with auguments which contains from
* standard input
*/
b[i].bookdetail(title1, publisher_name1, published_date1,content1, author_name1);
}
for(int i=0;i<n;i++){
/*call the methode printbook withe no aurgument*/
b[i].printbook();
}
} /*end of main function here*/
/* bookdetail method gets the book details and save in the respective variables*/
public void bookdetail(String tit , String pub, String dat, String cont, ArrayList<String> aut) {
title=tit;
publisher_name=pub;
published_date=dat;
author_name.addAll(aut);
content=cont;
} /*bookdetail method ends here*/
/*printbook method prints the n book details
* on the standard output
*/
public void printbook()
{
System.out.println("Title: "+title);
System.out.println("publisherName: "+publisher_name);
System.out.println("publisher_date: "+published_date);
System.out.println("Authors name: ");
for(int j=0;j<author_name.size();j++) {
System.out.println(" "+author_name.get(j));
}
System.out.println("content of book:"+content);
System.out.println("");
}
} /*here ends Book class*/