Tassos

π.χ. : #1 - Με αντικειμενοστραφή σκέψη.

Mar 16th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. {                                      Visit:   http://g-lts.info/  for more code!                            }
  2.  
  3.  
  4. package first;
  5.  
  6. import java.util.Scanner;
  7.  
  8. class Product
  9. {
  10.     private String name;
  11.     private int score;
  12.     private double price;
  13.    
  14.    
  15.     public Product()
  16.     {
  17.         name = "";
  18.         score = 0;
  19.         price = 1;
  20.     }
  21.    
  22.     public void printData()
  23.     {
  24.         System.out.println("Name : " + name);
  25.         System.out.println("Price : " + price);
  26.         System.out.println("Score : " + score);
  27.        
  28.     }
  29.    
  30.    
  31.     public void read()
  32.     {
  33.         Scanner in = new Scanner(System.in);
  34.        
  35.         System.out.println("Please enter the product name : ");
  36.         name = in.nextLine();
  37.        
  38.         System.out.println("Please enter the product price : ");
  39.         price = in.nextDouble();
  40.        
  41.         System.out.println("Please enter the product score : ");
  42.         score = in.nextInt();
  43.        
  44.     }
  45.    
  46.     public boolean is_better_than ( Product other )
  47.     {
  48.         if ( (score/price) > (other.score/other.price) )
  49.             return true;
  50.        
  51.         return false;
  52.            
  53.     }
  54.    
  55.    
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62. // Main..
  63.  
  64.  
  65. public class Main {
  66.    
  67.    
  68.     public static void main (String[] args)
  69.     {
  70.        
  71.         Scanner in = new Scanner(System.in);
  72.  
  73.         Product best = new Product(); // Το βέλτιστο προϊόν.
  74.        
  75.        
  76.         boolean more = true ;
  77.        
  78.         while ( more )
  79.         {
  80.  
  81.            
  82.             Product current = new Product(); // Αντιπροσωπεύει το τρέχω προϊόν.
  83.            
  84.             current.read();
  85.            
  86.             if ( current.is_better_than(best) )
  87.             {
  88.                 best = current ;
  89.             }
  90.            
  91.            
  92.            
  93.             System.out.println("More data ? (1=Yes , 2=No) :");
  94.             int answer = in.nextInt();
  95.            
  96.             if ( answer != 1 )
  97.                 more = false;
  98.             in.nextLine();
  99.            
  100.            
  101.         }
  102.        
  103.  
  104.         best.printData();
  105.  
  106.        
  107.        
  108.     }
  109.  
  110. }
  111.  
  112.  
  113. {                                      Visit:   http://g-lts.info/  for more code!                            }
Advertisement
Add Comment
Please, Sign In to add comment