Advertisement
JoshuaStrutt

9.02.15 Informatik HA OOP Auto

Feb 9th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1.  
  2. public class AutoErsteller {
  3.  
  4.     public static void main(String[] args) {
  5.        
  6.         Auto fiat500 = new Auto("Fiat500");
  7.         fiat500.fahren();
  8.         fiat500.fahren();
  9.         fiat500.fahren();
  10.         fiat500.tankAuffuellen();
  11.         fiat500.tankAuffuellen();
  12.         fiat500.getBoardComputer().logBoardComputer();
  13.  
  14.     }
  15.  
  16. }
  17.  
  18.  
  19. public class Auto {
  20.    
  21.     private String carType;
  22.     public String getCarType() {
  23.         return carType;
  24.     }
  25.     public void setCarType(String carType) {
  26.         this.carType = carType;
  27.     }
  28.    
  29.     private BoardComputer _BoardComputer = new BoardComputer();
  30.     public BoardComputer getBoardComputer(){
  31.         return _BoardComputer;
  32.     }
  33.    
  34.     public void fahren(){
  35.         _BoardComputer.setKiloMeterStand(_BoardComputer.getKiloMeterStand() + 10);
  36.     }
  37.    
  38.     public void tankAuffuellen(){
  39.         _BoardComputer.setTankFuellung(_BoardComputer.getTankFuellung() + 10);
  40.     }
  41.    
  42.     public Auto(String nType){
  43.         carType = nType;
  44.     }
  45.    
  46. }
  47.  
  48.  
  49. import java.lang.reflect.*;
  50. public class BoardComputer {
  51.    
  52.     private Double kiloMeterStand = 0.0;
  53.     public Double getKiloMeterStand() {
  54.         return kiloMeterStand;
  55.     }
  56.     public void setKiloMeterStand(Double kiloMeterStand) {
  57.         this.kiloMeterStand = kiloMeterStand;
  58.     }
  59.    
  60.     private Double tankFuellung = 0.0;
  61.     public Double getTankFuellung() {
  62.         return tankFuellung;
  63.     }
  64.     public void setTankFuellung(Double tankFuellung) {
  65.         this.tankFuellung = tankFuellung;
  66.     }
  67.    
  68.  
  69.     public void logBoardComputer(){
  70.         Field[] fields = this.getClass().getDeclaredFields();
  71.         for(int i = 0; i< fields.length;i++){
  72.             try{
  73.                 System.out.println("[BoardComputer]: " + fields[i].getName() + " = " + String.valueOf(fields[i].get(this)));
  74.             }
  75.             catch(Exception e){
  76.                 System.out.println("[BoardComputer]: Could not get the value of." + fields[i].getName() + ". Exception thrown: " + e.toString());
  77.             }
  78.         }
  79.     }
  80.    
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement