Here is my code. my problem now is how to convert it to GUI.

public class ProductController extends ProductModel{
	
	public String getPname(int barCode){
		   return this.pname(barCode);
	   }   
	
	public double getPrice(int barCode) 
	   {
		return this.price(barCode);
	   }
	
	public void displayCartControl( int retryTrigger ){
		this.displayShoppingCart(retryTrigger);
	}
	
}
public class ProductModel {
	
	public static double[] pprice = new double[999];
	public static String[] pname = new String[999];
	public static int[] qty = new int[999];
	public static int getIndex = 0;
	public static double finalSubtotal = 0;
	public static double finalTax = 0;
	public static double finalTotal = 0;
	public static ProductMain mainInt = new ProductMain();
	
		public String pname(int barCode){
		   String[] productName = new String[99999];
		   productName[11240] = "Safegaurd";
		   productName[22012] = "TJ Hotdog Jumbo";
		   productName[32102] = "Colgate 360 Toothbrush 2s";
		   productName[44112] = "Green Cross Alcohol";
		   productName[10023] = "Mentos Candy";
		   
		   return productName[barCode];
	   }
	   public double price(int barCode) 
	   {
		   double[] productPrice = new double[99999];
		   productPrice[11240] = 15.75;
		   productPrice[22012] = 77.75;
		   productPrice[32102] = 99.50;
		   productPrice[44112] = 23.25;
		   productPrice[10023] = 14.05;
		   
		   return productPrice[barCode];
	   }
	   
	   public static void displayShoppingCart( int retry ){
			
			if(retry == 0){
				getIndex--;
			}
			
			finalSubtotal = 0;
			finalTotal = 0;
			finalTax = 0;
			System.out.println("\n===================================================================");
			System.out.println("Product Name \t Qty \t Price \t Subtotal\n");
			for(int x = 0; x <= getIndex;x++){
				double subtotal = pprice[x]*qty[x];
				finalSubtotal = finalSubtotal + subtotal;
				finalTax = finalSubtotal*0.12;
				finalTotal = finalSubtotal + finalTax;
				System.out.println(pname[x] + " \t " + qty[x] + " \t " + pprice[x] + " \t " + subtotal);
			}
			
			System.out.println("===================================================================");
			System.out.println("Subtotal: " + finalSubtotal);
			System.out.println("Tax: " + finalTax);
			System.out.println("Total: " + finalTotal);
			if(retry == 1){
				getIndex++;
				mainInt.main(null);
			}else{
				System.out.println("Thank you for Shopping.");
				System.out.println("Please Come Back Again.");
				System.exit(0);
			}
		}
	
}
import java.util.Scanner;

public class ProductMain extends ProductModel{
	
   public static void main(String[] args) {
	   Scanner input = new Scanner(System.in);
		System.out.print("Enter Product Bar Code:");
		
		int productCode = 0;
		int prodQuantity = 0;
		try{ 
			productCode = input.nextInt();
		}catch(Exception e){ 
			System.out.println("Processing: "); 
			displayShoppingCart(0); 
		} 
		System.out.print("Qty:");
		prodQuantity = input.nextInt();
		
		if(productCode > 99999){
			System.out.println("!Error: " + productCode + ". Too much digits. The program only accepts 5 digits"); 
			retryAgain();
		}else if(productCode < 0){
			System.out.println("!Error: " + productCode + " is not a valid barcode."); 
			retryAgain(); 
		}
		
		System.out.println();
		System.out.println("Product Bar Code:" + productCode);
		ProductController getProduct = new ProductController();
		String productName = getProduct.getPname(productCode);
		double productPrice = getProduct.getPrice(productCode);
		
		
		if(productName == null){
			System.out.println("Product Not Found");
			retryAgain();
		}else{
			pprice[getIndex] = productPrice;
			pname[getIndex] = productName;
			qty[getIndex] = prodQuantity;
			displayInfo(productName, productPrice);
			displayShoppingCart(1);
		}  
	   }
	   
	   static void displayInfo(String productName, double productPrice) {
			System.out.println("Product Name : " + productName);
			System.out.println("Product Price: $" + productPrice);
		}

		static void retryAgain(){
			Scanner input = new Scanner(System.in);
			System.out.print("Do you want to try again? [Y]Yes: ");
			String confirm = input.nextLine();
			if(confirm.equalsIgnoreCase("Y") || confirm.equalsIgnoreCase("Yes")){
				System.out.println();
				main(null);
			}else{
				System.out.println();
				System.out.println("Thank You");
				System.exit(0);
			}
		}
	}

My unfinished code for GUI as of now

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class GUI extends JFrame{
	JLabel error,total,quantity,barcode,product1,product2,product3,product4,product5;
	JTextField errorTF,quantityTF,barcodeTF,totalTF;
	JButton compute,exit,clear,sub;
	
	public GUI(){
		setTitle("Product Barcode");
		setSize(400,200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		product1 = new JLabel(" Safeguard [11240] = 15.75 php ",SwingConstants.CENTER);
		product2 = new JLabel(" TJ Hotdog Jumbo [22012] = 77.75 php ",SwingConstants.CENTER);
		product3 = new JLabel(" Colgate 360 Toothbrush 2s [32102] = 99.50 php ",SwingConstants.CENTER);
		product4 = new JLabel(" Green Cross Alchohol [44112] = 23.95 php ",SwingConstants.CENTER);
		product5 = new JLabel(" Menthos [10023] = 14.05 php ",SwingConstants.CENTER);
		barcode = new JLabel(" Barcode: ",SwingConstants.CENTER);
		quantity = new JLabel(" Quantity: ",SwingConstants.CENTER);
		total = new JLabel("Total: ",SwingConstants.CENTER);
		error = new JLabel("Error: ",SwingConstants.CENTER);
		
		
		barcodeTF = new JTextField(10);
		quantityTF = new JTextField(10);
		totalTF = new JTextField(10);
		errorTF = new JTextField(10);
		
		
		compute = new JButton("Compute");
		ButtonHandler calculatex = new ButtonHandler();
		compute.addActionListener(calculatex);
		exit = new JButton("End!");
		exitHandler exits = new exitHandler();
		exit.addActionListener(exits);
		clear = new JButton("Clear");
		clearHandler clears = new clearHandler();
		clear.addActionListener(clears);
		sub = new JButton("Submitted By: Michael Bern Gasendo");
		sub.setEnabled(false);
		
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(4,1));
		pane.add(product1);
		pane.add(product2);
		pane.add(product3);
		pane.add(product4);
		pane.add(product5);
		pane.add(barcode);
		pane.add(barcodeTF);
		pane.add(quantity);
		pane.add(quantityTF);
		pane.add(total);
		pane.add(totalTF);
		pane.add(compute);
		pane.add(exit);
		pane.add(clear);
		pane.add(sub);
		
		
		totalTF.setEditable(false);
		setVisible(true);
			
	}
	 class clearHandler implements ActionListener{
		 public void actionPerformed(ActionEvent e){
			 barcodeTF.setText("");
			 quantityTF.setText("");
			 totalTF.setText("");
			 
			 
			 
		 } 
		 }
	 class ButtonHandler implements ActionListener{
		 public void actionPerformed(ActionEvent e){
			
			 double[] pprice = new double[999];
			 String[] pname = new String[999];
			 int[] qty = new int[999];
			 int getIndex = 0;
			 double finalSubtotal = 0;
			 double finalTax = 0;
			 double finalTotal = 0;
			 
			
			   double[] productPrice = new double[99999];
			   productPrice[11240] = 15.75;
			   productPrice[22012] = 77.75;
			   productPrice[32102] = 99.50;
			   productPrice[44112] = 23.25;
			   productPrice[10023] = 14.05;
			   
			   	
				finalSubtotal = 0;
				finalTotal = 0;
				finalTax = 0;
				for(int x = 0; x <= getIndex;x++){
					double subtotal = pprice[x]*qty[x];
					finalSubtotal = finalSubtotal + subtotal;
					finalTax = finalSubtotal*0.12;
					finalTotal = finalSubtotal + finalTax;
					
					int productCode = 0;
					int prodQuantity = 0;
					
					productCode = Integer.parseInt(barcodeTF.getText());
					prodQuantity = Integer.parseInt(quantityTF.getText());
					
					
				
					
					if(productCode > 99999){
					errorTF.setText("Too much digits. The program only accepts 5 digits"); 
						
					}else if(productCode < 0){
						errorTF.setText("Too much digits. The program only accepts 5 digits"); 
					}		
				}
		 }
		 }
		 class subHandler implements ActionListener{
			 public void actionPerformed(ActionEvent e){
				 
				 
			 }
		 }
		
				 

		public static void main (String[]args){
			GUI x = new GUI();
		
		}
		 class exitHandler implements ActionListener{
			 public void actionPerformed(ActionEvent e){
				 System.exit(0);
				
	}
				
		}
		 }

Recommended Answers

All 2 Replies

the use of the program is to compute all product with corresponding barcodes,a user defined quantity and print the total in notepad or messagebox. Help pls!

What data you need from the user and what do you want to display to the user?
Can those things be done with some text fields and buttons?
Get rid of the Scanner and replace it with the above.

Do you have any specific questions?

commented: same results +1 +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.