0% found this document useful (0 votes)
27 views14 pages

Use Case

Uploaded by

sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views14 pages

Use Case

Uploaded by

sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

USE –CASE

Creation of Tables

Create Table Items_


(ICode Varchar2(4) Primary Key,
IName Varchar2(20) Not Null,
Rate Number(8,2) Check (rate>0),
Stock Number(5) check (stock>=0));
alter table items_ add constraint uq_items_iname_ unique(iname);

Create Table Bill_Master


(BillNo Varchar2(4) Primary Key,
BDate date default sysdate,
BTotal Number(8,2) default 0);

Create Table Bill_Transactions


(BillNo Varchar2(4) references BMast(BillNo),
ICode Varchar2(4) references Items(ICode),
Qty Number(5) check (qty>0));
1. Create Sequences to Fill Bill Nos. Start from 0001 to 9999
Create Sequence SEQ_Items_Icode_

Start With 1000

Increment By 1

MinValue 1000

MaxValue 9999;

Create Sequence SEQ_BMast_Bno_

Start With 1

Increment By 1

MinValue 1

MaxValue 9999;

2. Create Procedures to
a. Add Row to Items
b. Add Rows to Bill_Master
a. Create or replace Procedure Add_Items(vICode Varchar2,vIName Varchar2,vRate
Number,vStock Number) is
Begin
insert into Items_ values (vIcode,vIName,vRate,vStock);
Dbms_OutPut.Put_Line ('One Item Added to DataBase');
Exception
When Dup_Val_on_Index Then
Dbms_OutPut.Put_Line ('Item ID Already Exists in Database, Insert Aborted');
When Others Then
Dbms_OutPut.Put_Line (SQLERRM);
End Add_Items ;

b. Create or replace Procedure Add_Bill (vBno Varchar2) is

Begin

Insert into Bill_Master (Billno) Values(vBno);

Dbms_OutPut.Put_Line ('One Bill Generated');

Exception

When Others Then

Dbms_OutPut.Put_Line (SQLERRM);

End Add_Bill;

3. Create Procedures to add Rows to Bill_Trans with following


conditions. Conditions need to be validated by Functions.
a. Stock for the Item should be available
create or replace Function Check_Stock (vICode Varchar2) Return Boolean is

vStock items_.stock%Type;

Begin
select stock into vStock from items_ where icode=vICode;

if vStock>0 then

Return TRUE;

else

Return False;

end if;

exception

when others then

Return FALSE;

End Check_Stock;

b. Requested quantity should not exceed stock available


create or replace Function Check_Stock_Qty (vICode Varchar2,vQty Number) Return
Boolean is

vStock items_.stock%Type;

Begin

select stock into vStock from Items_ where icode=vicode;

if vstock>=vQty then

return TRUE;

else

return False;

end if;
Exception

when no_data_found then

return False;

when others then

return False;

End Check_Stock_Qty;

c. Update Bill Total after each item purchased.

Create or replace Procedure Add_BTrans (vBno Varchar2,vICode Varchar2,vQty Number)


is

vRate items_.rate%Type;

vBAmt Bill_Master.btotal%type:=0;

Begin

if CHECK_STOCK(vICode) Then

if Check_Stock_Qty(vIcode,vQty) Then

select rate into vRate from items_ where icode=vIcode;

vBamt:=(vRate*vQty);

insert into Bill_Transactions values (vBno,vICode,vQty);

dbms_output.put_line('One Item Added to the Bill');

update Bill_master set btotal=btotal+vbamt where billno=vbno;


dbms_output.put_line('Bill Amount updated in BMaster');

update items_ set stock=stock-vqty where icode=vicode;

dbms_output.put_line('Item Stock Updated');

else

dbms_output.put_line('Requested Quantity Exceeds Stock Available');

end if;

else

dbms_output.put_line('Item Out of Stock');

end if;

exception

when others then

dbms_output.put_line(sqlerrm);

End Add_BTrans;

4. Generate a Report which can be used as a Bill/Invoice


Create or replace Procedure Show_Bill (vBno Varchar2) is

cursor my_cur1(vbno number) is select * from Bill_Master where billno=vbno;

v1 Bill_Master%rowtype;

cursor my_cur2(vbno number) is select * from bill_transactions where billno=vbno;

v2 bill_transactions%rowtype;

v3 items_%rowtype;
begin

Open my_cur1(vBno);

Loop

Fetch my_cur1 INTO v1;

EXIT when my_cur1%notfound;

dbms_output.put_line('Bill No:-'|| v1.Billno);

dbms_output.put_line('Bill Date:-'||v1.BDate);

dbms_output.put_line('-------------------------------------');

dbms_output.put_line('Code Desc Rate Qty Amt ');

dbms_output.put_line('-------------------------------------');

open my_cur2(vBno);

loop

Fetch my_cur2 into v2;

Exit when my_cur2%NotFound;

select * into v3 from items_ where icode=v2.icode;

dbms_output.put_line(v2.Icode||' '||v3.iname||' '||to_char(v3.rate,'9,999.99')||'


'||v2.Qty||' '||

to_char(v2.qty*v3.rate,'9,999.99'));

end loop;

close my_cur2;

dbms_output.put_line('-------------------------------------');

dbms_output.put_line('------------------------'||v1.BTotal);

dbms_output.put_line('------------------------------------');

End Loop;
Close my_cur1;

Exception

when others then

dbms_output.put_line(sqlerrm);

End Show_Bill;

Exec Add_Items(1000,'Bread',50,5);

Exec Add_Items(1001,'Butter',250,5);

Exec Add_Bill (1);

Exec Add_Btrans(1,1000,2);

Exec Add_Btrans(1,1001,1);

Exec Show_bill(1);

5. Create Package for all above


Create or Replace Package Body PKG_Billing As

Procedure Add_Items(vICode Varchar2,vIName Varchar2,vRate Number,vStock Number)


is

Begin

insert into Items_ values (vIcode,vIName,vRate,vStock);

Dbms_OutPut.Put_Line ('One Item Added to DataBase');

Exception

When Dup_Val_on_Index Then

Dbms_OutPut.Put_Line ('Item ID Already Exists in Database, Insert Aborted');

When Others Then


Dbms_OutPut.Put_Line (SQLERRM);

End Add_Items ;

Procedure Add_Bill (vBno Varchar2) is

Begin

Insert into Bill_Master (Billno) Values(vBno);

Dbms_OutPut.Put_Line ('One Bill Generated');

Exception

When Others Then

Dbms_OutPut.Put_Line (SQLERRM);

End Add_Bill;

Function Check_Stock (vICode Varchar2) Return Boolean is

vStock items_.stock%Type;

Begin

select stock into vStock from items_ where icode=vICode;

if vStock>0 then

Return TRUE;

else

Return False;

end if;

exception

when others then

Return FALSE;
End Check_Stock;

Function Check_Stock_Qty (vICode Varchar2,vQty Number) Return Boolean is

vStock items_.stock%Type;

Begin

select stock into vStock from Items_ where icode=vicode;

if vstock>=vQty then

return TRUE;

else

return False;

end if;

Exception

when no_data_found then

return False;

when others then

return False;

End Check_Stock_Qty;

Procedure Add_BTrans (vBno Varchar2,vICode Varchar2,vQty Number) is

vRate items_.rate%Type;

vBAmt Bill_Master.btotal%type:=0;

Begin

if CHECK_STOCK(vICode) Then

if Check_Stock_Qty(vIcode,vQty) Then

select rate into vRate from items_ where icode=vIcode;


vBamt:=(vRate*vQty);

insert into Bill_Transactions values (vBno,vICode,vQty);

dbms_output.put_line('One Item Added to the Bill');

update Bill_master set btotal=btotal+vbamt where billno=vbno;

dbms_output.put_line('Bill Amount updated in BMaster');

update items_ set stock=stock-vqty where icode=vicode;

dbms_output.put_line('Item Stock Updated');

else

dbms_output.put_line('Requested Quantity Exceeds Stock Available');

end if;

else

dbms_output.put_line('Item Out of Stock');

end if;

exception

when others then

dbms_output.put_line(sqlerrm);

End Add_BTrans;

Procedure Show_Bill (vBno Varchar2) is

cursor my_cur1(vbno number) is select * from Bill_Master where billno=vbno;

v1 Bill_Master%rowtype;

cursor my_cur2(vbno number) is select * from bill_transactions where billno=vbno;

v2 bill_transactions%rowtype;
v3 items_%rowtype;

begin

Open my_cur1(vBno);

Loop

Fetch my_cur1 INTO v1;

EXIT when my_cur1%notfound;

dbms_output.put_line('Bill No:-'|| v1.Billno);

dbms_output.put_line('Bill Date:-'||v1.BDate);

dbms_output.put_line('-------------------------------------');

dbms_output.put_line('Code Desc Rate Qty Amt ');

dbms_output.put_line('-------------------------------------');

open my_cur2(vBno);

loop

Fetch my_cur2 into v2;

Exit when my_cur2%NotFound;

select * into v3 from items_ where icode=v2.icode;

dbms_output.put_line(v2.Icode||' '||v3.iname||' '||to_char(v3.rate,'9,999.99')||'


'||v2.Qty||' '||

to_char(v2.qty*v3.rate,'9,999.99'));

end loop;

close my_cur2;

dbms_output.put_line('-------------------------------------');

dbms_output.put_line('------------------------'||v1.BTotal);

dbms_output.put_line('------------------------------------');
End Loop;

Close my_cur1;

Exception

when others then

dbms_output.put_line(sqlerrm);

End Show_Bill;

End PKG_Billing;

You might also like