zoukankan      html  css  js  c++  java
  • 使用Iterator遍历hashMap

      1 package com.ls.service;
      2 
      3 import java.util.*;
      4 import com.ls.domain.Book;
      5 import com.sun.media.sound.AlawCodec;
      6 public class MyCartService {
      7     
      8     HashMap<String,Book> hm=new HashMap<String, Book>();
      9     //Add2 books
     10     
     11     public void addBook(String id){
     12         if(hm.containsKey(id)){
     13         Book book =hm.get(id);
     14         int shoppingNum=book.getShoppingNum();
     15         book.setShoppingNum(shoppingNum+1);
     16         }else{
     17         hm.put(id, new BookService().getBookById(id));
     18         }
     19     }
     20     
     21     
     22     
     23     //Add books
     24     public void addBook(String id,Book book){
     25         if(hm.containsKey(id)){
     26         System.out.println("hm.containsKey(id)");
     27         int shoppingNum=book.getShoppingNum();
     28         book.setShoppingNum(shoppingNum+1);
     29         }else{
     30             System.out.println("xxx");
     31             hm.put(id, book);    
     32         }
     33     }
     34     
     35     
     36     
     37     //Delete books
     38     public void deleteBook(String id){
     39         hm.remove(id);
     40     }
     41     
     42     
     43     
     44     
     45     //Update books/myCart   numbers
     46     
     47     public void updateBook(String id,String num){
     48         //取出ID对应book
     49         Book book =hm.get(id);
     50         book.setShoppingNum(Integer.parseInt(num));
     51         
     52     }
     53     
     54     
     55     //显示购物车中的所有商品信息
     56     public ArrayList showMyCart(){
     57         
     58                   ArrayList<Book> alBook=new ArrayList<Book>();
     59         
     60     //遍历hashMap
     61     
     62         
     63     Iterator iterator =hm.keySet().iterator();
     64     while(iterator.hasNext()){
     65     //key
     66     String id=(String)iterator.next();
     67     //Book
     68     Book book=hm.get(id);
     69     alBook.add(book);
     70         }
     71         return alBook;
     72     }
     73     
     74     //返回该购物车的总价
     75     public float getToatlPrice(){
     76         float totalPrice=0.0f;
     77         
     78         //得到总价格
     79         ArrayList<Book> al =new ArrayList<Book>();
     80         
     81         Iterator iterator=hm.keySet().iterator();
     82         while(iterator.hasNext()){
     83             //取出书号
     84             
     85             String bookId=(String)iterator.next();
     86             //取出书号对应的bookbean
     87             Book book=hm.get(bookId);
     88     totalPrice+=book.getPrice()*book.getShoppingNum();
     89             
     90         }
     91         
     92         
     93         return totalPrice;
     94     }
     95     
     96     //Clear books/myCart
     97     public void cleanBook(){
     98         hm.clear();
     99     }
    100     
    101 
    102 }
  • 相关阅读:
    一些你可能用到的代码
    iOS 键盘下去的方法
    iOS设计模式汇总
    随笔
    Spring cloud config 分布式配置中心 (三) 总结
    Spring cloud config 分布式配置中心(二) 客户端
    Spring cloud config 分布式配置中心(一) 服务端
    jdbcUrl is required with driverClassName spring boot 2.0版本
    JpaRepository接口找不到 spring boot 项目
    解决IntelliJ “Initialization failed for 'https://start.spring.io'
  • 原文地址:https://www.cnblogs.com/lshblogs/p/4740027.html
Copyright © 2011-2022 走看看