Search code examples
javaswingjframejpaneljoptionpane

How to put this program on a Jframe?


I'm having trouble on how to make this program be put on a JFrame. I'm not sure exactly how to do it. It would be great if anyone gave me some help. Thank you very much I appreciate it. Help anyone :/?

   package VendMach;

import java.util.Scanner;

import javax.swing.JComponent;
 import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class VendMachMain extends JComponent {

public static class groceryshopping{
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);

        int choice = 1;
        double subtotal = 0;
        double price = 0;
        double discount = 0;

    do {    
        System.out.println( "Quick Deli" + "\n" +"1. Lunchables             $ 1.99 per   box" + 
    "\n" + "2. Chips                $ 0.99 per bag" + "\n" + "3. Chocolate Bars     $ 0.99 each" + "\n" + 
    "4. Soda                $ 0.99 a can" + "\n" + "5. Cold Cut Sandwiches  $ 2.99 each" + "\n" + "6. Apple Slices          $ 0.75 per bag" + 
    "\n" + "7. Juice Boxes          $ 0.99 each" + "\n" + "8. Cinnabons         $ 0.99 each" + "\n" + "9. Cookies               $ 1.99 per bag" +
    "\n" + "10.Gum                  $ 1.15 per pack" + "\n" + " " + "\n" + "0. Quit" + "\n" + " " + "\n" + "Your subtotal is $ " +(int)(subtotal * 100) / 100.0 
    + "\n" );

        System.out.println("What would you like to purchase?  \nIf you have completed your checkout, enter 0.");
            choice = input.nextInt();


    if (choice == 0)
        break;

        System.out.println("How many do you want?");
        int qty = input.nextInt();

        switch (choice)    {
            case 1:
                price = 1.99;
                break;
            case 2:
                price = 0.99;                        
                break;
            case 3:
                price = 0.99;
                break;
            case 4:
                price = 0.99;
                break;
            case 5:
                price = 2.99;
                break;
            case 6:
                price = 0.75;
                break;
            case 7:
                price = 0.99;
                break;
            case 8:
                price = 0.99;
                break;
            case 9:
                price = 1.99;
                break;
            case 10:
                price = 1.15;
        }        
            subtotal = subtotal + (qty * price);
        }

        while(choice > 0);

        System.out.println("Do you have the special discount card? (Y/N)");
            String discountInput = input.next();
        char discountClub = discountInput.charAt(0);    

        if (discountClub == 'y' || discountClub == 'Y'){
            discount = subtotal * .20;
        }    

        double tax = ((subtotal - discount) * 0.075); 
        double finalCost = subtotal - discount + tax;

        JOptionPane.showMessageDialog(null,"The subtotal is $ " + (int)(subtotal * 100) / 100.0 + "\n" + "Discount $ " + (int) (discount * 100) / 100.0 
+ "\n" + "Tax $ " + (int)(tax* 100) / 100.0 + "\n" + "Total price $ " + (int)(finalCost * 100) /  100.0);
    }
}
}

Solution

  • There are no shortcuts except using something like "windowbuilder" in eclipse fx to make them with a graphical interface. But you still need to understand the logic of many items you use to manipulate them the way you want.

    I recommended you go through a tutorial and learn the basics at least (there are no shortcuts in programming).

    Like this fx: http://www.fortystones.com/creating-simple-gui-beginners-java-tutorial/