Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JOption

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JOption

    Hello everyone good day!!
    I just want to ask help from you sir/madam about my java program, by the way my program is like POS, this is made with the use of (import java.util.Scanner
    Now i have a problem about converting it to (import javax.swing.*) using JOption or simply converting it to GUI. Can anyone please help me do it? Heres my program:
    Import java.util.*;
    public class Cart {
     
       //To publicly access the quantity as it updates every time you wish to buy again
       public  static  int 
       	total_quantity=0;
       	//I have declared 4 products here
       	public static String[]
       	products={null,
       	"1.) Quarter-Pounder Burger (Karina) 50.00",
       	"2.) One-Percenter (2-Stacks of Quarter Pounder Patty)99.00",
       	"3.) TOmbstone Piledriver (3 Stacks of Quarter Pounder Patty)150",
       	"4.) God's Last Gift (Tombstone Burger with 100g of Carolina Reaper)200.00"
       	};
       	//Prices in accordance of their elements
       	public static int[]
       	product_price
       		={0,50,99,150,200
       		};
       		//This is the cart for the check-out of all order you hAve
       		public static int[]
       		final_qty=new int[5];
     
       		public static void main (String[] args) {
       			Scanner s= new Scanner(System.in);
       			int choose,quantity;
       			int p1=0;
       			int p2=0;
       			int p3=0;
       			int p4=0;
       			char decision;
     
       			System.out.println ("Welcome to Burger Legends, please choose any of the menu there..");
       			do{
       				for(int i=0; i<
       					products.length;i++)
       					{ 
       						if(products[i] !=null)
     
       				System.out.println (products[i]);		
       			}
       			 System.out.println ("Choose any items:");
       			 choose=s.nextInt();
     
       			 System.out.println ("How many pieces?");
       			 quantity=s.nextInt();
     
       			 switch(choose)
       			 {
       			 	case 1:
       			 		p1 +=quantity;
       			 		final_qty[1]=p1;
       			 		break;
     
       			 		case 2:
       			 		p2 +=quantity;
       			 		final_qty[2]=p2;
       			 		break;	
     
       			 		case 3:
       			 		p3 +=quantity;
       			 		final_qty[3]=p3;
       			 		break;	
     
       			 		case 4:
       			 		p4 +=quantity;
       			 		final_qty[4]=p4;
       			 		break;	
       			 }
       			 System.out.println ("Would you like to order again?Y/N/y/n:");
     
       			 decision=s.next().charAt(0);
       			}while(decision != 'n' &&
       				decision != 'N');
     
       				System.out.println ("==Final cart==");
       				System.out.println ("\t\\item\t\\Qty\t\\Total");
       				int sum=0;
       				for(int i=0; i<final_qty.length; i++)
       				{
       					if(final_qty[i] !=0)
       					{
     
       			System.out.println (products[i]+"\\"+final_qty[i]+"\\"+final_qty[i]*product_price[i]);
     
       			sum=sum+final_qty[i]*product_price[i];
       					}
       			}
       			System.out.println ("Total Purchased items:"+(p1+p2+p3+p4));
       			System.out.println ("Total Purchased: "+sum);
       			int payment=0;
       			do{
       				System.out.println ("Enter Payment:");
       				payment=s.nextInt();
       				}while(sum>payment);
       				System.out.println ("Thank you for buying! here's your change:"+(payment-sum));	
     
     
    }
     
     
    }
    Hope you can help me thank you, God bless !!
    Last edited by Ash4k; March 24th, 2019 at 08:21 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: JOption

    Take a look at the tutorial on how to write a Swing GUI: https://docs.oracle.com/javase/tutor...ing/index.html

    Using the JOPtionPane class's methods instead of the Scanner class will be much simpler.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JOption

    Thank you, its my 1st time here and also to learn java .

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: JOption

    There are lots of formatting problems in the posted code that make it hard to read and understand. For example with do{}while statements,
    the do and the while should start in the same column.
    The same with for loops: the for { and the ending } should be in the same column.
    Statements inside of an if statement should be indented so their nesting level shows the logic.

    If you are just learning java, I would recommend trying the JOptionPane first.
    There is a long path to learning how to create a Swing GUI.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JOption

    Thanks

Similar Threads

  1. Calculator Using JOption
    By Jrams in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 19th, 2018, 10:31 AM
  2. JOption help
    By Ryn440 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 27th, 2014, 12:45 PM
  3. Java joption
    By darking123 in forum Object Oriented Programming
    Replies: 9
    Last Post: February 5th, 2013, 10:51 AM
  4. [SOLVED] I have a problem with this Joption programme.
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 12th, 2011, 03:31 AM
  5. JOption Pane help
    By dallas1125 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 18th, 2009, 05:08 PM