please convert the following java code to python! please makepython code as simple as possible! Thanks in
Question:
please convert the following java code to python! please makepython code as simple as possible! Thanks in advanced, If you cannot complete entirely do not attempt or thumbs down! Isimply need it translated line by line.
import java.text.*;
import java.util.*;
public class TextParser implements PhoneBillParser {
private String[] tokens;
private String filename;
private boolean doPrint = false; // only activatedwhen the -print is called
/**
* Initialize the parser with data to parse
* @param tokens string to parse
*/
public TextParser(String[] tokens) {
this.tokens = tokens;
}
/**
* Returns the filename where to write results
* @return filename
*/
public String getFilename() {
return this.filename;
}
/**
* Returns if the print to console should beenabled
* @return boolean
*/
public boolean isPrintActive() {
return this.doPrint;
}
/**
* Parses a string and returns a phone bill
* @return abstract phone bill if successful
* @throws exception if phone bill is unformatted
*/
public AbstractPhoneBill parse() throwsParserException {
// split the the text throughtokens
boolean optionsDone = false;
String[] arguments = newString[7];
int argumentsIndex = 0;
for(int i = 0; i
if(token.startsWith("-")) {
// this means the token is an option
if(optionsDone)
throw newParserException("Options should go first before arguments. Pleasedo not mix.");
if(token.equals("-textFile")) {
// expect the next text asthe filename, check if the next text exists
i++;
if(i >=tokens.length)
throw newParserException("Incomplete option arguments.");
this.filename =tokens[i];
} else if(token.equals("-print")) {
// activate the print (fordebug perhaps)
doPrint = true;
} else if(token.equals("-README")) {
// displays about the projectthen terminates
System.out.println("Project2: Writes phone bills (with a phone call) to text files.");
System.out.println("usage:java edu.pdx.cs410J..Project2 [options] ");
System.out.println(" args are (in this order):");
System.out.println(" customer Person whose phone billwe’re modeling");
System.out.println(" callerNumber Phone number of caller");
System.out.println(" calleeNumber Phone number of person who wascalled");
System.out.println(" startTime Date and time call began(24-hour time)");
System.out.println(" endTime Date andtime call ended (24-hour time)");
System.out.println(" options are (options may appear inany order):");
System.out.println(" -textFilefile Where to read/write the phone bill");
System.out.println(" -print Prints adescription of the new phone call");
System.out.println(" -README Prints aREADME for this project and exits");
System.out.println("Dates andtimes should be in the format: mm/dd/yyyy hh:mm");
System.exit(0);
} else {
throw newParserException("Unknown option " + token);
}
} else {
// options are done, so we should expectarguments next, in the correct order
optionsDone = true;
arguments[argumentsIndex] = token;
argumentsIndex++;
}
}
// make sure that all arguments areprovided
if(argumentsIndex != 7)
throw newParserException("Incomplete or too much arguments");
// get the date and time
String startTime = arguments[3] + "" + arguments[4];
String endTime = arguments[5] + " "+ arguments[6];
// validate first the format of thedate
if(!this.isDateValid(startTime))
throw newParserException("The start time date format is invalid");
if(!this.isDateValid(endTime))
throw newParserException("The end time date format is invalid");
// based from the arguments createa phone call and phone bill
// caller number, callee number,start time, and end time
PhoneCall call = newPhoneCall(arguments[1], arguments[2], startTime, endTime);
// create a phone bill adding thecall to it
PhoneBill bill = newPhoneBill(arguments[0], call);
// return the bill
return bill;
}
/**
* Validates a date string if it is on the correctformat
* @param date date to be validate
* @return true if valid, otherwise false
*/
private boolean isDateValid(String date) {
SimpleDateFormat sdf = newSimpleDateFormat("MM/dd/yyyy HH:mm");
try {
Date temp =sdf.parse(date);
} catch(Exception error) {
returnfalse;
}
return true;
}
}
SAMPLE OUTPUT
PARSER
/**
* Classes that implement this interface read some source andfrom it
* create a phone bill.
*/
public interface PhoneBillParser {
/**
* Parses some source and returns a phone bill
*
* @throws ParserException
* If the source cannot beparsed
*/
public AbstractPhoneBill parse() throwsParserException;
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill