testclasstotesttheworkingsofcard.java
// Test class to test the workings of Card
import java.io.*;
public class TestCards
{
public static void main (String args[]) throws IOException
{
String s;
String answer; // used for the do while loop
int n,p;
String r;
int menu; // used for the switch statement
// intialise the buffered reader
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("Welcome.");
System.out.println("Please enter 0 for the whole suit
to be shown");
System.out.println("Please enter 1 to choose a card ");
System.out.println("Please enter 2 to choose the card
and the suit");
System.out.println("Please enter 3 to generate a random
card");
System.out.flush(); // clears the buffer reader
s=in.readLine();// reads the users input in
menu = Integer.parseInt(s); // converts the input from
a string into an integer
switch (menu)
{
case 0:
System.out.println("You have selected
no input. The cards are");
Card c1 = new Card(); //initialises
a new instance of the class cards
System.out.println("The cards are "
+ c1.getCard());
System.out.flush();
break;
case 1:
System.out.println("You have selected
one input.Please enter the value of the card. \n 1= ace - 13 = king");
System.out.flush();
s=in.readLine(); // reads the users
input in
n=Integer.parseInt(s); // Converts the
input into an integer
n=n-1;
Card c2 = new Card(n); // intialises
a new instanbce of the card class
System.out.println("The card is " +
c2.getCard());
break;
case 2:
System.out.println("You have selected
two inputs.Please enter the value of the card. \n 1= ace - 13 = king");
System.out.flush();
s=in.readLine(); // reads the users
input in
n=Integer.parseInt(s); // Converts the
input into an integer
n=n-1;
System.out.println(" Please enter 0
for Hearts, 1 for Diamonds, 2 for Spades and 3 for clubs");
System.out.flush();
s=in.readLine(); //reads the users input
in
p=Integer.parseInt(s);
Card c3 = new Card(n,p); // intialises
a new instanbce of the card class
System.out.println("The card is " +
c3.getCard());
break;
case 3:
System.out.println("You have selected
the random card generator.Press any key to continue");
System.out.flush();
s=in.readLine(); // reads the users
input in
r=s;
Card c4 = new Card(r); // intialises
a new instanbce of the card class
System.out.println("The card is " +
c4.getCard());
break;
default :
System.out.println("Please enter either
no number of a 1");
break;
}
System.out.println(" Enter Y to continue of anything else to
quit");
System.out.flush();
answer= in.readLine();
} while (answer.equalsIgnoreCase("Y"));
}// ends the main
}
Return to : Java Programming Hints
and Tips