Java RMI Discount Calculator: Client-Server Setup

Computer science7.02 KB

Java RMI Buyer Discount Application

This Java application demonstrates the use of Java Remote Method Invocation (RMI) to calculate sales discounts across a distributed client-server architecture.

Remote Interface: IComprador

The remote interface defines the methods available to clients across the network:

import java.net.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;

public interface IComprador extends Remote {
    public int getId() throws RemoteException;
    public void setId(int id) throws RemoteException;
    public String getNome() throws RemoteException;
    public void setNome(String nome) throws RemoteException;
    public String getEmail() throws RemoteException;
    public void setEmail(String Email) throws RemoteException;
    public double getTotalCompra() throws RemoteException;
    public void setTotalCompra(double totalCompra) throws RemoteException;
    public double getPorcentagemDesconto() throws RemoteException;
    public double getTotalPagar() throws RemoteException;
    public void calcularPorcentagemDesconto() throws RemoteException;
    public void calcularTotalPagar() throws RemoteException;
}

Remote Implementation: Comprador

The Comprador class implements IComprador and extends UnicastRemoteObject to provide server-side business logic:

import java.net.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;

public class Comprador extends UnicastRemoteObject implements IComprador {
    private int id;
    private String nome;
    private String Email;
    private double totalCompra;
    private double porcentagemDesconto;
    private double totalPagar;

    public Comprador() throws RemoteException {}

    public Comprador(int id, String nome, String Email, double totalCompra, double porcentagemDesconto, double totalPagar) throws RemoteException {
        this.id = id;
        this.nome = nome;
        this.Email = Email;
        this.totalCompra = totalCompra;
        this.porcentagemDesconto = porcentagemDesconto;
        this.totalPagar = totalPagar;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

    public double getTotalCompra() {
        return totalCompra;
    }

    public void setTotalCompra(double totalCompra) {
        this.totalCompra = totalCompra;
    }

    public double getPorcentagemDesconto() {
        return porcentagemDesconto;
    }

    public double getTotalPagar() {
        return totalPagar;
    }

    public void calcularPorcentagemDesconto() throws RemoteException {
        if (totalCompra >= 0.01 && totalCompra <= 999.99) {
            porcentagemDesconto = 0.1;
        } else if (totalCompra >= 1000.00 && totalCompra <= 4999.99) {
            porcentagemDesconto = 0.15;
        } else if (totalCompra >= 5000.00) {
            porcentagemDesconto = 0.2;
        }
    }

    public void calcularTotalPagar() throws RemoteException {
        totalPagar = totalCompra - (totalCompra * porcentagemDesconto);
    }
}

RMI Server: Servidor

The server instantiates the remote object and registers it within the local RMI registry using Naming.rebind:

import java.net.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;

public class Servidor {
    public static void main(String args[]) {
        try {
            // Criar o objeto que implementa o Comprador
            Comprador obj = new Comprador();
            // Cadastrando o objeto comprador no registry
            Naming.rebind("//localhost/comprador", obj);
            System.out.println("Publicando objeto Comprador");
        } catch (Exception e) {
            System.out.println("erro: " + e.getMessage());
        }
    }
}

RMI Client: Cliente

The client looks up the remote object in the registry, updates buyer properties, calculates totals, and displays the results:

import java.net.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;

public class Cliente {
    public static void main(String args[]) {
        try {
            IComprador obj = (IComprador) Naming.lookup("comprador");

            obj.setId(2020);
            obj.setNome("Maria");
            obj.setEmail("[email protected]");
            obj.setTotalCompra(800);
            obj.calcularPorcentagemDesconto();
            obj.calcularTotalPagar();
            System.out.println("Id ----" + obj.getId());
            System.out.println("Nome ----" + obj.getNome());
            System.out.println("Email ----" + obj.getEmail());
            System.out.println("Total da Compra ----" + obj.getTotalCompra());
            System.out.println("Porcentagem de Desconto ----" + obj.getPorcentagemDesconto() * 100);
            System.out.println("Valor total a pagar ----" + obj.getTotalPagar());
            System.out.println("-------------------------------------------------------------------------");

            IComprador obj2 = (IComprador) Naming.lookup("comprador");

            obj2.setId(1003);
            obj2.setNome("Mara");
            obj2.setEmail("[email protected]");
            obj2.setTotalCompra(2000);
            obj2.calcularPorcentagemDesconto();
            obj2.calcularTotalPagar();
            System.out.println("Id ----" + obj2.getId());
            System.out.println("Nome ----" + obj2.getNome());
            System.out.println("Email ----" + obj2.getEmail());
            System.out.println("Total da Compra ----" + obj2.getTotalCompra());
            System.out.println("Porcentagem de Desconto ----" + obj2.getPorcentagemDesconto() * 100);
            System.out.println("Valor total a pagar ----" + obj2.getTotalPagar());
            System.out.println("-------------------------------------------------------------------------");

            IComprador obj3 = (IComprador) Naming.lookup("comprador");

            obj3.setId(3465);
            obj3.setNome("Marcia");
            obj3.setEmail("[email protected]");
            obj3.setTotalCompra(7000);
            obj3.calcularPorcentagemDesconto();
            obj3.calcularTotalPagar();
            System.out.println("Id ----" + obj3.getId());
            System.out.println("Nome ----" + obj3.getNome());
            System.out.println("Email ----" + obj3.getEmail());
            System.out.println("Total da Compra ----" + obj3.getTotalCompra());
            System.out.println("Porcentagem de Desconto ----" + obj3.getPorcentagemDesconto() * 100);
            System.out.println("Valor total a pagar ----" + obj3.getTotalPagar());

        } catch (Exception e) {
            System.out.println(" erro: " + e.getMessage());
        }
        System.exit(0);
    }
}

Download

PDF Word HTML