首页 > 解决方案 > 使用数组和各种类的总利润计算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();
        }
    }
}

}

在此处输入图像描述

标签: javaarrays

解决方案


跟进博恩达尔所说的话。

您的商店清单包含来自食品和文具的商品。所以你需要单独添加它们。

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();
    }
}

推荐阅读