Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Updates a field of a Car
- *
- * @param name the search string for the Car
- * @param field the field which is to be updated
- * @param val the new Value for the field
- */
- //Set the field for the car with the given name and year.
- //Your function should support updating all fields except the car name and maker name.
- public void updateField(String name,int year, String field, String val){
- //TODO: Fill in
- Iterator<Car> carIter = cars.iterator();
- Car car;
- while (carIter.hasNext())
- {
- car = (Car) carIter.next();
- if (car.getName().equals(name))
- //if (car.getYear().equals(year)) Cannot invoke equals(int) on the primitive type int
- if (car.getYear()==year)
- {
- switch(field)
- {
- case "mpg":
- {
- car.setMpg(Integer.parseInt(val));
- }
- case "power":
- {
- car.setPower(Integer.parseInt(val));
- }
- case "year":
- {
- car.setYear(Integer.parseInt(val));
- }
- case "region":
- {
- car.setRegion(Integer.parseInt(val));
- }
- case "cylinders":
- {
- car.setCylinders(Integer.parseInt(val));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment