Compare commits

...

13 Commits

Author SHA1 Message Date
Konrad Jasiński
ac3360c82f changed class name typo 2021-10-12 21:12:20 +02:00
Konrad Jasiński
f70652e7a6 rearranged code 2021-10-12 21:11:21 +02:00
Konrad Jasiński
95b1bcface send notification when buying last item 2021-10-12 21:07:53 +02:00
Konrad Jasiński
fb1a69e3d3 adjusted to the requirements - added quantity 2021-10-12 20:44:54 +02:00
Konrad Jasiński
7ee97c9a7a added preparing meals 2021-10-12 19:21:15 +02:00
Konrad Jasiński
02c1d29e9f added input of money 2021-10-12 19:07:31 +02:00
Konrad Jasiński
707b932c8b added Inventory and VendingMachine 2021-10-11 21:54:58 +02:00
Konrad Jasiński
cb38d7392b changed menu layout 2021-10-11 19:54:58 +02:00
Konrad Jasiński
01b89d7e77 added meal and meal menu 2021-10-11 19:49:48 +02:00
Konrad Jasiński
3a1266c485 added snack and snack menu 2021-10-11 19:42:41 +02:00
Konrad Jasiński
ecf6089dbb added drink menu 2021-10-11 19:32:35 +02:00
Konrad Jasiński
2d89456f54 added first order sketch to main 2021-10-11 19:28:38 +02:00
Konrad Jasiński
8b94f4945a initial commit 2021-10-10 16:30:16 +02:00
27 changed files with 576 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Projekt_APO-Automat_spozywczy.iml" filepath="$PROJECT_DIR$/.idea/Projekt_APO-Automat_spozywczy.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,16 @@
package main.java.products;
import java.util.ArrayList;
public abstract class AbstractMenu<T extends Product> extends ArrayList<T> {
public void showMenu() {
showMenuName();
for (int i = 0; i < size(); i++) {
System.out.println(i + 1 + ". " + get(i) + " " + get(i).getPrice());
}
}
protected abstract void showMenuName();
}

View File

@ -0,0 +1,15 @@
package main.java.products;
public enum Denomination {
TEN_GR(0.1), TWENTY_GR(0.2), FIFTY_GR(0.5), ONE(1), TWO(2), FIVE(5), TEN(10), TWENTY(20), FIFTY(50), HUNDRED(100), TWO_HUNDRET(200);
private double denomination;
private Denomination(double denomination) {
this.denomination = denomination;
}
public double getDenomination() {
return denomination;
}
}

View File

@ -0,0 +1,34 @@
package main.java.products;
public class Drink implements Product {
private final String name;
private final double price;
int quantity;
public Drink(String aName, double aPrice, int aQuantity) {
this.name = aName;
this.price = aPrice;
this.quantity = aQuantity;
}
@Override
public double getPrice() {
return price;
}
@Override
public int getQuantity() {
return quantity;
}
@Override
public String toString() {
return name;
}
public void setQuantity(int aQuantity) {
quantity = aQuantity;
}
}

View File

@ -0,0 +1,21 @@
package main.java.products;
public class DrinkMenu extends AbstractMenu<Drink> {
DrinkMenu() {
Drink cola = new Drink("Cola", 4.50, 1);
Drink water = new Drink("Water", 2.10, 5);
Drink orangeJuice = new Drink("Orange juice", 3.50, 5);
add(cola);
add(water);
add(orangeJuice);
}
@Override
protected void showMenuName() {
System.out.println("Drinks menu");
System.out.println("Choose your drink");
}
}

View File

@ -0,0 +1,200 @@
package main.java.products;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private final static Scanner sc = new Scanner(System.in);
public static void main(String[] aArg) {
useMachine();
}
public static void useMachine() {
DrinkMenu drinkMenu = new DrinkMenu();
SnackMenu snackMenu = new SnackMenu();
MealMenu mealMenu = new MealMenu();
while (true) {
System.out.println("Welcome!\nWhat do you want to buy?");
System.out.println("1. A drink");
System.out.println("2. A snack");
System.out.println("3. A meal");
int selectedType = sc.nextInt();
double price = 0;
if (selectedType == 1) {
Drink drink = orderADrink(drinkMenu);
price = drink.getPrice();
} else if (selectedType == 2) {
Snack snack = orderASnack(snackMenu);
price = snack.getPrice();
} else if (selectedType == 3) {
price = orderAMeal(mealMenu);
}
System.out.println("Choose payment option");
System.out.println("1. Cash");
System.out.println("2. Credit card");
int paymentType = sc.nextInt();
if (paymentType == 1) {
payInCash(price);
} else if (paymentType == 2) {
payWithCard(price);
}
if (selectedType == 3) {
System.out.println("Preparing a meal...");
System.out.println("Take your meal");
} else {
System.out.println("Take your product. Thank you!");
}
}
}
private static Drink orderADrink(DrinkMenu drinkMenu) {
drinkMenu.showMenu();
int opt;
Drink orderedDrink = null;
while (orderedDrink == null) {
opt = sc.nextInt();
if (opt < 1 || opt > 4) {
System.out.println("You have chosen wrong option");
} else {
orderedDrink = drinkMenu.get(opt - 1);
}
}
System.out.println("You ordered " + orderedDrink);
System.out.println("Price: " + orderedDrink.getPrice());
if (orderedDrink.getQuantity() == 1) {
System.out.println("This was the last " + orderedDrink.toString());
System.out.println("Notification was sent to the service staff about the lack of drink");
drinkMenu.remove(orderedDrink);
}
orderedDrink.setQuantity(orderedDrink.getQuantity() - 1);
return orderedDrink;
}
private static Snack orderASnack(SnackMenu snackMenu) {
snackMenu.showMenu();
int opt;
Snack orderedSnack = null;
while (orderedSnack == null) {
opt = sc.nextInt();
if (opt < 1 || opt > 4) {
System.out.println("You have chosen wrong option");
} else {
orderedSnack = snackMenu.get(opt - 1);
}
}
System.out.println("You ordered " + orderedSnack);
System.out.println("Price: " + orderedSnack.getPrice());
if (orderedSnack.getQuantity() == 1) {
System.out.println("This was the last " + orderedSnack.toString());
System.out.println("Notification was sent to the service staff about the lack of drink");
snackMenu.remove(orderedSnack);
}
orderedSnack.setQuantity(orderedSnack.getQuantity() - 1);
return orderedSnack;
}
private static double orderAMeal(MealMenu mealMenu) {
mealMenu.showMenu();
int opt;
Meal orderedMeal = null;
while (orderedMeal == null) {
opt = sc.nextInt();
if (opt < 1 || opt > 4) {
System.out.println("You have chosen wrong option");
} else {
orderedMeal = mealMenu.get(opt - 1);
}
}
System.out.println("You ordered " + orderedMeal);
System.out.println("Price: " + orderedMeal.getPrice());
if (orderedMeal.getQuantity() == 1) {
System.out.println("This was the last " + orderedMeal.toString());
System.out.println("Notification was sent to the service staff about the lack of meal");
System.out.println("Staff prepared 5 new meals");
orderedMeal.setQuantity(5);
}
orderedMeal.setQuantity(orderedMeal.getQuantity() - 1);
return orderedMeal.getPrice();
}
private static void payWithCard(double price) {
System.out.println("Follow the instructions on the pin pad");
System.out.println("To pay: " + price);
System.out.println("Type 'pay' to pay");
while (true) {
String cardPayment = sc.nextLine();
if (cardPayment.equals("pay")) {
break;
} else if (cardPayment.equals("c")) {
cancelTransaction(price);
return;
}
}
}
private static void payInCash(double price) {
System.out.println("Insert a coin or banknote.");
double sum = 0;
while (true) {
String payment;
payment = sc.next();
if (payment.equals("c")) {
cancelTransaction(sum);
return;
}
if (Arrays.stream(Denomination.values()).anyMatch((t) -> t.getDenomination() == (Double.parseDouble(payment)))) {
sum += Double.parseDouble(payment);
} else {
System.out.println("Wrong input");
continue;
}
System.out.println("Credits: " + sum);
if (sum == price) {
return;
} else if (sum > price) {
giveChange(sum, price);
return;
}
}
}
private static void cancelTransaction(double sum) {
System.out.println("Take change from the dispenser: " + (sum));
}
private static void giveChange(double sum, double price) {
DecimalFormat df = new DecimalFormat("###.##");
System.out.println("Take change from the dispenser: " + df.format((sum - price)));
}
}

View File

@ -0,0 +1,34 @@
package main.java.products;
public class Meal implements Product {
private final String name;
private final double price;
private int quantity;
public Meal(String aName, double aPrice, int aQuantity) {
this.name = aName;
this.price = aPrice;
this.quantity = aQuantity;
}
@Override
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int aQuantity) {
quantity = aQuantity;
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,18 @@
package main.java.products;
public class MealMenu extends AbstractMenu<Meal> {
MealMenu() {
Meal pizza = new Meal("Pizza", 15.50, 1);
Meal spaghetti = new Meal("Spaghetti", 14.10, 5);
Meal fries = new Meal("Fries", 6.40, 5);
add(pizza);
add(spaghetti);
add(fries);
}
@Override
protected void showMenuName() {
System.out.println("Meals menu");
System.out.println("Choose your meal");
}
}

View File

@ -0,0 +1,10 @@
package main.java.products;
public interface Product {
double getPrice();
int getQuantity();
void setQuantity(int aQuantity);
}

View File

@ -0,0 +1,36 @@
package main.java.products;
public class Snack implements Product {
private final String name;
private final double price;
int quantity;
public Snack(String aName, double aPrice, int aQuantity) {
this.name = aName;
this.price = aPrice;
this.quantity = aQuantity;
}
@Override
public double getPrice() {
return price;
}
@Override
public int getQuantity() {
return quantity;
}
@Override
public void setQuantity(int aQuantity) {
quantity = aQuantity;
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,19 @@
package main.java.products;
public class SnackMenu extends AbstractMenu<Snack> {
SnackMenu() {
Snack lion = new Snack("Lion", 3.50, 5);
Snack mars = new Snack("Mars", 3.10, 5);
Snack snickers = new Snack("Snickers", 3.40, 5);
add(lion);
add(mars);
add(snickers);
}
@Override
protected void showMenuName() {
System.out.println("Snacks menu");
System.out.println("Choose your snack");
}
}