Pankaj-Str / JAVA-SE-Tutorial-codeswithpankaj

Pankaj-Str's GitHub, 'JAVA-SE-Tutorial-codeswithpankaj,' is a concise compendium of Java SE tutorials. Ideal for developers and learners, it offers clear and insightful code snippets, providing an efficient pathway to enhance Java programming skills. A valuable resource for mastering essential concepts
https://codeswithpankaj.com
32 stars 15 forks source link

Advanced Billing System Using Inheritance in Java #13

Open Pankaj-Str opened 1 month ago

Pankaj-Str commented 1 month ago

Objective:

To create a billing system that manages orders, clients, and credit limits using object-oriented principles, specifically inheritance in Java.


Problem Statement:

You are required to design a Java application for a billing system that tracks orders placed by clients and their respective credit limits. The application must include the following classes:

  1. Class Order:

    • Attributes:
      • orderId: Unique identifier for the order.
      • orderNumber: Number associated with the order.
      • location: The location where the order is placed.
      • amount: The total amount of the order.
      • gst: GST applicable on the order.
      • totalAmount: Total amount including GST.
    • Methods:
      • calculateTotalAmount(): Calculates the total amount by adding GST to the amount.
  2. Class Client:

    • Attributes:
      • clientName: Name of the client.
      • clientCity: City where the client is located.
      • clientCreditLimit: Credit limit assigned to the client.
      • totalCredit: Total credit used by the client.
    • Methods:
      • updateCredit(): Updates the total credit used by the client.
      • checkCreditLimit(): Checks if the client has exceeded their credit limit.
  3. Class CreditLimit:

    • Attributes:
      • totalClient: Total number of clients.
    • Methods:
      • displayClientDetails(): Displays details of all clients and their current credit status.
  4. Main Class:

    • The main class should include the logic to create multiple Order and Client objects, calculate the total amount including GST for orders, update the client’s credit usage, and check the credit limit for each client.

Input:

Details provided include:


Output:

A summary displaying each client’s details, total amount of orders, GST applied, total credit used, and whether the credit limit is exceeded.


Example Output:

Client Summary:


Client: Rajesh Kumar
City: Delhi
Total Credit Used: ₹50,000
Credit Limit: ₹1,00,000
Credit Limit Exceeded: No


Client: Priya Sharma
City: Mumbai
Total Credit Used: ₹1,20,000
Credit Limit: ₹1,00,000
Credit Limit Exceeded: Yes


Client: Amit Patel
City: Ahmedabad
Total Credit Used: ₹80,000
Credit Limit: ₹1,50,000
Credit Limit Exceeded: No


Client: Neha Verma
City: Bangalore
Total Credit Used: ₹1,10,000
Credit Limit: ₹1,00,000
Credit Limit Exceeded: Yes


Client: Anil Desai
City: Pune
Total Credit Used: ₹30,000
Credit Limit: ₹50,000
Credit Limit Exceeded: No


Client: Suman Reddy
City: Hyderabad
Total Credit Used: ₹95,000
Credit Limit: ₹1,00,000
Credit Limit Exceeded: No


Client: Arvind Singh
City: Jaipur
Total Credit Used: ₹1,00,000
Credit Limit: ₹80,000
Credit Limit Exceeded: Yes


Client: Kavita Joshi
City: Chennai
Total Credit Used: ₹70,000
Credit Limit: ₹1,00,000
Credit Limit Exceeded: No


Client: Manish Gupta
City: Kolkata
Total Credit Used: ₹40,000
Credit Limit: ₹70,000
Credit Limit Exceeded: No


Client: Anjali Das
City: Bhubaneswar
Total Credit Used: ₹1,20,000
Credit Limit: ₹1,00,000
Credit Limit Exceeded: Yes


System Summary:


Order Summary for Each Client:


Order ID: 1001
Order Number: 23456
Location: Delhi
Amount: ₹40,000
GST: ₹2,000
Total Amount (Including GST): ₹42,000


Order ID: 1002
Order Number: 23457
Location: Mumbai
Amount: ₹1,10,000
GST: ₹5,500
Total Amount (Including GST): ₹1,15,500


Order ID: 1003
Order Number: 23458
Location: Ahmedabad
Amount: ₹75,000
GST: ₹3,750
Total Amount (Including GST): ₹78,750


Order ID: 1004
Order Number: 23459
Location: Bangalore
Amount: ₹1,05,000
GST: ₹5,250
Total Amount (Including GST): ₹1,10,250


Order ID: 1005
Order Number: 23460
Location: Pune
Amount: ₹29,000
GST: ₹1,450
Total Amount (Including GST): ₹30,450


Order ID: 1006
Order Number: 23461
Location: Hyderabad
Amount: ₹93,000
GST: ₹4,650
Total Amount (Including GST): ₹97,650


Order ID: 1007
Order Number: 23462
Location: Jaipur
Amount: ₹96,000
GST: ₹4,800
Total Amount (Including GST): ₹1,00,800


Order ID: 1008
Order Number: 23463
Location: Chennai
Amount: ₹68,000
GST: ₹3,400
Total Amount (Including GST): ₹71,400


Order ID: 1009
Order Number: 23464
Location: Kolkata
Amount: ₹38,000
GST: ₹1,900
Total Amount (Including GST): ₹39,900


Order ID: 1010
Order Number: 23465
Location: Bhubaneswar
Amount: ₹1,16,000
GST: ₹5,800
Total Amount (Including GST): ₹1,21,800


End of Summary
This output example demonstrates the total credit used by each client, whether the credit limit is exceeded, and details of the orders including GST calculations. The system efficiently tracks client credit usage and ensures that any credit limit breaches are highlighted.

AnmolDixitB13 commented 1 month ago

import java.util.Scanner ;
import java.util.ArrayList ;

class Order
{
float tot_amt, gst_amt ;
float temp ;

String orderId, location ; 
float amount, gst, totalAmount ;
int orderNumber ;

    Order(String orderId, int orderNumber, String location, float amount, float gst, float totalAmount)
    {
    System.out.println("Order constructor invoked.") ;
    this.orderId = orderId  ;
    this.orderNumber = orderNumber  ;
    this.location = location  ;
    this.amount = amount  ;
    this.gst = gst  ;
    this.totalAmount = totalAmount ;
    System.out.println("Order constructor exited.") ;
    }

void calculateTotalAmount()

    {
    this.gst_amt = amount*gst/100 ;
    this.temp = this.amount + this.gst_amt ;
        //this.temp = amount + gst_amt ;    // this might also work correctly
    this.tot_amt = temp ;
    } // end of calculateTotalAmount() method

} // end of class Order

class Client extends Order
{
float temp ;
String creditExceeded ;

String clientName, clientCity ;
float clientCreditLimit, totalCreditUsed ;

    Client(String orderId, int orderNumber, String location, float amount, float gst, float totalAmount, String clientName, String clientCity, float clientCreditLimit, float totalCreditUsed)
    {
    super(orderId, orderNumber, location, amount, gst, totalAmount) ;
    this.clientName =  clientName ;
    this.clientCity =  clientCity ;
    this.clientCreditLimit = clientCreditLimit  ;
    this.totalCreditUsed =  totalCreditUsed ;
    System.out.println("Client constructor exited.") ;
    }

void updateCreditLimit()

    {
    this.temp = clientCreditLimit - totalCreditUsed ;
    //this.clientCreditLimit = temp ; // to set new credit limit after suitable deduction
    } // end of updateCreditLimit() method

void checkCreditLimit()

    {
    if (temp < 0)
        {
        this.creditExceeded = "Yes" ;
        }
    else
        {
        this.creditExceeded = "No" ;
        }
    } // end of checkCreditLimit() method
} // end of class Client

class CreditLimit extends Client
{
int totalClients ;
    CreditLimit(String orderId, int orderNumber, String location, float amount, float gst, float totalAmount, String clientName, String clientCity, float clientCreditLimit, float totalCreditUsed, int totalClients)
    {
    super(orderId, orderNumber, location, amount, gst, totalAmount, clientName, clientCity, clientCreditLimit, totalCreditUsed) ;
    this.totalClients = totalClients ;
    System.out.println("creditLimit constructor exited.") ;
    }

void getTotalClients()
    {
    System.out.println("Total no. of clients: "+totalClients) ;
    } // end of getTotalClients() method

void displayClientDetails()
    {
    System.out.println("_______________________________________________") ;
    System.out.println("**********Client Details**********") ;
    System.out.println("Client: "+clientName) ;
    System.out.println("City: "+clientCity) ;
    System.out.println("Total Credit Used: "+totalCreditUsed) ;
    System.out.println("Credit Limit: "+clientCreditLimit) ;
    System.out.println("Credit Limit Exceeded: "+creditExceeded) ;
    System.out.println("_______________________________________________") ;
    } // end of displayClientDetails() method

void displayOrderDetails()
    {
    System.out.println("_______________________________________________") ;
    System.out.println("**********Order Details**********") ;
    System.out.println("Order ID: "+orderId) ;
    System.out.println("Order Number: "+orderNumber) ;
    System.out.println("Location: "+location) ;
    System.out.println("Amount: "+amount) ;
    System.out.println("GST %: "+gst) ;
    System.out.println("GST: "+gst_amt) ;
    System.out.println("Total Amount: "+tot_amt) ;
    System.out.println("_______________________________________________") ;
    } // end of displayOrderDetails() method

} // end of class CreditLimit

class Main extends CreditLimit
{

Main(String orderId, int orderNumber, String location, float amount, float gst, float totalAmount, String clientName, String clientCity, float clientCreditLimit, float totalCreditUsed, int totalClients)
    {
    super(orderId, orderNumber, location, amount, gst, totalAmount, clientName, clientCity, clientCreditLimit, totalCreditUsed, totalClients) ;
    }

public static void main(String args[])
{
Scanner S = new Scanner(System.in) ;
String ans = "Yes" ;
int totalClients, i ;
float totalAmount = 0.0f ;

ArrayList<String> arr_orderId = new ArrayList<>() ;
ArrayList<Integer> arr_orderNumber = new ArrayList<>() ;
ArrayList<String> arr_location = new ArrayList<>() ;
ArrayList<Float> arr_amount = new ArrayList<>() ;
ArrayList<Float> arr_gst_per = new ArrayList<>() ;
ArrayList<Float> arr_totalAmount = new ArrayList<>() ;
ArrayList<String> arr_clientName = new ArrayList<>() ;
ArrayList<String> arr_clientCity = new ArrayList<>() ;
ArrayList<Float> arr_clientCreditLimit = new ArrayList<>() ;
ArrayList<Float> arr_totalCreditUsed = new ArrayList<>() ;

System.out.println("Enter the details as and when prompted to.\n") ;

do{
System.out.println("Enter the customer order id:\t") ;
arr_orderId.add(S.next()) ;

System.out.println("Enter the customer order number:\t") ;
arr_orderNumber.add(S.nextInt()) ;

System.out.println("Enter the customer location:\t") ;
arr_location.add(S.next()) ;

System.out.println("Enter the order amount:\t") ;
arr_amount.add(S.nextFloat()) ;

System.out.println("Enter the gst percent (write 0 if gst is not applicable):\t") ;
arr_gst_per.add(S.nextFloat()) ;

System.out.println("Enter the client name:\t") ;
arr_clientName.add(S.next()) ;

System.out.println("Enter client city name:\t") ;
arr_clientCity.add(S.next()) ;

System.out.println("Enter the client's existing credit limit:\t") ;
arr_clientCreditLimit.add(S.nextFloat()) ;

System.out.println("Enter the credit that the client has used\t") ;
arr_totalCreditUsed.add(S.nextFloat()) ;

System.out.println("Do you want to add another client ?\t") ;
ans = S.next() ;

}while(ans.equalsIgnoreCase("Yes")) ;

totalClients = arr_clientName.size() ;
Main m ;
for(i = 0; i < totalClients; ++i)
    {
    m = new Main(arr_orderId.get(i), arr_orderNumber.get(i), arr_location.get(i), arr_amount.get(i), arr_gst_per.get(i), totalAmount, arr_clientName.get(i), arr_clientCity.get(i), arr_clientCreditLimit.get(i), arr_totalCreditUsed.get(i), totalClients) ;

    m.calculateTotalAmount() ;
    m.updateCreditLimit() ;
    m.checkCreditLimit() ;

    m.getTotalClients() ;
    m.displayClientDetails() ;
    m.displayOrderDetails() ;

    } // end of for() loop

} // end of main() method
} // end of class Main

/*
Future Scope / Possible Enhancements to this program:
1. I/P validation can be incorporated with either if-else or with suitable Exception Handling 
to ensure the data entered by the user is valid. eg. price/amount can't be negative, gst percent 
should be between 0 and 28 %... and so on.
2. Discount/Concession feature on Total Amount can be added, just as we have feature to charge GST.
3. A suitable UI can be designed for the same along with a backend.
*/
PsychoNick23 commented 1 month ago

import java.util.Scanner;

class Order {
    int orderId;
    int orderNumber;
    String location;
    double amount;
    double gst;
    double totalAmount;

   Order(int orderId, int orderNumber, String location, double amount, double gst) {
        this.orderId = orderId;
        this.orderNumber = orderNumber;
        this.location = location;
        this.amount = amount;
        this.gst=gst;

    }

    void calculateTotalAmount() {
        this.totalAmount = amount + gst;

    }
}

class Client extends Order{
        String clientName;
        String clientCity;
        double clientCreditLimit;
        double totalCredit;

        Client(int orderId, int orderNumber, String location, double amount, double gst, String clientName, String clientCity, double clientCreditLimit){
           super(orderId,orderNumber,location,amount,gst);
           this.clientName=clientName;
           this.clientCity=clientCity;
           this.clientCreditLimit=clientCreditLimit;

        }
        void updateCredit(double totalCredit){
            this.totalCredit=totalCredit;
        }
        void checkCreditLimit(){
            if(totalCredit>clientCreditLimit){
                System.out.println("Credit limit exceeded: Yes");

            }
            else{
                System.out.println("Credit limit exceeded: No");

            }

        }

}

class CreditLimit extends Client {
    int totalClient;

    CreditLimit(int orderId, int orderNumber, String location, double amount, double gst, String clientName, String clientCity, double clientCreditLimit, int totalClient) {
        super(orderId, orderNumber, location, amount, gst, clientName, clientCity, clientCreditLimit);
        this.totalClient = totalClient;

    }

    void displayClientDetails() {
        System.out.println("Client: " + clientName);
        System.out.println("City: " + clientCity);
        System.out.println("Total Credit Used: " + totalCredit);
        System.out.println("Credit Limit: " + clientCreditLimit);
        checkCreditLimit();
    }

    void orderSummary() {
        System.out.println("Order ID: " +orderId);
        System.out.println("Order Number: " +orderNumber);
        System.out.println("Location: " + location);
        System.out.println("Amount: "+amount);
        System.out.println("GST: " + gst);
        System.out.println("Total Amount(Including Gst)" + totalAmount);
    }

}

public class advbill {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int[] orderId={1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
        int[] orderNumber={23456,23457,23458,23459,23460,23461,23462,23463,23464,23465};
        String[] location={"Delhi","Mumbai","Ahmedabad","Banglore","Pune","Hyderabad","Jaipur","Chennai","Kolkata","Bhubaneshwar"};
        double[] amount={40000,110000,75000,105000,29000,93000,96000,68000,38000,116000};
        double[] gst={2000,5500,3750,5250,1450,4650,4800,3400,1900,5800};
        String[] clientName={"Rajesh Kumar","Priya Sharma","Amit Patel","Neha Verma","Anil Desai","Suman Reddy","Arvind Singh","Kavita Joshi","Manish Gupta","Anjali Das"};
        String[] clientCity={"Delhi","Mumbai","Ahmedabad","Banglore","Pune","Hyderabad","Jaipur","Chennai","Kolkata","Bhubaneshwar"};
        double[] totalCredit={50000,120000,80000,110000,30000,95000,100000,70000,40000,120000};
        double[] clientCreditLimit={100000,100000,150000,100000,50000,100000,80000,100000,70000,100000,};
        int totalClient=clientName.length;

        CreditLimit[] cred=new CreditLimit[10];
        for(int i=0;i<10;i++){
            cred[i]=new CreditLimit(orderId[i],orderNumber[i],location[i],amount[i],gst[i],clientName[i],clientCity[i], clientCreditLimit[i],totalClient);
            cred[i].updateCredit(totalCredit[i]);
            cred[i].calculateTotalAmount();
        }
        String type;
        System.out.println("Client Summary or Order Summary???");
        type=sc.nextLine();
        if(type.equals("C")){
            System.out.println("Client Summary: ");
            for(int i=0;i<10;i++){
                cred[i].displayClientDetails();
                System.out.println();
            }
        }
        else{
            System.out.println("Order Summary: ");
            for(int i=0;i<10;i++){
                cred[i].orderSummary();
                System.out.println();
            }
        }
        System.out.println();
        System.out.println("System Summary: ");
        System.out.println();
        System.out.println("Total No of clients: "+clientName.length);

    }
}
ChinmayParab77 commented 4 weeks ago
package Augest10_24;

// Base class: Client
class Client {
    String clientName;
    String clientCity;
    String clientId;
    double clientCreditLimit;
    double totalCredit;

    // Constructor
    Client(String clientName, String clientId, String clientCity, double clientCreditLimit) {
        this.clientName = clientName;
        this.clientId = clientId;
        this.clientCity = clientCity;
        this.clientCreditLimit = clientCreditLimit;
        this.totalCredit = 0.0;  // Initially, the total credit used is 0
    }

    // Method to update the total credit used by the client
    void updateCredit(double amount) {
        totalCredit += amount;
    }

    // Method to check if the client has exceeded their credit limit
    boolean checkCreditLimit() {
        return totalCredit <= clientCreditLimit;
    }

    // Method to display client information
    void displayClientInfo() {
        System.out.println("Client Name: " + clientName);
        System.out.println("Client ID: " + clientId);
        System.out.println("Client City: " + clientCity);
        System.out.println("Credit Limit: $" + clientCreditLimit);
        System.out.println("Total Credit Used: $" + totalCredit);
    }
}

// Derived class: Order
class Order extends Client {
    String orderId;
    int orderNumber;
    String location;
    double amount;
    double gst;
    double totalAmount;

    // Constructor
    Order(String clientName, String clientId, String clientCity, double clientCreditLimit,
          String orderId, int orderNumber, String location, double amount, double gst) {
        super(clientName, clientId, clientCity, clientCreditLimit);
        this.orderId = orderId;
        this.orderNumber = orderNumber;
        this.location = location;
        this.amount = amount;
        this.gst = gst;
        this.totalAmount = 0.0;  // Initially, the total amount is 0
    }

    // Method to calculate the total amount by adding GST to the base amount
    void calculateTotalAmount() {
        totalAmount = amount + (amount * gst / 100);
    }

    // Method to display order details including the total amount
    void displayOrderInfo() {
        System.out.println("Order ID: " + orderId);
        System.out.println("Order Number: " + orderNumber);
        System.out.println("Location: " + location);
        System.out.println("Order Amount: " + amount);
        System.out.println("GST: " + gst + "%");
        System.out.println("Total Amount: " + totalAmount);
    }

    // Method to place the order
    void placeOrder() {
        System.out.println("Order placed successfully for client: " + clientName + " at location: " + location);
    }
}

// Derived class: CreditLimit
class CreditLimit extends Client {
    double creditLimit;
    static int totalClient = 0;

    // Constructor
    CreditLimit(String clientName, String clientId, String clientCity, double creditLimit) {
        super(clientName, clientId, clientCity, creditLimit);
        this.creditLimit = creditLimit;
        totalClient++;
    }

    // Method to display the credit limit of the client
    void displayCreditLimit() {
        System.out.println("Client Name: " + clientName);
        System.out.println("Client ID: " + clientId);
        System.out.println("Client City: " + clientCity);
        System.out.println("Credit Limit: " + creditLimit);
        System.out.println("Total Credit Used: " + totalCredit);
    }

    // Method to check if the order amount is within the credit limit
    boolean checkCredit(double orderAmount) {
        return orderAmount <= creditLimit;
    }

    // Static method to display details of all clients and their credit status
    static void displayClientDetails(CreditLimit[] clients) {
        System.out.println("Total Clients: " + totalClient);
        for (CreditLimit client : clients) {
            System.out.println("\nClient Name: " + client.clientName);
            System.out.println("Client ID: " + client.clientId);
            System.out.println("Client City: " + client.clientCity);
            System.out.println("Credit Limit: " + client.creditLimit);
            System.out.println("Total Credit Used: " + client.totalCredit);
            System.out.println("Within Credit Limit: " + (client.totalCredit <= client.creditLimit));
        }
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Creating CreditLimit objects
        CreditLimit client1 = new CreditLimit("Nishant", "C123", "Kolkata", 1000.0);
        CreditLimit client2 = new CreditLimit("Shubham", "C124", "Mumbai", 1200.0);

        // Creating Order objects
        Order order1 = new Order("Nishant", "C123", "Kolkata", 1000.0, "O789", 101, "Bangolore", 500.0, 18.0);
        Order order2 = new Order("Shubham", "C124", "Mumbai", 1200.0, "O790", 102, "Assam", 1200.0, 15.0);

        // Calculate the total amount including GST
        order1.calculateTotalAmount();
        order2.calculateTotalAmount();

        // Display order and client details
        order1.displayOrderInfo();
        order2.displayOrderInfo();
        client1.updateCredit(order1.totalAmount);
        client2.updateCredit(order2.totalAmount);

        // Display details of all clients
        CreditLimit[] clients = {client1, client2};
        CreditLimit.displayClientDetails(clients);
    }
}