lamiastella

updateField

Jun 28th, 2014
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. /**
  2.      * Updates a field of a Car
  3.      *
  4.      * @param name the search string for the Car
  5.      * @param field the field which is to be updated
  6.      * @param val the new Value for the field
  7.      */
  8.    
  9.  
  10.     //Set the field for the car with the given name and year.
  11.     //Your function should support updating all fields except the car name and maker name.
  12.     public void updateField(String name,int year, String field, String val){
  13.         //TODO: Fill in
  14.        
  15.         Iterator<Car> carIter = cars.iterator();
  16.         Car car;
  17.         while (carIter.hasNext())
  18.                 {
  19.                     car = (Car) carIter.next();
  20.                     if (car.getName().equals(name))
  21.                         //if (car.getYear().equals(year)) Cannot invoke equals(int) on the primitive type int
  22.                         if (car.getYear()==year)
  23.                         {
  24.                             switch(field)
  25.                             {
  26.                                
  27.                                case "mpg":
  28.                                 {
  29.                                     car.setMpg(Integer.parseInt(val));
  30.                                 }
  31.                                    
  32.                                
  33.                                 case "power":
  34.                                 {
  35.                                     car.setPower(Integer.parseInt(val));
  36.                                 }
  37.                                    
  38.                                
  39.                                 case "year":
  40.                                 {
  41.                                     car.setYear(Integer.parseInt(val));
  42.                                 }
  43.                                    
  44.                                
  45.                                 case "region":
  46.                                 {  
  47.                                     car.setRegion(Integer.parseInt(val));
  48.                                 }
  49.                                    
  50.                                
  51.                                 case "cylinders":
  52.                                 {
  53.                                     car.setCylinders(Integer.parseInt(val));
  54.                                 }
  55.                                
  56.                            }
  57.                
  58.                         }
  59.                 }
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment