🚀 1. ANDROID APP VERSION (JAVA)
📱 Profit & Loss Calculator — Android Studio Project
Step 1: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center"
android:layout_height="match_parent">
<EditText
android:id="@+id/etCP"
android:hint="Enter Cost Price"
android:inputType="numberDecimal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/etSP"
android:hint="Enter Selling Price"
android:inputType="numberDecimal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btnCalc"
android:text="Calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/tvResult"
android:text=""
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Step 2: MainActivity.java
package com.example.profitloss;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
EditText etCP, etSP;
Button btnCalc;
TextView tvResult;
@Override
protected void onCreate
(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etCP = findViewById(R.id.etCP);
etSP = findViewById(R.id.etSP);
btnCalc = findViewById(R.id.btnCalc);
tvResult = findViewById(R.id.tvResult);
btnCalc.setOnClickListener(v -> {
double cp = Double.parseDouble(etCP.getText().toString());
double sp = Double.parseDouble(etSP.getText().toString());
if (sp > cp) {
double profit = sp - cp;
double pp = profit / cp * 100;
tvResult.setText("Profit: " + profit + "\nProfit %: " + pp);
} else if (cp > sp) {
double loss = cp - sp;
double lp = loss / cp * 100;
tvResult.setText("Loss: " + loss + "\nLoss %: " + lp);
} else {
tvResult.setText("No Profit No Loss");
}
});
}
}
🎨 2. JavaFX GUI VERSION
Step 1: Main Application (Main.java)
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
TextField cpField = new TextField();
cpField.setPromptText("Enter Cost Price");
TextField spField = new TextField();
spField.setPromptText("Enter Selling Price");
Button calcBtn = new Button("Calculate");
Label result = new Label();
calcBtn.setOnAction(e -> {
double cp = Double.parseDouble(cpField.getText());
double sp = Double.parseDouble(spField.getText());
if (sp > cp) {
double profit = sp - cp;
double percent = (profit / cp) * 100;
result.setText("Profit: " + profit + " | " + percent + "%");
} else if (cp > sp) {
double loss = cp - sp;
double percent = (loss / cp) * 100;
result.setText("Loss: " + loss + " | " + percent + "%");
} else {
result.setText("No Profit, No Loss");
}
});
VBox root = new VBox(10, cpField, spField, calcBtn, result);
root.setPadding(new Insets(20));
stage.setScene(new Scene(root, 300, 250));
stage.setTitle("Profit & Loss Calculator");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
🌐 3. WEB VERSION (JSP + SERVLET)
Structure:
WebApp/
├── index.jsp
├── result.jsp
└── ProfitLossServlet.java
Step 1: index.jsp
<!DOCTYPE html>
<html>
<body>
<h2>Profit & Loss Calculator</h2>
<form action="ProfitLossServlet" method="post">
Cost Price: <input type="text" name="cp"><br><br>
Selling Price: <input type="text" name="sp"><br><br>
<input type="submit" value="Calculate">
</form>
</body>
</html>
Step 2: ProfitLossServlet.java
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.IOException;
public class ProfitLossServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
double cp = Double.parseDouble(request.getParameter("cp"));
double sp = Double.parseDouble(request.getParameter("sp"));
String result;
if (sp > cp) {
double profit = sp - cp;
double pp = profit / cp * 100;
result = "Profit: " + profit + " ( " + pp + "% )";
} else if (cp > sp) {
double loss = cp - sp;
double lp = loss / cp * 100;
result = "Loss: " + loss + " ( " + lp + "% )";
} else {
result = "No Profit No Loss";
}
request.setAttribute("result", result);
RequestDispatcher rd = request.getRequestDispatcher("result.jsp");
rd.forward(request, response);
}
}
Step 3: result.jsp
<!DOCTYPE html>
<html>
<body>
<h2>Result</h2>
<p><b>${result}</b></p>
<a href="index.jsp">Go Back</a>
</body>
</html>