import java.util.*; public class Hallmark
//line 1 This will make all classes in the java.util package available to me and if I want to I can use any of them. For example Arrays.sort() could have been since Arrays is a class in the java.util package
//line 2 This will make a public class, meaning that everything declared in here will be available to all of the other
classes. In this case the Scanner console.
{
static Scanner console = new Scanner(System.in);
// This will create a Scanner object that will in this case watch out for all inputted data streams.
public static void main(String[] args)
{
// variable declaration
boolean isInt = true;
Card userCard;
int iUserInput;
int iInputCounter = 1;
String sSender = toPerson();
String sRecipient = fromPerson();
These are a number of variables that will be available to all of the classes in this area, as can be seen by the public access modifier and the static makes it so the class is global and anything that happens in it is available to all other classes/subclasses. The Card userCard is a userdefined datatype and userCard is the object of the Card class that includes the abstract makeCard(); method that was used to create the various cards.
case 1:
userCard = new Christmas(sSender, sRecipient);
break;
This case and the others all do the same thing (even though the number of parameters may differ). They all assign userCard(object) to a certain subclass of Card and will pass the user input to the class's constructor.
//generic information
userCard.makeCard();
This calls our makeCard(); method however; it is dependent on the type of card, and the number of parameters. Generally userCard could be say Christmas, and this access the makeCard() method in the Christmas class and pass the information stored in userCard (sSender, sRecipient) and will create the custom card.
public static String sMessage()
{
System.out.println("Please enter your message to the recipient: ");
// clear buffer
console.nextLine();
String sMessageToRecipient = console.nextLine();
return sMessageToRecipient;
}
}
This method is similar to the other methods however; seeming as it is dealing with String you should clear the buffer to make sure no hooplah is in the way to making the code work.
Thank you: Jon.Kiparsky and CasiOo for all the help you have provided for me. Now for the final code in one piece
import java.util.*;
public class Hallmark
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
// variable declaration
boolean isInt = true;
Card userCard;
int iUserInput;
int iInputCounter = 1;
String sSender = toPerson();
String sRecipient = fromPerson();
//menu, point 3
String sMenu = "Please select your card:" +
"\n\t1) Christmas" +
"\n\t2) Valentines" +
"\n\t3) Birthday" +
"\n\t4) Get well soon" +
"\n\t5) Anniversary" +
"\n\t6) New Baby" +
"\n\t7) Thank You" +
"\n\t8) Congratulations" +
"\n\t9) Blank Card\n";
//print menu
System.out.print(sMenu);
//confirm input
do
{
System.out.println("Please enter a valid choice");
try
{
iUserInput = console.nextInt();
if ((iUserInput >= 0) && (iUserInput <= 9))
{
//decrease counter if successful range
iInputCounter--;
switch(iUserInput)
{
//christmas card, point 1
case 1:
userCard = new Christmas(sSender, sRecipient);
break;
//valentines card, point 1
case 2:
userCard = new Valentines(sSender, sRecipient);
break;
//birthday card, point 1
case 3:
int iAge = agePerson();
userCard = new Birthday(sSender, sRecipient, iAge);
break;
//getwellsoon card, point 1
case 4:
userCard = new GetWellSoon(sSender, sRecipient);
break;
//anniversary card, point 1
case 5:
userCard = new Anniversary(sSender, sRecipient);
break;
//newbaby card, point 1
case 6:
userCard= new NewBaby(sSender, sRecipient);
break;
//thankyou card, point 1
case 7:
userCard = new ThankYou(sSender, sRecipient);
break;
//congratulations card, point 1
case 8:
userCard = new Congratulations(sSender, sRecipient);
break;
//blank card, point 1
default:
String sMessageToRecipient = sMessage();
userCard = new BlankCard(sSender, sRecipient, sMessageToRecipient);
break;
}
//generic information
userCard.makeCard();
}
else
{
System.out.println("You have not entered a valid choice");
continue;
}
}
catch (InputMismatchException e)
{
System.out.println("You have entered an invalid input");
console.next();
continue;
}
}while(iInputCounter > 0);
}
//point 2
public static String toPerson()
{
System.out.println("Please enter your name");
String sSender = console.nextLine();
return sSender;
}
//point 2
public static String fromPerson()
{
System.out.println("Please enter recipients name");
String sRecipient = console.nextLine();
return sRecipient;
}
//point 4
public static int agePerson()
{
int iAge = 0;
do
{
System.out.println("Please enter recipients age");
try
{
iAge = console.nextInt();
return iAge;
}
catch(InputMismatchException e)
{
System.out.println("You have entered invalid input");
continue;
}
}while(true);
}
//point 5
public static String sMessage()
{
System.out.println("Please enter your message to the recipient: ");
// clear buffer
console.nextLine();
String sMessageToRecipient = console.nextLine();
return sMessageToRecipient;
}
}
Truly thank you guys for all of your help

New Topic/Question
Reply





MultiQuote



|