嗨,我是编程新手,正在努力弄清楚如何增加商店中各种商品的利润。我有一个主要方法,其中商店中的商品数组已初始化。我在特定项目类别中有一个单独的方法来计算利润,但是不确定如何为相同的特定项目类别计算总利润,因此我试图获取所有食品的总利润和所有文具的总利润。项目,如果那一切都说得通的话。我希望有一种解决此问题的基本方法,因为我是编程新手。对于经验丰富的程序员来说,这可能真的很简单,但是我正在努力使自己摆脱困境。
任何指示或帮助将不胜感激?
非常感谢
包装车间;
公共类商店{
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();
}
}
}
}
最佳答案
好吧,整个编辑我的答案。
首先,我建议您对继承进行更深入的研究,因为您要做的事情确实很尴尬,将来如果您要正确构建继承关系,代价会很高。
我还发布了必要的代码,因为否则它将太大。
我希望您进一步探讨一些主题:
遗产
编码原则
命名
单一责任原则
您伤害了其中的每一个,这就是为什么您的代码难以维护的原因。您可以说您以后会做,或者以后做任何事,但是以后会很痛苦。
我的意思是:
命名我已经告诉过您:例如,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);