Skip to content

Instantly share code, notes, and snippets.

@IanSSenne
Created January 27, 2020 12:40
Show Gist options
  • Save IanSSenne/ae24de776e91afa2d46a5f0b43ce672b to your computer and use it in GitHub Desktop.
Save IanSSenne/ae24de776e91afa2d46a5f0b43ce672b to your computer and use it in GitHub Desktop.
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Will output an html file to disk containing the code needed for a resume
* based off of user input.
*
* still need to do another pass over it and revise my comments a bit, there is a good ammount of 3am code in this.
*
* @author Ian Senne
* @version 2
*/
public class ResumeIanSenneEC {
Scanner keyboard;
int totalEnteredCharacters = 0;
public ResumeIanSenneEC() {
System.out.println("running EC version");
keyboard = new Scanner(System.in);
//initialize Scanner
String firstName = queryUser("please enter your first name");
String lastName = queryUser("please enter your last name");
String email = queryUser("please enter your email");
String phone = queryUser("please enter your phone number");
String linkedIn = queryUser("please enter the URL to your LinkedIn page");
String schoolYear = queryUser("please enter your year in school");
String major = queryUser("please enter your major");
String softSkill = queryUser("please enter your best soft skill");
String techSkill = queryUser("please enter your best technical skill");
String[] jobs = new String[100];
for (int i = 0; i < jobs.length; i++) {
jobs[i]="";
}
String hasJobs;
int jobsIndex = 0;
do{
hasJobs = queryUser("would you like to enter a job?(Y/n)", false);
System.out.println(hasJobs.length());
}while(!hasJobs.equals("Y") && !hasJobs.equals("n"));
if(hasJobs.equals("Y")){
String doneInput;
do{
jobs[jobsIndex] = getJob();
do{
doneInput=queryUser("enter another job?(Y/n)", false);
}while(!doneInput.equals("Y") && !doneInput.equals("n"));
jobsIndex++;
}while(doneInput.equals("Y"));
}
// query user for required fields.
// construct html string from createElement calls...
String contents = createElement("html", "",new String[] { //<html>
createElement("head", "", new String[] { // <head>
createElement("title", "", new String[] { // <title>
firstName, " ", lastName, " Resume" // firstName lastName Resume
}) // </title>
}), // </head>
createElement("body", "", new String[] { // <body>
createElement("h1", "", new String[] { // <h1>
firstName, " ", lastName // firstName lastName
}), // </h1>
createElement("p", "", new String[] { // <p>
email, " ", phone, " ", linkedIn // email pnone linkedIn
}), // </p>
createElement("h3", "", new String[] { // <h3>
"Summary" // Summary
}), // </h3>
createElement("p", "", new String[] { // <p>
capitalizeOnlyFirstLetter(schoolYear), // capitalizeOnlyFirstLetter(schoolYear) major.toLowerCase() major with a soft skill of softSkill.toLowerCase()
" ",
major.toLowerCase(),
" major with a soft skill of ",
softSkill.toLowerCase()
}), // </p>
createElement("h3", "", new String[] { // <h3>
"Technical Skills" // Technical Skills
}), // </h3>
createElement("ul", "", new String[] { // <ul>
createElement("li", "", new String[] { // <li>
techSkill // techSkill
}) // </li>
}), // </ul>
createElement("h3", "", new String[] { // <h3>
"Work Experience" // Work Experience
}), // </h3>
createElement("span", "", jobs), // <span>...JOBS</span>
createElement("i", "", new String[] { // <i>
"(" + totalEnteredCharacters + ")" // (totalEnteredCharacters);
}) // </i>
}) // </body>
}); //</html>
writeToDisk(getCompleteHTMLDocument(contents));
}
private String getJob() {
String recentJobTitle = queryUser("please enter your last job title");
String recentCompany = queryUser("please enter the last company you worked at");
String recentWorkDates = queryUser("please enter dates of work");
String recentWorkResponsability = queryUser("please enter a responsability you held at your last position");
return createElement("span", "", new String[]{
createElement("p", "", new String[] { // <p>
createElement("b", "", new String[] { // <b>capitalizeOnlyFirstLetter(recentCompany)</b>,recentWorkDates
capitalizeOnlyFirstLetter(recentCompany)
}),
",",
recentWorkDates
}), // </p>
createElement("p", "", new String[] { // <p>
createElement("i", "", new String[] { // <i>
capitalizeOnlyFirstLetter(recentJobTitle) // capitalizeOnlyFirstLetter(recentJobTitle)
}) // </i>
}), // </p>
createElement("ul", "", new String[] { // <ul>
createElement("li", "", new String[] { // <li>
capitalizeOnlyFirstLetter(recentWorkResponsability) // capitalizeOnlyFirstLetter(recentWorkResponsability)
}) // </li>
})
}); // </ul>;
}
public static void main(String[] args) {
new ResumeIanSenneEC();
// since the code utilizes data on the instance main will just instantiate a now
// instance of the resume class.
}
private String capitalizeOnlyFirstLetter(String str) {
if (str.length() < 2) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
}
private void writeToDisk(String content) {
PrintWriter outputStream = null;
// create placeholder to assign PrintWriter to.
try {
outputStream = new PrintWriter("resume.html");
outputStream.print(content);
outputStream.close();
// write to and close the PrintWriter.
System.out.println("wrote " + content.length() + "bytes to resume.html");
// notify the user that <n> bytes were written to a file.
} catch (FileNotFoundException e) {
System.out.println("file not found, logging html string.");
System.out.println(content);
// if there is an error notify the user and print the html string
}
}
private String queryUser(String msg) {
return queryUser(msg, true);
}
//overload of queryUser for 1 argument. assume cound is default
private String queryUser(String msg,Boolean count){
String res;
String finalInput;
do {
res = "";
System.out.println(msg + ": ");
res = keyboard.nextLine();
finalInput=res;
res = res.trim();
// this is here per the specification that states all use input should be
// trimmed.
} while (res.length() == 0);
// this do-while loop impliments a check to ensure that all inputed data is
// atleast 1 character
if (count)
totalEnteredCharacters += finalInput.length();
// add inputed text length to the total character count.
return res;
}
//query use but config count characters
private String createElement(String name, String props, String[] children) {
String el = "<" + name;
// all html elements will start with a < followed by the element name
if (props.length() > 0) {
el += " " + props;
// if props is not "" then add a space followed by props to the opening tag, ex:
// createElement("div","id=\"test\"",new String[]{}) => "<div id=\"test\"></div>"
}
el += ">";
// close the opening tag for the current element
for (int i = 0; i < children.length; i++) {
if (children[i].length() > 0 && children[i].charAt(0) != '<'){
children[i] = children[i].replace(" ", "&nbsp;").replace("<","&lt;").replace(">","&gt;");
// if the current child does not start with < and is not empty then replace " "
// with "&nbsp;"
}
el += children[i];
// append the current child to the current element string.
}
el += "</" + name + ">";
// append end tag to end of string, this implimentation does not support self
// closing tags currently
return el;
}
private String getCompleteHTMLDocument(String contents) {
return "<!DOCTYPE html>" + contents;
// add the doctype tag to the beggining of the contents.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment