Skip to content
This repository has been archived by the owner on Jun 11, 2018. It is now read-only.

Commit

Permalink
Added GUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirpichik committed Mar 25, 2018
1 parent a10446f commit 1be0d14
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 24 deletions.
4 changes: 2 additions & 2 deletions resources/logging.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
handlers = java.util.logging.ConsoleHandler
.level = FINE
java.util.logging.ConsoleHandler.level = FINE
.level=INFO
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format = [%1$tH:%1$tM:%1$tS %4$s] [%3$s]: %5$s%6$s%n
5 changes: 5 additions & 0 deletions src/org/polushin/carfactory/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public Stock<Prod> getStock() {
return stock;
}

/**
* @return Кол-во произведенной продукции.
*/
public abstract int getCount();

}
19 changes: 2 additions & 17 deletions src/org/polushin/carfactory/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package org.polushin.carfactory;

import org.polushin.carfactory.accessories.AccessoriesFactory;
import org.polushin.carfactory.carcass.CarcassesFactory;
import org.polushin.carfactory.cars.CarsFactory;
import org.polushin.carfactory.dealers.CarsShowroom;
import org.polushin.carfactory.engines.EnginesFactory;
import org.polushin.carfactory.gui.MainFrame;

import java.io.FileReader;
import java.io.IOException;
Expand All @@ -22,18 +18,7 @@ public static void main(String[] args) throws IOException {
config = ProductionConfig.fromReader(reader);
}

AccessoriesFactory accessories = new AccessoriesFactory(config);
CarcassesFactory carcasses = new CarcassesFactory(config);
EnginesFactory engines = new EnginesFactory(config);
CarsFactory cars = new CarsFactory(engines.getStock(), accessories.getStock(), carcasses.getStock(), config);
CarsShowroom showroom = new CarsShowroom(cars.getStock(), config);
new MainFrame(config);

Factory[] factories = new Factory[] {accessories, carcasses, engines, cars, showroom};

System.in.read();
System.out.println("EXIT");

for (Factory factory : factories)
factory.stopFactory();
}
}
15 changes: 14 additions & 1 deletion src/org/polushin/carfactory/accessories/AccessoriesFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.polushin.carfactory.Factory;
import org.polushin.carfactory.ProductionConfig;
import org.polushin.carfactory.ProductionProvider;

import java.util.logging.Logger;

Expand All @@ -12,16 +13,28 @@ public class AccessoriesFactory extends Factory<Accessory> {

static final Logger log = Logger.getLogger("Accessories");

private final ProductionProvider[] providers;

public AccessoriesFactory(ProductionConfig config) throws RuntimeException {
super(AccessoriesFactory.class.getSimpleName(), config.accessoriesProviders,
new AccessoriesStock(config.accessoriesStockSize));

providers = new ProductionProvider[config.accessoriesProviders];

for (int i = 0; i < config.accessoriesProviders; i++) {
try {
pool.runTask(new AccessoriesProvider(stock, config));
pool.runTask(providers[i] = new AccessoriesProvider(stock, config));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

@Override
public int getCount() {
int sum = 0;
for (ProductionProvider provider : providers)
sum += provider.getCount();
return sum;
}
}
10 changes: 9 additions & 1 deletion src/org/polushin/carfactory/carcass/CarcassesFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.polushin.carfactory.Factory;
import org.polushin.carfactory.ProductionConfig;
import org.polushin.carfactory.ProductionProvider;

import java.util.logging.Logger;

Expand All @@ -12,13 +13,20 @@ public class CarcassesFactory extends Factory<Carcass> {

static final Logger log = Logger.getLogger("Carcasses");

private final ProductionProvider provider;

public CarcassesFactory(ProductionConfig config) throws RuntimeException {
super(CarcassesFactory.class.getSimpleName(), 1, new CarcassesStock(config.carcassesStockSize));

try {
pool.runTask(new CarcassesProvider(stock, config));
pool.runTask(provider = new CarcassesProvider(stock, config));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public int getCount() {
return provider.getCount();
}
}
8 changes: 8 additions & 0 deletions src/org/polushin/carfactory/cars/CarsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public Stock<Car> getStock() {
return stock;
}

@Override
public int getCount() {
int sum = 0;
for (CarsFactoryWorker provider : workers)
sum += provider.getCount();
return sum;
}

@Override
public void stopFactory() {
controller.interrupt();
Expand Down
23 changes: 22 additions & 1 deletion src/org/polushin/carfactory/dealers/CarsShowroom.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.polushin.carfactory.Factory;
import org.polushin.carfactory.ProductionConfig;
import org.polushin.carfactory.ProductionProvider;
import org.polushin.carfactory.Stock;
import org.polushin.carfactory.cars.Car;

Expand All @@ -14,14 +15,34 @@ public class CarsShowroom extends Factory<Money> {

static final Logger log = Logger.getLogger("Showroom");

private final Dealer[] providers;

public CarsShowroom(Stock<Car> stock, ProductionConfig config) throws RuntimeException {
super("Car showroom", config.dealersCount, null);

providers = new Dealer[config.dealersCount];

for (int i = 0; i < config.dealersCount; i++) {
try {
pool.runTask(new Dealer(stock, config));
pool.runTask(providers[i] = new Dealer(stock, config));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

@Override
public int getCount() {
int sum = 0;
for (ProductionProvider provider : providers)
sum += provider.getCount();
return sum;
}

public int getCash() {
int sum = 0;
for (Dealer provider : providers)
sum += provider.getCash();
return sum;
}
}
10 changes: 9 additions & 1 deletion src/org/polushin/carfactory/engines/EnginesFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.polushin.carfactory.Factory;
import org.polushin.carfactory.ProductionConfig;
import org.polushin.carfactory.ProductionProvider;

import java.util.logging.Logger;

Expand All @@ -12,13 +13,20 @@ public class EnginesFactory extends Factory<Engine> {

static final Logger log = Logger.getLogger("Engines");

private final ProductionProvider provider;

public EnginesFactory(ProductionConfig config) throws RuntimeException {
super(EnginesFactory.class.getSimpleName(), 1, new EnginesStock(config.enginesStockSize));

try {
pool.runTask(new EnginesProvider(stock, config));
pool.runTask(provider = new EnginesProvider(stock, config));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

@Override
public int getCount() {
return provider.getCount();
}
}
85 changes: 85 additions & 0 deletions src/org/polushin/carfactory/gui/CarPartsPanel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,95 @@
package org.polushin.carfactory.gui;

import org.polushin.carfactory.ProductionConfig;
import org.polushin.carfactory.accessories.AccessoriesFactory;
import org.polushin.carfactory.carcass.CarcassesFactory;
import org.polushin.carfactory.engines.EnginesFactory;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;

/**
* Панель поставщиков для запчастей автомобилей.
*/
public class CarPartsPanel extends JPanel {

private static final int MAX_DELAY = 10000;
private static final String DISPLAY_FORMAT = "<html>%s: %d/%d,<br>Произведено: %d</html>";
private static final String ACCESSORIES = "Аксессуары";
private static final String CARCASSES = "Кузова";
private static final String ENGINES = "Двигатели";

private final JLabel accessoriesLabel;
private final JLabel carcassesLabel;
private final JLabel enginesLabel;

private final AccessoriesFactory accessories;
private final CarcassesFactory carcasses;
private final EnginesFactory engines;

private final ProductionConfig config;

public CarPartsPanel(AccessoriesFactory accessories, CarcassesFactory carcasses, EnginesFactory engines,
ProductionConfig config) {
this.accessories = accessories;
this.carcasses = carcasses;
this.engines = engines;
this.config = config;

setPreferredSize(new Dimension(600, 100));
setLayout(new GridLayout(1, 3));
Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
setBorder(BorderFactory.createRaisedBevelBorder());

JPanel panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(2, 1));
panel.add(accessoriesLabel = new JLabel());
JSlider accessoriesSlider = new JSlider(JSlider.HORIZONTAL, 0, MAX_DELAY, config.accessoriesDelay);
panel.add(accessoriesSlider);
accessoriesSlider.addChangeListener(e -> config.accessoriesDelay = accessoriesSlider.getValue());
add(panel);

panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(2, 1));
panel.add(carcassesLabel = new JLabel());
JSlider carcassesSlider = new JSlider(JSlider.HORIZONTAL, 0, MAX_DELAY, config.carcassesDelay);
panel.add(carcassesSlider);
carcassesSlider.addChangeListener(e -> config.carcassesDelay = carcassesSlider.getValue());
add(panel);

panel = new JPanel();
panel.setBorder(border);
panel.setLayout(new GridLayout(2, 1));
panel.add(enginesLabel = new JLabel());
JSlider enginesSlider = new JSlider(JSlider.HORIZONTAL, 0, MAX_DELAY, config.enginesDelay);
panel.add(enginesSlider);
enginesSlider.addChangeListener(e -> config.enginesDelay = enginesSlider.getValue());
add(panel);

updateStockValues();
new Timer(MainFrame.REPAINT_DELAY, e -> repaint()).start();
}

@Override
public void repaint() {
updateStockValues();
super.repaint();
}

private void updateStockValues() {
if (accessoriesLabel == null)
return;
accessoriesLabel.setText(String.format(DISPLAY_FORMAT, ACCESSORIES, accessories.getStock().getSize(),
config.accessoriesStockSize, accessories.getCount()));
carcassesLabel.setText(
String.format(DISPLAY_FORMAT, CARCASSES, carcasses.getStock().getSize(), config.carcassesStockSize,
carcasses.getCount()));
enginesLabel.setText(
String.format(DISPLAY_FORMAT, ENGINES, engines.getStock().getSize(), config.enginesStockSize,
engines.getCount()));
}
}
43 changes: 43 additions & 0 deletions src/org/polushin/carfactory/gui/CarsFactoryPanel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
package org.polushin.carfactory.gui;

import org.polushin.carfactory.ProductionConfig;
import org.polushin.carfactory.cars.CarsFactory;

import javax.swing.*;
import java.awt.*;

/**
* Панель фабрики автомобилей.
*/
public class CarsFactoryPanel extends JPanel {

private static final int MAX_DELAY = 10000;
private static final String DISPLAY_FORMAT = "<html>%s: %d/%d,<br>Произведено: %d</html>";
private static final String CARS = "Автомобили";

private final JLabel carsLabel;
private final CarsFactory cars;

private final ProductionConfig config;

public CarsFactoryPanel(CarsFactory cars, ProductionConfig config) {
this.cars = cars;
this.config = config;

setPreferredSize(new Dimension(600, 100));
setLayout(new GridLayout(1, 2));
setBorder(BorderFactory.createRaisedBevelBorder());

add(carsLabel = new JLabel());
JSlider carsSlider = new JSlider(JSlider.HORIZONTAL, 0, MAX_DELAY, config.carsDelay);
add(carsSlider);
carsSlider.addChangeListener(e -> config.carsDelay = carsSlider.getValue());

updateStockValues();
new Timer(MainFrame.REPAINT_DELAY, e -> repaint()).start();
}

@Override
public void repaint() {
updateStockValues();
super.repaint();
}

private void updateStockValues() {
if (carsLabel == null)
return;
carsLabel.setText(
String.format(DISPLAY_FORMAT, CARS, cars.getStock().getSize(), config.carsStockSize, cars.getCount()));
}

}
Loading

0 comments on commit 1be0d14

Please sign in to comment.