Commit f4d01e03 authored by Bhargava Rellu's avatar Bhargava Rellu

Product storing bug fixed

parent 705ec12b
......@@ -3,16 +3,22 @@ package com.retail_store.order_management.application;
import com.retail_store.order_management.service.RetailStore;
import com.retail_store.order_management.service.RetailStoreService;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.logging.Logger;
import static com.retail_store.order_management.utils.Utility.getInt;
/**
* The project run starts from here
*/
public class OrderManagement {
private Scanner scanner;
private final Logger logger;
/**
* No-Arg Constructor for class OrderManagement with their private
* instance variables
*/
public OrderManagement() {
System.setProperty("java.util.logging.SimpleFormatter.format",
"%5$s %n");
......@@ -20,9 +26,13 @@ public class OrderManagement {
this.logger = Logger.getLogger(OrderManagement.class.getName());
}
/**
* Method menu where it shows there regular options to go particular
* functionality regarding the selected option in the console.
*/
public void menu() {
boolean flag = true;
logger.info("Welcome to GAP Business Management");
logger.info("Welcome to RetailStore Business Management");
RetailStore gap = new RetailStoreService();
String menu = "\n------------------------\nMenu\n------------------------\n" +
"1. Add product to your stock\n------------------------\n" +
......@@ -35,12 +45,7 @@ public class OrderManagement {
menuOption = getInt();
switch (menuOption) {
case 1:
try {
gap.addProducts();
} catch (Exception e) {
e.printStackTrace();
menu();
}
logger.info("\n------------------------\nPlease enter 5 for menu\n------------------------\n" +
"To exit please enter 4\n------------------------\n" +
"To Add more enter 1\n------------------------\n");
......@@ -56,7 +61,7 @@ public class OrderManagement {
"To exit please enter 4");
break;
case 4:
logger.info("Good Bye.");
flag = false;
break;
case 5:
......@@ -69,7 +74,12 @@ public class OrderManagement {
}
}
public static void main(String[] args) throws SQLException {
/**
* Main method where this application execution starts
*
* @param args
*/
public static void main(String[] args) {
OrderManagement orderManagement = new OrderManagement();
orderManagement.menu();
}
......
package com.retail_store.order_management.constants;
/**
* Here using Enum class types as the requirement has constants to
* use multiple times in the project.
*
* So this enumeration class of category shows all options
* according to requirement
*/
public enum Category {
/**
* Different types of Category enum
*/
EXIT(0), SHIRTS(1), PANTS(2), JEANS(3), SHORTS(4), T_SHIRTS(5), POLOS(6), HOODIES(7), KURTA(8), SWEATSHIRTS(9), DRESS(10), TOPS(11);
int option;
public int getOption() {
......
package com.retail_store.order_management.constants;
/**
* Enumeration class of name Color
* Used different constants according to the project requirement
*
* According to the given option in the console this class methods or functionality
* is called
*/
public enum Color {
/**
* Different types of Color enum
*/
EXIT(0), RED(1), GREEN(2), BLUE(3), BROWN(4), GREY(5), WHITE(6), PINK(7), YELLOW(8), BLACK(9), PURPLE(10), ORANGE(11), SKY_BLUE(12);
int option;
......
package com.retail_store.order_management.constants;
/**
* Enumeration class of name Gender
*
* According to the selected option this class behaviour is called to select
* particular gender
*/
public enum Gender {
/**
* Different types of Gender enum
*/
EXIT(0,"exit"),MEN(1, "Men"), WOMEN(2, "Women");
int option;
String gender;
......
......@@ -26,11 +26,19 @@ public class RetailStoreService implements RetailStore, ProductFilter {
String id = productIdGenerator();
getLogger().info("Enter Product details \n Enter Product Name");
String name = getScanner().nextLine();
// getLogger().info(name);
Category category = getCategory(true, "Enter Product Category");
if (category == null) {
return false;
}
Color color = getColor(true, "Select color of the product");
double price = getDouble(true, "Enter price of the product");
if (category == null) {
return false;
}
Gender gender = getGender(true, "Product is ideal for");
if (category == null) {
return false;
}
double price = getDouble(true, "Enter price of the product");
Map<Size, Integer> size = getProductSizes();
Connection connection = getConnection();
try {
......@@ -84,7 +92,7 @@ public class RetailStoreService implements RetailStore, ProductFilter {
getLogger().info("Please enter Customer name");
String name = getScanner().nextLine();
getLogger().info("Please enter Phone number");
String phone = getScanner().next();
String phone = phoneNumberValidator();
Date date = Date.valueOf(LocalDate.now());
Time time = Time.valueOf(LocalTime.now());
double bill = orderedProductList.stream().map(product -> product.getPrice() * product.getQuantity()).reduce(0.00, (totalBill, productBill) -> totalBill + productBill);
......@@ -125,11 +133,11 @@ public class RetailStoreService implements RetailStore, ProductFilter {
connection.rollback();
}
return true;
} catch (SQLException e) {
} catch (Exception e) {
getLogger().warning(e.getMessage());
try {
connection.rollback();
} catch (SQLException ex) {
} catch (Exception ex) {
getLogger().warning(ex.getMessage());
}
return true;
......@@ -166,7 +174,6 @@ public class RetailStoreService implements RetailStore, ProductFilter {
break;
}
if (!addProductToCart(cart, product, connection)) {
getLogger().info("\n-----------------------------\nSorry to say this. Currently this product was out of stock.\n-----------------------------\n");
continue;
} else {
getLogger().info("\n-----------------------------\nYour product : " + product.getName() + " is added to your cart.\n-----------------------------\n");
......@@ -210,12 +217,20 @@ public class RetailStoreService implements RetailStore, ProductFilter {
@Override
public synchronized boolean addProductToCart(Collection<OrderedProduct> cart, Product product, Connection connection) {
if (product.outOfStock()) {
getLogger().info("\n-----------------------------\nSorry to say this. Currently this product was out of stock.\n-----------------------------\n");
return false;
}
getLogger().info(product.showSizes());
Size size = getSize(true, "Please Select Size");
if (size == null) {
return false;
}
getLogger().info("\n-----------------------------\nPlease enter quantity required\n-----------------------------\n");
int quantity = getInt();
if (quantity <= 0) {
getLogger().info("\n-----------------------------\nPlease enter atleast 1 quantity.\n-----------------------------\n");
return false;
}
int availableQuantity = product.getSize().get(size);
if (quantity <= availableQuantity) {
int remaining = availableQuantity - quantity;
......
......@@ -19,15 +19,17 @@ public final class Utility {
}
public static Category getCategory(boolean check, String message) {
Category categoryValue;
String categoryOptions = Arrays.stream(Category.values()).filter(category -> !category.equals(Category.EXIT)).map(category -> "(" + category.getOption() + ")" + category).reduce((categories, category) -> categories + " " + category).get() + "\nPress 0 to go back.";
if (check) {
getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + categoryOptions + "\n------------------------\n");
}
int option = getInt();
return Arrays.stream(Category.values()).filter(category -> category.getOption() == option).findFirst().orElseGet(() -> {
categoryValue = Arrays.stream(Category.values()).filter(category -> category.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\n+Please enter valid category\n------------------------\n" + categoryOptions + "\n------------------------\n");
return getCategory(false, message);
});
return categoryValue.equals(Category.EXIT) ? null : categoryValue;
}
public static Size getSize(boolean check, String message) {
......@@ -40,33 +42,36 @@ public final class Utility {
sizeValue = Arrays.stream(Size.values()).filter(size -> size.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\nPlease enter valid category\n------------------------\n" + categoryOptions + "\n------------------------\n");
return getSize(false, message);
});
return sizeValue.equals(Size.EXIT) ? null : sizeValue;
}
public static Gender getGender(boolean check, String message) {
Gender genderValue;
String genderOptions = Arrays.stream(Gender.values()).filter(gender -> !gender.equals(Gender.EXIT)).map(gender -> "(" + gender.getOption() + ")" + gender).reduce((genders, gender) -> genders + " " + gender).get() + "\nPress 0 to go back.";
if (check) {
getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + genderOptions + "\n------------------------\n");
}
int option = getInt();
return Arrays.stream(Gender.values()).filter(gender -> gender.getOption() == option).findFirst().orElseGet(() -> {
genderValue = Arrays.stream(Gender.values()).filter(gender -> gender.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\nPlease enter valid gender\n------------------------\n" + genderOptions + "\n------------------------\n");
return getGender(false, message);
});
return genderValue.equals(Gender.EXIT) ? null : genderValue;
}
public static Color getColor(boolean check, String message) {
Color colorValue;
String colorOptions = Arrays.stream(Color.values()).filter(color -> !color.equals(Color.EXIT)).map(color -> "(" + color.getOption() + ")" + color).reduce((categories, category) -> categories + " " + category).get() + "\nPress 0 to go back.";
if (check) {
getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + colorOptions + "\n------------------------\n");
}
int option = getInt();
return Arrays.stream(Color.values()).filter(color -> color.getOption() == option).findFirst().orElseGet(() -> {
colorValue = Arrays.stream(Color.values()).filter(color -> color.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\n+Please enter valid color\n------------------------\n" + colorOptions + "\n------------------------\n");
return getColor(false, message);
});
return colorValue.equals(Color.EXIT) ? null : colorValue;
}
public static double getDouble(boolean flag, String message) {
......@@ -104,6 +109,20 @@ public final class Utility {
}
}
public static String phoneNumberValidator() {
String phoneNumber;
try {
phoneNumber = getScanner().next();
if (phoneNumber == null || phoneNumber.isBlank() || phoneNumber.isEmpty() || phoneNumber.length() != 10) {
throw new RuntimeException("Please enter valid number");
}
} catch (Exception e) {
getLogger().warning("Please enter valid phone number");
return phoneNumberValidator();
}
return phoneNumber.toString();
}
public static Logger getLogger() {
return LOGGER == null ? Logger.getLogger(OrderManagement.class.getName()) : LOGGER;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment