Note for monitoring buy/sell item list












1












$begingroup$


I'm making note where you can add items that you buy or sell, any suggestions?



  public class Main {

private static Scanner in = new Scanner(System.in);
private static String commands = "[-1]Quit, [0]Commands, [1]Available items, [2] Sold items, [3]Add, [4]Delete,"
+ " [5]Edit, [6]Sell. [7]Bilans [8]Details";

public static void main(String args) {

Storage storage = new Storage(5);

storage.addItem();


System.out.println("| RESELL NOTE |");
System.out.println(commands);

boolean flag = true;
while (flag) {
System.out.println("Choose option: (0 - print list)");


int answer = in.nextInt();
switch (answer) {

default:
System.out.println("Wrong command");
break;

case -1:
System.out.println("QUIT");
flag = false;
break;

case 0:
System.out.println(commands);
break;
case 1:
storage.availableItems();
break;
case 2:
storage.soldItems();
break;
case 3:
storage.addItem();
break;
case 4:
storage.removeItem();
break;
case 5:
System.out.println("modify item");
break;
case 6:
storage.sellItem();
break;
case 7:
storage.bilans();
break;
case 8:
storage.details();
}
}
}
}


public class Storage {

private int maxCapacity;

public Storage(int maxCapacity) {
this.maxCapacity = maxCapacity;
}

Scanner in = new Scanner(System.in);
private Map<Integer, Item> items = new TreeMap<>();


public void availableItems() {
System.out.println("Available items:");
for (Map.Entry<Integer, Item> entry : items.entrySet()) {
if (!entry.getValue().sold) {
System.out.println(entry.getKey() + ". " + entry.getValue().getName());
}
}
}


public void soldItems() {
System.out.println("Sold items:");
for (Map.Entry<Integer, Item> entry : items.entrySet()) {
if (entry.getValue().sold) {
System.out.println(entry.getKey() + "." + entry.getValue().getName()
+ " - (" + (entry.getValue().soldPrice - entry.getValue().price + "PLN profit)"));
}
}
}

public void addItem() {
if (items.size() >= maxCapacity) {
System.out.println("You cant add more items, storage full! (" + items.size() + "/" + maxCapacity + ")");
} else {
items.put(Item.assignId(), new Shoes("Piraty", 1500, 7, "red", 11));
items.put(Item.assignId(), new Shoes("Belugi", 1500, 7, "red", 11));
items.put(Item.assignId(), new Shoes("Zebry", 1500, 7, "red", 11));
items.put(Item.assignId(), new Shoes("Creamy", 1500, 7, "red", 11));
items.put(Item.assignId(), new Shoes("Sezame", 1500, 7, "red", 11));
System.out.println("Item added");
}
}


public void modifyItem() { // work in progress
printInLine();
System.out.println("nPick item to modify: ");
int id = in.nextInt();

if (items.containsKey(id)) {
System.out.println("Enter new name for " + items.get(id).getName());
in.nextLine();
String newName = in.nextLine();
items.get(id).setName(newName);
} else {
System.out.println("Item not found");
}
}

public void sellItem() {
printInLine();
System.out.println("nChoose item to mark as sold: ");
int id = in.nextInt();

if (items.containsKey(id)) {
items.get(id).setSold(true);
System.out.println("How much did you get for " + items.get(id).getName() + "?: ");
items.get(id).setSoldPrice(in.nextInt());
System.out.println("You marked " + items.get(id).getName() + " as sold");
System.out.println("Your profit is " + (items.get(id).soldPrice - items.get(id).price) + " PLN");
} else {
System.out.println("Item not found");
}
}

public void removeItem() {
printInLine();
System.out.println("nChoose item to remove: ");
int id = in.nextInt();

if (items.containsKey(id)) {
System.out.println(items.get(id).getName() + " removed");
items.remove(id);
} else {
System.out.println("Item not found");
}
}


public void bilans() {

int spendMoney = 0;
int earnedMoney = 0;
int profit = 0;

for (Map.Entry<Integer, Item> entry : items.entrySet()) {
if (!entry.getValue().sold) {
spendMoney += entry.getValue().getPrice();
} else {
earnedMoney += entry.getValue().getSoldPrice();
profit += (entry.getValue().getSoldPrice() - entry.getValue().getPrice());
}
}
System.out.println("You have already spended: " + spendMoney + " PLN");
System.out.println("You sold items for: " + earnedMoney + " PLN");
System.out.println("Current profit: " + profit + " PLN");

if (earnedMoney > spendMoney) {
System.out.println("Wow, you are on +");
} else {
System.out.println("Keep trying");
}
}

public void details() {

printInLine();
System.out.print("nPick item: ");
int pickItem = in.nextInt();

if (items.containsKey(pickItem)) {
System.out.println("n| Details |");
System.out.println("Name: " + items.get(pickItem).getName() +
"nPrice: " + items.get(pickItem).getPrice() +
"nCondition: " + items.get(pickItem).getCondition() + "/10" +
"nColor: ");
} else {
System.out.println("Item not found");
}
}

private void printInLine() {
for (Map.Entry<Integer, Item> entry : items.entrySet()) {
if (!entry.getValue().isSold()) {
System.out.print(" - " + entry.getKey() + "." + entry.getValue().getName());
}
}
}
}









share|improve this question











$endgroup$

















    1












    $begingroup$


    I'm making note where you can add items that you buy or sell, any suggestions?



      public class Main {

    private static Scanner in = new Scanner(System.in);
    private static String commands = "[-1]Quit, [0]Commands, [1]Available items, [2] Sold items, [3]Add, [4]Delete,"
    + " [5]Edit, [6]Sell. [7]Bilans [8]Details";

    public static void main(String args) {

    Storage storage = new Storage(5);

    storage.addItem();


    System.out.println("| RESELL NOTE |");
    System.out.println(commands);

    boolean flag = true;
    while (flag) {
    System.out.println("Choose option: (0 - print list)");


    int answer = in.nextInt();
    switch (answer) {

    default:
    System.out.println("Wrong command");
    break;

    case -1:
    System.out.println("QUIT");
    flag = false;
    break;

    case 0:
    System.out.println(commands);
    break;
    case 1:
    storage.availableItems();
    break;
    case 2:
    storage.soldItems();
    break;
    case 3:
    storage.addItem();
    break;
    case 4:
    storage.removeItem();
    break;
    case 5:
    System.out.println("modify item");
    break;
    case 6:
    storage.sellItem();
    break;
    case 7:
    storage.bilans();
    break;
    case 8:
    storage.details();
    }
    }
    }
    }


    public class Storage {

    private int maxCapacity;

    public Storage(int maxCapacity) {
    this.maxCapacity = maxCapacity;
    }

    Scanner in = new Scanner(System.in);
    private Map<Integer, Item> items = new TreeMap<>();


    public void availableItems() {
    System.out.println("Available items:");
    for (Map.Entry<Integer, Item> entry : items.entrySet()) {
    if (!entry.getValue().sold) {
    System.out.println(entry.getKey() + ". " + entry.getValue().getName());
    }
    }
    }


    public void soldItems() {
    System.out.println("Sold items:");
    for (Map.Entry<Integer, Item> entry : items.entrySet()) {
    if (entry.getValue().sold) {
    System.out.println(entry.getKey() + "." + entry.getValue().getName()
    + " - (" + (entry.getValue().soldPrice - entry.getValue().price + "PLN profit)"));
    }
    }
    }

    public void addItem() {
    if (items.size() >= maxCapacity) {
    System.out.println("You cant add more items, storage full! (" + items.size() + "/" + maxCapacity + ")");
    } else {
    items.put(Item.assignId(), new Shoes("Piraty", 1500, 7, "red", 11));
    items.put(Item.assignId(), new Shoes("Belugi", 1500, 7, "red", 11));
    items.put(Item.assignId(), new Shoes("Zebry", 1500, 7, "red", 11));
    items.put(Item.assignId(), new Shoes("Creamy", 1500, 7, "red", 11));
    items.put(Item.assignId(), new Shoes("Sezame", 1500, 7, "red", 11));
    System.out.println("Item added");
    }
    }


    public void modifyItem() { // work in progress
    printInLine();
    System.out.println("nPick item to modify: ");
    int id = in.nextInt();

    if (items.containsKey(id)) {
    System.out.println("Enter new name for " + items.get(id).getName());
    in.nextLine();
    String newName = in.nextLine();
    items.get(id).setName(newName);
    } else {
    System.out.println("Item not found");
    }
    }

    public void sellItem() {
    printInLine();
    System.out.println("nChoose item to mark as sold: ");
    int id = in.nextInt();

    if (items.containsKey(id)) {
    items.get(id).setSold(true);
    System.out.println("How much did you get for " + items.get(id).getName() + "?: ");
    items.get(id).setSoldPrice(in.nextInt());
    System.out.println("You marked " + items.get(id).getName() + " as sold");
    System.out.println("Your profit is " + (items.get(id).soldPrice - items.get(id).price) + " PLN");
    } else {
    System.out.println("Item not found");
    }
    }

    public void removeItem() {
    printInLine();
    System.out.println("nChoose item to remove: ");
    int id = in.nextInt();

    if (items.containsKey(id)) {
    System.out.println(items.get(id).getName() + " removed");
    items.remove(id);
    } else {
    System.out.println("Item not found");
    }
    }


    public void bilans() {

    int spendMoney = 0;
    int earnedMoney = 0;
    int profit = 0;

    for (Map.Entry<Integer, Item> entry : items.entrySet()) {
    if (!entry.getValue().sold) {
    spendMoney += entry.getValue().getPrice();
    } else {
    earnedMoney += entry.getValue().getSoldPrice();
    profit += (entry.getValue().getSoldPrice() - entry.getValue().getPrice());
    }
    }
    System.out.println("You have already spended: " + spendMoney + " PLN");
    System.out.println("You sold items for: " + earnedMoney + " PLN");
    System.out.println("Current profit: " + profit + " PLN");

    if (earnedMoney > spendMoney) {
    System.out.println("Wow, you are on +");
    } else {
    System.out.println("Keep trying");
    }
    }

    public void details() {

    printInLine();
    System.out.print("nPick item: ");
    int pickItem = in.nextInt();

    if (items.containsKey(pickItem)) {
    System.out.println("n| Details |");
    System.out.println("Name: " + items.get(pickItem).getName() +
    "nPrice: " + items.get(pickItem).getPrice() +
    "nCondition: " + items.get(pickItem).getCondition() + "/10" +
    "nColor: ");
    } else {
    System.out.println("Item not found");
    }
    }

    private void printInLine() {
    for (Map.Entry<Integer, Item> entry : items.entrySet()) {
    if (!entry.getValue().isSold()) {
    System.out.print(" - " + entry.getKey() + "." + entry.getValue().getName());
    }
    }
    }
    }









    share|improve this question











    $endgroup$















      1












      1








      1


      0



      $begingroup$


      I'm making note where you can add items that you buy or sell, any suggestions?



        public class Main {

      private static Scanner in = new Scanner(System.in);
      private static String commands = "[-1]Quit, [0]Commands, [1]Available items, [2] Sold items, [3]Add, [4]Delete,"
      + " [5]Edit, [6]Sell. [7]Bilans [8]Details";

      public static void main(String args) {

      Storage storage = new Storage(5);

      storage.addItem();


      System.out.println("| RESELL NOTE |");
      System.out.println(commands);

      boolean flag = true;
      while (flag) {
      System.out.println("Choose option: (0 - print list)");


      int answer = in.nextInt();
      switch (answer) {

      default:
      System.out.println("Wrong command");
      break;

      case -1:
      System.out.println("QUIT");
      flag = false;
      break;

      case 0:
      System.out.println(commands);
      break;
      case 1:
      storage.availableItems();
      break;
      case 2:
      storage.soldItems();
      break;
      case 3:
      storage.addItem();
      break;
      case 4:
      storage.removeItem();
      break;
      case 5:
      System.out.println("modify item");
      break;
      case 6:
      storage.sellItem();
      break;
      case 7:
      storage.bilans();
      break;
      case 8:
      storage.details();
      }
      }
      }
      }


      public class Storage {

      private int maxCapacity;

      public Storage(int maxCapacity) {
      this.maxCapacity = maxCapacity;
      }

      Scanner in = new Scanner(System.in);
      private Map<Integer, Item> items = new TreeMap<>();


      public void availableItems() {
      System.out.println("Available items:");
      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (!entry.getValue().sold) {
      System.out.println(entry.getKey() + ". " + entry.getValue().getName());
      }
      }
      }


      public void soldItems() {
      System.out.println("Sold items:");
      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (entry.getValue().sold) {
      System.out.println(entry.getKey() + "." + entry.getValue().getName()
      + " - (" + (entry.getValue().soldPrice - entry.getValue().price + "PLN profit)"));
      }
      }
      }

      public void addItem() {
      if (items.size() >= maxCapacity) {
      System.out.println("You cant add more items, storage full! (" + items.size() + "/" + maxCapacity + ")");
      } else {
      items.put(Item.assignId(), new Shoes("Piraty", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Belugi", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Zebry", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Creamy", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Sezame", 1500, 7, "red", 11));
      System.out.println("Item added");
      }
      }


      public void modifyItem() { // work in progress
      printInLine();
      System.out.println("nPick item to modify: ");
      int id = in.nextInt();

      if (items.containsKey(id)) {
      System.out.println("Enter new name for " + items.get(id).getName());
      in.nextLine();
      String newName = in.nextLine();
      items.get(id).setName(newName);
      } else {
      System.out.println("Item not found");
      }
      }

      public void sellItem() {
      printInLine();
      System.out.println("nChoose item to mark as sold: ");
      int id = in.nextInt();

      if (items.containsKey(id)) {
      items.get(id).setSold(true);
      System.out.println("How much did you get for " + items.get(id).getName() + "?: ");
      items.get(id).setSoldPrice(in.nextInt());
      System.out.println("You marked " + items.get(id).getName() + " as sold");
      System.out.println("Your profit is " + (items.get(id).soldPrice - items.get(id).price) + " PLN");
      } else {
      System.out.println("Item not found");
      }
      }

      public void removeItem() {
      printInLine();
      System.out.println("nChoose item to remove: ");
      int id = in.nextInt();

      if (items.containsKey(id)) {
      System.out.println(items.get(id).getName() + " removed");
      items.remove(id);
      } else {
      System.out.println("Item not found");
      }
      }


      public void bilans() {

      int spendMoney = 0;
      int earnedMoney = 0;
      int profit = 0;

      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (!entry.getValue().sold) {
      spendMoney += entry.getValue().getPrice();
      } else {
      earnedMoney += entry.getValue().getSoldPrice();
      profit += (entry.getValue().getSoldPrice() - entry.getValue().getPrice());
      }
      }
      System.out.println("You have already spended: " + spendMoney + " PLN");
      System.out.println("You sold items for: " + earnedMoney + " PLN");
      System.out.println("Current profit: " + profit + " PLN");

      if (earnedMoney > spendMoney) {
      System.out.println("Wow, you are on +");
      } else {
      System.out.println("Keep trying");
      }
      }

      public void details() {

      printInLine();
      System.out.print("nPick item: ");
      int pickItem = in.nextInt();

      if (items.containsKey(pickItem)) {
      System.out.println("n| Details |");
      System.out.println("Name: " + items.get(pickItem).getName() +
      "nPrice: " + items.get(pickItem).getPrice() +
      "nCondition: " + items.get(pickItem).getCondition() + "/10" +
      "nColor: ");
      } else {
      System.out.println("Item not found");
      }
      }

      private void printInLine() {
      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (!entry.getValue().isSold()) {
      System.out.print(" - " + entry.getKey() + "." + entry.getValue().getName());
      }
      }
      }
      }









      share|improve this question











      $endgroup$




      I'm making note where you can add items that you buy or sell, any suggestions?



        public class Main {

      private static Scanner in = new Scanner(System.in);
      private static String commands = "[-1]Quit, [0]Commands, [1]Available items, [2] Sold items, [3]Add, [4]Delete,"
      + " [5]Edit, [6]Sell. [7]Bilans [8]Details";

      public static void main(String args) {

      Storage storage = new Storage(5);

      storage.addItem();


      System.out.println("| RESELL NOTE |");
      System.out.println(commands);

      boolean flag = true;
      while (flag) {
      System.out.println("Choose option: (0 - print list)");


      int answer = in.nextInt();
      switch (answer) {

      default:
      System.out.println("Wrong command");
      break;

      case -1:
      System.out.println("QUIT");
      flag = false;
      break;

      case 0:
      System.out.println(commands);
      break;
      case 1:
      storage.availableItems();
      break;
      case 2:
      storage.soldItems();
      break;
      case 3:
      storage.addItem();
      break;
      case 4:
      storage.removeItem();
      break;
      case 5:
      System.out.println("modify item");
      break;
      case 6:
      storage.sellItem();
      break;
      case 7:
      storage.bilans();
      break;
      case 8:
      storage.details();
      }
      }
      }
      }


      public class Storage {

      private int maxCapacity;

      public Storage(int maxCapacity) {
      this.maxCapacity = maxCapacity;
      }

      Scanner in = new Scanner(System.in);
      private Map<Integer, Item> items = new TreeMap<>();


      public void availableItems() {
      System.out.println("Available items:");
      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (!entry.getValue().sold) {
      System.out.println(entry.getKey() + ". " + entry.getValue().getName());
      }
      }
      }


      public void soldItems() {
      System.out.println("Sold items:");
      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (entry.getValue().sold) {
      System.out.println(entry.getKey() + "." + entry.getValue().getName()
      + " - (" + (entry.getValue().soldPrice - entry.getValue().price + "PLN profit)"));
      }
      }
      }

      public void addItem() {
      if (items.size() >= maxCapacity) {
      System.out.println("You cant add more items, storage full! (" + items.size() + "/" + maxCapacity + ")");
      } else {
      items.put(Item.assignId(), new Shoes("Piraty", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Belugi", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Zebry", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Creamy", 1500, 7, "red", 11));
      items.put(Item.assignId(), new Shoes("Sezame", 1500, 7, "red", 11));
      System.out.println("Item added");
      }
      }


      public void modifyItem() { // work in progress
      printInLine();
      System.out.println("nPick item to modify: ");
      int id = in.nextInt();

      if (items.containsKey(id)) {
      System.out.println("Enter new name for " + items.get(id).getName());
      in.nextLine();
      String newName = in.nextLine();
      items.get(id).setName(newName);
      } else {
      System.out.println("Item not found");
      }
      }

      public void sellItem() {
      printInLine();
      System.out.println("nChoose item to mark as sold: ");
      int id = in.nextInt();

      if (items.containsKey(id)) {
      items.get(id).setSold(true);
      System.out.println("How much did you get for " + items.get(id).getName() + "?: ");
      items.get(id).setSoldPrice(in.nextInt());
      System.out.println("You marked " + items.get(id).getName() + " as sold");
      System.out.println("Your profit is " + (items.get(id).soldPrice - items.get(id).price) + " PLN");
      } else {
      System.out.println("Item not found");
      }
      }

      public void removeItem() {
      printInLine();
      System.out.println("nChoose item to remove: ");
      int id = in.nextInt();

      if (items.containsKey(id)) {
      System.out.println(items.get(id).getName() + " removed");
      items.remove(id);
      } else {
      System.out.println("Item not found");
      }
      }


      public void bilans() {

      int spendMoney = 0;
      int earnedMoney = 0;
      int profit = 0;

      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (!entry.getValue().sold) {
      spendMoney += entry.getValue().getPrice();
      } else {
      earnedMoney += entry.getValue().getSoldPrice();
      profit += (entry.getValue().getSoldPrice() - entry.getValue().getPrice());
      }
      }
      System.out.println("You have already spended: " + spendMoney + " PLN");
      System.out.println("You sold items for: " + earnedMoney + " PLN");
      System.out.println("Current profit: " + profit + " PLN");

      if (earnedMoney > spendMoney) {
      System.out.println("Wow, you are on +");
      } else {
      System.out.println("Keep trying");
      }
      }

      public void details() {

      printInLine();
      System.out.print("nPick item: ");
      int pickItem = in.nextInt();

      if (items.containsKey(pickItem)) {
      System.out.println("n| Details |");
      System.out.println("Name: " + items.get(pickItem).getName() +
      "nPrice: " + items.get(pickItem).getPrice() +
      "nCondition: " + items.get(pickItem).getCondition() + "/10" +
      "nColor: ");
      } else {
      System.out.println("Item not found");
      }
      }

      private void printInLine() {
      for (Map.Entry<Integer, Item> entry : items.entrySet()) {
      if (!entry.getValue().isSold()) {
      System.out.print(" - " + entry.getKey() + "." + entry.getValue().getName());
      }
      }
      }
      }






      java console e-commerce






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 4 hours ago









      Roman

      700214




      700214










      asked Feb 12 at 10:41









      SharkmenSharkmen

      61




      61






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          Responsibilities



          A class should have only one responsibility. Robert C. Martin describes it with




          "There should never be more than one reason for a class to change".




          When we have a look into Storage we can find multiple responsibilities:




          • read inputs from the console


            public void sellItem() {
            // ..
            int id = in.nextInt();
            // ..
            }



          • provide the user with information


            public void bilans() {
            // ..
            System.out.println("You have already spended: " + spendMoney + " PLN");
            System.out.println("You sold items for: " + earnedMoney + " PLN");
            System.out.println("Current profit: " + profit + " PLN");
            // ..
            }



          • manage items


          Focus on one Responsibility



          The class Storage should focus only on the logic to manage items.



          Advantage Over Multiple Responsibilities



          Multiple companies bought your tool. Now imagine the following two scenarios:



          5 Years later Company A and B want different customized information. You can't find a class like InformationFormate. But after some search you found it in Storage, you go into it and change it. You built an if-statement to check for A or B and the default case to print some information.



          Now company C calls you and sad that you should build them a graphical user interface. And again you go into the Storage after you didn't found a class like UserInterface and build a new if-statement to check for C and have build your gui logic into it and for all other the text user interface.



          You can see, that with the number of responsibilities the number of possible changes grows for one class and it gets bigger and bigger. Additional when you search for these task you will not think at first that displaying information would be in a class named Storage.



          Replace Conditional With Polymorphism




          switch (answer) {
          default:
          System.out.println("Wrong command");
          break;
          case -1:
          System.out.println("QUIT");
          flag = false;
          break;
          case 0:
          System.out.println(commands);
          break;
          case 1:
          // ...
          case 8:
          storage.details();
          }



          What this huge switch tries to express is




          Find a command by a number and execute the associated task.




          We could simplyfy the switch with



          Command command = commandFactory.getBy(answer);
          boolean shouldContinueProgram = command.execute();


          Factory Method Pattern



          In the Unit above you found




          Command command = commandFactory.provide(answer);
          command.execute();



          This is using the factory method pattern.



          The commandFactory is an object which gives you the correct Instance of type Command, which is an interface and could be one of your 8 given commands to choose.



          interface Command {
          boolean execute();
          }

          class WrongInput implements Command {
          @Override
          public boolean execute() {
          System.out.println("Wrong command");
          return true;
          }
          }

          class Quite implements Command {
          @Override
          public boolean execute() {
          System.out.println("Wrong command");
          return false;
          }
          }

          interface CommandFactory<T> {
          Command provide(T t);
          }

          class NumberCommandFactory implements CommandFactory<Integer> {

          private Map<Integer, Command> numberByCommand;

          public NumberCommandFactory() {
          numberByCommand.put(-1, new Quite());
          // ...
          }

          @Override
          public Command provide(Integer numberOfCommand) {
          return numberByCommand.getOrDefault(numberOfCommand, new WrongInput());
          }
          }





          share|improve this answer









          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213295%2fnote-for-monitoring-buy-sell-item-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0












            $begingroup$

            Responsibilities



            A class should have only one responsibility. Robert C. Martin describes it with




            "There should never be more than one reason for a class to change".




            When we have a look into Storage we can find multiple responsibilities:




            • read inputs from the console


              public void sellItem() {
              // ..
              int id = in.nextInt();
              // ..
              }



            • provide the user with information


              public void bilans() {
              // ..
              System.out.println("You have already spended: " + spendMoney + " PLN");
              System.out.println("You sold items for: " + earnedMoney + " PLN");
              System.out.println("Current profit: " + profit + " PLN");
              // ..
              }



            • manage items


            Focus on one Responsibility



            The class Storage should focus only on the logic to manage items.



            Advantage Over Multiple Responsibilities



            Multiple companies bought your tool. Now imagine the following two scenarios:



            5 Years later Company A and B want different customized information. You can't find a class like InformationFormate. But after some search you found it in Storage, you go into it and change it. You built an if-statement to check for A or B and the default case to print some information.



            Now company C calls you and sad that you should build them a graphical user interface. And again you go into the Storage after you didn't found a class like UserInterface and build a new if-statement to check for C and have build your gui logic into it and for all other the text user interface.



            You can see, that with the number of responsibilities the number of possible changes grows for one class and it gets bigger and bigger. Additional when you search for these task you will not think at first that displaying information would be in a class named Storage.



            Replace Conditional With Polymorphism




            switch (answer) {
            default:
            System.out.println("Wrong command");
            break;
            case -1:
            System.out.println("QUIT");
            flag = false;
            break;
            case 0:
            System.out.println(commands);
            break;
            case 1:
            // ...
            case 8:
            storage.details();
            }



            What this huge switch tries to express is




            Find a command by a number and execute the associated task.




            We could simplyfy the switch with



            Command command = commandFactory.getBy(answer);
            boolean shouldContinueProgram = command.execute();


            Factory Method Pattern



            In the Unit above you found




            Command command = commandFactory.provide(answer);
            command.execute();



            This is using the factory method pattern.



            The commandFactory is an object which gives you the correct Instance of type Command, which is an interface and could be one of your 8 given commands to choose.



            interface Command {
            boolean execute();
            }

            class WrongInput implements Command {
            @Override
            public boolean execute() {
            System.out.println("Wrong command");
            return true;
            }
            }

            class Quite implements Command {
            @Override
            public boolean execute() {
            System.out.println("Wrong command");
            return false;
            }
            }

            interface CommandFactory<T> {
            Command provide(T t);
            }

            class NumberCommandFactory implements CommandFactory<Integer> {

            private Map<Integer, Command> numberByCommand;

            public NumberCommandFactory() {
            numberByCommand.put(-1, new Quite());
            // ...
            }

            @Override
            public Command provide(Integer numberOfCommand) {
            return numberByCommand.getOrDefault(numberOfCommand, new WrongInput());
            }
            }





            share|improve this answer









            $endgroup$


















              0












              $begingroup$

              Responsibilities



              A class should have only one responsibility. Robert C. Martin describes it with




              "There should never be more than one reason for a class to change".




              When we have a look into Storage we can find multiple responsibilities:




              • read inputs from the console


                public void sellItem() {
                // ..
                int id = in.nextInt();
                // ..
                }



              • provide the user with information


                public void bilans() {
                // ..
                System.out.println("You have already spended: " + spendMoney + " PLN");
                System.out.println("You sold items for: " + earnedMoney + " PLN");
                System.out.println("Current profit: " + profit + " PLN");
                // ..
                }



              • manage items


              Focus on one Responsibility



              The class Storage should focus only on the logic to manage items.



              Advantage Over Multiple Responsibilities



              Multiple companies bought your tool. Now imagine the following two scenarios:



              5 Years later Company A and B want different customized information. You can't find a class like InformationFormate. But after some search you found it in Storage, you go into it and change it. You built an if-statement to check for A or B and the default case to print some information.



              Now company C calls you and sad that you should build them a graphical user interface. And again you go into the Storage after you didn't found a class like UserInterface and build a new if-statement to check for C and have build your gui logic into it and for all other the text user interface.



              You can see, that with the number of responsibilities the number of possible changes grows for one class and it gets bigger and bigger. Additional when you search for these task you will not think at first that displaying information would be in a class named Storage.



              Replace Conditional With Polymorphism




              switch (answer) {
              default:
              System.out.println("Wrong command");
              break;
              case -1:
              System.out.println("QUIT");
              flag = false;
              break;
              case 0:
              System.out.println(commands);
              break;
              case 1:
              // ...
              case 8:
              storage.details();
              }



              What this huge switch tries to express is




              Find a command by a number and execute the associated task.




              We could simplyfy the switch with



              Command command = commandFactory.getBy(answer);
              boolean shouldContinueProgram = command.execute();


              Factory Method Pattern



              In the Unit above you found




              Command command = commandFactory.provide(answer);
              command.execute();



              This is using the factory method pattern.



              The commandFactory is an object which gives you the correct Instance of type Command, which is an interface and could be one of your 8 given commands to choose.



              interface Command {
              boolean execute();
              }

              class WrongInput implements Command {
              @Override
              public boolean execute() {
              System.out.println("Wrong command");
              return true;
              }
              }

              class Quite implements Command {
              @Override
              public boolean execute() {
              System.out.println("Wrong command");
              return false;
              }
              }

              interface CommandFactory<T> {
              Command provide(T t);
              }

              class NumberCommandFactory implements CommandFactory<Integer> {

              private Map<Integer, Command> numberByCommand;

              public NumberCommandFactory() {
              numberByCommand.put(-1, new Quite());
              // ...
              }

              @Override
              public Command provide(Integer numberOfCommand) {
              return numberByCommand.getOrDefault(numberOfCommand, new WrongInput());
              }
              }





              share|improve this answer









              $endgroup$
















                0












                0








                0





                $begingroup$

                Responsibilities



                A class should have only one responsibility. Robert C. Martin describes it with




                "There should never be more than one reason for a class to change".




                When we have a look into Storage we can find multiple responsibilities:




                • read inputs from the console


                  public void sellItem() {
                  // ..
                  int id = in.nextInt();
                  // ..
                  }



                • provide the user with information


                  public void bilans() {
                  // ..
                  System.out.println("You have already spended: " + spendMoney + " PLN");
                  System.out.println("You sold items for: " + earnedMoney + " PLN");
                  System.out.println("Current profit: " + profit + " PLN");
                  // ..
                  }



                • manage items


                Focus on one Responsibility



                The class Storage should focus only on the logic to manage items.



                Advantage Over Multiple Responsibilities



                Multiple companies bought your tool. Now imagine the following two scenarios:



                5 Years later Company A and B want different customized information. You can't find a class like InformationFormate. But after some search you found it in Storage, you go into it and change it. You built an if-statement to check for A or B and the default case to print some information.



                Now company C calls you and sad that you should build them a graphical user interface. And again you go into the Storage after you didn't found a class like UserInterface and build a new if-statement to check for C and have build your gui logic into it and for all other the text user interface.



                You can see, that with the number of responsibilities the number of possible changes grows for one class and it gets bigger and bigger. Additional when you search for these task you will not think at first that displaying information would be in a class named Storage.



                Replace Conditional With Polymorphism




                switch (answer) {
                default:
                System.out.println("Wrong command");
                break;
                case -1:
                System.out.println("QUIT");
                flag = false;
                break;
                case 0:
                System.out.println(commands);
                break;
                case 1:
                // ...
                case 8:
                storage.details();
                }



                What this huge switch tries to express is




                Find a command by a number and execute the associated task.




                We could simplyfy the switch with



                Command command = commandFactory.getBy(answer);
                boolean shouldContinueProgram = command.execute();


                Factory Method Pattern



                In the Unit above you found




                Command command = commandFactory.provide(answer);
                command.execute();



                This is using the factory method pattern.



                The commandFactory is an object which gives you the correct Instance of type Command, which is an interface and could be one of your 8 given commands to choose.



                interface Command {
                boolean execute();
                }

                class WrongInput implements Command {
                @Override
                public boolean execute() {
                System.out.println("Wrong command");
                return true;
                }
                }

                class Quite implements Command {
                @Override
                public boolean execute() {
                System.out.println("Wrong command");
                return false;
                }
                }

                interface CommandFactory<T> {
                Command provide(T t);
                }

                class NumberCommandFactory implements CommandFactory<Integer> {

                private Map<Integer, Command> numberByCommand;

                public NumberCommandFactory() {
                numberByCommand.put(-1, new Quite());
                // ...
                }

                @Override
                public Command provide(Integer numberOfCommand) {
                return numberByCommand.getOrDefault(numberOfCommand, new WrongInput());
                }
                }





                share|improve this answer









                $endgroup$



                Responsibilities



                A class should have only one responsibility. Robert C. Martin describes it with




                "There should never be more than one reason for a class to change".




                When we have a look into Storage we can find multiple responsibilities:




                • read inputs from the console


                  public void sellItem() {
                  // ..
                  int id = in.nextInt();
                  // ..
                  }



                • provide the user with information


                  public void bilans() {
                  // ..
                  System.out.println("You have already spended: " + spendMoney + " PLN");
                  System.out.println("You sold items for: " + earnedMoney + " PLN");
                  System.out.println("Current profit: " + profit + " PLN");
                  // ..
                  }



                • manage items


                Focus on one Responsibility



                The class Storage should focus only on the logic to manage items.



                Advantage Over Multiple Responsibilities



                Multiple companies bought your tool. Now imagine the following two scenarios:



                5 Years later Company A and B want different customized information. You can't find a class like InformationFormate. But after some search you found it in Storage, you go into it and change it. You built an if-statement to check for A or B and the default case to print some information.



                Now company C calls you and sad that you should build them a graphical user interface. And again you go into the Storage after you didn't found a class like UserInterface and build a new if-statement to check for C and have build your gui logic into it and for all other the text user interface.



                You can see, that with the number of responsibilities the number of possible changes grows for one class and it gets bigger and bigger. Additional when you search for these task you will not think at first that displaying information would be in a class named Storage.



                Replace Conditional With Polymorphism




                switch (answer) {
                default:
                System.out.println("Wrong command");
                break;
                case -1:
                System.out.println("QUIT");
                flag = false;
                break;
                case 0:
                System.out.println(commands);
                break;
                case 1:
                // ...
                case 8:
                storage.details();
                }



                What this huge switch tries to express is




                Find a command by a number and execute the associated task.




                We could simplyfy the switch with



                Command command = commandFactory.getBy(answer);
                boolean shouldContinueProgram = command.execute();


                Factory Method Pattern



                In the Unit above you found




                Command command = commandFactory.provide(answer);
                command.execute();



                This is using the factory method pattern.



                The commandFactory is an object which gives you the correct Instance of type Command, which is an interface and could be one of your 8 given commands to choose.



                interface Command {
                boolean execute();
                }

                class WrongInput implements Command {
                @Override
                public boolean execute() {
                System.out.println("Wrong command");
                return true;
                }
                }

                class Quite implements Command {
                @Override
                public boolean execute() {
                System.out.println("Wrong command");
                return false;
                }
                }

                interface CommandFactory<T> {
                Command provide(T t);
                }

                class NumberCommandFactory implements CommandFactory<Integer> {

                private Map<Integer, Command> numberByCommand;

                public NumberCommandFactory() {
                numberByCommand.put(-1, new Quite());
                // ...
                }

                @Override
                public Command provide(Integer numberOfCommand) {
                return numberByCommand.getOrDefault(numberOfCommand, new WrongInput());
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 6 hours ago









                RomanRoman

                700214




                700214






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213295%2fnote-for-monitoring-buy-sell-item-list%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to make a Squid Proxy server?

                    Is this a new Fibonacci Identity?

                    19世紀