java - 使用数组和各种类计算总利润java

标签 java arrays

enter image description here您好,我是编程新手,正在努力弄清楚如何增加商店中各种商品的利润。我有一个主要方法,其中商店中的商品数组已初始化。我在特定项目类别中有一个单独的方法来计算利润,但不确定如何计算相同特定项目类别的总利润,因此我试图获取所有食品项目的总利润和所有文具项目的总利润,如果这一切都有意义的话。我希望有一个基本的方法来解决这个问题,因为我是编程新手。对于更有经验的程序员来说,这可能非常简单,但我正在努力解决这个问题。

任何指导或帮助将不胜感激? 非常感谢

包装店;

公开课商店{

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() {

}

//食品类

包装店;

公共(public)课食品延伸商店{

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)课文具延伸商店{

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

}

}

//主类

包装店;

公共(public)类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();
        }
    }
}

}

enter image description here

最佳答案

好的,对我的答案进行完整编辑。

首先,我建议您真正深入研究继承,因为您尝试做的事情确实很尴尬,并且将来如果您想正确构建它,它的成本非常高。

我还发布了必要的代码,因为否则它会太大。

我希望您进一步研究一些主题:

  • 继承
  • 编码原则
  • 命名
  • 单一职责原则

你伤害了他们中的每一个,这就是为什么你的代码如此难以维护。你可以说稍后再做或者其他什么,但稍后会很痛苦。

我的意思是:

命名我已经告诉过你了:例如 Shop 就不是一个好名字,你以前的数组名称 shop1 也不是那么好。

但是,是的,伤害你的是单一编码原则。

在你的函数 stationeryStats() 和 foodStats() 中发生了很多事情,很难得到你想要的。它打印东西,计算东西,设置新东西并返回东西。我什至不知道是什么。 它确实设置了一些东西,所以我不知道这个东西是什么。

食品/文具类的对象只知道自己的参数,因此它只能显示自己的“统计数据”。如果您想要一个摘要,则必须将其放置在有关 PrintShop 中所有对象的信息已知的位置,但是是的,我认为这种方式仍然是错误的。 你真的应该花点时间思考你的设计,因为它有很多缺陷,而且没有人真正知道你想做什么。

但这里是这样计算利润的:

double foodProfit = 0;
double stationeryProfit = 0;

for (int i = 0; i < shopDetails; i++) {
    Shop shop = shops[i];
    if (shop instanceof Food) {
        foodProfit += shops.getProfit(); // you have to define the profit for each object how to calculate it, you can put it in the Shop class if the calculation for Food and Stationery is the same and then you don't need a cast.
    } else {
        stationeryProfit += shop.getProfit();
    }
}

System.out.println("Profit stationery: " + stationeryProfit);
System.out.println("Profit food: " + foodProfit);

关于java - 使用数组和各种类计算总利润java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59343388/

相关文章:

c++ - 在 C++ 中调用的第二个构造函数(错误输出)

Java 修复文本字段上的字符串类型错误?

java - 为什么我的自定义 ListCellRenderer 忽略我对 setText 的调用?

java - 如何修复 'Potential Path Traversal (File Read)' 的 Findbugs 安全警告

php - 在 PHP 中创建数组的简单方法

arrays - 数组或字典扩展中的 Swift 3.0 : compiler error when calling global func min<T>(T, T)

c++ - 从 ProtoBuf RepeatedField 获取所有元素的最有效方法是什么?

命令行参数,打印列和行

java - 如何在使用 Stream API 通过函数更改字符串变量时跟踪它?

java - scala 中 REPL 变量的后增量和预增量未按预期工作