Oktoberfest

If else statements in java

Page 1 of 1

8 Replies - 1130 Views - Last Post: 30 September 2010 - 04:55 PM Rate Topic: -----

#1 tonyfingures  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-September 10

Oktoberfest

Posted 28 September 2010 - 08:29 PM

Hey! guys, i'm having issues writing this java code. Below is the question and below that is my attempt at coding it. Any help would be appreciated.

Jack has decided his student organization is going to sell Oktoberfest shirts at Oktoberfest. In order to compete with other clubs, he wants to offer a 10% discount to anyone who buys 5 or more shirts. Also, Jack is going to have several promotional ads before Oktoberfest to try and boost sales at his booth. If a customer mentions the ad, they receive $2.00 off their order. Jack wants the output of the program to display the quantity ordered plus the total due so everyone working at the booth can see how many shirts need to be given to the customer and also how much money to collect. Jack will be selling the shirts for $10.00 each.
Complete this program using JOptionPane and if-else statements.

import javax.swing.JOptionPane;

public class Oktoberfest2
{
	public static void main(String[] args)
	{
		double shirtCount, promo, total;

		shirtCount = 0.0;
		promo = 0.0;
		decimalDiscount = 0.1;
		total = 0.0;

		shirtCount = Double.parseDouble(JOptionPane.showInputDialog("Enter number of shirts"));

		if (shirtCount>=5)
			{
				decimal discount = (shirtCount * 10) * 0.1;
				total = (shirtCount * 10) -discount;
			}

		else
			{
				total = (shirtCount * 10);
			}
		}
	}


This post has been edited by no2pencil: 28 September 2010 - 08:34 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Oktoberfest

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Oktoberfest

Posted 28 September 2010 - 08:35 PM

Quote

i'm having issues writing this java code.

Then why did you post it under the student sub forum?

** Moved to Java **
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Oktoberfest

Posted 28 September 2010 - 09:25 PM

This isn't quite finished yet. What is it that you need help on? Conceptual help?
Was This Post Helpful? 0
  • +
  • -

#4 tonyfingures  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-September 10

Re: Oktoberfest

Posted 29 September 2010 - 07:10 AM

Ok, so i got the 1st math part right but i want to have a dialogue box for the $2 discount if ad is mentioned(yes and no) but the problem is i don't know how to write that part out in java.
Was This Post Helpful? 0
  • +
  • -

#5 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Oktoberfest

Posted 29 September 2010 - 07:16 AM

Just like the other JOptionPane part. At least try.
Was This Post Helpful? 0
  • +
  • -

#6 tonyfingures  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-September 10

Re: Oktoberfest

Posted 30 September 2010 - 06:57 AM

this is my revisd attempt


import javax.swing.JOptionPane;


public class Oktoberfest3
{
public static void main(String[] args)
{
double shirtCount, discount, answer, total;


shirtCount = 0;
discount = 0.1;
total = 0.0;


shirtCount= Double.parseDouble(JOptionPane.showInputDialog("Enter total shirt count"));

if(shirtCount >= 5)
{
discount= (shirtCount* 10) * 0.1;
total= (shirtCount * 10) - discount;
}

else if (shirtCount <= 4);
{
total= (shirtCount * 10);

}


int answer1 = y;
int answer2 = n;


int y = JOptionPane.showConfirmDialog("Did customer mention ad?",JOptionPane.YES_NO_OPTION);
if (y == JOptionPane.YES_OPTION)
{
discount= (shirtCount* 10) * 0.1;
total= ((shirtCount * 10) - discount) - 2;
}
else if (n == JOptionPane.NO_OPTION)
{
total= (shirtCount * 10);
}

}

}
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Oktoberfest

Posted 30 September 2010 - 02:46 PM

With 14 posts, you should be able to use the code tags. Use them. If you don't use them again, we will have issues...

:code:
Was This Post Helpful? 0
  • +
  • -

#8 tonyfingures  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-September 10

Re: Oktoberfest

Posted 30 September 2010 - 04:27 PM

I apologise for that. I used the code feature with this one. If this doesn't work for you, let me know. I have re-writen my code but i'm still having issues. I do want to remind you that i'm doing this using trial and error with no guide what so ever. So some creative input is what i'm looking for. I would appreciate it if someone would point out what i'm doing wrong and suggest areas of imporvement. Currently, i'm having a problem having the yes option do the calculations and display the result. I have tried and failed.



import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JFrame;


public class Oktoberfest3
{
	public static void main(String[] args)
	{
		double	shirtCount, discount, answer, total;


		shirtCount = 0;
		discount = 0.1;
		total = 0.0;


		shirtCount= Double.parseDouble(JOptionPane.showInputDialog("Enter total shirt count"));

	if(shirtCount >= 5)
			{
				discount= (shirtCount* 10) * 0.1;
				total= (shirtCount * 10) - discount;

	System.out.println((shirtCount * 10)-discount);
			}

	else if (shirtCount <= 4);
			{
				total= (shirtCount * 10);
			}

    JFrame parent = new JFrame();
    JOptionPane optionPane = new JOptionPane("Has the customer mentioned the ad?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
    JDialog dialog = optionPane.createDialog(parent, "Manual Creation");
    dialog.setVisible(true);
			{

	if (JOptionPane.YES_OPTION);
			{

	System.out.println(((shirtCount * 10)-discount) - 2);

			}

	else if (JOptionPane.NO_OPTION);

			{
	System.out.println((shirtCount * 10)-discount);
			}
		}


    }

}



Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • Joined: 27-December 08

Re: Oktoberfest

Posted 30 September 2010 - 04:55 PM

This line is more complex than needs to be. 10 * .1 = 1, so just assign discount = shirtCount;.
discount= (shirtCount* 10) * 0.1;  



In this section, there are a lot of logic errors. First, no need for all the braces, especially after you set the JDialog visible. First, don't use semi-colons at the end of if statements like you are doing. Also, the JOptionPane constants are ints, not booleans, so you can't use them in an if statement without comparing them to a value.
			{

	if (JOptionPane.YES_OPTION);
			{

	System.out.println(((shirtCount * 10)-discount) - 2);

			}

	else if (JOptionPane.NO_OPTION);

			{
	System.out.println((shirtCount * 10)-discount);
			}
		}


    }

}


Probably easier to do:
int x = JOptionPane.showConfirmDialog(null,"Message","Title",JOptionPane.YES_NO_OPTION);

if(x == JOptionPane.YES_OPTION){
   //code
}
else if(x == JOptionPane.NO_OPTION){
   //code
}



So no need to use a JFrame and JDialog either.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1