Advertisement
Guest User

Untitled

a guest
Jan 19th, 2013
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4.  
  5. public class OutputFormatted {
  6.  
  7.     private List<String> items = new ArrayList<String>();
  8.     private List<Integer> quantities = new ArrayList<Integer>();
  9.     private List<Double> prices = new ArrayList<Double>();
  10.  
  11.     private int longestItemName;
  12.     private int longestQuantity;
  13.     private int longestPrice;
  14.     private int longestTotal;
  15.    
  16.     public static void main(String[] args) {
  17.  
  18.         OutputFormatted output = new OutputFormatted();
  19.        
  20.         output.newPurchase("Soap", 5, 1.95);
  21.         output.newPurchase("Computer", 2, 1050.50);
  22.         output.newPurchase("Chips", 10, 0.95);
  23.        
  24.         output.output();
  25.     }
  26.    
  27.     public void newPurchase(String name, int quantity, double price) {
  28.        
  29.         items.add(name);
  30.         quantities.add(quantity);
  31.         prices.add(price);
  32.        
  33.         // whenever we add a new item, we compare the length of the string
  34.         // it would produce with what is the current longest string.
  35.         longestItemName = longest(longestItemName, name);
  36.         longestQuantity = longest(longestQuantity, quantity);
  37.         longestPrice = longestPrice(longestPrice, price);
  38.         longestTotal = longestPrice(longestTotal, (quantity * price));
  39.     }
  40.    
  41.     public void output() {
  42.        
  43.         // extra padding just to make it look a bit nicer.
  44.         // caution: it gets added whenever you call output.
  45.         int extraSpacePadding = 5;
  46.         longestItemName += extraSpacePadding;
  47.         longestQuantity += extraSpacePadding;
  48.         longestPrice += extraSpacePadding;
  49.         longestTotal += extraSpacePadding;
  50.        
  51.         for(int i = 0; i < items.size(); i++) {
  52.            
  53.             // using the %-NUMBERd formatting where
  54.             // NUMBER represents how much space is padded in.
  55.             System.out.printf("%-"+longestItemName+"s %-"+longestQuantity+"d %-"+longestPrice+".2f %-"+longestTotal+".2f\n", items.get(i), quantities.get(i), prices.get(i), quantities.get(i) * prices.get(i));
  56.         }
  57.     }
  58.    
  59.     private int longest(int i1, String s2) {
  60.        
  61.         String s1 = String.valueOf(i1);
  62.        
  63.         if(s1.length() > s2.length()) return s1.length();
  64.        
  65.         return s2.length();
  66.     }
  67.  
  68.     private int longestPrice(double d1, double d2) {
  69.        
  70.         int i1 = (int)d1;
  71.         int i2 = (int)d2;
  72.        
  73.         // make sure they're always at 2 decimal points.
  74.         // basically converting it a int will get rid of
  75.         // all the decimal points that doubles may have.
  76.         // add 2 since we always format prices as 2 decimal
  77.         // points. beware of rounding errors. i was too lazy
  78.         // to check that.
  79.        
  80.         return longest(i1, i2) + 2;
  81.     }
  82.    
  83.     private int longest(int i1, int i2) {
  84.        
  85.         String s2 = String.valueOf(i2);
  86.        
  87.         return longest(i1, s2);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement