dsaaaa12 2022-09-20 10:30 采纳率: 100%
浏览 90
已结题

java scanner 对于多行销售数据的读取 并找出最大和最小单价的对应的信息 还有不同商品其对应的的数据平均值

问题遇到的现象和发生背景

java scanner 就是我知道如何用它 但是我现在有的数据是

img


也就是这样的
6
10/15/21 17:00 phone 85.50 12 4.0 8
11/24/21 13:30 phone 959.90 44 4.5 7
3/18/22 1:16 jewelry 1699.00 1 3.5 19
4/7/22 20:45 phone 1699.00 1 5.0 3
5/17/22 23:45 book 85.50 11 4.1 5
5/19/22 07:16 book 15.50 2 3.0 12

分别对应的是 MM/DD/YY, HH:MM, Category, Price, Quantity, Rating, Duration
然后我现在在scanner这步困住了 我已经建立了我的class 并且要把我的数据 一一把它放进去 然后达到我想要的效果

用代码块功能插入代码请勿粘贴截图
import java.util.Scanner;
public class Deal {
    private String date;
    private String time;
    private String type;
    private Double price;
    private Double quantity;
    private Double rating;
    private Double duration;

    public Deal(String date, String time, String type, Double price, Double quantity, Double rating, Double duration) {
        this.date = date;
        this.time = time;
        this.type = type;
        this.price = price;
        this.quantity = quantity;
        this.rating = rating;
        this.duration = duration;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        s.nextInt() = 

    }





我的解答思路和尝试过的方法

我尝试了用scanner 但是我不知道如何处理多行数据 就是我只会把一行的数据分类 然后后面就完全不知道了
然后我也不知道该怎么去找对应的数值
就比如说我把每一行的价格都找到 然后归类到我的price里 这样我就会有一个都是price数值的array 那我可以找到一个最大的值 可是我怎么去找相对的卖的东西是什么,时间是什么 就是我不知道在我写的code的基础上如何去用scanner来完成我想要达到的效果

我想要达到的结果

img


这个是我想要的结果
第一个是找最大单价的东西 并且相对应的数值
第一个是最低单价
后面就是按照种类的平均数

  • 写回答

2条回答 默认 最新

  • Java大魔王 2022-09-20 10:51
    关注
    
    import java.io.File;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Scanner;
    
    public class Deal {
        
        private String date;
        private String time;
        private String type;
        private Double price;
        private Double quantity;
        private Double rating;
        private Double duration;
     
        public Deal(String date, String time, String type, Double price, Double quantity, Double rating, Double duration) {
            this.date = date;
            this.time = time;
            this.type = type;
            this.price = price;
            this.quantity = quantity;
            this.rating = rating;
            this.duration = duration;
        }
    
        public Double getPrice() {
            return price;
        }
    
        public String getDate() {
            return date;
        }
    
        public String getTime() {
            return time;
        }
    
        public String getType() {
            return type;
        }
    
        public Double getQuantity() {
            return quantity;
        }
    
        public Double getRating() {
            return rating;
        }
    
        public Double getDuration() {
            return duration;
        }
    
        public static void main(String args[]) throws Exception{
            List<Deal> list = new ArrayList<>();
            //读取文件
            File file = new File("test.txt");
            Scanner sc = new Scanner(file);
            //文件每行取值
            while(sc.hasNextLine()){
                String line = sc.nextLine();
                String[] argsArr = line.split(" ");
                //对象生成
                Deal deal = new Deal(argsArr[0], argsArr[1], argsArr[2], Double.parseDouble(argsArr[3]), 
                        Double.parseDouble(argsArr[4]), Double.parseDouble(argsArr[5]), Double.parseDouble(argsArr[6]));
                list.add(deal);
            }
            //最大最小值处理
            Deal max = list.get(0);
            Deal min = list.get(0);
            for(Deal deal : list) {
                if(deal.getPrice() > max.getPrice()) {
                    max = deal;
                }
                if(deal.getPrice() < min.getPrice()) {
                    min = deal;
                }
            }
            System.out.println("Highest per unit sale:");
            System.out.println("\tWhen:" + max.getDate() + " " + max.getTime());
            System.out.println("\tCategory:" + max.getType());
            System.out.println("\tPrice:" + max.getPrice());
            System.out.println("\tRating:" + max.getRating());
            System.out.println("Lowest per unit sale:");
            System.out.println("\tWhen:" + min.getDate() + " " + min.getTime());
            System.out.println("\tCategory:" + min.getType());
            System.out.println("\tPrice:" + min.getPrice());
            System.out.println("\tRating:" + min.getRating());
            //类别平均处理
            Map<String, List<Deal>> map = new HashMap<>();
            for(Deal deal : list) {
                if(map.get(deal.getType()) == null) {
                    List<Deal> itemList = new ArrayList<>();
                    itemList.add(deal);
                    map.put(deal.getType(), itemList);
                }else {
                    map.get(deal.getType()).add(deal);
                }
            }
            HashMap<String, String> resultMap = new HashMap<String, String>();
            for(Entry<String, List<Deal>> entry : map.entrySet()) {
                Double sumPrice = 0.0;
                Double sumQuantity = 0.0;
                Double sumRating = 0.0;
                Double sumDuration = 0.0;
                List<Deal> valueList = entry.getValue();
                for(Deal deal : valueList) {
                    sumPrice = sumPrice + deal.getPrice();
                    sumQuantity = sumQuantity + deal.getQuantity();
                    sumRating = sumRating + deal.getRating();
                    sumDuration = sumDuration + deal.getDuration();
                }
                DecimalFormat dmf = new DecimalFormat(".00");
                String str = "Averages by " + entry.getKey() + ":" + "\n\tQuantity:" +
                    dmf.format((sumQuantity/valueList.size())) + "\n\tPrice:" +  
                    dmf.format((sumPrice/valueList.size())) + "\n\tRating:" +  
                    dmf.format((sumRating/valueList.size())) + "\n\tDuration:" +  
                    dmf.format((sumDuration/valueList.size()));
                resultMap.put(entry.getKey(), str);
            }
            System.out.println(resultMap.get("book"));
            System.out.println(resultMap.get("jewelry"));
            System.out.println(resultMap.get("phone"));
            
        }
        
    }
     
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月22日
  • 已采纳回答 9月21日
  • 赞助了问题酬金66元 9月21日
  • 创建了问题 9月20日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度