Write a two class application that creates a customer code to be placed on a mai
ID: 3675017 • Letter: W
Question
Write a two class application that creates a customer code to be placed on a mailing label for a magazine. Allow the user to input their full name with the first name entered first. Prompt them to separate their first from last name with a space.Ask for their birthdate in the format of mm/dd/yyyy. Ask for the month (number) they purchased a subscription and ask for their zip code.Your mailing label should contain last name, followed by their year of birth, the first three characters of the month they purchased the subscription and the last two digits of their zip. The code for Bob Clocksom born 01/22/1977, who purchased his subscription during the 10th month of the year and lists 32226 as his zip code would be Clocksom77Oct26.
Explanation / Answer
/**The java program that prompts user to enter first and last names,
* date of birth, subscription month and zip and print the mailing label
* that consists of lastname,last two digits of date of birth,
* subscription month as a string and last two digits of zip code*/
//MailingLabel.java
import java.util.Scanner;
public class MailingLabel
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//Declare variables
String firstName;
String lastName;
String dob;
int subscriptionMonth;
String zip;
String mailingLabel="";
System.out.println("Enter first name and last name ");
//read first name and last name
firstName=scanner.next();
lastName=scanner.nextLine();
System.out.println("Enter date of birth");
//read date of birth
dob=scanner.nextLine();
System.out.println("Enter month of subscription( 1 - 12 ) ");
//read month of subscription
subscriptionMonth=Integer.parseInt(scanner.nextLine());
System.out.println("Enter zip code");
//read zip code
zip=scanner.nextLine();
//get last two characters from the date of birth
String dobCode=dob.substring(dob.length()-2, dob.length());
//get month name of subscription month
String monthCode=getMonth(subscriptionMonth);
//get last two characters from zip
String zipCode=zip.substring(zip.length()-2, zip.length());
//Concatenate first name with mailingLabel
mailingLabel+=lastName;
//Concatenate date of birth with mailingLabel
mailingLabel+=dobCode;
//Concatenate month code with mailingLabel
mailingLabel+=monthCode;
//Concatenate zip code with mailingLabel
mailingLabel+=zipCode;
System.out.println("The code for "+firstName+""+lastName+
"born "+dob+", who purchased his subscription during the "+
subscriptionMonth+"th month of the year and lists "+
zip+" as his zip code would be "+mailingLabel);
}
//The method getMontht that takes integer values and returns
//month name as string
private static String getMonth(int subscriptionMonth)
{
String mNames[]={"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"};
//Return the month name of three characters
return mNames[subscriptionMonth-1];
}
}//end of class
------------------------------- ------------------------------- -------------------------------
Sample output:
Enter first name and last name
Bob Clocksom
Enter date of birth
01/22/1977
Enter month of subscription( 1 - 12 )
10
Enter zip code
32226
The code for Bob Clocksomborn 01/22/1977, who purchased his subscription during the 10th month of the year and lists 32226 as his zip code would be Clocksom77Oct26
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.