Guest User

Untitled

a guest
Apr 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. /*
  2.  * M N M Thanish
  3.  * UWU/CST/10/0040
  4.  * Mid Semester Examination
  5.  * 7/12/2011
  6.  *
  7.  */
  8.  
  9.  
  10. import java.util.*;
  11.  
  12. class Item
  13. {
  14.  
  15.   String code;
  16.   String title;
  17.   float uniprice;
  18.   float stock;
  19.   static float totalCost = 0;
  20.  
  21.   Item(){
  22.  
  23.   }
  24.  
  25.   Item(String ItemString){
  26.    
  27.     // RegEx in Java????
  28.  
  29.     this.code = ItemString.substring(0,7);
  30.     //System.out.println("["+this.code+"]");
  31.    
  32.     int titleStart,titleEnd;
  33.     titleStart = ItemString.indexOf('"');
  34.     titleEnd = ItemString.indexOf('"',titleStart+1);
  35.     if(titleStart!=-1 && titleEnd != -1)
  36.       this.title = ItemString.substring(titleStart+1,titleEnd);
  37.     else
  38.     {
  39.       titleStart = ItemString.indexOf(' ');
  40.       titleEnd = ItemString.indexOf(' ',titleStart+1);
  41.       this.title = ItemString.substring(titleStart+1,titleEnd);
  42.     }
  43.     //System.out.println("["+this.title+"]");
  44.    
  45.     int priceStart,priceEnd;
  46.     priceStart = ItemString.indexOf(' ',titleEnd);
  47.     priceEnd = ItemString.indexOf(' ',priceStart+1);
  48.     this.uniprice = Float.parseFloat(ItemString.substring(priceStart+1,priceEnd));
  49.     //System.out.println("["+this.uniprice+"]");
  50.    
  51.     int stockStart,stockEnd;
  52.     stockStart = priceEnd;
  53.     stockEnd = ItemString.length();
  54.     this.stock = Float.parseFloat(ItemString.substring(stockStart+1,stockEnd));
  55.     //System.out.println("["+this.stock+"]");
  56.  
  57.     Item.totalCost += this.uniprice * this.stock;
  58.  
  59.   }//Item()
  60.  
  61.   void display()
  62.   {
  63.  
  64.     System.out.println(this.code+"\t\t"+this.title+"\t\t"+this.uniprice+"\t\t\t"+this.stock);
  65.  
  66.   }
  67.  
  68.  
  69.  
  70. }//Item
  71.  
  72.  
  73.  
  74. class Store
  75. {
  76.  
  77.  public static void main(String args[])
  78.  {
  79.    
  80.    String items = "ITEM400 \"Sunlight soap\" 27 200 ITEM401 \"Anchor Packet (400gr)\" 170 50 ITEM412 Rice 80 300 ITEM420 Dhal 65.50 200 ITEM419 \"Cordial Bottle\" 225.50 10 ITEM411 \"Sun Flower Oil\" 160 20";
  81.  
  82.    int prevIndex = 0;
  83.    int currIndex = 0;
  84.    String currItemString;
  85.    
  86.    Vector v = new Vector();
  87.    
  88.    while(true){
  89.      currIndex = items.indexOf("ITEM",prevIndex+1);
  90.      if(currIndex == -1) break;
  91.      currItemString = items.substring(prevIndex,currIndex);
  92.      currItemString = currItemString.trim();
  93.      //System.out.println("["+currItemString+"]");
  94.      v.addElement(new Item(currItemString));
  95.      prevIndex = currIndex;
  96.      System.out.println();
  97.    }
  98.    
  99.    System.out.println("Item Code\tTitle\t\t\tUnit Price\t\tStock");
  100.    Item currentItem = new Item();
  101.    for(int i=0; i<v.size() ; i++) {
  102.     currentItem = (Item)v.get(i);
  103.     currentItem.display();
  104.    }
  105.    
  106.    System.out.println("Total Cost: "+Item.totalCost);
  107.    
  108.    
  109.  }//main()
  110.  
  111. }//Driver
Add Comment
Please, Sign In to add comment