✅ 1. Class-Based Java Program (Object-Oriented)
class ProfitLoss {
private double cp;
private double sp;
public ProfitLoss(double cp, double sp) {
this.cp = cp;
this.sp = sp;
}
public void calculate() {
if (sp > cp) {
double profit = sp - cp;
double profitPercent = (profit / cp) * 100;
System.out.println("Profit: " + profit);
System.out.println("Profit Percentage: " + profitPercent + "%");
} else if (cp > sp) {
double loss = cp - sp;
double lossPercent = (loss / cp) * 100;
System.out.println("Loss: " + loss);
System.out.println("Loss Percentage: " + lossPercent + "%");
} else {
System.out.println("No Profit, No Loss.");
}
}
}
public class ProfitLossMain {
public static void main(String[] args) {
ProfitLoss obj = new ProfitLoss(500, 650);
obj.calculate();
}
}
✅ 2. Menu-Driven Console Version
(Choose 1 for profit/loss, 2 to exit)
import java.util.Scanner;
public class ProfitLossMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n===== PROFIT & LOSS CALCULATOR =====");
System.out.println("1. Calculate Profit / Loss");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Cost Price (CP): ");
double cp = sc.nextDouble();
System.out.print("Enter Selling Price (SP): ");
double sp = sc.nextDouble();
if (sp > cp) {
double profit = sp - cp;
double profitPercent = (profit / cp) * 100;
System.out.println("Profit: " + profit);
System.out.println("Profit Percentage: " + profitPercent + "%");
} else if (cp > sp) {
double loss = cp - sp;
double lossPercent = (loss / cp) * 100;
System.out.println("Loss: " + loss);
System.out.println("Loss Percentage: " + lossPercent + "%");
} else {
System.out.println("No Profit, No Loss.");
}
break;
case 2:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid Choice! Try again.");
}
} while (choice != 2);
sc.close();
}
}
✅ 3. GUI Version (Java Swing)
This version shows a small graphical window for input and output.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ProfitLossGUI extends JFrame {
private JTextField cpField, spField;
private JButton btnCalculate;
private JLabel resultLabel;
public ProfitLossGUI() {
setTitle("Profit & Loss Calculator");
setSize(350, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 2));
add(new JLabel("Cost Price (CP): "));
cpField = new JTextField();
add(cpField);
add(new JLabel("Selling Price (SP): "));
spField = new JTextField();
add(spField);
btnCalculate = new JButton("Calculate");
add(btnCalculate);
resultLabel = new JLabel("");
add(resultLabel);
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double cp = Double.parseDouble(cpField.getText());
double sp = Double.parseDouble(spField.getText());
if (sp > cp) {
double profit = sp - cp;
double profitPer = (profit / cp) * 100;
resultLabel.setText("Profit: " + profit + " | " + profitPer + "%");
} else if (cp > sp) {
double loss = cp - sp;
double lossPer = (loss / cp) * 100;
resultLabel.setText("Loss: " + loss + " | " + lossPer + "%");
} else {
resultLabel.setText("No Profit, No Loss.");
}
}
});
setVisible(true);
}
public static void main(String[] args) {
new ProfitLossGUI();
}
}
🎉 All Versions Completed
You now have:
✔ Simple Java Program
✔ OOP/Class-based Program
✔ Menu-driven Console Program
✔ GUI Program using Swing
