Skip to content

Instantly share code, notes, and snippets.

@ansisec
Created April 18, 2023 16:10
Show Gist options
  • Save ansisec/d10625201a7bd3407d2582785fe9dc3c to your computer and use it in GitHub Desktop.
Save ansisec/d10625201a7bd3407d2582785fe9dc3c to your computer and use it in GitHub Desktop.
Shopping List
package pt.isec.pa.shopping_list.model.command;
import java.util.ArrayDeque;
import java.util.Deque;
public class CommandManager {
private Deque<ICommand> history;
private Deque<ICommand> redoCmds;
//private Stack<ICommand> history;
//private Stack<ICommand> redoCmds;
public CommandManager() {
history = new ArrayDeque<>();
redoCmds = new ArrayDeque<>();
//history = new Stack<>();
//redoCmds = new Stack<>();
}
public boolean invokeCommand(ICommand cmd) {
redoCmds.clear();
if (cmd.execute()) {
history.push(cmd);
return true;
}
history.clear();
return false;
}
public boolean undo() {
if (history.isEmpty())
return false;
ICommand cmd = history.pop();
cmd.undo();
redoCmds.push(cmd);
return true;
}
public boolean redo() {
if (redoCmds.isEmpty())
return false;
ICommand cmd = redoCmds.pop();
cmd.execute();
history.push(cmd);
return true;
}
public boolean hasUndo() {
return history.size()>0;
}
public boolean hasRedo() {
return redoCmds.size()>0;
}
}
package pt.isec.pa.shopping_list.model.command;
public interface ICommand {
boolean execute();
boolean undo();
}
package pt.isec.pa.shopping_list;
import pt.isec.pa.shopping_list.model.ShoppingListManager;
import pt.isec.pa.shopping_list.ui.ShoppingListUI;
public class Main {
public static void main(String[] args) {
ShoppingListManager sm = new ShoppingListManager();
ShoppingListUI ui = new ShoppingListUI(sm);
ui.start();
}
}
package pt.isec.pa.shopping_list.ui.utils;
import java.util.Scanner;
public final class PAInput {
private PAInput() {}
private static Scanner sc;
static {
resetScanner();
}
public static void resetScanner() {
sc = new Scanner(System.in);
}
public static String readString(String title,boolean onlyOneWord) {
String value;
do {
if (title != null)
System.out.print(title);
else
System.out.print("> ");
value = sc.nextLine().trim();
} while (value.isBlank());
if (onlyOneWord) {
Scanner auxsc = new Scanner(value);
value = auxsc.next();
}
return value;
}
public static int readInt(String title) {
while (true) {
if (title != null)
System.out.print(title);
else
System.out.print("> ");
if (sc.hasNextInt()) {
int intValue = sc.nextInt();
sc.nextLine();
return intValue;
} else
sc.nextLine();
}
}
public static double readNumber(String title) {
while (true) {
if (title != null)
System.out.print(title);
else
System.out.print("> ");
if (sc.hasNextDouble()) {
double doubleValue = sc.nextDouble();
sc.nextLine();
return doubleValue;
} else
sc.nextLine();
}
}
public static int chooseOption(String title, String ... options) {
int option = -1;
do {
if (title != null)
System.out.println(System.lineSeparator()+title);
System.out.println();
for(int i = 0; i < options.length; i++) {
System.out.printf("%3d - %s\n",i+1,options[i]);
}
System.out.print("\nOption: ");
if (sc.hasNextInt())
option = sc.nextInt();
sc.nextLine();
} while (option < 1 || option > options.length);
return option;
}
}
package pt.isec.pa.shopping_list.model.data;
record Product(String name, double qt) {
@Override
public String toString() {
return String.format("%-20s %8.2f",name,qt);
}
}
package pt.isec.pa.shopping_list.model.data;
import java.util.ArrayList;
public class ShoppingList {
private ArrayList<Product> list;
public ShoppingList() {
list = new ArrayList<>();
}
public boolean addProduct(String name, double qt) {
if (name!=null && !name.isBlank() && qt>0) {
list.add(new Product(name, qt));
return true;
}
return false;
}
public boolean removeProduct(String name, double qt) {
return list.remove(new Product(name,qt));
}
public double getQuantity(String name) {
for(Product p : list)
if (p.name().equals(name))
return p.qt();
return -1;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Shopping List:\n");
for(Product p : list) sb.append("\t- ").append(p).append("\n");
return sb.toString();
}
}
package pt.isec.pa.shopping_list.ui;
import pt.isec.pa.shopping_list.model.ShoppingListManager;
import pt.isec.pa.shopping_list.ui.utils.PAInput;
public class ShoppingListUI {
ShoppingListManager sm;
public ShoppingListUI(ShoppingListManager sm) {
this.sm = sm;
}
public void start() {
boolean finish = false;
do {
System.out.println("\n\n"+sm+"\n");
int op = PAInput.chooseOption("Shopping List",
"Add product","Remove product","Undo","Redo","Quit");
switch (op) {
case 1 -> sm.addProduct(
PAInput.readString("Product name: ",false),
PAInput.readNumber("Quantity: ")
);
case 2 -> sm.removeProduct(
PAInput.readString("Product name: ",false),
PAInput.readNumber("Quantity: ")
);
case 3 -> sm.undo();
case 4 -> sm.redo();
case 5 ->finish = true;
}
} while (!finish);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment