шпаргалки
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

2 years ago
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gazstation;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author denis
*/
class Car extends Thread{
public int tank;
private int max = 40;
public String name;
@Override
public void run() {
while(true){
fuel();
try {Thread.sleep(1000);} catch (InterruptedException ex) {}
int fuel = new Random().nextInt(40);
tank -= fuel;
System.out.println(name + ": израсходовал " + fuel + " л. Объем топлива: " + tank);
}
}
public Car(String name) {
this.name = name;
this.tank = new Random().nextInt(max-20);
}
public void fuel(){
int fuel = max - tank;
boolean res = GazStation.getState().column1.getFuel(fuel);
if(!res) {
System.out.println(name + ": не достаточно топлива. Ожидает.");
try {Thread.sleep(2000);} catch (InterruptedException ex) {}
fuel();
}else {
tank += fuel;
System.out.println("--------------------------" + name + ": заправил " + fuel + " л. Объем топлива: " + tank);
}
}
}