Java 我需要往哪个方向去完成这个购物车任务?
所以我基本上完全不知道我该去哪里完成这个任务。这就是作业要求我做的 “第一部分: 设计一个名为Item的类,该类包含有关商店中待售商品的信息。商品具有以下属性:名称、供应商、价格、重量、应纳税(一个布尔字段,指示是否对该商品征税) 第二部分: 设计一个包含商品的ShoppingCart类 添加项以将项目插入购物车。 CartTotal计算购物车的总价。 CartTaxAmount接收税率并计算当前购物车中项目的总税费。此计算仅应考虑应税项目 如果它使事情变得更简单,那么可以假设ShoppingClass的容量固定为10个项目 请记住: 每个类应该至少有一个接收参数的构造函数。 使数据成员私有,并在必要时创建访问器和mutator方法 每个类都应该有一个toString方法,用于显示类的内容。 编写一个独立函数,允许用户输入项目信息并将项目返回给调用函数。 " 我只是没有考虑如何在另一个类的数组中存储类项Java 我需要往哪个方向去完成这个购物车任务?,java,Java,所以我基本上完全不知道我该去哪里完成这个任务。这就是作业要求我做的 “第一部分: 设计一个名为Item的类,该类包含有关商店中待售商品的信息。商品具有以下属性:名称、供应商、价格、重量、应纳税(一个布尔字段,指示是否对该商品征税) 第二部分: 设计一个包含商品的ShoppingCart类 添加项以将项目插入购物车。 CartTotal计算购物车的总价。 CartTaxAmount接收税率并计算当前购物车中项目的总税费。此计算仅应考虑应税项目 如果它使事情变得更简单,那么可以假设ShoppingC
import java.util.Scanner;
class Item {
//data members
private String Name;
private String Vendor;
private double Price;
private double Weight;
private boolean Taxable;
//constructor
Item(String conName, String conVendor, double conPrice, double conWeight, boolean Tax)
{
Name = conName;
Vendor = conVendor;
Price = conPrice;
Weight = conWeight;
Taxable = Tax;
}
//Aceessor methods
public String getname(){
return Name;
}
public String getvendor(){
return Vendor;
}
public double getprice(){
return Price;
}
public double getweight(){
return Weight;
}
public boolean gettaxable(){
return Taxable;
}
public String toString()
{
String s = "Item Name is " + Name + " and the vendor is " + Vendor + ". The Price of the item is " + Price + " and the weight of the item is " + Weight + ". Item is taxable = " + Taxable + ".";
return s;
}
}
public class ShoppingCart {
private Item[] itemsToBuy;
private double taxRate;
private int numItems;
ShoppingCart(int capacity, double taxamount) {
itemsToBuy = new Item[capacity];
taxRate = taxamount;
numItems = 0;
}
//Accessor methods
public double gettaxRate(){
return taxRate;
}
public int getItemNum() {
return numItems;
}
public Item getitemsToBuy(int i) {
return itemsToBuy[i];
}
//Methods
public void AddItem() {
}
抬起头来;你在正确的轨道上。您可以将项目传递给
AddItem
方法,然后将其插入itemsToBy
数组中。你只要检查一下,确保车里有地方
试试这个:
public void AddItem(Item item) {
if(numItems < itemsToBuy.length){
itemsToBuy[numItems] = item; //insert item in the array
numItems++; //increment the number of items
} else {
System.out.println("Your cart is full.");
}
}
public void AddItem(项目){
if(numItems
为什么不尝试实现一个从购物车中删除最新项目的removeLastItem()
方法呢。这应该有助于你练习
我建议您观看Java的完整初学者视频。当我第一次开始学习Java时,它们非常有用。我不能给你一个现成的答案,因为这显然是你应该做的家庭作业。但以下几点可以帮助您更好地理解问题: 项目 您需要创建一个名为Item的公共类。这看起来与您已有的类似,只是您可以进行一些更改:
导入java.util.List;
导入java.util.ArrayList;
公共类购物车{
私有最终列表项;//项可以是任意大小
私人双重税率;
公共购物车(){
this.items=new ArrayList();
}
//项目列表的getter
公共列表getItems(){
归还此物品;
}
//我们有一个“加法器”,而不是setter
//添加一个元素。您可以调用此方法
//将项目一次添加到项目列表中的步骤
公共无效附加项(项目){
此.items.add(item);
}
//或者可以使用varargs加法器
//您可以方便地添加多个
//一次一件
公共无效附加项(项…项){
if(items!=null&&items.length>0){
用于(项目:项目){
此.items.add(item);
}
}
}
//税率吸气剂
公共双getTaxRate(){
返回此税率;
}
//税率制定者
公共税收税率(双重税率){
这个。税率=税率<0?0:税率;
}
//已计算项计数;它不是字段获取程序
public int getItemCount(){
返回此.items.size();
}
//计算不含税的总价
公共双计算CartPriceTotalNotAX(){
双倍合计=0.0;
用于(项目:此.items){
合计+=项目。getPrice();
}
返回总数;
}
//计算含税总价
公共双计算CartPriceTotal(){
double totalNoTax=calculateCartPriceTotalNoTax();
double total=totalNoTax*getTaxRate()/100;
返回总数;
}
//计算含税总价(不同的计算方法)
公共双计算CartPriceTotal2(){
双倍合计=0.0;
double taxRate=getTaxRate();
用于(项目:此.items){
总计+=(item.getPrice()*getTaxRate()/100);
}
返回总数;
}
//用于ShoppingCart的toString方法。
公共字符串toString(){
StringBuilder sb=新的StringBuilder();
sb.append(getClass().getName()).append(“\n”)
.append(“\t items:”).append(getItems()).append(“\n”)
.append(“\t taxRate:”).append(getTaxRate()).append(“\n”)
.append(“\t totalPriceNoTax:”).append(calculateCartPriceTotalNoTax()).append(“\n”)
.append(“\t totalPriceWithTax:”).append(calculateCartPriceTotal()).append(“\n”);
使某人返回字符串();
}
}
注意:
也许你应该开始使用Eclipse(或者IntelliJ Idea,如果你喜欢的话)。这将使编写Java代码变得更加容易,并帮助您避免许多错误。这似乎是您应该向教授或助教提出的问题。我可能会建议一些类似于让AddItem获取一个项目和
<pre>
import java.util.List;
import java.util.ArrayList;
public class ShoppingCart {
private final List<Item> items; // items can be of any size
private double taxRate;
public ShoppingCart() {
this.items = new ArrayList<Item>();
}
// getter for items List
public List<Item> getItems() {
return this.items;
}
// instead of a setter, we have an 'adder' which
// adds one element. you can call this method
// to add items one at a time to the items List
public void addItem(Item item) {
this.items.add(item);
}
// or you can use a varargs adder, using which
// you have the convenience of adding more than
// one item at a time
public void addItem(Item... items) {
if(items != null && items.length > 0) {
for(Item item : items) {
this.items.add(item);
}
}
}
// getter for taxRate
public double getTaxRate() {
return this.taxRate;
}
// setter for taxRate
public void setTaxRate(double taxRate) {
this.taxRate = taxRate < 0 ? 0 : taxRate;
}
// item count is calculated; it's not a field getter
public int getItemCount() {
return this.items.size();
}
// calculates total price without taxes
public double calculateCartPriceTotalNoTax() {
double total = 0.0;
for(Item item : this.items) {
total += item.getPrice();
}
return total;
}
// calculates total price with taxes
public double calculateCartPriceTotal() {
double totalNoTax = calculateCartPriceTotalNoTax();
double total = totalNoTax * getTaxRate() / 100;
return total;
}
// calculates total price with taxes (different way of doing it)
public double calculateCartPriceTotal2() {
double total = 0.0;
double taxRate = getTaxRate();
for(Item item : this.items) {
total += (item.getPrice() * getTaxRate() / 100);
}
return total;
}
// toString method for the ShoppingCart.
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getName()).append("\n")
.append("\t items: ").append(getItems()).append("\n")
.append("\t taxRate: ").append(getTaxRate()).append("\n")
.append("\t totalPriceNoTax: ").append(calculateCartPriceTotalNoTax()).append("\n")
.append("\t totalPriceWithTax: ").append(calculateCartPriceTotal()).append("\n");
return sb.toString();
}
}
</pre>