Skip to content

Instantly share code, notes, and snippets.

@keysRB
Created November 3, 2014 16:38
Show Gist options
  • Save keysRB/60392e6a759182ffb8b1 to your computer and use it in GitHub Desktop.
Save keysRB/60392e6a759182ffb8b1 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class LetterFormat {
String firstName;
String lastName;
String userAddress;
String userLocation;
String[] userInfo;
int[] charLength;
public LetterFormat() {
//Default Constructor
}
public void getInput() {
Scanner userInput = new Scanner(System.in);
System.out.printf("Please enter your first name: ");
firstName = userInput.nextLine();
System.out.printf("Please enter your last name:");
lastName = userInput.nextLine();
System.out.printf("Please enter your address:");
userAddress = userInput.nextLine();
System.out.printf("Please enter your City, State Zip Code:");
userLocation = userInput.nextLine();
userInfo = new String[3]; // Reserve memory for the array
userInfo[0] = firstName + " " + lastName; // Put user input into an array:
userInfo[1] = userAddress;
userInfo[2] = userLocation;
}
public void getLength() {
charLength = new int[3];
charLength[0] = firstName.length() + lastName.length() + 1;
charLength[1] = userAddress.length();
charLength[2] = userLocation.length();
}
public void printLetter() {
for (int i = 0; i<1; i++ ){ //Print top half of envelope
System.out.println("+------------------------------------------------------------+"); //Print Top Format Line
System.out.println("| #### |");
System.out.println("| #### |");
System.out.println("| #### |");
System.out.println("| |");
System.out.println("| |");
printUserInfo(); // Print the user Info
System.out.println("+------------------------------------------------------------+");
}
}
public void printUserInfo() {
printName();
printAddress();
printLocation();
}
public void printName() {
// *******Format name line with information to fit the box formatting: ********************
System.out.printf("|");
int totalSpace = (60 - charLength[0]); // Use the array to find out how long the input is
int spaceLeft = totalSpace / 2;
for (int j = 0; j<spaceLeft; j++ ) { //Need to count number of spaces to print before and after name
System.out.printf(" ");
}
System.out.printf(userInfo[0]);
for (int k = 0; k < spaceLeft; k++) {
System.out.printf(" ");
}
System.out.println("|");
// ****************************************************************************************
}
public void printAddress() {
// *******Format address line with information to fit the box formatting: ********************
System.out.printf("|");
//int addressLength = userAddress.length(); // Find length of address
int totalSpace = (60 - charLength[1]);
int spaceLeft = totalSpace / 2;
for (int j = 0; j<spaceLeft; j++ ) {
System.out.printf(" ");
}
System.out.printf(userInfo[1]);
for (int k = 0; k < spaceLeft; k++) {
System.out.printf(" ");
}
System.out.println("|");
// ****************************************************************************************
}
public void printLocation() {
// *******Format location line with information to fit the box formatting: ********************
System.out.printf("|");
int totalSpace = (60 - charLength[2]);
int spaceLeft = totalSpace / 2;
for (int j = 0; j<spaceLeft; j++ ) {
System.out.printf(" ");
}
System.out.printf(userInfo[2]);
for (int k = 0; k < spaceLeft; k++) {
System.out.printf(" ");
}
System.out.println("|");
// ****************************************************************************************
}
}
//Purpose: Print a letter to yourself stating your name and address. Formatting should be included around info.
public class LetterToYourself {
public static void main(String[] args) {
boolean exit = false;
do {
LetterFormat getUserInput = new LetterFormat();
getUserInput.getInput();
getUserInput.getLength();
getUserInput.printLetter();
exit = true;
} while(exit == false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment