SHOW:
|
|
- or go back to the newest paste.
| 1 | /****************************************************************************** | |
| 2 | ||
| 3 | Welcome to GDB Online. | |
| 4 | GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, | |
| 5 | C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. | |
| 6 | Code, Compile, Run and Debug online from anywhere in world. | |
| 7 | ||
| 8 | *******************************************************************************/ | |
| 9 | import java.util.*; | |
| 10 | ||
| 11 | public class Main | |
| 12 | {
| |
| 13 | static String[] categories = {"toy", "car", "candies", "cake", "milk", "fish", "apple", "watermelon" };
| |
| 14 | ||
| 15 | public static void main(String[] args) {
| |
| 16 | /*StockService stock = new Stock(); | |
| 17 | stock.generateProducts(); | |
| 18 | stock.print();*/ | |
| 19 | ||
| 20 | Stock stock = new Stock(Currency.Grn); | |
| 21 | stock.generateProducts(); | |
| 22 | stock.print(); | |
| 23 | ||
| 24 | Wallet wallet = new Wallet(10, Currency.Dollars); | |
| 25 | wallet.print(); | |
| 26 | ||
| 27 | Basket basket = new Basket(); | |
| 28 | basket.putRandomItemsFromStock(stock); | |
| 29 | ||
| 30 | System.out.println("=== BUY ===");
| |
| 31 | List<Product> successed = wallet.transaction(basket); | |
| 32 | System.out.println("=== END - BUY ===");
| |
| 33 | ||
| 34 | wallet.print(); | |
| 35 | } | |
| 36 | ||
| 37 | static class Basket {
| |
| 38 | List<Product> toBuy; | |
| 39 | ||
| 40 | Basket() {
| |
| 41 | toBuy = new ArrayList(); | |
| 42 | } | |
| 43 | ||
| 44 | void putRandomItemsFromStock(Stock stockService) {
| |
| 45 | List<Product> products = stockService.getAll(); | |
| 46 | ||
| 47 | for (int i = 0; i < products.size(); i++) {
| |
| 48 | if (Generator.bool()) {
| |
| 49 | toBuy.add(products.get(i)); | |
| 50 | } | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | List<Product> getAll() {
| |
| 55 | return toBuy; | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | static class Money {
| |
| 60 | double value; | |
| 61 | Currency currency; | |
| 62 | ||
| 63 | Money(double value, Currency currency) {
| |
| 64 | this.value = value; | |
| 65 | this.currency = currency; | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | static class Wallet {
| |
| 70 | double money; | |
| 71 | Currency currency; | |
| 72 | ||
| 73 | Wallet(double money, Currency currency) {
| |
| 74 | this.money = money; | |
| 75 | this.currency = currency; | |
| 76 | } | |
| 77 | ||
| 78 | Wallet(Wallet wallet) {
| |
| 79 | this.money = wallet.money; | |
| 80 | this.currency = wallet.currency; | |
| 81 | } | |
| 82 | ||
| 83 | List<Product> transaction(Basket basket) {
| |
| 84 | List<Product> buyedProducts = new ArrayList(); | |
| 85 | List<Product> basketProducts = basket.getAll(); | |
| 86 | ||
| 87 | for (int i = 0; i < basketProducts.size(); i++) {
| |
| 88 | if (this.transaction(basketProducts.get(i).price, basketProducts.get(i).category)) | |
| 89 | buyedProducts.add(basketProducts.get(i)); | |
| 90 | } | |
| 91 | ||
| 92 | return buyedProducts; | |
| 93 | } | |
| 94 | ||
| 95 | boolean transaction(Money money, String name) {
| |
| 96 | double tmpMoney = this.money; | |
| 97 | ||
| 98 | tmpMoney -= this.currency == money.currency ? | |
| 99 | money.value | |
| 100 | : | |
| 101 | currency.getDependence(money.currency) * money.value; | |
| 102 | ||
| 103 | if (tmpMoney > -1) {
| |
| 104 | this.money = tmpMoney; | |
| 105 | ||
| 106 | System.out.println("=== TRANSACTION ===\nPrice: " + money.value + "\nResult: " + this.money + "\n -- Name: " + name);
| |
| 107 | ||
| 108 | return true; | |
| 109 | } | |
| 110 | ||
| 111 | return false; | |
| 112 | } | |
| 113 | ||
| 114 | boolean transaction(Money money) {
| |
| 115 | double tmpMoney = this.money; | |
| 116 | ||
| 117 | tmpMoney -= this.currency == money.currency ? | |
| 118 | money.value | |
| 119 | : | |
| 120 | currency.getDependence(money.currency) * money.value; | |
| 121 | ||
| 122 | if (tmpMoney > -1) {
| |
| 123 | this.money = tmpMoney; | |
| 124 | ||
| 125 | System.out.println("======= TRANSACTION ======\nPrice: " + money.value + "\nResult: " + this.money);
| |
| 126 | ||
| 127 | return true; | |
| 128 | } | |
| 129 | ||
| 130 | return false; | |
| 131 | } | |
| 132 | ||
| 133 | Wallet convert(Currency convertTo) {
| |
| 134 | Wallet oldWallet = new Wallet(this); | |
| 135 | ||
| 136 | this.money *= currency.getDependence(convertTo); | |
| 137 | this.currency = convertTo; | |
| 138 | ||
| 139 | System.out.println("===== CONVERT =====\n" + oldWallet.money + " " + oldWallet.currency + " in " + convertTo + " = " + this.money + convertTo.flag + "\n");
| |
| 140 | ||
| 141 | return this; | |
| 142 | } | |
| 143 | ||
| 144 | void print() {
| |
| 145 | System.out.println(this); | |
| 146 | } | |
| 147 | ||
| 148 | @Override | |
| 149 | public String toString() {
| |
| 150 | return "===== WALLET ======\nMoney: " + money + "\nCurrency: " + currency; | |
| 151 | } | |
| 152 | } | |
| 153 | ||
| 154 | enum Currency {
| |
| 155 | Euro("eur", 1.10, 0.014, 0.038),
| |
| 156 | Dollars("$", 0.90909090909, 0.016, 0.042),
| |
| 157 | Rub("rub", 70.93, 63.98, 2.67),
| |
| 158 | Grn("grn", 26.53, 23.89, 0.37);
| |
| 159 | ||
| 160 | String flag; | |
| 161 | double[] dependency; | |
| 162 | ||
| 163 | Currency(String flag, double... dependency) {
| |
| 164 | this.flag = flag; | |
| 165 | this.dependency = dependency; | |
| 166 | } | |
| 167 | ||
| 168 | double getDependence(Currency a) {
| |
| 169 | int index = a.ordinal() - | |
| 170 | (ordinal() == 0 || (a.ordinal() > ordinal() && ordinal() != dependency.length) ? 1 : 0); | |
| 171 | ||
| 172 | return dependency[index]; | |
| 173 | } | |
| 174 | } | |
| 175 | ||
| 176 | interface StockService {
| |
| 177 | StockService generateProducts(); | |
| 178 | List<Product> getAll(); | |
| 179 | void print(); | |
| 180 | } | |
| 181 | ||
| 182 | static class Stock {
| |
| 183 | List<Product> products; | |
| 184 | Currency currOfProducts; | |
| 185 | ||
| 186 | Stock(Currency currOfProducts) {
| |
| 187 | products = new ArrayList(); | |
| 188 | ||
| 189 | this.currOfProducts = currOfProducts; | |
| 190 | } | |
| 191 | ||
| 192 | Stock generateProducts() {
| |
| 193 | int col = Generator.generateNumber(6) + 3; | |
| 194 | for (int i = 0; i < col; i++) {
| |
| 195 | products.add(new Product( | |
| 196 | Generator.generateName(), | |
| 197 | categories[Generator.generateNumber(categories.length)], | |
| 198 | Generator.generateNumber(5) + 1, | |
| 199 | new Money(Generator.generateNumber(100) + 20, | |
| 200 | currOfProducts))); | |
| 201 | } | |
| 202 | ||
| 203 | return this; | |
| 204 | } | |
| 205 | ||
| 206 | List<Product> getAll() {
| |
| 207 | return products; | |
| 208 | } | |
| 209 | ||
| 210 | void print() {
| |
| 211 | System.out.println(getStockName(82)); | |
| 212 | System.out.println(generateProductDescription()); | |
| 213 | for (Product product : products) {
| |
| 214 | System.out.println(product); | |
| 215 | } | |
| 216 | } | |
| 217 | ||
| 218 | void print(List<Product> products) {
| |
| 219 | System.out.println(getStockName(82)); | |
| 220 | System.out.println(generateProductDescription()); | |
| 221 | for (Product product : products) {
| |
| 222 | System.out.println(product); | |
| 223 | } | |
| 224 | } | |
| 225 | ||
| 226 | private String getStockName(int length) {
| |
| 227 | String data = "Stock"; | |
| 228 | StringBuilder builderFill = new StringBuilder(); | |
| 229 | ||
| 230 | for (int i = 0; i < (length - data.length() - 2) / 2; i++) {
| |
| 231 | builderFill.append("=");
| |
| 232 | } | |
| 233 | ||
| 234 | String fill = builderFill.toString(); | |
| 235 | ||
| 236 | return fill + ((length - data.length()) % 2 == 0 ? "" : " ") + " " + data + " " + fill; | |
| 237 | } | |
| 238 | ||
| 239 | private String generateProductDescription() {
| |
| 240 | StringBuilder lineBuilder = new StringBuilder(); | |
| 241 | ||
| 242 | lineBuilder.append(generateDataField("ID", 10));
| |
| 243 | lineBuilder.append(generateDataField("NAME", 14));
| |
| 244 | lineBuilder.append(generateDataField("CATEGORY", 16));
| |
| 245 | lineBuilder.append(generateDataField("MASS", 12));
| |
| 246 | lineBuilder.append(generateDataField("PRICE", 12));
| |
| 247 | lineBuilder.append(generateDataField("CURRENCY", 12));
| |
| 248 | ||
| 249 | return lineBuilder.toString(); | |
| 250 | } | |
| 251 | ||
| 252 | private String generateDataField(String data, int length) {
| |
| 253 | StringBuilder builderFill = new StringBuilder(); | |
| 254 | ||
| 255 | for (int i = 0; i < (length - data.length() - 6) / 2; i++) {
| |
| 256 | builderFill.append(" ");
| |
| 257 | } | |
| 258 | ||
| 259 | String fill = builderFill.toString(); | |
| 260 | ||
| 261 | return fill + ((length - data.length()) % 2 == 0 ? "" : " ") + " - " + data + " - " + fill + " "; | |
| 262 | } | |
| 263 | } | |
| 264 | ||
| 265 | static class Product {
| |
| 266 | int id; | |
| 267 | String name; | |
| 268 | String category; | |
| 269 | double mass; | |
| 270 | /*double price; | |
| 271 | Currency currency;*/ | |
| 272 | Money price; | |
| 273 | //double col; | |
| 274 | ||
| 275 | Product() { }
| |
| 276 | ||
| 277 | Product(int id, String name, String category, double mass, Money price) {//double price) {//, double col) {
| |
| 278 | this.id = id; | |
| 279 | this.name = name; | |
| 280 | this.category = category; | |
| 281 | this.mass = mass; | |
| 282 | this.price = price; | |
| 283 | //this.col = col; | |
| 284 | } | |
| 285 | ||
| 286 | Product(String name, String category, double mass, Money price) {//double price, Currency currency) {//, double col) {
| |
| 287 | this.id = Generator.generateId(); | |
| 288 | this.name = name; | |
| 289 | this.category = category; | |
| 290 | this.mass = mass; | |
| 291 | this.price = price; | |
| 292 | //this.col = col; | |
| 293 | } | |
| 294 | ||
| 295 | private String toString(int number, int length) {
| |
| 296 | return toString(String.valueOf(number), length); | |
| 297 | } | |
| 298 | ||
| 299 | private String toString(double number, int length) {
| |
| 300 | return toString(String.valueOf(number), length); | |
| 301 | } | |
| 302 | ||
| 303 | private String toString(String data, int length) {
| |
| 304 | StringBuilder builderFill = new StringBuilder(); | |
| 305 | ||
| 306 | for (int i = 0; i < (length - data.length()) / 2; i++) {
| |
| 307 | builderFill.append(" ");
| |
| 308 | } | |
| 309 | ||
| 310 | String fill = builderFill.toString(); | |
| 311 | ||
| 312 | return fill + ((length - data.length()) % 2 == 0 ? "" : " ") + data + fill; | |
| 313 | } | |
| 314 | ||
| 315 | @Override | |
| 316 | public String toString() {
| |
| 317 | return toString(id, 10) + "|" + | |
| 318 | toString(name, 14) + "|" + | |
| 319 | toString(category, 16) + "|" + | |
| 320 | toString(mass, 12) + "|" + | |
| 321 | toString(price.value, 12) + "|" + | |
| 322 | toString(String.valueOf(price.currency), 12); | |
| 323 | } | |
| 324 | } | |
| 325 | ||
| 326 | static class Generator {
| |
| 327 | static Random random; | |
| 328 | ||
| 329 | static {
| |
| 330 | random = new Random(); | |
| 331 | } | |
| 332 | ||
| 333 | static int generateInt() {
| |
| 334 | return random.nextInt(Short.MAX_VALUE * 2 + 1) - Short.MAX_VALUE; | |
| 335 | } | |
| 336 | ||
| 337 | static int generateId() {
| |
| 338 | return random.nextInt(Short.MAX_VALUE); | |
| 339 | } | |
| 340 | ||
| 341 | static int generateNumber(int upscale) {
| |
| 342 | return random.nextInt(upscale); | |
| 343 | } | |
| 344 | ||
| 345 | static char generateLetter() {
| |
| 346 | int letter = generateNumber(25); | |
| 347 | ||
| 348 | return (char) (97 + letter); | |
| 349 | } | |
| 350 | ||
| 351 | static boolean bool() {
| |
| 352 | return random.nextBoolean(); | |
| 353 | } | |
| 354 | ||
| 355 | static String generateName() {
| |
| 356 | StringBuilder builder = new StringBuilder(); | |
| 357 | ||
| 358 | int length = generateNumber(4) + 6; | |
| 359 | for (int i = 0; i < length; i++) {
| |
| 360 | builder.append(generateLetter()); | |
| 361 | } | |
| 362 | ||
| 363 | return builder.toString(); | |
| 364 | } | |
| 365 | } | |
| 366 | } |