北美微论坛

App下载
扫码下载 App
联系我们
1框架
查看: 1003|回复: 9
打印 上一主题 下一主题

[CS/EE] JAVA求助

[复制链接]

小学生

Rank: 1

跳转到指定楼层
楼主
发表于 2017-10-29 14:19:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
In this programming assignment, the goal is to create a smart shopping cart.  Think of the self-checkout option in many stores these days.

Design a ShoppingCart class that contains Items.  This uses composition since there is an array of Item objects inside a ShoppingCart.  Several interesting methods are

  • addItem to insert an item to the cart.  This is a void method since it modifies the state of the array in the ShoppingCart object.  There are different ways to implement this method.  One was is to have a single parameter that is an already constructed Item.  Another way is to have a set of parameters that are the parameters to the constructor of the Item class.
  • cartTotal computes the total price of the cart.  This method returns a double but does not need any parameters since it works with data members of the ShoppingCart object.
  • cartTaxAmount receives the tax rate and computes the total tax to charge for the items currently in the cart.  Only Taxable items should be considered for this calculation.  This method also returns a doublebut does not need any parameters since it works with data members of the ShoppingCart object.

For simplicity, you can assume that the capacity of a ShoppingCart is fixed at 10 items.

Keep in mind:

  • Each class should have at least one constructor that receives arguments.
  • Make data members private and create accessor and mutator methods whenever necessary.
  • Each class should have a toString method that you use to display the contents of the class.

请问各位大神现:
1、An array of Item, 数组里面是什么变量好?
2、如何用addItem()把Item现添加到数组里面?

感谢各位大神!
收藏收藏 分享分享 赞赞赞!赞赞赞! 踩踩踩!踩踩踩! 新浪微博微博分享
回复

使用道具 举报

助理教授

Rank: 8Rank: 8

沙发
发表于 2017-10-29 15:32:32 | 只看该作者
1. 存item object
2. addItem method里面定义一个ArrayList<Item>比如名字叫list。然后list.add(item object)就完了
回复

使用道具 举报

小学生

Rank: 1

板凳
 楼主| 发表于 2017-10-29 15:52:33 来自手机 | 只看该作者
semeku 发表于 2017-10-29 15:32
1. 存item object
2. addItem method里面定义一个ArrayList比如名字叫list。然后list.add(item object)就完 ...

非常感谢,可以说具体一点吗?
来自: iPhone客户端
回复

使用道具 举报

禁止访问

地板
发表于 2017-10-29 18:54:56 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
留学生课程辅导——www.51lxf.com
回复

使用道具 举报

小学生

Rank: 1

5#
 楼主| 发表于 2017-10-29 19:23:48 | 只看该作者
龙猫123 发表于 2017-10-29 18:54
如果需要辅导可以加我微信哦 yuff996

免费的吗?我只是一枚穷学生。。。。。
回复

使用道具 举报

初中生

Rank: 2

6#
发表于 2017-10-29 20:34:39 | 只看该作者
本帖最后由 niubilityNo.1 于 2017-10-29 20:48 编辑

public class ShoppingCart{
        private List<Item> list;
        public ShoopingCart(){
              list = = new ArrayList<Item>();
      }
       void addItem(Item item){
                list.add(item)
      }
}


用hashmap比较好,list你不知道怎么计算商品的数量
public class ShoppingCart{
        private Map<Item,Integer> map;        public ShoopingCart(){
              map = new HashMap<Item,Integer>();
      }
       void addItem(Item item){
              Integer  count = map.get(item);
                if( count != null){
                      map.put(item,count+1);
               }else{
                     map.put(item,1)
              }
      }
}




class Item{
      private String name;
      private float price;
      public String getName(){
            return Name
      }

      public void setName(String name){
              this.name = name;
     }
      这里省略其它属性的get,set方法...........................................................
     @Ovrride
      public String toString(){
              return "Name: " + this.name + " price:" + this.price;
       }
}
回复

使用道具 举报

助理教授

Rank: 8Rank: 8

7#
发表于 2017-10-30 01:49:35 | 只看该作者
niubilityNo.1 发表于 2017-10-29 20:34
public class ShoppingCart{
        private List list;
        public ShoopingCart(){

你用hashmap,他算不来总价了呀。哈哈哈哈。。还是arraylist,直接forloop算价钱,他容易算些。
回复

使用道具 举报

小学生

Rank: 1

8#
 楼主| 发表于 2017-10-30 16:10:06 来自手机 | 只看该作者
非常感谢各位大神帮忙……
来自: iPhone客户端
回复

使用道具 举报

禁止访问

9#
发表于 2017-10-31 18:54:37 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
留学生课程辅导——www.51lxf.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

返回顶部