Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Welcome to GDB Online.
- GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
- C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
- Code, Compile, Run and Debug online from anywhere in world.
- *******************************************************************************/
- import java.util.*;
- public class Main
- {
- static String[] categories = {"toy", "car", "candies", "cake", "milk", "fish", "apple", "watermelon" };
- public static void main(String[] args) {
- /*StockService stock = new Stock();
- stock.generateProducts();
- stock.print();*/
- Stock stock = new Stock(Currency.Grn);
- stock.generateProducts();
- stock.print();
- Wallet wallet = new Wallet(10, Currency.Dollars);
- wallet.print();
- Basket basket = new Basket();
- basket.putRandomItemsFromStock(stock);
- System.out.println("=== BUY ===");
- List<Product> successed = wallet.transaction(basket);
- System.out.println("=== END - BUY ===");
- wallet.print();
- }
- static class Basket {
- List<Product> toBuy;
- Basket() {
- toBuy = new ArrayList();
- }
- void putRandomItemsFromStock(Stock stockService) {
- List<Product> products = stockService.getAll();
- for (int i = 0; i < products.size(); i++) {
- if (Generator.bool()) {
- toBuy.add(products.get(i));
- }
- }
- }
- List<Product> getAll() {
- return toBuy;
- }
- }
- static class Money {
- double value;
- Currency currency;
- Money(double value, Currency currency) {
- this.value = value;
- this.currency = currency;
- }
- }
- static class Wallet {
- double money;
- Currency currency;
- Wallet(double money, Currency currency) {
- this.money = money;
- this.currency = currency;
- }
- Wallet(Wallet wallet) {
- this.money = wallet.money;
- this.currency = wallet.currency;
- }
- List<Product> transaction(Basket basket) {
- List<Product> buyedProducts = new ArrayList();
- List<Product> basketProducts = basket.getAll();
- for (int i = 0; i < basketProducts.size(); i++) {
- if (this.transaction(basketProducts.get(i).price, basketProducts.get(i).category))
- buyedProducts.add(basketProducts.get(i));
- }
- return buyedProducts;
- }
- boolean transaction(Money money, String name) {
- double tmpMoney = this.money;
- tmpMoney -= this.currency == money.currency ?
- money.value
- :
- currency.getDependence(money.currency) * money.value;
- if (tmpMoney > -1) {
- this.money = tmpMoney;
- System.out.println("=== TRANSACTION ===\nPrice: " + money.value + "\nResult: " + this.money + "\n -- Name: " + name);
- return true;
- }
- return false;
- }
- boolean transaction(Money money) {
- double tmpMoney = this.money;
- tmpMoney -= this.currency == money.currency ?
- money.value
- :
- currency.getDependence(money.currency) * money.value;
- if (tmpMoney > -1) {
- this.money = tmpMoney;
- System.out.println("======= TRANSACTION ======\nPrice: " + money.value + "\nResult: " + this.money);
- return true;
- }
- return false;
- }
- Wallet convert(Currency convertTo) {
- Wallet oldWallet = new Wallet(this);
- this.money *= currency.getDependence(convertTo);
- this.currency = convertTo;
- System.out.println("===== CONVERT =====\n" + oldWallet.money + " " + oldWallet.currency + " in " + convertTo + " = " + this.money + convertTo.flag + "\n");
- return this;
- }
- void print() {
- System.out.println(this);
- }
- @Override
- public String toString() {
- return "===== WALLET ======\nMoney: " + money + "\nCurrency: " + currency;
- }
- }
- enum Currency {
- Euro("eur", 1.10, 0.014, 0.038),
- Dollars("$", 0.90909090909, 0.016, 0.042),
- Rub("rub", 70.93, 63.98, 2.67),
- Grn("grn", 26.53, 23.89, 0.37);
- String flag;
- double[] dependency;
- Currency(String flag, double... dependency) {
- this.flag = flag;
- this.dependency = dependency;
- }
- double getDependence(Currency a) {
- int index = a.ordinal() -
- (ordinal() == 0 || (a.ordinal() > ordinal() && ordinal() != dependency.length) ? 1 : 0);
- return dependency[index];
- }
- }
- interface StockService {
- StockService generateProducts();
- List<Product> getAll();
- void print();
- }
- static class Stock {
- List<Product> products;
- Currency currOfProducts;
- Stock(Currency currOfProducts) {
- products = new ArrayList();
- this.currOfProducts = currOfProducts;
- }
- Stock generateProducts() {
- int col = Generator.generateNumber(6) + 3;
- for (int i = 0; i < col; i++) {
- products.add(new Product(
- Generator.generateName(),
- categories[Generator.generateNumber(categories.length)],
- Generator.generateNumber(5) + 1,
- new Money(Generator.generateNumber(100) + 20,
- currOfProducts)));
- }
- return this;
- }
- List<Product> getAll() {
- return products;
- }
- void print() {
- System.out.println(getStockName(82));
- System.out.println(generateProductDescription());
- for (Product product : products) {
- System.out.println(product);
- }
- }
- void print(List<Product> products) {
- System.out.println(getStockName(82));
- System.out.println(generateProductDescription());
- for (Product product : products) {
- System.out.println(product);
- }
- }
- private String getStockName(int length) {
- String data = "Stock";
- StringBuilder builderFill = new StringBuilder();
- for (int i = 0; i < (length - data.length() - 2) / 2; i++) {
- builderFill.append("=");
- }
- String fill = builderFill.toString();
- return fill + ((length - data.length()) % 2 == 0 ? "" : " ") + " " + data + " " + fill;
- }
- private String generateProductDescription() {
- StringBuilder lineBuilder = new StringBuilder();
- lineBuilder.append(generateDataField("ID", 10));
- lineBuilder.append(generateDataField("NAME", 14));
- lineBuilder.append(generateDataField("CATEGORY", 16));
- lineBuilder.append(generateDataField("MASS", 12));
- lineBuilder.append(generateDataField("PRICE", 12));
- lineBuilder.append(generateDataField("CURRENCY", 12));
- return lineBuilder.toString();
- }
- private String generateDataField(String data, int length) {
- StringBuilder builderFill = new StringBuilder();
- for (int i = 0; i < (length - data.length() - 6) / 2; i++) {
- builderFill.append(" ");
- }
- String fill = builderFill.toString();
- return fill + ((length - data.length()) % 2 == 0 ? "" : " ") + " - " + data + " - " + fill + " ";
- }
- }
- static class Product {
- int id;
- String name;
- String category;
- double mass;
- /*double price;
- Currency currency;*/
- Money price;
- //double col;
- Product() { }
- Product(int id, String name, String category, double mass, Money price) {//double price) {//, double col) {
- this.id = id;
- this.name = name;
- this.category = category;
- this.mass = mass;
- this.price = price;
- //this.col = col;
- }
- Product(String name, String category, double mass, Money price) {//double price, Currency currency) {//, double col) {
- this.id = Generator.generateId();
- this.name = name;
- this.category = category;
- this.mass = mass;
- this.price = price;
- //this.col = col;
- }
- private String toString(int number, int length) {
- return toString(String.valueOf(number), length);
- }
- private String toString(double number, int length) {
- return toString(String.valueOf(number), length);
- }
- private String toString(String data, int length) {
- StringBuilder builderFill = new StringBuilder();
- for (int i = 0; i < (length - data.length()) / 2; i++) {
- builderFill.append(" ");
- }
- String fill = builderFill.toString();
- return fill + ((length - data.length()) % 2 == 0 ? "" : " ") + data + fill;
- }
- @Override
- public String toString() {
- return toString(id, 10) + "|" +
- toString(name, 14) + "|" +
- toString(category, 16) + "|" +
- toString(mass, 12) + "|" +
- toString(price.value, 12) + "|" +
- toString(String.valueOf(price.currency), 12);
- }
- }
- static class Generator {
- static Random random;
- static {
- random = new Random();
- }
- static int generateInt() {
- return random.nextInt(Short.MAX_VALUE * 2 + 1) - Short.MAX_VALUE;
- }
- static int generateId() {
- return random.nextInt(Short.MAX_VALUE);
- }
- static int generateNumber(int upscale) {
- return random.nextInt(upscale);
- }
- static char generateLetter() {
- int letter = generateNumber(25);
- return (char) (97 + letter);
- }
- static boolean bool() {
- return random.nextBoolean();
- }
- static String generateName() {
- StringBuilder builder = new StringBuilder();
- int length = generateNumber(4) + 6;
- for (int i = 0; i < length; i++) {
- builder.append(generateLetter());
- }
- return builder.toString();
- }
- }
- }
Add Comment
Please, Sign In to add comment