Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * M N M Thanish
- * UWU/CST/10/0040
- * Mid Semester Examination
- * 7/12/2011
- *
- */
- import java.util.*;
- class Item
- {
- String code;
- String title;
- float uniprice;
- float stock;
- static float totalCost = 0;
- Item(){
- }
- Item(String ItemString){
- // RegEx in Java????
- this.code = ItemString.substring(0,7);
- //System.out.println("["+this.code+"]");
- int titleStart,titleEnd;
- titleStart = ItemString.indexOf('"');
- titleEnd = ItemString.indexOf('"',titleStart+1);
- if(titleStart!=-1 && titleEnd != -1)
- this.title = ItemString.substring(titleStart+1,titleEnd);
- else
- {
- titleStart = ItemString.indexOf(' ');
- titleEnd = ItemString.indexOf(' ',titleStart+1);
- this.title = ItemString.substring(titleStart+1,titleEnd);
- }
- //System.out.println("["+this.title+"]");
- int priceStart,priceEnd;
- priceStart = ItemString.indexOf(' ',titleEnd);
- priceEnd = ItemString.indexOf(' ',priceStart+1);
- this.uniprice = Float.parseFloat(ItemString.substring(priceStart+1,priceEnd));
- //System.out.println("["+this.uniprice+"]");
- int stockStart,stockEnd;
- stockStart = priceEnd;
- stockEnd = ItemString.length();
- this.stock = Float.parseFloat(ItemString.substring(stockStart+1,stockEnd));
- //System.out.println("["+this.stock+"]");
- Item.totalCost += this.uniprice * this.stock;
- }//Item()
- void display()
- {
- System.out.println(this.code+"\t\t"+this.title+"\t\t"+this.uniprice+"\t\t\t"+this.stock);
- }
- }//Item
- class Store
- {
- public static void main(String args[])
- {
- 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";
- int prevIndex = 0;
- int currIndex = 0;
- String currItemString;
- Vector v = new Vector();
- while(true){
- currIndex = items.indexOf("ITEM",prevIndex+1);
- if(currIndex == -1) break;
- currItemString = items.substring(prevIndex,currIndex);
- currItemString = currItemString.trim();
- //System.out.println("["+currItemString+"]");
- v.addElement(new Item(currItemString));
- prevIndex = currIndex;
- System.out.println();
- }
- System.out.println("Item Code\tTitle\t\t\tUnit Price\t\tStock");
- Item currentItem = new Item();
- for(int i=0; i<v.size() ; i++) {
- currentItem = (Item)v.get(i);
- currentItem.display();
- }
- System.out.println("Total Cost: "+Item.totalCost);
- }//main()
- }//Driver
Add Comment
Please, Sign In to add comment