Created
May 13, 2017 23:05
-
-
Save 13andrew13/bcb67a742f28a91dfbe94a7b7ca37888 to your computer and use it in GitHub Desktop.
Создать таблицу «Меню в ресторане». Колонки: название блюда,
его стоимость, вес, наличие скидки. Написать код для
добавления записей в таблицу и их выборки по критериям
«стоимость от-до», «только со скидкой», выбрать набор блюд
так, чтобы их суммарный вес был не более 1 КГ.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ua.kiev.prog; | |
import java.util.ArrayList; | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
/** | |
* Created by andrew on 04.05.17. | |
*/ | |
public class App { | |
static Menu menu = new Menu(); | |
static Scanner scanner = new Scanner(System.in); | |
public static void main(String[] args) { | |
while (true){ | |
try{ | |
System.out.println("1: add meal"); | |
System.out.println("2: take meals which price is between"); | |
System.out.println("3: take meals with sales"); | |
System.out.println("4: take meals which sum weight would be equal 1 kg"); | |
int x = scanner.nextInt(); | |
switch (x){ | |
case 1:addMeal();break; | |
case 2:mealsWithPrice();break; | |
case 3: mealsWithDiscount();break; | |
case 4:mealsWithSumWeight1KG();break; | |
default:break; | |
} | |
} | |
catch (Exception ex){ | |
ex.printStackTrace(); | |
break; | |
} | |
} | |
} | |
private static void mealsWithSumWeight1KG() { | |
ArrayList<Meal> meals = menu.mealsWithSumWeight1KG(); | |
for (Meal meal : meals) { | |
System.out.println(meal); | |
} | |
} | |
private static void mealsWithDiscount() { | |
ArrayList<Meal> meals = menu.mealsWithDiscount(); | |
for (Meal meal : meals) { | |
System.out.println(meal); | |
} | |
} | |
private static void mealsWithPrice() { | |
System.out.println("print price from:"); | |
float start = scanner.nextFloat(); | |
System.out.println("to:"); | |
float end = scanner.nextFloat(); | |
ArrayList<Meal> meals = menu.mealsWithPrice(start,end); | |
for (Meal meal : meals) { | |
System.out.println(meal); | |
} | |
} | |
private static void addMeal() { | |
System.out.println("print name of meal:"); | |
String name = scanner.next(); | |
System.out.println("print price:"); | |
float price = scanner.nextFloat(); | |
System.out.println("print weight:"); | |
float weihgt = scanner.nextFloat(); | |
System.out.println("print if there sales true/false:"); | |
boolean sales = scanner.nextBoolean(); | |
menu.addMeal(new Meal(name,price,weihgt,sales)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ua.kiev.prog; | |
import javax.persistence.*; | |
import java.io.Serializable; | |
/** | |
* Created by andrew on 04.05.17. | |
*/ | |
@Entity | |
@Table(name ="Menu") | |
public class Meal { | |
@Id | |
@GeneratedValue | |
@Column(name = "id") | |
private long id; | |
@Column(name = "name") | |
private String name; | |
@Column(name = "price") | |
private float price; | |
@Override | |
public String toString() { | |
return "Meal{" + | |
"name='" + name + '\'' + | |
", price=" + price + | |
", weight=" + weight + | |
", sales=" + sales + | |
'}'; | |
} | |
@Column(name = "weight") | |
private float weight; | |
@Column(name= "sales") | |
private boolean sales; | |
public Meal(String name, float price, float weight, boolean sales) { | |
this.name = name; | |
this.price = price; | |
this.weight = weight; | |
this.sales = sales; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public float getPrice() { | |
return price; | |
} | |
public void setPrice(float price) { | |
this.price = price; | |
} | |
public float getWeight() { | |
return weight; | |
} | |
public void setWeight(float weight) { | |
this.weight = weight; | |
} | |
public boolean isSales() { | |
return sales; | |
} | |
public void setSales(boolean sales) { | |
this.sales = sales; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<persistence xmlns="http://java.sun.com/xml/ns/persistence" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" | |
version="2.0"> | |
<persistence-unit name="MENUtest"> | |
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |
<properties> | |
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> | |
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/> | |
<property name="javax.persistence.jdbc.user" value="root" /> | |
<property name="javax.persistence.jdbc.password" value="9707anton" /> | |
<property name="hibernate.show_sql" value="true" /> | |
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> | |
<property name="hibernate.hbm2ddl.auto" value="create"/> | |
</properties> | |
</persistence-unit> | |
</persistence> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment