Loops and JOptionpane - java

I wrote a program that allows the user to enter the Identity Number, Name, Nationality, Job type of the client (ordinary, academician, medical person), Destination country, amount to be transferred in SAR, type of currency to be received (SAR, local currency)and then displays the Identity Number, Name, Nationality, Destination country, Amount to be transferred in SAR, Amount to be received, total amount to be paid by the client I need to know how do I do this : After each program run, I need to ask the user if she wants to continue using the program (using confirmation dialog of JOptionPane). If the user clicks YES, the program will continue and ask the user for another input. This process continues until the user clicks NO ... Please help!

Generally in cases like this I believe you need to loop round until the user selects a no option:
public class SO{
public static void main(String[] args) {
int choice = 0;
do{
choice = JOptionPane.showConfirmDialog(null, "Continue?");
} while(choice == JOptionPane.YES_OPTION);
}
}

Use showConfirmDialog as follows:
int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?", "Close?", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
System.exit(0);
}

use do while loop..
do{
// your all operations...
// finally ask yes or no..
}while(choice.equalsIgnoreCase("yes"));

Related

Trying to make an banking deposit with player input

I'm fairly new to coding spigot but I am trying to make a banking script where the player would click the deposit button and be prompted to type in the amount of money they want to deposit. However, when this happens in game, it automatically determines the input as 0 and doesn't give the player enough time to type in their message. Here is the code:
private void deposit(Player p, String gang) {
deposited.add(p);
p.closeInventory();
p.sendMessage(ChatManager.Feedback("jahseh", "Please type how much money you want to deposit"));
int input = (int) MyListener.typed;
if (Main.getEconomy().getBalance(p) >= input){
Main.getEconomy().bankDeposit(gang, input);
p.sendMessage(ChatManager.Feedback("jahseh", "You have deposited " + input + " stonebucks into your gang's bank."));
}
if (Main.getEconomy().getBalance(p) < input){
p.sendMessage(ChatManager.Feedback("alert", "You are trying to deposit more money than you currently have!"));
}
}
public void input (AsyncPlayerChatEvent event){
Player p = event.getPlayer();
String typed = event.getMessage();
if(Bank.deposited.contains(p)) {
Bank.deposited.remove(p);
event.setCancelled(true);
}
}
}
You have to wait for the listener to be executed, and only once it has been executed, run the code below.

How would I use user input to prompt the user to play different games in java?

I am creating a program for class; the program needs to be able to play 4 games depending on which one the user chooses. If the user chooses game1; then the user would play game1, if the user chooses game2, then the user would play game2 etc..
I am trying to figure out how to create a method depending on which game the user wants to play. For example, if the user wants to play game1, I would create a playGame1 method that has the instructions to play game1.
If there is a better way to make it happen, please let me know, Thanks!
So far i'm trying to get user input which creates an if statement that leads the user to play.
import java.util.Scanner;
public class Attendant
{
public Attendant()
{
Scanner input = new Scanner(System.in);
String choice = input.next();
System.out.println( " Which game would you like to play? game1 ; game2 ; game3 ; game4 ");
if (choice.equalsIgnoreCase ( "game1"))
{
}
if (choice.equalsIgnoreCase ( "game2"))
{
}
if (choice.equalsIgnoreCase ( "game3"))
{
}
if (choice.equalsIgnoreCase ( "game4"))
{
}
}
}
Consider using switch statements
switch (choice) {
case "game1":
//gameInstructions go here
break;
...
default:
//this will be used when user will provide invalid input (like for ex. "gaem1" (mistype)
}
this code above will handle your user's game selection, remember to put break; to stop code going further than you want.

End the Game from a separate method

I have two different classes: TextBasedGame and IntroductionChoices.
IntroductionChoices makes all the decisions while TextBasedGame basically just prints the story (it's the one with the main method).
What I want to know is how to make the game END if the user makes a wrong choice.
In this case, if the user types in "Take down the group.", a message is displayed and the game ends. In my case, in the main method, it just keeps on going. How do I stop the game from running?
I was thinking I could somehow, in the main method, check if return from IntroductionChoicesis "GAME OVER" and end the game there.
//Snippet from the main method
System.out.println("What do you do?");
System.out.println(choice.choice1());
System.out.println("Next part...");
//Method choice1 from IntroductionChoices
public static String choice1()
{
Scanner scan = new Scanner(System.in);
System.out.println("Warn her of her shadows.");
System.out.println("Take down the group.");
String decision = scan.next();
if (decision.equalsIgnoreCase("right decision"))
{
System.out.println("some text");
return "some more text in continuation to continue story in main";
}
else if (decision.equalsIgnoreCase("wrong decision"))
{
System.out.println("You creep towards the men, hoping to surprise them from behind.");
System.out.println("Consequence");
return "GAME OVER";
}
else
{
return "That is not a valid response.";
}
}
System.exit(0)
else if (decision.equalsIgnoreCase("wrong decision"))
{
System.out.println("You creep towards the men, hoping to surprise them from behind.");
System.out.println("Consequence");
System.exit(0);
You won't need to return anything, it will end the program.
OR
Give the user an option:
Start new game-> start a new game
Quit -> System.exit(0)
I'd also use a switch to check through the user options, you're less liekly to make mistakes and it can be neater and quicker when the options start accumulating.
You can create an enum of options or use final ints with meaningful names so your code is easier to navigate and then match with user options.
From java the docs.

JOptionPane.showConfirmDialog variable usage

So I am very new to this and I am trying to get the variables to add up during the process of the code in order to print the total at the end, in doing so, I need it to read the selection from the JOptionPane that the user entered and add the value to the total at the end. The code is almost complete but is missing the calculation of the variables.
Q: How do I get the value of any variable based on what the user selects from the JOptionPane and add it to the cumulative total to println at the end?
An ex is as follows:
(Sorry if it isn't formatted correctly, I am working on learning how it should look)
int addons = 0;
do{
addons = JOptionPane.showConfirmDialog(null, "Would the customer like to add Salad, Smoothie, Sundae or Cone?");
if (addons == JOptionPane.NO_OPTION);
else if (addons == JOptionPane.YES_OPTION){
String[] choices2 = { "Salad $"+salad, "Smoothie$"+smoothie, "Sundae $"+sundae, "Cone $"+cone};
String extras = (String) JOptionPane.showInputDialog(null, "Which addon?",
"Extras", JOptionPane.QUESTION_MESSAGE, null, // Use
choices2, // Array of choices
choices2[0]); // Initial choice
count++;//Add item to count
System.out.println(extras);
} //close YES_OPTION parameters
} //close do parameters
while(addons == JOptionPane.YES_OPTION); // Exit do while loop6

Trying to add if statement values to the total answer in Java

quite new to Java so trying to understand it can be difficult.
Anyways the problem, as part of a task I have to code a theatre tickets console application. I've almost finished I'm just trying to add the last part
As part of the task we have to ask the user if they want the tickets to be posted, if yes this incurs a charge which will be applied to the total charge. Currently I'm unsure of how to do this. At the moment I am currently working with If and else statements and.. Well you'll see below.
System.out.println ("Do you require the tickets to be posted?\n(£2.34 for post and packing for the entire order)\n(please enter 'Yes' on the next line if you require postage)");
String postinp = scanner.next();
if ("Yes".equals(postinp)){
System.out.println("Postage will be added to your cost" );
}
else
System.out.println("Postage will not be added to your cost");
Well I'm trying to code, if the user enters 'yes' then it adds on the postage charge to the total, but in this section of code I'm unsure how to do that.
Any help would be appreciated. Thanks!
Inside the if-statement all we need to do is add your £2.34 to the total sum being calculated by the program.
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double total = 10.00;
System.out.println("Do you require the tickets to be posted?\n(£2.34 for post and packing for the entire order)\n(please enter 'Yes' on the next line if you require postage)");
if ("Yes".equalsIgnoreCase(input.next())) {
System.out.println("Postage will be added to your cost");
total = total + 2.34;
} else
System.out.println("Postage will not be added to your cost");
}
use scanner.nextLine() insead of scanner.next();
and use "Yes" instead of "Ye"
What I would do is change
if ("Ye".equals(postinp)) {
to
if ("yes".equals(postinp.toLowerCase())) {
That way it won't be case sensitive to what the user inputs, because otherwise they'd have to input Yes exactly.
You can add more code within the then-else blocks of an if statement
if ( "Yes".equals(postinp) )
{
this.cost += 2.34 ; // or whatever it is you need to do
System.out.println("Postage will be added to your cost" );
}
else
{
System.out.println("Postage will not be added to your cost");
}
You seem to need to resolve two things--evaluating the answer and then acting accordingly.
Try this:
String postinp = scanner.next();
if ("Yes".equalsIgnoreCase(postinp)) {
cost += postage;
System.out.println("Postage will be added to your cost" );
}
else {
System.out.println("Postage will not be added to your cost");
}
The if block checks if the result is some form of "yes" without regard to case. Then it assumes that cost and postage variables (floats) are available and initialized from somewhere. In the "yes" case, cost is augmented by postage.
Also, since you are just learning it isn't a big deal, but at some point you might want to consider your postage to be a value consumed from a constant or from a configuration file. Maybe the initial cost can be too.

Categories