In: Computer Science
Part One
Part Two
Both Parts
Samples:
John Public;Dinner;29.95;6/7/2014 Jane Public;Conference;499.00;8/9/2014 Abby Lawrence;Dinner;23.45;10/10/2014
John Public;Dinner;29.95;6/7/2014 Abby Lawrence;Dinner;23.45;10/10/2014
Jane Public;Conference;499.0;8/9/2014
The program to write data to Sample.txt is SalesInfo.java and for the part 2 its HotelRecords.java
Part one:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class SalesInfo {
public static void main(String[] args) throws Exception {
File file=null;
// Enter the name of the file from which data has to be parsed , example : Sample.txt
/*If the file is C:\Users\<name>\Desktop\Sample.txt
* then Enter the filename as C:\\Users\\<name>\\Desktop\\Sample.txt
* Because java takes '\' as escape sequence hence '\\' is used . Please enter the filename properly
*/
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the filename from which data has to be parsed");
String filename=in.readLine();
try {
file = new File(filename);
if(!file.exists())
file.createNewFile();
while(true){
System.out.println("Enter the name of Client: ");
String client=in.readLine();
System.out.println("Enter the Service category sold: ");
String Category=in.readLine();
System.out.println("Enter the amount of the sale: ");
String amount=in.readLine();
System.out.println("Please enter the date in the correct format(dd/MM/yyyy)");
String date=in.readLine();
String value= client+";"+Category+";"+amount+";"+date;
System.out.println(value);
FileWriter fr = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fr);
bw.write("\n"+value);
bw.close();
System.out.println("Do you want to input more data(Y/N) ?");
String s=in.readLine();
System.out.println(s);
if(s.equals("N"))
break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
// finally {
// br.close();
// }
}
}
Part 2:
import java.io.*;
public class HotelRecords {
public static void main(String[] args) throws Exception {
File file=null;
BufferedReader br=null;
// Enter the name of the file from which data has to be parsed , example : Sample.txt
/*If the file is C:\Users\<name>\Desktop\Sample.txt
* then Enter the filename as C:\\Users\\<name>\\Desktop\\Sample.txt
* Because java takes '\' as escape sequence hence '\\' is used . Please enter the filename properly
*/
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the filename from which data has to be parsed");
String filename=in.readLine();
try {
file = new File(filename);
//if there is no such file , it throws file not found exception
br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
String input[] = st.split(";");
//Please enter the path as described above with '\\' in place of '\'
System.out.println("Enter the path where you want to create the files");
String path=in.readLine();
//The data separated by ; has to be 4 according to question, if not throws UnknownTrnsaction
if(input.length==4){
File f1 = new File(path +"\\"+ input[1] + ".txt");
// If the file does not exists
if (!f1.exists())
f1.createNewFile();
FileWriter fr = new FileWriter(f1, true);
BufferedWriter bw = new BufferedWriter(fr);
bw.write(st+"\n");
bw.close();
}
else{
throw new UnknownTransaction("The format for the transaction is not known");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(UnknownTransaction e){
System.out.println(e.getMessage());
}
catch(IOException e) {
e.printStackTrace();
}
finally {
br.close();
}
}
}
Custom Exception:
class UnknownTransaction extends Exception
{
public UnknownTransaction(String s)
{
// Call constructor of parent Exception
super(s);
}
}