Using Scanner class in Java
This class transaction is displaying a menu and accepting values in
different formats using scanner class methods.
import java.util.Scanner;
public class Transaction
{
public static void main(String[] args) {
// symbolic constants
final int FREE_YOGURT_STAMPS = 7;
final char PURCHASE_OPTION = 'p';
final char STOP_OPTION = 's';
final char YES_OPTION = 'y';
final char NO_OPTION = 'n';
final String MENU = "Menu:n P (process Purchase)n S (Shut
down)nn Your choice: ";
final String INVALID_CHOICE = "n *** Use P or S, please.
***n";
final String PROMPT_AWARD = "nYou qualify for a free yogurt.
Would you like to use your credits? (Y or N)";
final String PROMPT_NUM_YOGURT = "nHow many yogurts would you
like to buy? ";
final String EARN_STAMPS = "n You just earned %d stamps and
have a total of %d to use.nn";
final String USE_STAMPS = "nYou have just used %d stamps and
have a total of %d to use.nEnjoy your free yogurt.nn";
final String INVALID_RESP = "n *** Invalid response ***n";
Scanner in = new Scanner(System.in);
// number of stamps of the single customer
int stamps = 0;
// the main loop, stop until user entered s
while (true) {
System.out.print(MENU);
String line = in.nextLine().trim(); // read one line
if (line.length() > 0) {
// check the first letter
char first = line.charAt(0);
first = Character.toLowerCase(first); // convert to
lower case
if (first == STOP_OPTION) {
// first letter is an upper or lower case 's'
// break the loop to end the program
break;
}
if (first == PURCHASE_OPTION) {
// the operators choose purchase
if (stamps >= FREE_YOGURT_STAMPS) {
// ask the customer to choose award
transaction
System.out.println(PROMPT_AWARD);
// check the first letter
line = in.nextLine().trim();
if (line.length() > 0) {
first = line.charAt(0);
first = Character.toLowerCase(first); //
convert to lower case
if (first == YES_OPTION) {
stamps = stamps - FREE_YOGURT_STAMPS;
System.out.printf(USE_STAMPS,
FREE_YOGURT_STAMPS, stamps);
continue;
} else if (first == NO_OPTION) {
// flow down to the normal transaction
} else {
// invalid option
System.out.println(INVALID_RESP);
continue;
}
}
}
// normal transaction
System.out.println(PROMPT_NUM_YOGURT);
int num = in.nextInt();
in.nextLine(); // ignore the n in the line
if (num >= 0) {
stamps = stamps + num;
System.out.printf(EARN_STAMPS, num, stamps);
} else {
// invalid number
System.out.println(INVALID_RESP);
}
continue;
}
}
// otherwise, invalid input
System.out.println(INVALID_CHOICE);
}
}
}

Java Assignment Help

  • 2.
    Using Scanner classin Java This class transaction is displaying a menu and accepting values in different formats using scanner class methods. import java.util.Scanner; public class Transaction { public static void main(String[] args) { // symbolic constants final int FREE_YOGURT_STAMPS = 7; final char PURCHASE_OPTION = 'p'; final char STOP_OPTION = 's'; final char YES_OPTION = 'y'; final char NO_OPTION = 'n'; final String MENU = "Menu:n P (process Purchase)n S (Shut down)nn Your choice: "; final String INVALID_CHOICE = "n *** Use P or S, please. ***n"; final String PROMPT_AWARD = "nYou qualify for a free yogurt. Would you like to use your credits? (Y or N)"; final String PROMPT_NUM_YOGURT = "nHow many yogurts would you like to buy? "; final String EARN_STAMPS = "n You just earned %d stamps and have a total of %d to use.nn"; final String USE_STAMPS = "nYou have just used %d stamps and have a total of %d to use.nEnjoy your free yogurt.nn"; final String INVALID_RESP = "n *** Invalid response ***n"; Scanner in = new Scanner(System.in); // number of stamps of the single customer int stamps = 0; // the main loop, stop until user entered s while (true) { System.out.print(MENU); String line = in.nextLine().trim(); // read one line if (line.length() > 0) {
  • 3.
    // check thefirst letter char first = line.charAt(0); first = Character.toLowerCase(first); // convert to lower case if (first == STOP_OPTION) { // first letter is an upper or lower case 's' // break the loop to end the program break; } if (first == PURCHASE_OPTION) { // the operators choose purchase if (stamps >= FREE_YOGURT_STAMPS) { // ask the customer to choose award transaction System.out.println(PROMPT_AWARD); // check the first letter line = in.nextLine().trim(); if (line.length() > 0) { first = line.charAt(0); first = Character.toLowerCase(first); // convert to lower case if (first == YES_OPTION) { stamps = stamps - FREE_YOGURT_STAMPS; System.out.printf(USE_STAMPS, FREE_YOGURT_STAMPS, stamps); continue; } else if (first == NO_OPTION) { // flow down to the normal transaction } else { // invalid option System.out.println(INVALID_RESP); continue; } } } // normal transaction System.out.println(PROMPT_NUM_YOGURT); int num = in.nextInt(); in.nextLine(); // ignore the n in the line if (num >= 0) { stamps = stamps + num; System.out.printf(EARN_STAMPS, num, stamps); } else { // invalid number System.out.println(INVALID_RESP); } continue; } }
  • 4.
    // otherwise, invalidinput System.out.println(INVALID_CHOICE); } } }