gpt4 book ai didi

java - 我需要帮助在 Java 中制作最低价格杂货程序

转载 作者:行者123 更新时间:2023-12-02 08:40:32 25 4
gpt4 key购买 nike

下面是我的代码

import java.util.*;
import java.io.*;

public class main {
public int finalcost;

public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner input = new Scanner(System.in);
int totalAdd = 0;
boolean done = false;
// to store specific properties ex item size and price
HashMap<String, String> specificPropsMap = new HashMap<String, String>();

// stores an array list of all the sizes and price
ArrayList<HashMap<String, String>> propsList = new ArrayList<HashMap<String, String>>();

// the item list stores the name of the item as the KEY and item size and price
// as data
HashMap<String, ArrayList<HashMap<String, String>>> itemsMap = new HashMap<String, ArrayList<HashMap<String, String>>>();

// this stores the service name than the cost.this is the one we will be adding
// too after getting the data;
HashMap<String, Integer> cartMap = new HashMap<String, Integer>();

BufferedReader br = new BufferedReader(new FileReader("PA2.txt"));
String line = br.readLine();
while ((line = br.readLine()) != null) {
// stores the data base on commas
String[] fields = line.split(",");
String serviceName = fields[0];
String fileName = fields[1];
String cost = fields[2];

if (specificPropsMap.containsKey(serviceName)) {
// do nothing
} else {
specificPropsMap.put(serviceName, null);
}
// gets the file name from the first reader
BufferedReader brCVS = new BufferedReader(new FileReader(fileName));
String lineCVS = brCVS.readLine();
while ((lineCVS = brCVS.readLine()) != null) {
String[] cvsFields = lineCVS.split(",");
String brandName = cvsFields[0];
String nameItem = cvsFields[1];
String itemSize = cvsFields[2];
String itemPrice = cvsFields[3];

// check if itemname is in specificPropsMap
if (specificPropsMap.containsKey(cvsFields[1])) {
// do nothing
} else {
specificPropsMap.put(itemSize, nameItem);
}

propsList.add(specificPropsMap);

if (itemsMap.containsKey(nameItem)) {
// do nothing
} else {
itemsMap.put(nameItem, null);
}

} // end inner while loop

int stringCosttoInt = Integer.parseInt(cost.trim());
if (cartMap.containsKey(serviceName)) {
// do nothing
} else {
cartMap.put(serviceName, stringCosttoInt);
}
} // end outer while loop

System.out.println("Welcome to Assisgnment two app");

while (done == false) {

System.out.println("Enter the item you are looking for or enter done check out: ");

String key = input.nextLine(); // use key to find if item is in the itemsMap;
if (key == "done") {
done = true;
} else {
if (itemsMap.containsKey(key)) {
System.out.println("Enter the Size you want :");
// now check for the item name, than size, if not return item not there;
int sizeKey = input.nextInt(); // use key to find item in the area
if (itemsMap.containsValue(specificPropsMap.containsKey(sizeKey))) {

System.out.println("How many of " + key + " do you want:");
int numOfProduct = input.nextInt();

// add it to the cart;
} else {
System.out.println("No: " + sizeKey + " in the system");

}
} else {
System.out.println("No item by the name of: " + key + " in the system");
}
}
}
br.close();

input.close();
}

}


问题:如何设置用户输入来搜索项目类别以返回最低的总成本。用户输入元素名称、尺寸和数量。我知道将名称存储在项目中,但我不知道如何将其与用户输入相匹配,也不知道如何获取该数据(如果有意义的话)。第二,当我读取文件并将其存储为一个字符串,我不知道如何将其转换为整数,以便我可以用它进行数学计算..

最佳答案

只是为了给您提供一些有关该方法的帮助,我不会详细介绍解析,我想您已经了解了。

从另一个角度来看,从输入来看。程序的流程是怎样的?

  • 用户输入必填itemsize
  • 看看 item存在
  • 用户输入数量
  • 添加到购物车
  • 重复
  • “完成”后,检查哪项服务提供的总价格最低

注意:假设某个品牌中存在的商品存在于所有品牌中

所以让我们设计一个可以支持这一点的数据结构:

  • 我们需要维护 Cart对于每个 Service 。最后,我们可以循环浏览每个购物车并返回最低的。
    • 维护Map<String, Integer>哪里StringService ,和Integertotal price
    • 每当 Item由用户添加,添加该价格Item分别为Service
    • 例如。如果添加了 Lays,则将 Prime 中的 Lays 价格添加到 Prime 总价中,将 InstaCart 中的 Lays 价格添加到 InstaCart 总价中,等等
  • 您需要查找的第一件事是是否 Item存在;我们可以将其存储在Map中,比如说itemsMap
  • 每个项目都有哪些具体属性?有service , brand , size , price 。我们可以将它存储在另一个Map中,比如说specificPropsMap .
  • 每个Item可以具有多个特定属性。即,size 可以有不同的组合, service等相同Item 。所以每个Item需要存储多个特定属性,可以是List ,比如说propsList .

要理解上述内容:

//Starting from the bottom-up
//To store specific properties
//Contains key-value pairs like: service = Amazon, brand = Nestle, size = 10g, price = 2
HashMap<String, String> specificPropsMap; //Has one set of specific props

//Consider an Item; it can have multiple specific properties, which we'll store as a List
//Eg: For Item = 'dark chocolate'
//specificProps1: service = Amazon, brand = Nestle, size = 10, price = 2
//specificProps1: service = InstaCart, brand = Cadbury, size = 10, price = 3
//Required: List<specificPropsMap>
ArrayList<HashMap<String, String>> propsList; //Has a list of specificPropsMaps for one item

//Now we need to store all Items; it can be a Map
//Required: Map<ItemName, propsList for that Item>
HashMap<String, ArrayList<HashMap<String, String>>> itemsMap;


//Initialize cart Map while parsing services
HashMap<String, Integer> cartMap; //Has initial values "Amazon" = 0, InstaCart = 0

//To find if an Item is present:
itemsMap.contains("Dark chocolate");

//Find prices from each service for that Item and size, and quantity
int reqSize = 10; //Assume req. size = 10
int reqQuantity = 5;
ArrayList<HashMap<String, String>> propsList = itemsMap.get("Dark chocolate")
for (HashMap<String, String> specificPropsMap : propsList) { //For each set of props
int size = Integer.parseInt(specificPropsMap.get("size"));
if (size == reqSize) {
String service = specificPropsMap.get("service"); //Say Amazon
int price = Integer.parseInt(specificPropsMap.get("price")); //Say 2
int initialPriceInCart = cartMap.get(service); //Initially 0
int finalPriceInCart = initialPriceInCart + price * reqQuantity;
cartMap.put(service, finalPriceInCart); //Cart price updated
}
}

//Find lowest priced service
String lowestPrice = Integer.MAX_VALUE; //Initially set as high as possible
String lowestService = "";
for (String key : cartMap.keySet()) {
if (cartMap.get(key) < lowestPrice) {
lowestPrice = cartMap.get(key);
lowestService = key;
}
}


我能想到的一般指示:
填充:从文件读取时首先填充所有这些值。
转换:将尺寸、价格等值转换为标准(公斤、美分/美元等)
命名:最好保持描述性( brandbrandName 而不是 bName )
错误处理:在必要的地方添加检查/try-catch block

编辑:添加步骤。可能看起来有点复杂,因为 txt 中有一些条目还有一些在 csv ,但实际上很简单:

  • 阅读 txt文件;你现在有一个service和一个 csv文件。因此,对于每个 service (外循环)
    • 添加 service作为 cartMap 的关键如果它还不存在
    • 创建一个新的specificPropsMap ,添加service对此
  • 阅读csv对应于service 。对于每个item (内循环)
    • 将所有 Prop 存储在同一个 specificPropsMap
    • 您现在拥有 Prop item来自csv
    • 检查是否 item存在于 itemsMap
    • 如果存在,请跳至下一步。如果没有,请添加itemitemsMap作为键,带有空 List作为值(value)
    • itemsMap.get(item) ,您将拥有 propsList
    • 添加specificPropsMappropsList
  • 对所有人重复 item以及所有service

关于java - 我需要帮助在 Java 中制作最低价格杂货程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61416854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com