java assignment

profilesehj
Activity1B.zip

Activity1B.java

Activity1B.java

public   class   Activity1B   {
    public   static   void  main ( String []  args )   {
       Item []  inventory  =   new   Item [ 4 ];

      inventory [ 0 ]   =   new   Item ( "machine bolt" ,   50 ,   "M50-H" ,   120 );
      inventory [ 1 ]   =   new   Item ( "phillips screw" ,   25 ,   "P25-16" ,   750 );
      inventory [ 2 ]   =   new   Item ( "lock washer" ,   12 ,   "W12-L" ,   400 );
      inventory [ 3 ]   =   new   Item ( "drywall screw" ,   38 ,   "P38-DW" ,   2400 );

      printInventory ( inventory );

      sell ( inventory [ 0 ],   90 );
      sell ( inventory [ 1 ],   750 );
      sell ( inventory [ 2 ],   600 );
      sell ( inventory [ 3 ],   2500 );

      printInventory ( inventory );

      inventory [ 0 ]. order ( 90 );
      inventory [ 1 ]. order ( 1 );
      inventory [ 2 ]. order ( 220 );
      inventory [ 3 ]. order ( 50 );

      printInventory ( inventory );

       System . out . println ( "\nEnd of processing." );
    }

    public   static   void  sell ( Item  item ,   int  amount )   {
     int  inStock ;

    inStock  =  item . sell ( amount );

     System . out . print ( "Selling "   +  amount  +   " of item: " );
     if   ( inStock  ==  amount )   {
         System . out . println ( "all are in stock." );
     }   else   {
         System . out . println ( inStock  +   " in stock, "   +   ( amount  -  inStock )   +   " on backorder." );
     }
    }

    public   static   void  printInventory ( Item []  inventory )   {
     System . out . println ( "Inventory:" );
     for   ( int  i  =   0 ;  i  <  inventory . length ;  i ++ )   {
         System . out . println ( inventory [ i ]);
     }
    }
}

class   Item   {
    private   String  name ;
    private   int  size ;
    private   String  code ;
    private   int  quantity ;

    public   Item ( String  name ,   int  size ,   String  code ,   int  quantity )   {
       this . name  =  name ;
       this . size  =  size ;
       this . code  =  code ;
       this . quantity  =  quantity ;
    }

    public   int  sell ( int  amount )   {
     int  result  =  amount ;

     if   ( amount  >  quantity )   {
        result  =  quantity ;
     }
    quantity  -=  amount ;

     return  result ;
    }

    public   void  order ( int  amount )   {
      quantity  +=  amount ;
    }

    public   String  toString ()   {
     String  result ;
    
    result  =   "Item '"   +  name  +   "' size="   +  size  +   "mm code='"   +  code ;
     if   ( quantity  >=   0 )   {
        result  +=   " quantity="   +  quantity ;
     }   else   {
        result  +=   " quantity=0 ("   +   - quantity  +   " backordered)" ;
     }
    
     return  result ;
    }
}