- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧!所以我对 Java 还是个新手,我们这周才开始学习实例和类。
我的问题是我的教授分配了一个程序(我已经花了大约 10 个小时,我已经取得了进展,但我仍然遇到问题),该程序的说明有点模糊,我不知道如何将它们合并到我的程序中。
这是一个购物车程序,获取产品的名称、描述、价格和数量。所有这些品质都体现在 Item2
中。类,我没有遇到问题,然后使用 toString()
进行打印Item2
中的方法类。
接下来,我获取该名称并将其添加到 ShoppingCart2
中的数组中。类( addItems
方法),然后创建 getTotal
将经历 arrayItems
的方法,并将每个项目的价格相加(应调用每个 Item 对象的 calculateUnitTotal()
方法并相加)。
我的问题是,通过尝试调用 calculateUnitTotal()
,我要么收到错误:
non static method cannot be referenced from a static context
或取消引用错误。
我的教授不希望我从 Item2
调用单位和价格对象类,我需要专门调用这个方法。
我知道我需要创建一个实例来执行此操作,并且对象无法转换为 double ,但我尝试的所有操作似乎都不起作用。
我不确定我做错了什么。我也知道代码有点乱,我尽力清理它。任何建议将不胜感激!
Item2.java:
class Item2{
private String productName;
public class Item2{
private String productName;
private String productDesc;
private double unitPrice;
private int units;
public String getProductName(){
return productName;
}
public String getproductDesc(){
return productDesc;
}
public double getUnitPrice(){
return unitPrice;
}
public int getUnits(){
return units;
}
public void setProductName(String newProductName){
productName = newProductName;
}
public void setProductDesc(String newProductDesc){
productDesc = newProductDesc;
}
public void setUnitPrice(double newUnitPrice){
unitPrice = newUnitPrice;
}
public void setUnits(int newUnits){
units = newUnits;
}
void Item2(){
productName = "";
productDesc = "";
unitPrice = -1;
units = -1;}
public void Item2(String newProductName, String newProductDesc,
double newUnitPrice, int newUnits) {
productName = newProductName;
productDesc = newProductDesc;
unitPrice = newUnitPrice;
units = newUnits;
}
public double calculateUnitTotal(){
double total = unitPrice * units;
return total;
}
public String toStrings() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (productName + "\t" + fmt.format(unitPrice) + "\t" +
units + "\t" + fmt.format(unitPrice * units));
}
}
ShoppingCart2.java:
class ShoppingCart2{
private String[] arrayItems;
private int numItems;
public ShoppingCart2(){
arrayItems = new String[20];
numItems = 0;
}
public void addItems(String itemName){
for(int i = 0; i < numItems; i++){
if(numItems==arrayItems.length)
System.out.println("Cart is full.");
else{
arrayItems[numItems]= itemName;
numItems++;
}
}
}
public ShoppingCart2 getTotal(){
ShoppingCart2 total = new ShoppingCart2();
for(int i = 0; i < numItems; i++){
/////I've tried several different methods here, they always
////lead to either
/// a need to dereference or the non static method error
}
return total;}
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String cart = "\nShopping Cart\n";
for (int i = 0; i < numItems; i++)
cart += arrayItems[i] + "\n";
cart += "\nTotal Price: " + fmt.format(total);
cart += "\n";
return cart;
}
}
我希望输出是数组中项目的名称以及所有项目的总数。
最佳答案
在我看来,这个想法是使用 Item2 类来封装所有属性,然后使用 ShoppingCart2 来管理您对它们的操作。
因此,您的 ShoppingCart2 可能必须如下所示:
class ShoppingCart{
private ArrayList<Item2> items;
public ShoppingCart2(){
items = new ArrayList<Item2>();
}
public void addItem(Item2 item){
items.add(item);
}
public void addItems(ArrayList<Items> items){
this.items.addAll(items)
}
public double getTotal(){
double total = 0L;
for(Item2 item : items){
total += item.calculateUnitTotal();
}
return total;
}
public String toString(){
StringBuffer sb = new StringBuffer();
for (Item2 item: items){
// Print each item here
sb.append(item.toString());
sb.append("----------------\n");
}
sb.append("*************");
sb.append("Total Price: ");
sb.append(getTotal());
return sb.toString();
}
}
注意:此代码尚未经过充分测试。
* 更新 *
仔细一看,你的 Item2 类是错误的。这是一个更干净的版本。
class Item2 {
private String productName;
private String productDesc;
private double unitPrice;
private int units;
public Item2(){
productName = "";
productDesc = "";
unitPrice = -1;
units = -1;
}
public Item2(String newProductName,
String newProductDesc,
double newUnitPrice,
int newUnits) {
productName = newProductName;
productDesc = newProductDesc;
unitPrice = newUnitPrice;
units = newUnits;
}
public String getProductName(){
return productName;
}
public String getproductDesc(){
return productDesc;
}
public double getUnitPrice(){
return unitPrice;
}
public int getUnits(){
return units;
}
public void setProductName(String newProductName){
productName = newProductName;
}
public void setProductDesc(String newProductDesc){
productDesc = newProductDesc;
}
public void setUnitPrice(double newUnitPrice){
unitPrice = newUnitPrice;
}
public void setUnits(int newUnits){
units = newUnits;
}
public double calculateUnitTotal(){
return unitPrice * (double) units;
}
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
StringJoiner sj = new StringJoiner("\t");
sj.add(productName).add(unitPrice).add(units).add(fmt.format(unitPrice * units));
return sj.toString();
}
}
一些解释
连接字符串时希望使用 StringJoiner 或 StringBuffer 的原因是它比使用加号 +
运算符更有效。
加号运算符会产生大量开销,因为它需要两个参数、字符串,并从中创建一个全新的字符串。
StringBuffer 和 StringJoiner 允许您添加字符串,并在最后一刻将它们全部变成一个字符串。
关于java - 如何正确调用 Item2.calculateUnitTotal() 以便将金额添加到总计变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55799797/
这里我试图在 FlatList 中显示一个名为“posts”的数组。 render() { console.log(this.props.posts); return (
这是我的代码: {{day(list)}} {{list.weather[0].description}}
我是 Mahout 的新手,并且仍在使用它。 我的问题是,将 Item-Item 和 User-Item 结合起来是否合适? 我的用例是,一个社交网络应用会尝试根据用户历史数据(优先级较高)为当前用户
下午好, 我按数据库搜索以测试特定类测试,当我放置一个新项目时,如果列表包含该项目。 @Test public void insertAndDeleteTask() throws Interrupte
我有一个关于 ionic 框架的问题,我希望有人能帮助我...我有一个带有“ion-item-right”的 ionic 列表。这一切都可以,按钮在右边。现在我需要其他三个居中的图标,这样我就有了:文
我经常遇到类似下面的代码: if ( items != null) { foreach(T item in items) { //... } } 基本上,if 条件确
我最近问了a question about LocalStorage .使用 JSON.parse(localStorage.item) 和 JSON.parse(localStorage['item
我最近问了a question about LocalStorage .使用 JSON.parse(localStorage.item) 和 JSON.parse(localStorage['item
这个问题已经有答案了: Type mismatch: cannot convert from Item to Item (1 个回答) 已关闭 7 年前。 我很困惑。我无法将外部类的实例变量 Node
我目前正在使用 MUI Grid(但我对替代解决方案持开放态度)并且我想要并排放置两个组件:最右边的组件占 400px宽度和左侧组件占据其余部分。 || || || 当页面宽度缩小时: | | ||
我最近问过a question about LocalStorage 。使用 JSON.parse(localStorage.item) 和 JSON.parse(localStorage['item
public class Document extends Model { ... @ManyToMany public Set accessors; ... } 我想选择访问者包含某个用户的所有文档
我正在使用 selenium webdriver 为单页 Web 应用程序开发一个 Java 框架,使用以下模式:PageObject、SlowLoadableComponent(责任链)、PageF
最近在学习C,在网上发现了一个问题。问题是: What is the problem with this function in terms of memory allocation? What is
我有这个代码 ( -1 ? true : false} /> {genre.item.name}
在ASP.Net中使用DataGrid时真的没有快捷方法吗 (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.A
我正在使用工作流程根据数据和一组要求将大量 pdf 从一个位置复制到其他大坝位置。我正在使用以下代码 Assets damAsset = manager.createAsset(path, is, m
我是 PowerShell 的新手。 我正在尝试自动将 dll 组件从源服务器上的文件夹部署到目标服务器上的多个文件夹。这看起来应该很简单:将组件从源服务器上的源(部署)文件夹复制到目标服务器上的文件
我的代码: for column_name, column_data in summary_words.iteritems(): if column_name != "summary" and
我的代码: for column_name, column_data in summary_words.iteritems(): if column_name != "summary" and
我是一名优秀的程序员,十分优秀!