Netbeans - Trying to get jCombobox selected item into another JFrame TextArea - java

Hello wonderful people!
I've relatively new to this java and I'm practicing by creating an online booking system with multiple JFrames (I've heard this could be an issue).
In short, I want to retrieve the value of this jComboBox and display it in a text area on a separate frame. The problem is: How do I undergo this?
private void filmSelecterActionPerformed(java.awt.event.ActionEvent evt) {
if (filmSelecter.getSelectedIndex() == 0)
continueButton.setEnabled(false);
else {
continueButton.setEnabled(true);
}
Second JFrame
private void jQty2ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void reviewButtonActionPerformed(java.awt.event.ActionEvent evt) {
// -------Review Order Button----
double Qty1 = Double.parseDouble(jQty1.getText());
double Qty2 = Double.parseDouble(jQty2.getText());
double Qty3 = Double.parseDouble(jQty3.getText());
double Qty4 = Double.parseDouble(jQty4.getText());
double total, subTotal, ticket1, ticket2, ticket3, ticket4;
String adultPrice = String.format("%.2f", adultprice);
subTotal1.setText(adultPrice);
String studentPrice = String.format("%.2f", studentprice);
subTotal2.setText(studentPrice);
String childPrice = String.format("%.2f", childprice);
subTotal3.setText(childPrice);
String seniorPrice = String.format("%.2f", seniorprice);
subTotal4.setText(seniorPrice);
ticket1 = Qty1 * adultprice;
ticket2 = Qty2 * studentprice;
ticket3 = Qty3 * childprice;
ticket4 = Qty4 * seniorprice;
//subTotal
String sub1 = String.format("£%.2f", ticket1);
subTotal1.setText(sub1);
String sub2 = String.format("£%.2f", ticket2);
subTotal2.setText(sub2);
String sub3 = String.format("£%.2f", ticket3);
subTotal3.setText(sub3);
String sub4 = String.format("£%.2f", ticket4);
subTotal4.setText(sub4);
subTotal = ticket1 + ticket2 + ticket3 + ticket4;
// Total ticket price
String subTotalAll = String.format("£%.2f", subTotal);
jTotalPrice.setText(subTotalAll);
// Receipt
String Title = "\tGreenwich Peninsula Cinema\n\n\n";
String Movie = "Movie Name: " + "\n";
//String MovieDay = "Movie Day" + filmSelecter.getSelectedItem() + "\n";
String MovieTime = "Movie Time: \n";
String Barrier = "=========================================" + "\n";
String Adult = "Adult:" + subTotal1.getText() + "\nNumber of Tickets: " + Qty1 + "\n";
String Student = "Student:" + subTotal2.getText() + "\nNumber of Tickets: " + Qty2 + "\n";
String Child = "Child:" + subTotal3.getText() + "\nNumber of Tickets: " + Qty3 + "\n";
String Senior = "Senior:" + subTotal4.getText() + "\nNumber of Tickets: " + Qty4 + "\n";
String Thanks = "\n\n\tEnjoy the film!";
String ShowReceipt = Barrier + Title + Movie + /*MovieDay +*/ MovieTime + Barrier + Adult + Student + Child + Senior + Thanks + "\n" + Barrier;
filmReceipt.append(ShowReceipt);
}
I hope this helps in anyway.

Related

Cant get this to run PAYROLL PROJECT java

package EmployeePayAppMRM;
import javax.swing.JOptionPane;
public class java
{
public static void main(String[] args)
{
String firstName;
String lastName;
String fullName;
String itemsSold;
String valueItems;
double basePay;
double totalPay;
double valueSold;
int numberSold;
final double ITEM_MIN = 10;
final int VALUE_MIN = 2500;
final int BONUS = 500;
firstName = JOptionPane.showInputDialog(" Enter employee's first name:");
lastName = JOptionPane.showInputDialog(" Enter employee's last name:");
fullName = JOptionPane.showInputDialog(" Enter base pay for " + firstName + " " + lastName + ":");
basePay = Double.parseDouble(fullName);
itemsSold = JOptionPane.showInputDialog(" Enter number of items sold for " + firstName + " " + lastName + ":");
totalPay = Double.parseDouble(fullName);
fullName = JOptionPane.showInputDialog(" Enter value of items sold for " + firstName + " " + lastName + ":");
valueSold = Double.parseDouble(fullName);
basePay = 1000;
if ( itemsSold > ITEM_MIN) //ERROR HERE (bad operand)
totalPay = (basePay + BONUS);
else if (valueSold >= VALUE_MIN)
totalPay = (basePay + BONUS);
else
totalPay = basePay;
System.out.println("this" + firstName + " " + lastName + " " + "will be paid" + totalPay "for selling" + itemsSold "items valued at $" + valueSold); //ERROR HERE
System.exit(0);
}
}
The first problem is about types, for the second you just miss some +
itemsSold is a String
ITEM_MIN is a double
Do like this, of parse it before
if(Double.parseDouble(itemsSold) > ITEM_MIN)
totalPay = (basePay + BONUS);
Finally, you don't need to define the variable and do the assignement later, just do it at the same time, once reoganisating all :
final double ITEM_MIN = 10;
final int VALUE_MIN = 2500;
final int BONUS = 500;
String firstName = JOptionPane.showInputDialog(" Enter employee's first name:");
String lastName = JOptionPane.showInputDialog(" Enter employee's last name:");
String fullName = JOptionPane.showInputDialog(" Enter base pay for " + firstName + " " + lastName + ":");
double itemsSold = Double.parseDouble(JOptionPane.showInputDialog(" Enter number of items sold for " + firstName + " " + lastName + ":"));
double valueSold = Double.parseDouble(JOptionPane.showInputDialog(" Enter value of items sold for " + firstName + " " + lastName + ":"));
double totalPay = 0;
double basePay = 1000;
if (itemsSold > ITEM_MIN) {
totalPay = (basePay + BONUS);
} else if (valueSold >= VALUE_MIN) {
totalPay = (basePay + BONUS);
} else {
totalPay = basePay;
}
System.out.println("this" + firstName + " " + lastName + " " + "will be paid" + totalPay + "for selling" + itemsSold + "items valued at $" + valueSold);

code works but it keeps giving me the error: InputMismatchException and

I'm trying to work on a java program that takes prices from a text file, allows people to enter in the quantity of what they want, and print it out into another text file.
sample code
import java.io.*;
import java.lang.Math;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.*;
// work on invoice
public class lab2_notes{
public static void main(String[] args)throws IOException {
Scanner fileIn = null;
try
{
// Attempt to open the file
// file should be in working directory
fileIn = new Scanner(new FileInputStream("prices.txt"));
}
catch (FileNotFoundException e)
{
// If the file could not be found, this code is executed
// and then the program exits
System.out.println("File not found.");
// Shut the entire operation down
System.exit(0);
}
//convert file items to strings and numbers
String food_one;
double price_one;
String food_two;
double price_two;
String food_three;
double price_three;
//give strings and numbers varibles
food_one = fileIn.nextLine();
//fileIn.nextLine();
price_one = fileIn.nextDouble();
fileIn.nextLine();
food_two = fileIn.nextLine();
//fileIn.nextLine();
price_two = fileIn.nextDouble();
//fileIn.nextLine();
food_three = fileIn.nextLine();
//fileIn.nextLine();
price_three = fileIn.nextDouble();
//give input varibles for user to enter in how much food they want
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = keyboard.nextLine( );
System.out.println("Enter your zip code: ");
int zip = keyboard.nextInt( );
System.out.println(food_one + " " + price_one);
int quanity_one = keyboard.nextInt( );
System.out.println(food_two + " " + price_two);
int quanity_two = keyboard.nextInt( );
System.out.println(food_two + " " + price_two);
int quanity_three = keyboard.nextInt( );
//use methods to work the code out
double pr_one = total_for_items(quanity_one, price_one);
double pr_two = total_for_items(quanity_two, price_two);
double pr_three = total_for_items(quanity_three, price_three);
double sub_total = (pr_one + pr_two + pr_three);
double taxation = tax(sub_total);
double final_total = grand_total(sub_total, taxation);
String invoice = Invoice(name, zip);
//convert to deciminal class
DecimalFormat df = new DecimalFormat("$#,###,##.##");
String con_sub = df.format(sub_total);
String con_tax = df.format(taxation);
String con_final = df.format(final_total);
String con_one = df.format(pr_one);
String con_two = df.format(pr_two);
String con_three = df.format(pr_three);
//print out recept on screen
System.out.println("Invoice Number: " + invoice);
System.out.println("Item Quantity Price Total");
System.out.println("======================================");
System.out.printf(food_one + " " + con_one + " " + price_one + " " + pr_one);
System.out.println(food_two + " " + con_two + " " + price_two + " " + pr_two);
System.out.println(food_three + " " + con_three + " " + price_three + " " + pr_three);
System.out.println("======================================");
System.out.println("Subtotal: " + con_sub);
System.out.println("6.25% sales tax: " + con_tax);
System.out.println("Total: " + con_final);
String a = "Invoice Number: " + invoice + "\n";
String b = "Item Quantity Price Total" + "\n";
String c = food_one + " " + con_one + " " + price_one + " " + pr_one + "\n";
String d = food_two + " " + con_two + " " + price_two + " " + pr_two + "\n";
String e = food_three + " " + con_three + " " + price_three + " " + pr_three + "\n";
String f = "======================================";
String g = "Subtotal: " + con_sub + "\n";
String h = "Total: " + con_final + "\n";
//print recept on a self-created text file
PrintWriter recept = new PrintWriter("recept.txt", "UTF-8");
recept.println(a + b + c + d + e + f + g + h);
recept.println();
recept.close();
fileIn.close();
}
public static double total_for_items(double qone, double price){
double total_one = qone * price;
return total_one;
}
public static double tax(double total){
double tax_amt = total * Math.pow(6.25, -2);
return tax_amt;
}
public static double grand_total(double total, double tax){
double g_total = total + tax;
return g_total;
}
public static String Invoice(String name, int zip_code){
String part_one = name;
int part_two = zip_code;
Scanner pileIn = null;
try
{
// Attempt to open the file
// file should be in working directory
pileIn = new Scanner(new FileInputStream(part_one));
}
catch (FileNotFoundException e)
{
// If the file could not be found, this code is executed
// and then the program exits
System.out.println("File not found.");
// Shut the entire operation down
System.exit(0);
}
String Fname;
String Lname;
Fname = pileIn.nextLine();
pileIn.nextLine();
Lname = pileIn.nextLine();
//first part of name
String fnone = Fname.valueOf(Fname.charAt(1));
String fntwo = Fname.valueOf(Fname.charAt(2));
//second part of name
String lnone = Lname.valueOf(Lname.charAt(1));
String lntwo = Lname.valueOf(Lname.charAt(1));
//convert letters to uppercase
String con_fone_let = fnone.toUpperCase();
String con_ftwo_let = fntwo.toUpperCase();
String con_lnone_let = lnone.toUpperCase();
String con_lntwo_let = lntwo.toUpperCase();
String invoice_result = con_fone_let + con_ftwo_let + con_lnone_let + con_lntwo_let + zip_code;
return invoice_result;
}
}
However every time I start it, this error message pops up:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at lab2_notes.main(lab2_notes.java:46)
I'm new to coding with java and have been screwing with line 46 for hour now trying to figure it out.
what am I doing wrong?
Thanks, HG
Take input food_one to price three like this:
food_one = fileIn.nextLine();
price_one = fileIn.nextDouble();
fileIn.nextLine();
food_two = fileIn.nextLine();
price_two = fileIn.nextDouble();
fileIn.nextLine();
food_three = fileIn.nextLine();
price_three = fileIn.nextDouble();
It should work.
I changed the format in which you are storing the food and price (in prices.txt) to the following (with food and price separated by a delimiter, in this case a colon)
Pizz:20.10
Burger:10.30
Coffee:5.99
I changed the code to the following and now it is working. Hope this helps
// give strings and numbers varibles
String[] foodPrice = fileIn.nextLine().split(":");
food_one = foodPrice[0];
price_one = Double.parseDouble(foodPrice[1]);
foodPrice = fileIn.nextLine().split(":");
food_two = foodPrice[0];
price_two = Double.parseDouble(foodPrice[1]);
foodPrice = fileIn.nextLine().split(":");
food_three = foodPrice[0];
// fileIn.nextLine();
price_three = Double.parseDouble(foodPrice[1]);
Forgot to comment out fileIn.nextLine(); around like 46
your trying to put the name of a food into a nextDouble()
//give strings and numbers varibles
food_one = fileIn.nextLine();
//fileIn.nextLine();
price_one = fileIn.nextDouble();
fileIn.nextLine();
food_two = fileIn.nextLine();
//fileIn.nextLine();
price_two = fileIn.nextDouble();
//fileIn.nextLine();
food_three = fileIn.nextLine();
//fileIn.nextLine();
price_three = fileIn.nextDouble();
Should be:
//give strings and numbers varibles
food_one = fileIn.nextLine();
//fileIn.nextLine();
price_one = fileIn.nextDouble();
//fileIn.nextLine();
food_two = fileIn.nextLine();
//fileIn.nextLine();
price_two = fileIn.nextDouble();
//fileIn.nextLine();
food_three = fileIn.nextLine();
//fileIn.nextLine();
price_three = fileIn.nextDouble();

I'm having problems accessing the total price I initialized inside my if else statements

How can I get the sum of the three Total prices inside the if else statements for red velvet, buttercream and vanilla? And display it in allTextArea text area?
Here's my code:
double redVelvetPrice = 4.00;
double buttercreamPrice = 7.00;
double vanillaPrice = 3.00;
double firstHalf = 6.00;
double firstDozen = 12.00;
double firstBox = 25;
double secondHalf = 6.00;
double secondDozen = 12.00;
double secondBox = 25;
double thirdHalf = 6.00;
double thirdDozen = 12.00;
double thirdBox = 25;
String s1 = "";
String s2 = "The flavors you selected is/are: \n";
String s3 = "";
String s4 = "";
String s5 = "";
String totalPrice = "";
if (redVelvetCB.isSelected()){
s1=s1+ " "+ redVelvetCB.getText() + '\n';
if (firstHalfRB.isSelected()){
s3 ="A " + firstHalfRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstHalf + '\n' + '\n';
}
else if (firstDozenRB.isSelected()){
s3 =firstDozenRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstDozen + '\n' + '\n';
}
else if (firstBoxRB.isSelected()){
s3 =firstBoxRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstBox + '\n' + '\n';
}
}
if (buttercreamCB.isSelected()){
s1=s1+ " "+ buttercreamCB.getText() + '\n';
if (secondHalfRB.isSelected()){
s4 ="A " + secondHalfRB.getText() + " of Buttercream Cupcakes\n = $" + buttercreamPrice * secondHalf + '\n' + '\n';
}
else if (secondDozenRB.isSelected()){
s4 =secondDozenRB.getText() + " of Buttercream Cupcakes\n = $" + buttercreamPrice * secondDozen + '\n' + '\n';
}
else if (secondBoxRB.isSelected()){
s4 =secondBoxRB.getText() + " of Buttercream Cupcakes\n = $" + buttercreamPrice * secondBox + '\n' + '\n';
}
}
if (vanillaCB.isSelected()){
s1=s1+ " "+ vanillaCB.getText() + '\n';
if (thirdHalfRB.isSelected()){
s5 ="A " + thirdHalfRB.getText() + " of Vanilla Cupcakes\n = $" + vanillaPrice * thirdHalf + '\n' + '\n';
}
else if (thirdDozenRB.isSelected()){
s5 =thirdDozenRB.getText() + " of Vanilla Cupcakes\n = $" + vanillaPrice * thirdDozen + '\n' + '\n';
}
else if (thirdBoxRB.isSelected()){
s5 =thirdBoxRB.getText() + " of Vanilla Cupcakes\n = $" + vanillaPrice * thirdBox + '\n' + '\n';
}
allTextArea.setText(greeting+s2+"\n"+s1+"\n"+s3+s4+s5+"\nTotal Price is "+totalPrice+".\n"+deliv+payment+bye);
I tried to set the total Price equal to redVelvetPrice + butercreamPrice + vanillaPrice, but the answer would be 14.0 which is the sum of the variables I initialized outside the if else statements (4.00,7.00,3.00),
I want it to be the sum of the prices of the three after selecting the quantity of each flavor. Help me.
You should use an other data type for the totalPrice. It's easier to calculate with numbers than strings.
double totalPrice = 0f;
After that you add up all the different combinations in your if/else statements
if (firstHalfRB.isSelected()){
s3 = "A " + firstHalfRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstHalf + "\n\n";
totalPrice += (redVelvetPrice * firstHalf);
}
else if (firstDozenRB.isSelected()){
s3 = firstDozenRB.getText() + " of Red Velvet Cupcakes\n = $" + redVelvetPrice * firstDozen + "\n\n";
totalPrice += (redVelvetPrice * firstDozen);
}
When printing the total in the TextArea you may want to format it so it only has 2 positions after decimal point:
String.format("%.2f", totalPrice);

Java project returning null values

Hi I am new to Java programming. Why are my values returning null after I enter them on the input dialog. I have two classes, one called VehicleApp and the other called VehicleFactory. Help would be appreciated. Thanks.
Full code VehicleApp.java
package romprojectname;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class VehicleApp{
public static void main(String[] args) {
String firstname = JOptionPane.showInputDialog("Enter your first name");
String lastname = JOptionPane.showInputDialog("Enter your last name");
long phone = Long.parseLong(JOptionPane.showInputDialog("Enter your phone"));
int nbrVehicles = Integer.parseInt(JOptionPane.showInputDialog("Enter number of vehicles"));
int nbrTanks = Integer.parseInt(JOptionPane.showInputDialog("Enter number of tanks"));
VehicleFactory vehicleObject = new VehicleFactory();
vehicleObject.getSummary();
vehicleObject.HayloFactory(firstname, lastname, phone, nbrVehicles, nbrTanks);
vehicleObject.calcFuelTankCost();
vehicleObject.calcManufacturingCost();
vehicleObject.calcSubtotal();
vehicleObject.calcTax();
vehicleObject.calcTotal();
}
}
Full code VehicleFactory.java
package romprojectname;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class VehicleFactory{
private String firstname;
private String lastname;
private Long phone;
private int nbrVehicles =0;
private int nbrTanks =0;
private double manufactureCost =0;
private double fuelTankCost =0;
private double subtotal =0;
private double tax =0;
private double total = 0;
private final double VEHICLE_PRICE = 500.19;
private final double FUELCELL_PRICE = 2.15;
private final int CELLS_PER_TANK = 12;
private final double taxrate = 7.25 / 100 ;
public void HayloFactory(String firstname, String lastname, Long phone, int nbrVehicles, int nbrTanks){
this.firstname = firstname;
this.lastname = lastname;
this.phone = phone;
this.nbrVehicles = nbrVehicles;
this.nbrTanks = nbrTanks;
}
public void calcManufacturingCost(){
double manufactureCost = nbrVehicles * VEHICLE_PRICE;
}
public void calcFuelTankCost(){
double fuelTankCost = nbrVehicles * nbrTanks * CELLS_PER_TANK * FUELCELL_PRICE;
}
public void calcSubtotal(){
double subtotal = manufactureCost + fuelTankCost;
}
public void calcTax(){
double tax = subtotal * taxrate;
}
public void calcTotal(){
double total = subtotal + tax;
}
NumberFormat cf = NumberFormat.getCurrencyInstance();
public void getSummary(){
String summary = "WELCOME TO HAYLO MANUFACTURING" + "\n" + "\n";
summary += "Customer Name: " + firstname + " " + lastname + "\n";
summary += "Customer Phone: " + phone + "\n";
summary += "Number of Vehicles: " + nbrVehicles + "\n";
summary += "Number of Tanks: " + nbrTanks + "\n";
summary += "Vehicle Cost ($500.19 / vehicle): " + cf.format(manufactureCost) + "\n";
summary += "Tanks Cost ($2.15 / fuel cell): " + cf.format(fuelTankCost) + "\n";
summary += "Subtotal: " + cf.format(subtotal) + "\n";
summary += "Tax (7.25%): " + cf.format(tax) + "\n";
summary += "Total: " + cf.format(total) + "\n";
//display the summary
JOptionPane.showMessageDialog(null, summary);
}
}
Problem (code taken from VehicleFactory.java)
All of the summaries such as customer name are returning null values and all the costs and totals are $0.00.
public void getSummary(){
String summary = "WELCOME TO HAYLO MANUFACTURING" + "\n" + "\n";
summary += "Customer Name: " + firstname + " " + lastname + "\n";
summary += "Customer Phone: " + phone + "\n";
summary += "Number of Vehicles: " + nbrVehicles + "\n";
summary += "Number of Tanks: " + nbrTanks + "\n";
summary += "Vehicle Cost ($500.19 / vehicle): " + cf.format(manufactureCost) + "\n";
summary += "Tanks Cost ($2.15 / fuel cell): " + cf.format(fuelTankCost) + "\n";
summary += "Subtotal: " + cf.format(subtotal) + "\n";
summary += "Tax (7.25%): " + cf.format(tax) + "\n";
summary += "Total: " + cf.format(total) + "\n";
//display the summary
JOptionPane.showMessageDialog(null, summary);
You haven't really described the problem, but I suspect this is the cause:
VehicleFactory vehicleObject = new VehicleFactory();
vehicleObject.getSummary();
vehicleObject.HayloFactory(firstname, lastname, phone, nbrVehicles, nbrTanks);
You're calling getSummary before you call HayloFactory - so it's trying to display the values in the object before you've set them to useful values.
Additionally, all your calcXyz methods are introducing new local variables, like this:
public void calcTotal(){
double total = subtotal + tax;
}
Instead, they should be setting the field values:
public void calcTotal(){
total = subtotal + tax;
}
If you change all of your calculation methods appropriately, then move the getSummary() call to the very end, it will work. (It's not quite how I'd have written the code, but that's a different matter.)
The variables defined insides your methods have local scope only for example
double manufactureCost = nbrVehicles * VEHICLE_PRICE; it actually hides your class variable manufactureCost .. they should be instead used as
manufactureCost = nbrVehicles * VEHICLE_PRICE;
This way you can actually set the class variable which in turn displayed inside your getSummary method

Connect Login to the Main Class (Object Oriented)

Here's my Login class:
package mangInasal;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MILogin extends JFrame {
private JLabel lblUsername, lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnLogin, btnCancel;
public MILogin() {
super("Login");
setLayout(new FlowLayout());
lblUsername = new JLabel("Username: ");
add(lblUsername);
txtUsername = new JTextField(10);
add(txtUsername);
lblPassword = new JLabel("Password: ");
add(lblPassword);
txtPassword = new JPasswordField(10);
add(txtPassword);
btnLogin = new JButton("Login");
add(btnLogin);
btnCancel = new JButton("Cancel");
add(btnCancel);
ActionListener listener = new ButtonListener();
btnLogin.addActionListener(listener);
btnCancel.addActionListener(listener);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
String username = txtUsername.getText().toUpperCase();
String pass = txtPassword.getText();
if (source == btnLogin) {
if (username.equals("TIPQC") && (pass.equals("ABET"))) {
String okay = "defimages/loginsuccess.gif";
java.net.URL imgURL = getClass().getClassLoader().getResource(okay);
ImageIcon okpic = new ImageIcon(imgURL);
JOptionPane.showMessageDialog(null, "WELCOME", "Correct Input", JOptionPane.PLAIN_MESSAGE, okpic);
dispose();
mangInasaldef manginasaldef = new mangInasaldef();
manginasaldef.run();
} else {
String okay = "defimages/loginfail.gif";
java.net.URL imgURL = getClass().getClassLoader().getResource(okay);
ImageIcon failpic = new ImageIcon(imgURL);
JOptionPane.showMessageDialog(null, "Incorrect Username or Password", "Message Dialog", JOptionPane.PLAIN_MESSAGE, failpic);
}
}
if (source == btnCancel) {
System.exit(1);
}
}
}
public void run() {
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
}
public static void main(String[] args) {
MILogin milogin = new MILogin();
milogin.run();
}
}
And here's my Main class:
package mangInasal;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class mangInasaldef extends JFrame{ //class name
private static String username = "";
private static String customername = "";
private static String dineOut = "";
private String order = "";
private String order1 = "";
private String order2 = "";
private String order3 = "";
private String order4 = "";
private String order5 = "";
private String order6 = "";
private String order7 = "";
private String order8 = "";
private String order9 = "";
private String order10 = "";
private String order11 = "";
private String order12 = "";
private String order13 = "";
private String order14 = "";
private String order15 = "";
private String order16 = "";
private String order17 = "";
private int total = 0;
private int totalSpicy = 0;
private int total1 = 0;
private int total2= 0;
private int total3 = 0;
private int total4 = 0;
private int total5 = 0;
private int total6 = 0;
private int total7 = 0;
private int total8 = 0;
private int total9 = 0;
private int total10 = 0;
private int total11 = 0;
private int total12 = 0;
private int total13 = 0;
private int total14 = 0;
private int total15 = 0;
private int total16 = 0;
private int total17 = 0;
private int totalOrder = total + totalSpicy + total1 + total2 + total3 + total4 + total5 + total6 + total7 + total8 + total9 + total10 + total11 + total12 + total13 + total14 + total15;
private double vatCollected = totalOrder * 0.12;
private boolean exact = true; // a boolean variable that is declared private in order to be accessed by different methods
private double change, cashTendered;
private static int cashAmount;
private static Scanner inp = new Scanner(System.in); // private static declared Scanner which allows the user to input characters or strings by different methods
private JLabel lblUsername, lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnLogin, btnCancel;
public mangInasaldef() {
boolean repeatLoop = true;
System.out.print("\nInput Customer Name: ");
String customerName = inp.nextLine();
customername = customerName;
System.out.print("\nInput Cashier Name: ");
String user = inp.nextLine();
username = user;
do{
System.out.print("\nInput either Dine In or Take Out: ");
String dInDOut = inp.nextLine();
dineOut = dInDOut;
if (dineOut.equals("Dine In") || dineOut.equals("Take Out")){
System.out.print("");
repeatLoop = false;
}
else{
JOptionPane.showMessageDialog(null, "Try again! Please input Dine In or Take Out only!","Error", JOptionPane.ERROR_MESSAGE);
repeatLoop = true;
System.out.print ("\f");
}
}while(repeatLoop);
System.out.print("\n\n\t\tCashier: " +username);
System.out.print(" "+dineOut);
System.out.print("\n\t\tCustomer Name: " +customername);
EventQueue.invokeLater(new Runnable() {
#Override
public void run()
{
new mangInasaldef();
}
});
}
public mangInasaldef() {
JFrame frame = new JFrame("Mang Inasal Ordering System");
JPanel panel = new JPanel();
String choices[] = {"Paborito Meal 1 Paa","Paborito Meal 1.5 (Spicy Paa with Thigh part)","Paborito Meal 2 (Pecho)","Paborito Meal 3 (Pork Barbeque 4 pcs)","Paborito Meal 4 (Bangus Sisig)","Paborito Meal 5 (Pork Sisig)","Paborito Meal 6 (Bangus Inihaw)","Sulit Meal 1 (Paa)","Sulit Meal 2 (Pork Barbeque 2 pcs)","Pancit Bihon","Dinuguan at Puto","Ensaladang Talong","Softdrinks","Iced Tea","HaloHalo","Leche Flan","Turon Split","Print Receipt"};
JComboBox combo = new JComboBox(choices);
combo.setBackground(Color.black);
combo.setForeground(Color.yellow);
panel.add(combo);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,100);
frame.setVisible(true);
combo.addItemListener(new ItemListener () {
#Override
public void itemStateChanged(ItemEvent e)
{
String item = (String)e.getItem();
if (e.getStateChange () == ItemEvent.SELECTED)
{
System.out.print("\nYou chose " +item);
if (item.equals("Paborito Meal 1 Paa"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of PM1");
Integer bilang= Integer.valueOf((String)val);
total = bilang * 99;
order = " " + bilang + " " + " PM1 PAA W/ RICE" +" "+ total + ".00V" + "\n"; //stores the values of the variables bilang and total in the variable order and prints them
}
if (item.equals("Paborito Meal 1.5 (Spicy Paa with Thigh part)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of PM1.5");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
totalSpicy = bilang * 99;
order1 = " " + bilang + " " + " PM1.5 PAA" +" "+ totalSpicy + ".00V" + "\n"; //stores the values of the variables bilang and total in the variable order and prints them
}
if (item.equals("Paborito Meal 2 (Pecho)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of PM2");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total1 = bilang * 99;
order2 = " " + bilang + " " + " PM2 PECHO W/ RICE" +" "+ total1 + ".00V" + "\n";
}
if (item.equals("Paborito Meal 3 (Pork Barbeque 4 pcs)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of PM3");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total2 = bilang * 99;
order3 = " " + bilang + " " + " PM3 PORK BBQ W/ RICE " +" "+ total2 + ".00V" + "\n";
}
if (item.equals("Paborito Meal 4 (Bangus Sisig)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of PM4");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total3 = bilang * 99;
order4 = " " + bilang + " " + " PM4 BANGUS SISIG " +" "+ total3 + ".00V" + "\n";
}
if (item.equals("Paborito Meal 5 (Pork Sisig)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of PM5");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total4 = bilang * 99;
order5 = " " + bilang + " " + " PM5 PORK SISIG " +" "+ total4 + ".00V" + "\n";
}
if (item.equals("Paborito Meal 6 (Bangus Inihaw)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of PM6");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total5 = bilang * 99;
order6 = " " + bilang + " " + " PM6 BANGUS INIHAW " +" "+ total5 + ".00V" + "\n";
}
if (item.equals("Sulit Meal 1 (Paa)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of SM1");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total6 = bilang * 59;
order7 = " " + bilang + " " + " SM1 PAA " +" "+ total6 + ".00V" + "\n";
}
if (item.equals("Sulit Meal 2 (Pork Barbeque 2 pcs)"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of SM2");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total7 = bilang * 59;
order8 = " " + bilang + " " + " SM2 PORK BBQ 2 " +" "+ total7 + ".00V" + "\n";
}
if (item.equals("Pancit Bihon"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of Pancit Bihon");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total8 = bilang * 49;
order9 = " " + bilang + " " + " Pancit Bihon " +" "+ total8 + ".00V" + "\n";
}
if (item.equals("Dinuguan at Puto"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of Dinuguan");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total9 = bilang * 49;
order10 = " " + bilang + " " + " Dinuguan at Puto " +" "+ total9 + ".00V" + "\n";
}
if (item.equals("Ensaladang Talong"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of Ensaladang Talong");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total10 = bilang * 29;
order12 = " " + bilang + " " + " Ensaladang Talong " +" "+ total10 + ".00V" + "\n";
}
if (item.equals("Softdrinks"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of Softdrinks");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total11 = bilang * 25;
order13 = " " + bilang + " " + " Softdrinks " +" "+ total11 + ".00V" + "\n";
}
if (item.equals("Iced Tea"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of Iced Tea");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total12 = bilang * 25;
order14 = " " + bilang + " " + " Iced Tea " +" "+ total12 + ".00V" + "\n";
}
if (item.equals("HaloHalo"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of HaloHalo");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total13 = bilang * 49;
order15 = " " + bilang + " " + " HaloHalo " +" "+ total13 + ".00V" + "\n";
}
if (item.equals("Leche Flan"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of Leche Flan");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total14 = bilang * 29;
order16 = " " + bilang + " " + " Leche Flan " +" "+ total14 + ".00V" + "\n";
}
if (item.equals("Turon Split"))
{
Object val = JOptionPane.showInputDialog("Please input quantity of Turon Split");
System.out.print(val);
Integer bilang= Integer.valueOf((String)val);
total15 = bilang * 39;
order17 = " " + bilang + " " + " Turon Split " +" "+ total15 + ".00V";
}
if (item.equals("Print Receipt")) // if Print Receipt was selected it will go to the method computeReceipt
{
System.out.print("\f"); // clears the screen
computeReceipt();
}
}
}
});
}
public void computeReceipt() {
totalOrder = total + totalSpicy + total1 + total2 + total3 + total4 + total5 + total6 + total7 + total8 + total9 + total10 + total11 + total12 + total13 + total14 + total15;
vatCollected = totalOrder * 0.12;
exact = true;
do{
System.out.print("\n\n\tYour total bill is: "+totalOrder);
exact = false;
Object cashMo = JOptionPane.showInputDialog("Please input here your cash");
System.out.println(cashMo);
cashAmount = Integer.valueOf((String)cashMo);
exact = false;
change = cashAmount - totalOrder;
if (cashAmount < totalOrder) {
JOptionPane.showMessageDialog(null, "Try again. Please enter sufficient money to proceed.","Invalid Input", JOptionPane.ERROR_MESSAGE);
exact = true;
}
}while(exact);
System.out.print("\f");
etoPoeReceipt();
}
public void etoPoeReceipt() {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
Date date = new Date();
System.out.print("\n MANG INASAL");
System.out.print("\n BLUMENTRITT BRANCH");
System.out.print("\n #1631-1633 BLUMENTRITT ST.,");
System.out.print("\n STA CRUZ. MANILA 0000");
System.out.print("\n (932) 885-5844");
System.out.print("\n Operated by: R L YU");
System.out.print("\n TIN 202-161-017-000 VAT");
System.out.print("\n ACC. NO.: 050-204079836-000019");
System.out.print("\n Tel. #: (02)493-6801");
System.out.print("\n\n Cashier: " +username);
System.out.print(" STATION: 2");
System.out.print("\n ---------------------------------------------");
System.out.print("\n O.R #: 84486");
System.out.print(" "+dineOut);
System.out.print("\n Customer Name: " +customername);
System.out.print(" 24");
System.out.print("\n ---------------------------------------------");
System.out.print("\n >>SETTLED<<\n");
System.out.print(""+order );
System.out.print(""+order1 );
System.out.print(""+order2 );
System.out.print(""+order3 );
System.out.print(""+order4 );
System.out.print(""+order5 );
System.out.print(""+order6 );
System.out.print(""+order7 );
System.out.print(""+order8 );
System.out.print(""+order9 );
System.out.print(""+order10 );
System.out.print(""+order11 );
System.out.print(""+order12 );
System.out.print(""+order13 );
System.out.print(""+order14 );
System.out.print(""+order15 );
System.out.print(""+order16 );
System.out.print(""+order17 );
System.out.print("\n\n SUB TOTAL: "+totalOrder+ ".00");
System.out.print("\n DELIVERY VAT: 0.00");
System.out.print("\n ======");
System.out.print("\n AMOUNT DUE: "+totalOrder+ ".00\n\n");
System.out.printf(" VAT COLLECTED %.2f",vatCollected); // declares that the variable should be in two decimal places. % is called format specifier and f is the converter
System.out.println("");
System.out.print(" CASH Tendered: "+cashAmount+ ".00");
System.out.print("\n ======");
System.out.printf("\n CHANGE: %.2f",change);
System.out.print("\n >>Ticket #: 62<<");
System.out.print("\n Created: ");
System.out.print(dateFormat.format(date));
System.out.print("\n SETTLED: ");
System.out.print(dateFormat.format(date));
System.out.print("\n\n *********************************************");
System.out.print("\n THIS SERVES AS AN OFFICIAL RECEIPT.");
System.out.print("\n\n For Feedback: TEXT MIO467(Comments/ Suggest");
System.out.print("\n ions) and SEND to 0917-5941111 or CALL US");
System.out.print("\n at 0917-5596258");
System.out.print("\n Email: feedback#manginasal.com");
System.out.print("\n\n THANK YOU FOR DINING WITH US!");
System.out.print("\n\n *********************************************");
System.out.print("\n S/N: 120416ASL03/1105-6105-9230");
System.out.print("\n DT S/N: 41-L6971 (P0S1)");
System.out.print("\n PERMIT NO: 0412-031-125295-000");
System.out.print("\n MIN: 120276752");
}
}
After the login class was successfully accessed, it does not continue to the main method. I don't know what codes are needed to manipulate to connect the login class to the main class. Thanks for those who will help! :)
Remove the main method from you login class:
public static void main(String[] args) {
MILogin milogin = new MILogin();
milogin.run();
}
Then instance the MILogin class from your Main Class:
MILogin milogin = new MILogin();
milogin.run();

Categories