Notes
Notes - notes.io |
import javax.swing.JOptionPane;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
/**
*
* @author mahdi
*/
public class DashBoard extends javax.swing.JFrame {
/**
* Creates new form DashBoard
*/
public DashBoard() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 48)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Dashboard");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 10, -1, -1));
getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 70, 1354, 10));
jButton1.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/addMedicine.png"))); // NOI18N
jButton1.setText("Add Medicine");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 180, 390, -1));
jButton2.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/viewUser.png"))); // NOI18N
jButton2.setText("View Medicine");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 330, 390, -1));
jButton3.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/updateUser.png"))); // NOI18N
jButton3.setText("Update Medicine");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(720, 180, 390, -1));
jButton4.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/sellMedicine.png"))); // NOI18N
jButton4.setText("Sell Medicine");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(720, 330, 390, -1));
jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/close.png"))); // NOI18N
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(1330, 10, 30, -1));
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/adminDashboardBackground.png"))); // NOI18N
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new ViewMedicine().setVisible(true);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
AddMedicine addMedicineWindow = new AddMedicine();
// Make the AddMedicine window visible
addMedicineWindow.setVisible(true);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int a = JOptionPane.showConfirmDialog(null, "Do you want to close the Application?", "Select", JOptionPane.YES_NO_OPTION);
if (a == 0) {
System.exit(0);
}
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new SellMedicine().setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DashBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DashBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DashBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DashBoard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DashBoard().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JSeparator jSeparator1;
// End of variables declaration
}
AddMedicine=========================================================
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
/**
*
* @author mahdi
*/
import javax.swing.*;
import java.io.*;
import java.util.Map;
public class AddMedicine extends javax.swing.JFrame {
public AddMedicine() {
initComponents();
setLocationRelativeTo(null);
loadFromFile(); // Load existing medicines on startup
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txtPurchaseRate = new javax.swing.JTextField();
txtPricePerUnit = new javax.swing.JTextField();
txtQuantity = new javax.swing.JTextField();
txtProductName = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Add Product");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 10, -1, -1));
jLabel2.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Product Name: ");
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 120, -1, -1));
jLabel3.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("Purchase Rate: ");
getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 180, 106, 22));
jLabel4.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("Price Per Unit: ");
getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 250, 106, -1));
jLabel5.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("Quantity: ");
getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 310, 106, -1));
getContentPane().add(txtPurchaseRate, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 180, 328, -1));
txtPricePerUnit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtPricePerUnitActionPerformed(evt);
}
});
getContentPane().add(txtPricePerUnit, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 250, 328, -1));
getContentPane().add(txtQuantity, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 310, 328, -1));
txtProductName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtProductNameActionPerformed(evt);
}
});
getContentPane().add(txtProductName, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 120, 328, -1));
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/close.png"))); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(1310, 10, 30, -1));
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/save.png"))); // NOI18N
jButton2.setText("Save");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 370, -1, 20));
jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/adminDashboardBackground.png"))); // NOI18N
getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1370, -1));
pack();
}// </editor-fold>
private void txtPricePerUnitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int a = JOptionPane.showConfirmDialog(null, "Do you want to close this window?", "Select", JOptionPane.YES_NO_OPTION);
if (a == JOptionPane.YES_OPTION) {
dispose();
}
}
private void txtProductNameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
saveButtonActionPerformed(evt);
}
/**
* @param args the command line arguments
*/
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
String productName = txtProductName.getText();
double purchaseRate = Double.parseDouble(txtPurchaseRate.getText());
double pricePerUnit = Double.parseDouble(txtPricePerUnit.getText());
int quantity = Integer.parseInt(txtQuantity.getText());
// Add medicine to MedicineManager
MedicineManager.addMedicine(productName, purchaseRate, pricePerUnit, quantity);
// Display confirmation message
JOptionPane.showMessageDialog(this, "Medicine information saved successfully!");
// Save to file (update MedicineManager's file handling directly)
MedicineManager.saveToFile();
// Clear input fields
clearFields();
}
private void clearFields() {
txtProductName.setText("");
txtPurchaseRate.setText("");
txtPricePerUnit.setText("");
txtQuantity.setText("");
}
private void loadFromFile() {
// Load medicines from file into MedicineManager directly
MedicineManager.loadFromFile();
}
public static void main(String args[]) {
// Your main method for launching the form
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AddMedicine().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField txtPricePerUnit;
private javax.swing.JTextField txtProductName;
private javax.swing.JTextField txtPurchaseRate;
private javax.swing.JTextField txtQuantity;
// End of variables declaration
}
Medicine=============================================================
import java.io.Serializable;
public class Medicine implements Serializable {
private static final long serialVersionUID = 1L;
private String productName;
private double purchaseRate;
private double pricePerUnit;
private int quantity;
public Medicine(String productName, double purchaseRate, double pricePerUnit, int quantity) {
this.productName = productName;
this.purchaseRate = purchaseRate;
this.pricePerUnit = pricePerUnit;
this.quantity = quantity;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getPurchaseRate() {
return purchaseRate;
}
public void setPurchaseRate(double purchaseRate) {
this.purchaseRate = purchaseRate;
}
public double getPricePerUnit() {
return pricePerUnit;
}
public void setPricePerUnit(double pricePerUnit) {
this.pricePerUnit = pricePerUnit;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "Medicine{" +
"productName='" + productName + ''' +
", purchaseRate=" + purchaseRate +
", pricePerUnit=" + pricePerUnit +
", quantity=" + quantity +
'}';
}
}
medicine manager=====================================================
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class MedicineManager {
private static final String FILENAME = "medicines.dat"; // File name for serialization
private static Map<String, Medicine> medicines = new HashMap<>();
public static void addMedicine(String productName, double purchaseRate, double pricePerUnit, int quantity) {
if (medicines.containsKey(productName)) {
// If medicine already exists, update its quantity
Medicine existingMedicine = medicines.get(productName);
existingMedicine.setQuantity(existingMedicine.getQuantity() + quantity);
} else {
// If medicine doesn't exist, create and add new medicine
Medicine newMedicine = new Medicine(productName, purchaseRate, pricePerUnit, quantity);
medicines.put(productName, newMedicine);
}
}
public static Medicine getMedicine(String productName) {
return medicines.get(productName);
}
public static Map<String, Medicine> getAllMedicines() {
return medicines;
}
public static void sellMedicine(String productName, int quantity) {
Medicine medicine = medicines.get(productName);
if (medicine != null) {
int currentQuantity = medicine.getQuantity();
if (currentQuantity >= quantity) {
medicine.setQuantity(currentQuantity - quantity);
System.out.println(quantity + " units of " + productName + " sold successfully.");
} else {
System.out.println("Not enough quantity available for " + productName + ".");
}
} else {
System.out.println("Medicine " + productName + " not found.");
}
}
public static void saveToFile() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILENAME))) {
oos.writeObject(medicines);
System.out.println("Medicines saved to file successfully.");
} catch (IOException e) {
System.out.println("Error saving medicines to file: " + e.getMessage());
}
}
@SuppressWarnings("unchecked")
public static void loadFromFile() {
File file = new File(FILENAME);
if (!file.exists()) {
System.out.println("No existing data to load.");
return;
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
Object obj = ois.readObject();
if (obj instanceof Map) {
// Update medicines with the loaded data
medicines = (Map<String, Medicine>) obj;
System.out.println("Medicines loaded from file successfully.");
} else {
System.out.println("Invalid data format in file.");
}
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading medicines from file: " + e.getMessage());
}
}
public static void main(String[] args) {
// Load existing data from file (if any)
loadFromFile();
// Example usage
addMedicine("Paracetamol", 5.0, 10.0, 100);
addMedicine("Aspirin", 3.0, 8.0, 50);
// Selling some quantity of Paracetamol
sellMedicine("Paracetamol", 20);
// Saving data to file
saveToFile();
// Printing all medicines
for (Medicine med : getAllMedicines().values()) {
System.out.println(med);
}
}
}
sell medicine======================================================
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class SellMedicine extends JFrame {
private List<JTextField> nameFields = new ArrayList<>();
private List<JTextField> quantityFields = new ArrayList<>();
private JButton addButton;
private JButton calculateButton;
private JPanel panel;
private int fieldCount = 1;
public SellMedicine() {
setTitle("Sell Medicine");
setSize(400, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
panel = new JPanel(new GridLayout(0, 3, 10, 10));
addFields();
addButton = new JButton("+");
calculateButton = new JButton("Calculate");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addFields();
}
});
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
calculateTotalPrice();
}
});
panel.add(new JLabel());
panel.add(addButton);
panel.add(calculateButton);
add(panel);
}
private void addFields() {
JTextField nameField = new JTextField();
JTextField quantityField = new JTextField();
nameFields.add(nameField);
quantityFields.add(quantityField);
panel.add(new JLabel("Name " + fieldCount + ":"));
panel.add(nameField);
panel.add(quantityField);
fieldCount++;
panel.revalidate();
panel.repaint();
}
private void calculateTotalPrice() {
double totalPrice = 0.0;
for (int i = 0; i < nameFields.size(); i++) {
String name = nameFields.get(i).getText();
int quantity = Integer.parseInt(quantityFields.get(i).getText());
Medicine med = MedicineManager.getMedicine(name);
if (med != null) {
double price = med.getPricePerUnit();
totalPrice += price * quantity;
MedicineManager.sellMedicine(name, quantity);
}
}
JOptionPane.showMessageDialog(this, "Total Price: " + totalPrice);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SellMedicine().setVisible(true);
}
});
}
}
viewMedicine=========================================================
import javax.swing.*;
import java.awt.*;
import java.util.Map;
public class ViewMedicine extends JFrame {
private JTable table;
private JScrollPane scrollPane;
public ViewMedicine() {
setTitle("View Medicine");
setSize(600, 400); // Adjusted size for better table view
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
String[] columnNames = {"Name", "Purchase Rate", "Price Per Unit", "Quantity"};
String[][] data = getMedicineData();
table = new JTable(data, columnNames);
scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
}
private String[][] getMedicineData() {
Map<String, Medicine> medicines = MedicineManager.getAllMedicines();
String[][] data = new String[medicines.size()][4];
int i = 0;
for (Medicine med : medicines.values()) {
data[i][0] = med.getProductName();
data[i][1] = String.valueOf(med.getPurchaseRate());
data[i][2] = String.valueOf(med.getPricePerUnit());
data[i][3] = String.valueOf(med.getQuantity());
i++;
}
return data;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ViewMedicine viewMedicine = new ViewMedicine();
viewMedicine.setVisible(true);
}
});
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
