java - 使用数组和各种类的总利润计算java
问题描述
嗨,我是编程新手,正在努力弄清楚如何为商店中的各种商品增加利润。我有一个主要方法,其中商店中的商品数组已被初始化。我在特定项目类别中有一个单独的方法来计算利润,但我不确定如何计算相同特定项目类别的总利润,所以我试图获得所有食品的总利润和所有文具的总利润项目,如果这一切都有意义的话。我希望有一种基本的方法来解决这个问题,因为我是编程新手。对于更有经验的程序员来说,这可能真的很简单,但我正在努力解决这个问题。
任何方向或帮助将不胜感激?非常感谢
包装店;
公共类商店{
private String name;
private double sellingPrice, costPrice, amount, valueOfSoldItems, costPerMonth;
private int stockMonth, stockRem, volSoldMonth;
public Shop(String name, double sellingPrice, double costPrice, int stockMonth, int volSoldMonth) {
this.name = name;
this.sellingPrice = sellingPrice;
this.costPrice = costPrice;
this.stockMonth = stockMonth;
this.volSoldMonth = volSoldMonth;
}
public double foodStats() {
return getAmount();
}
public double stationeryStats() {
return getAmount();
}
public void toolsStats() {
}
// 食物类
包装店;
公共类食品扩展商店{
double totalProfit;
public Food(String name, double sellingPrice, double costPrice, int stockMonth, int volSoldMonth) {
super(name, sellingPrice, costPrice, stockMonth, volSoldMonth);
}
public double foodStats() {
System.out.printf("%-15s%-15s", "Food: ", getName());
System.out.printf("%-15s%-10s", "Selling Price: £", getSellingPrice());
System.out.printf("%-15s%-10s", "Cost Price: £", getCostPrice());
System.out.printf("%-15s%-10s", "Stock Ordered This Month: ", getStockMonth());
System.out.printf("%-15s%-10s", "Volume Sold this Month: ", getVolSoldMonth());
setStockRem(getStockMonth() - getVolSoldMonth());
System.out.printf("%-10s%-5s", "Stock remaining: ", getStockRem());
setCostPerMonth(getCostPrice() * getStockMonth());
setValueOfSoldItems(getSellingPrice() * getVolSoldMonth());
if (getValueOfSoldItems() > getCostPerMonth()) {
setAmount(getValueOfSoldItems() - getCostPerMonth());
System.out.printf("%-10s%-5s", " Profit: £", getAmount());
} else if (getCostPerMonth() > getValueOfSoldItems()) {
setAmount(getCostPerMonth() - getValueOfSoldItems());
System.out.printf("%-10s%-5s", " Loss: £", getAmount());
} else {
System.out.print(" No Profit No Loss! ");
}
return getAmount();
}
//文具类
包装店;
公共类文具扩展商店{
public Stationery(String name, double sellingPrice, double costPrice, int stockMonth, int volSoldMonth) {
super(name, sellingPrice, costPrice, stockMonth, volSoldMonth);
}
public double stationeryStats() {
System.out.printf("%-15s%-15s", "Stationery: ", getName());
System.out.printf("%-15s%-10s", "Selling Price: £", getSellingPrice());
System.out.printf("%-15s%-10s", "Cost Price: £", getCostPrice());
System.out.printf("%-15s%-10s", "Stock Ordered This Month: ", getStockMonth());
System.out.printf("%-15s%-10s", "Volume Sold this Month: ", getVolSoldMonth());
setStockRem(getStockMonth() - getVolSoldMonth());
System.out.printf("%-10s%-5s", "Stock remaining: ", getStockRem());
setCostPerMonth(getCostPrice() * getStockMonth());
setValueOfSoldItems(getSellingPrice() * getVolSoldMonth());
if (getValueOfSoldItems() > getCostPerMonth()) {
setAmount(getValueOfSoldItems() - getCostPerMonth());
System.out.printf("%-10s%-5s", " Profit: £", getAmount());
} else if (getCostPerMonth() > getValueOfSoldItems()) {
setAmount(getCostPerMonth() - getValueOfSoldItems());
System.out.printf("%-10s%-5s", " Loss: £", getAmount());
} else {
System.out.print(" No Profit No Loss! ");
}
return getAmount();
}
}
// 主类
包装店;
公共类PrintShop {
public static void main(String[] args) {
Shop[] shops = new Shop[] { new Food("Jellies", 0.80, 0.30, 40, 35), new Food("Chocolate", 1.50, 1.80, 50, 45),
new Food("Potatoes", 0.85, 0.25, 80, 70), new Stationery("Pens", 2.00, 1.00, 25, 18),
new Stationery("Paper", 45.00, 23.00, 10, 4) };
for (Shop shop : shops) {
System.out.println(shop instanceof Food);
}
int shopDetails = 5;
double foodProfit = 0;
double stationeryProfit = 0;
for (int i = 0; i < shopDetails; i++) {
Shop s = shop[i];
if (shop instanceof Food) {
foodProfit += shops[i].foodStats();
} else {
stationeryProfit += shops[i].stationeryStats();
}
}
}
}
解决方案
跟进博恩达尔所说的话。
您的商店清单包含来自食品和文具的商品。所以你需要单独添加它们。
1) 让您的 getFoodStats() 和 getStationeryStats() 返回利润金额。
public double stationeryStats() { //changed the return
//<snip-your printing parts>
//<snip calculations>
return getAmount();
}
2)然后在您的主要功能中总结每个项目的总数。
double foodProfit=0;
double stationeryProfit=0;
for (int i = 0; i < shopDetails; i++) {
Shop shop = shop1[i];
if( shop instanceof Food){
foodProfit += shop.foodStats();
}else {
stationeryProfit += shop.stationeryStats();
}
}
推荐阅读
- java - 如何将jar内的txt文件加载为字符串形式
- javascript - dom-to-image 仅从 html 生成空白的白色图像
- javascript - TypeORM - 将新项目附加到数组类型列(Postgres)
- npm - ng add @angular/material 失败,命令中止
- python - 从python安装tensorflow的问题
- r - 在闪亮的应用程序中为嵌套在其他 tabsetPanel() 中的 tabsetPanel() 创建不同的侧边栏
- javascript - 如何在不覆盖旧数据的情况下向 Firebase 中的特定字段添加其他数据
- java - 我想以这样的方式制作一个点击列表器,当我从下拉菜单中选择一个项目时,它会转到 android studio 中的所需页面
- google-api - 错误:配额指标超出配额 - pagespeed googleapis
- azure-data-factory-2 - Azure 数据工厂查找活动