我需要帮助用Java制作最低价的杂货店程序

我需要帮助用Java制作最低价的杂货店程序,java,Java,下面是我的代码 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

下面是我的代码

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();
    }

}


import java.util.*;
导入java.io.*;
公共班机{
公共财政成本;
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
扫描仪输入=新扫描仪(System.in);
int totalAdd=0;
布尔完成=假;
//存储特定属性(不包括项目大小和价格)
HashMap specificPropsMap=新HashMap();
//存储所有大小和价格的数组列表
ArrayList propsList=新的ArrayList();
//项目列表将项目名称存储为关键字、项目大小和价格
//作为数据
HashMap itemsMap=新HashMap();
//这将存储服务名称而不是成本。这是我们将要添加的名称
//在获得数据后也会出现这种情况;
HashMap cartMap=新的HashMap();
BufferedReader br=新的BufferedReader(新文件读取器(“PA2.txt”);
String line=br.readLine();
而((line=br.readLine())!=null){
//以逗号存储数据库
String[]fields=line.split(“,”);
字符串serviceName=字段[0];
字符串文件名=字段[1];
字符串成本=字段[2];
if(specificPropsMap.containsKey(服务名称)){
//无所事事
}否则{
specificPropsMap.put(serviceName,null);
}
//从第一个读取器获取文件名
BufferedReader brCVS=新的BufferedReader(新文件读取器(文件名));
字符串lineCVS=brCVS.readLine();
而((lineCVS=brCVS.readLine())!=null){
字符串[]cvsFields=lineCVS.split(“,”);
字符串brandName=cvsFields[0];
字符串名称项=cvsFields[1];
字符串itemSize=cvsFields[2];
字符串itemPrice=cvsFields[3];
//检查itemname是否在specificPropsMap中
if(specificPropsMap.containsKey(cvsFields[1])){
//无所事事
}否则{
specificPropsMap.put(项目大小、名称项目);
}
属性列表。添加(特定属性映射);
if(项目映射容器(名称项目)){
//无所事事
}否则{
itemsMap.put(nameItem,null);
}
}//结束内部while循环
int-stringCosttoInt=Integer.parseInt(cost.trim());
if(cartMap.containsKey(serviceName)){
//无所事事
}否则{
cartMap.put(serviceName,stringCosttoInt);
}
}//结束外部while循环
System.out.println(“欢迎使用援助二号应用”);
while(done==false){
System.out.println(“输入您要查找的项目或输入完成签出:”);
String key=input.nextLine();//使用key查找项是否在itemsMap中;
如果(键==“完成”){
完成=正确;
}否则{
if(项目映射容器(键)){
System.out.println(“输入所需的大小:”;
//现在检查项目名称,而不是大小,如果不返回项目不在那里;
int sizeKey=input.nextInt();//使用键查找区域中的项
if(itemsMap.containsValue(specificPropsMap.containsKey(sizeKey))){
System.out.println(“您想要多少“+key+””);
int numOfProduct=input.nextInt();
//将其添加到购物车中;
}否则{
System.out.println(“系统中的编号:+sizeKey+”);
}
}否则{
System.out.println(“系统中没有名为“+key+”的项”);
}
}
}
br.close();
input.close();
}
}

问题:如何设置用户输入以搜索item类以返回最低的总成本。用户输入项目名称、大小和金额。我知道如何将名称存储在项中,但我不知道如何将其与用户输入匹配,或者如果有意义的话,如何获取数据。第二,当我读取文件并将其存储为字符串时,我也有点困惑,我不知道如何将其转换为int,以便我可以对其进行数学运算。

只是为了给您一些方法上的帮助,我不会详细讨论解析,我想你已经了解了

从另一个角度来看,从输入的角度来看。程序的流程是什么

  • 用户输入所需的
    项目
    大小
  • 查看是否存在
  • 用户输入数量
  • 添加到购物车
  • 重复
  • 完成后,检查哪项服务的总价最低
注:假设一个品牌中的一个项目在所有品牌中都存在

因此,让我们设计一个数据结构来支持这一点:

  • 我们需要为每个
    服务
    维护一个
    购物车
    。最后,我们可以循环使用每个购物车并返回最低的购物车。
    • 维护一个
      地图
      ,其中
      字符串
      服务
      ,而
      整数
      总价
    • 每当用户添加
      项目
      时,将该
      项目
      的价格添加到每个相应的
      服务
    • 例如,如果添加了Lays,则将Prime中的Lays价格添加到Prime的总价中,将InstaCart中的Lays价格添加到InstaCart的总价中,等等
  • 首先需要查找的是
    项是否存在;我们可以将其存储在
    映射中,比如
    itemsMap
  • 每个项目都有哪些特定属性?有
    服务
    品牌
    尺寸
    价格
    。我们可以把它储存在另一个地方<
    //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;
        }
    }