我记下来了,你可以在哪里添加你买或卖的东西,有什么建议吗?
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());
}
}
}
}发布于 2019-02-23 05:49:50
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 static void loop() {
for (;;) {
int answer = in.nextInt();
switch (answer) {
case -1:
System.out.println("QUIT");
return;
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();
break;
default:
System.out.println("Wrong command");
}
}
}现在我们不需要flag变量了。我们只需永远循环,直到用户输入-1,然后从方法返回。
这使我们可以选择将方法移动到一个单独的类,简化Main类,并允许在多个程序中使用代码。
我发现在结尾使用default大小写比较容易,而且它是唯一没有break的情况。这并不是说另一种形式没有完美的功能。但是,除非您正在使用跌落,否则我只是觉得这样更简单。
考虑在给出无效命令时显示命令列表。因为有时一个人不记得0会打印命令列表。说“打印列表”可能并不意味着它会打印命令列表而不是项目列表。
你有
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()); } } }
和
private void printInLine() { for (Map.Entry<Integer, Item> entry : items.entrySet()) { if (!entry.getValue().isSold()) { System.out.print(" - " + entry.getKey() + "." + entry.getValue().getName()); } } }
如果我们换了
public static void printItemsOnSeparateLines(Iterable<Item> items) {
for (Item item : items) {
System.out.println(item.getID() + ". " + item.getName());
}
}
public static void printItemsOnSameLine(Iterable<Item> items) {
for (Item item : items) {
System.out.print(" - " + item.getID() + "." + item.getName());
}
}
public List<Item> findAvailableItems() {
return items.stream.filter(item -> !item.isSold()).collect(Collectors.toList);
}
public List<Item> findSoldItems() {
return items.stream.filter(item -> item.isSold()).collect(Collectors.toList);
}现在我们用它们就像
System.out.println("Available items:");
printItemsOnSeparateLines(findAvailableItems());我们的方法都比较简单。
我还从属性访问更改为isSold。这对我来说读得更好,而且封装得更好。
现在,如果我们想出新的标准,我们可以添加一个新的简单方法,只需重用我们现有的显示方法。或者我们可以添加一个新的显示并重用我们的业务逻辑。
这需要Item包含它的标识符,该标识符可能就在Map键中。
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 sellItem() {
printItemsOnSameLine(findAvailableItems());
Item item = selectItem("\nChoose item to mark as sold: ");
if (item == null) {
return;
}
if (item.isSold()) {
System.out.println("That item already sold.");
return;
}
item.setSold(true);
System.out.println("How much did you get for " + item.getName() + "?: ");
item.setSoldPrice(in.nextInt());
System.out.println("You marked " + item.getName() + " as sold");
System.out.println("Your profit is " + (item.soldPrice - item.price) + " PLN");
}使用
public Item selectItem(String query) {
System.out.println(query);
Item item = items.get(in.nextInt();
if (item == null) {
System.out.println("Item not found.");
}
return item;
}现在,我们不必不断地重写相同的代码(四次)。如果我们想要选择一个项目,我们可以调用该方法。
我还处理了一个原始代码没有的条件。如果有人输入了已经售出的物品的ID怎么办?
这段代码不是重复编写items.get(id),而是使用item。
我们不需要调用containsKey,因为我们只需调用get就可以得到相同的行为,无论如何我们都必须调用它。现在我们不用随身携带身份证了。
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()); } }
稍后,您将比较spendMoney和earnedMoney。但两者是不可比拟的。他们在不同的项目上。考虑一下
for (Item item : values()) {
spentMoney += item.getPrice();
if (item.isSold()) {
earnedMoney += item.getSoldPrice();
profit += item.getSoldPrice() - item.getPrice();
}
}现在,我们有了购买所有物品所花费的金额,而不是目前售出的物品所赚的钱。我们知道我们现在是否领先。或者,我们可以看看利润,看看我们在已经售出的产品上是否领先。
我也从entrySet更改为values,因为您从未使用过密钥。所以我们可以说是item而不是entry.getValue()。
我将spendMoney改为spentMoney,以与earnedMoney保持一致。
https://codereview.stackexchange.com/questions/213295
复制相似问题