program Receipt                                              
!-----------------------------------------------------------------------------------------------------------
! Christina Urbanczyk                      Lab 5 Assignment                                 February 24, 2011
! This program determines the total cost of purchases made at a department store in the Arcata Plaza.        
!    Variables used are:                                                                                     
!    ShirtQuantity:Number of shirts for purchase                                                                                         
!    ShirtTotal: Cost of all shirts for purchase                                                                                         
!    Subtotal:Cost of all purchases before sales tax                                                                                     
!    SalesTax:Tax from total purchase                                                                                                    
!    TotalCost: Total cost of purchase                                                                                                   
!    PantsQuantity: Number of pants for purchase                                                                                         
!    PantsTotal: Cost of all pants for purchase                                                                                          
!    ShoesQuantity: Number of shoes for purchase                                                                                         
!    ShoesTotal: Cost of all shoes for purchase                                                                                          
!    SuitQuantity: Number of suits for purchase                                                                                          
!    SuitTotal: Cost of all suits for purchase                                                                                           
!    IOerror: Input/Output error                                                                                                         
!    GetOrder: Subprogram to request items for purchase                                                                                  
!                                                                                                                                        
! Input: ShirtQuantity, PantsQuantity, ShoesQuantity, SuitsQuantity                                                                      
! Output: ShirtTotal,PantsTotal, ShoesTotal, SuitTotal, Subtotal, SalesTax, TotalCost                                                    
!-----------------------------------------------------------------------------------------------------------                             

IMPLICIT NONE
integer, parameter:: dp=selected_real_kind (15) !15 Significant Figures
integer:: ShirtQuantity, PantsQuantity,ShoesQuantity, SuitQuantity, ioerror
real(dp):: ShirtTotal, PantsTotal, ShoesTotal, SuitTotal, Subtotal, SalesTax, TotalCost  
real(dp),parameter:: ShoesCost=35.85,SuitCost=125.15,TaxRate=.09, ShirtCost=19.95,PantsCost=22.95

CALL GetOrder(ShirtCost,'Shirts',ShirtQuantity)
CALL GetOrder(PantsCost,'Pants',PantsQuantity) 
CALL GetOrder(ShoesCost,'Shoes',ShoesQuantity) 
CALL GetOrder(SuitCost,'Suits',SuitQuantity)   
                                               
!Calculate subtotal of individual items        
ShirtTotal= ShirtQuantity*ShirtCost            
PantsTotal= PantsQuantity*PantsCost            
ShoesTotal= ShoesQuantity*ShoesCost            
SuitTotal= SuitQuantity*SuitCost               

!Calculate subtotal of all items
Subtotal= ShirtTotal+PantsTotal+ShoesTotal+SuitTotal

!Calculate sales tax of purchase
SalesTax= Subtotal*TaxRate      

!Calculate total cost of purchase
TotalCost= Subtotal+SalesTax     

!Print receipt for purchase
  IF (Subtotal/=0) THEN    
write (*,*) "|--------------------------------------------|"
write (*,*) "|            Christina's Store               |"
write (*,*) "|                The Plaza                   |"
write (*,*) "|             Arcata, CA 95521               |"
write (*,*) "|                                            |"
write (*,*) "|--------------------------------------------|"
write (*,*) "| ITEM             NUMBER              COST  |"
write (*,*) "|--------------------------------------------|"
  END IF                                                    
                                                            
  IF (Subtotal<=0) THEN                                     
write (*,*) "Thanks for coming to Christina's Store. Have a good day."
  END IF                                                              

  IF (ShirtTotal>0) THEN
write (*,'(T2,A7,T22,I2,T39,F6.2,T47,A1)') "| Shirt"  ,ShirtQuantity,ShirtTotal,"|"
write (*,*) "|--------------------------------------------|"                       
  END IF                                                                           

  IF (PantsTotal>0) THEN
write (*,'(T2,A7,T22,I2,T39,F6.2,T47,A1)') "| Pants"  ,PantsQuantity,PantsTotal,"|"
write (*,*) "|--------------------------------------------|"
  END IF

  IF (ShoesTotal>0) THEN
write (*,'(T2,A7,T22,I2,T39,F6.2,T47,A1)') "| Shoes"  ,ShoesQuantity,ShoesTotal,"|"
write (*,*) "|--------------------------------------------|"
  END IF

  IF (SuitTotal>0) THEN
write (*,'(T2,A6,T22,I2,T39,F6.2,T47,A1)') "| Suit"  ,SuitQuantity,SuitTotal,"|"
write (*,*) "|--------------------------------------------|"
  END IF

  IF (Subtotal>0) THEN
write (*,'(T2,A26,T39,F6.2,T47,A1)') "|                 Subtotal",Subtotal,"|"
write (*,*) "|--------------------------------------------|"
write (*,'(T2,A19,T39,F6.2,T47,A1)') "|         Sales Tax",SalesTax,"|"
write (*,*) "|--------------------------------------------|"
write (*,*) "|--------------------------------------------|"
write (*,'(T2,A15,T39,F6.2,T47,A1)') "|         Total",TotalCost,"|"
  END IF

  IF (Subtotal>0) THEN
write (*,*) "|--------------------------------------------|"
write (*,*) "|                  THANK YOU                 |"
write (*,*) "|--------------------------------------------|"
  END IF

CONTAINS
!--GetOrder--------------------------------------------------------------------------------------------
!  The subprogram asks the shopper for the amount of items for purchase to calculate cost of each item.
!  ACCEPTS: ItemCost,ItemName
!  RETURNS: NumItem
!------------------------------------------------------------------------------------------------------
SUBROUTINE GetOrder(ItemCost,ItemName,NumItem)

real(dp),INTENT(in)::ItemCost
character(*),INTENT(in)::ItemName
integer,INTENT(out)::NumItem

DO
 write (*,'("How many ",A," would you like to buy at ",F6.2,"?")') ItemName,ItemCost
 read (*,*,iostat=ioerror) NumItem
   IF (ioerror/=0) THEN
      write (*,*) "Please enter a positive integer."
   ELSE IF (NumItem<0) THEN
       write (*,*) "Quantity cannot be a negative value. Please reenter item quantity you would like to purchase."
   ELSE IF (NumItem>1000) THEN
       write (*,*) "Item quantity requested is not available. Please enter a smaller number of items to purchase."
   ELSE
       IF (NumItem>=0 .and. NumItem<=1000) EXIT
   END IF
END DO

END SUBROUTINE

END PROGRAM
