王二是个卖烤鸭,请使用面向对象的分析和设计方法编写一个程序,统计王二共卖了多少烤鸭和赚了多少钱。
package com.chinasofti.java10.work;
public class RoastDuck {
private String name;
//价格
private int price;
//数量
private int stock;
//成本
private int cost;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}
//卖烤鸭的人
package com.chinasofti.java10.work;
public class Person {
private String name;
//销售总额
private int totalSalesMoney;
//销售总量
private int totalSalesCount;
//销售盈利
private int profit;
public boolean sell(RoastDuck roastDuck,int count){
if(roastDuck.getStock()<count){
return false;
}
int beforeStock=roastDuck.getStock();
int afterStock=beforeStock-count;
roastDuck.setStock(afterStock);
totalSalesMoney=totalSalesMoney+(roastDuck.getPrice()*count);
totalSalesCount=totalSalesCount+count;
//利润
profit=totalSalesMoney-(roastDuck.getCost()*count);
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotalSalesMoney() {
return totalSalesMoney;
}
public void setTotalSalesMoney(int totalSalesMoney) {
this.totalSalesMoney = totalSalesMoney;
}
public int getTotalSalesCount() {
return totalSalesCount;
}
public void setTotalSalesCount(int totalSalesCount) {
this.totalSalesCount = totalSalesCount;
}
public int getProfit() {
return profit;
}
public void setProfit(int profit) {
this.profit = profit;
}
}
package com.chinasofti.java10.work;
public class Run {
public static void main(String[] args) {
Person person=new Person();
person.setName("王二");
RoastDuck roastDuck=new RoastDuck();
roastDuck.setName("烤鸭");
//每只烤鸭的销售价
roastDuck.setPrice(100);
roastDuck.setStock(6);
//成本价每只20元
roastDuck.setCost(20);
person.sell(roastDuck, 2);
System.out.println("在这次活动中["+person.getName()+"]一共卖了["+person.getTotalSalesCount()+"]只烤鸭"+
"共计["+person.getTotalSalesMoney()+"]元,那么今天去除成本["+(roastDuck.getCost()*person.getTotalSalesCount())+"]就赚了["+person.getProfit()+"]元");
}
}
销售系统
最新推荐文章于 2022-08-26 14:58:06 发布