Skip to content
Open

Lab5 #34

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added AnalogClock/AnalogClock.jar
Binary file not shown.
Binary file added AnalogClock/resources/1.mp3
Binary file not shown.
Binary file added AnalogClock/resources/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 120 additions & 0 deletions AnalogClock/src/AnalogClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.net.URL;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class AnalogClock extends Application{
@Override
public void start(Stage stage) throws Exception {

Image image = new Image("/2.png", 580, 580, false, false);
ImageView imageView = new ImageView(image);
Calendar calendar = GregorianCalendar.getInstance();

URL resource = AnalogClock.class.getResource("/1.mp3");
Media media = new Media(resource.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();

Circle face = new Circle(300,300,7);
face.setFill(Color.BLACK);

Line secondHand = new Line(0,35,0,-200);
secondHand.setTranslateX(300);
secondHand.setTranslateY(300);
secondHand.setStroke(Color.YELLOW);
secondHand.setStrokeWidth(10);
secondHand.setStrokeLineCap(StrokeLineCap.ROUND);

Line minuteHand = new Line(0,0,0,-170);
minuteHand.setTranslateX(300);
minuteHand.setTranslateY(300);
minuteHand.setStroke(Color.YELLOW);
minuteHand.setStrokeWidth(10);
minuteHand.setStrokeLineCap(StrokeLineCap.ROUND);

Line hourHand = new Line(0,0,0,-140);
hourHand.setTranslateX(300);
hourHand.setTranslateY(300);
hourHand.setStroke(Color.YELLOW);
hourHand.setStrokeWidth(10);
hourHand.setStrokeLineCap(StrokeLineCap.ROUND);

double seedSecondDegrees = calendar.get(Calendar.SECOND)*(360/60);
double seedMinuteDegrees = (calendar.get(Calendar.MINUTE)+seedSecondDegrees/360.0)*(360/60);
double seedHourDegrees = (calendar.get(Calendar.HOUR)+seedMinuteDegrees/360.0)*(360/12);

Rotate secondRotate = new Rotate(seedSecondDegrees);
Rotate minuteRotate = new Rotate(seedMinuteDegrees);
Rotate hourRotate = new Rotate(seedHourDegrees);

secondHand.getTransforms().add(secondRotate);
minuteHand.getTransforms().add(minuteRotate);
hourHand.getTransforms().add(hourRotate);

final Timeline secondTime = new Timeline(
new KeyFrame(
Duration.seconds(60),
new KeyValue(
secondRotate.angleProperty(),
360 + seedSecondDegrees,
Interpolator.LINEAR
)
)
);

final Timeline minuteTime = new Timeline(
new KeyFrame(
Duration.minutes(60),
new KeyValue(
minuteRotate.angleProperty(),
360 + seedMinuteDegrees,
Interpolator.LINEAR
)
)
);

Timeline hourTime = new Timeline(
new KeyFrame(
Duration.hours(12),
new KeyValue(
hourRotate.angleProperty(),
360 + seedHourDegrees,
Interpolator.LINEAR
)
)
);

secondTime.setCycleCount(Animation.INDEFINITE);
minuteTime.setCycleCount(Animation.INDEFINITE);
hourTime.setCycleCount(Animation.INDEFINITE);

secondTime.play();
minuteTime.play();
hourTime.play();

Group root = new Group(imageView, hourHand, minuteHand, secondHand, face);
Scene scene = new Scene(root, 580,580);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}
Binary file added CoffeeAutomat/CoffeeAutomat.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions CoffeeAutomat/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>niit</groupId>
<artifactId>CoffeeAutomat</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>


</project>
166 changes: 166 additions & 0 deletions CoffeeAutomat/src/main/java/sample/Automat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package sample;

import sun.misc.IOUtils;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

public class Automat {
enum STAGES {
OFF, WAIT,ACCEPT, CHECK, COOK}
private int cash;
private ArrayList<String> menu = new ArrayList<String>();
int[] prices;
private STAGES stage;

Automat(){
//download menu
/* try{ //in the first time I wrote like that
// URL resource= Automat.class.getResource("/menu.txt");
// File file = Paths.get(resource.toURI()).toFile();
// FileReader fr = new FileReader(file);

//for jar need to read like this, but we into constructor(static method,
// toString() - not static!!!
InputStream inputStream=getClass().getResourceAsStream("/menu.txt");
String fr= (String) IOUtils.toString(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(fr);
String line=null;

while ((line=reader.readLine()) != null) {
menu.add(line);
}
}
catch (URISyntaxException e) {
e.printStackTrace(); }
catch (FileNotFoundException e){
e.printStackTrace(); }
catch (java.io.IOException e){
e.printStackTrace(); }
*/
//I am forced to initialize like this
menu.add(" Expresso");
menu.add(" Americano");
menu.add("Cappuccino");
menu.add(" Mokachino");
menu.add(" Dalgona");
prices = new int[]{30,35,45,40,45}; //array initialization
stage = STAGES.OFF; //ready for work!
}

public int getCash(){
return cash;
}

public STAGES on(){
if (stage == STAGES.OFF)
stage = STAGES.WAIT;

return stage;
}

public STAGES getStage(){
return stage;
}

public ArrayList<String> getMenu(){
ArrayList<String> theMenu = new ArrayList<String>();
if (stage == STAGES.WAIT || stage == STAGES.ACCEPT)
for (int i = 0; i < menu.size(); i++) {
theMenu.add(menu.get(i) + " " + prices[i] + " rub.");
}

return theMenu;
}

public int coin(int coins){
if (stage == STAGES.WAIT || stage == STAGES.ACCEPT) {
stage = STAGES.ACCEPT;
cash=cash+coins;
}

return cash;
}

public boolean choice(int numberCoffee){

if (stage == STAGES.ACCEPT) {
stage = STAGES.CHECK;
//System.out.println("Choose coffee from the menu."); //can to be, but we have args of this method

if(check(prices[numberCoffee])) { //check money
cook(numberCoffee);
return true;}

else //if cash<price
cancel(true); //go to WAIT
}

return false;
}
protected boolean check(int priceCoffee){
if (stage == STAGES.CHECK) {
if((cash-priceCoffee)>=0){
cash=cash-priceCoffee; //for surrender
return true;
}
}

return false;
}

protected STAGES cook(int numberCoffee){
if (stage == STAGES.CHECK) {
stage = STAGES.COOK;
//System.out.println("Making " + menu.get(numberCoffee) + " begins.");
//System.out.println("Expect...");
try {
Thread.sleep(1500);
} catch (InterruptedException e){
e.printStackTrace();
}
//System.out.println("Coffee tree for your coffee has already been planted.");
//System.out.println("It remains to wait for the first grains...");
try {
Thread.sleep(2000);
} catch (InterruptedException e){
e.printStackTrace();
}
//System.out.println("This is a joke)))");
finish(numberCoffee);
}

return stage;
}

protected int finish(int numberCoffee){
if (stage == STAGES.COOK) {
stage = STAGES.WAIT;
if(cash>0) //for surrender
cash=0;//System.out.println("Take your remaining money: "+cash+" rub.");
}

return cash;
}


public int cancel(boolean leaving){
if (stage == STAGES.ACCEPT || stage == STAGES.CHECK || stage == STAGES.WAIT) {
stage = STAGES.WAIT;
if((cash>0)&& !leaving) //for surrender in this method too, because user can not want coffee
cash=0;//System.out.println("Take your remaining money: "+cash+" rub.");
}

return cash;
}

public STAGES off(){
if (stage == STAGES.WAIT)
stage = STAGES.OFF;

return stage;
}
}
Loading