Advertisement
irmantas_radavicius

Untitled

Apr 2nd, 2022
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class TooManyWheelsException extends RuntimeException {
  5. }
  6.  
  7. class Vehicle {
  8.     private String name;
  9.     private ArrayList<Wheel> wheels = new ArrayList<>();
  10.     protected int needsWheels = 0;
  11.    
  12.     public Vehicle(String name){
  13.         setName(name);
  14.     }
  15.    
  16.     public void setName(String name){
  17.         this.name = name;
  18.     }
  19.    
  20.     public String getName(){
  21.         return name;
  22.     }
  23.    
  24.     public void addWheel(Wheel wheel){
  25.         if (!hasAllWheels())
  26.             wheels.add(wheel);
  27.         else
  28.             throw new TooManyWheelsException();
  29.     }
  30.    
  31.     public boolean hasAllWheels(){
  32.         return (wheels.size() == needsWheels);         
  33.     }
  34.    
  35.     public int getCurWheels(){
  36.         return wheels.size();
  37.     }
  38.    
  39.     public String toString(){
  40.         return "Vehicle " + name + " " + needsWheels;
  41.     }
  42. }
  43.  
  44. class Car extends Vehicle {
  45.     public Car(){
  46.         super("Car");
  47.         this.needsWheels = 4;
  48.     }
  49.    
  50.        
  51.     public String toString(){
  52.         return "Car " + needsWheels;
  53.     }
  54. }
  55.  
  56. class Bicycle extends Vehicle {
  57.     public Bicycle(){
  58.         super("Bicycle");      
  59.         this.needsWheels = 2;
  60.     }
  61.    
  62.     public String toString(){
  63.         return "Bicycle " + needsWheels;
  64.     }
  65. }
  66.  
  67. class Wheel {
  68.     public int diameter;
  69. }
  70.  
  71. public class Sandbox {  
  72.    
  73.     public static void main(String args[]) {       
  74.         try {
  75.             ArrayList<Vehicle> vehicles = new ArrayList<>();
  76.             for(int i = 0; i < 12; ++i){
  77.                 if (Math.random() < 0.3){
  78.                     vehicles.add(new Car());
  79.                 } else if (Math.random() < 0.6){
  80.                     vehicles.add(new Bicycle());
  81.                 } else {
  82.                     vehicles.add(new Vehicle("unknown"));
  83.                 }              
  84.                
  85.                 Vehicle v = vehicles.get(i);
  86.                 //while(!v.hasAllWheels())
  87.                 while(true)
  88.                 {
  89.                     try {
  90.                         System.out.println(v + " currently has " + v.getCurWheels() + " wheels");
  91.                         v.addWheel(new Wheel());
  92.                     } catch(TooManyWheelsException tmwe){
  93.                         break;
  94.                     }
  95.                 }
  96.                
  97.                
  98.             }
  99.             /*
  100.             for(int i = 0; i < vehicles.size(); ++i){
  101.                 System.out.println(vehicles.get(i));
  102.             }
  103.             */
  104.            
  105.         } catch(Exception e){
  106.             e.printStackTrace();           
  107.             System.out.println("Unexpected error, sorry!");
  108.         }          
  109.     }  
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement