Based on the OO Foundation, begin to learn design patterns carefully! Design patterns are essential in Java design!
Apple.java
Package strategy;
/**
* *
@author Andy * */Public
class Apple implements discountable {
//weight
private double Weight;
The exact calculation of design money in the actual development of unit Price is bigdecimal;
private double price;
Discount on purchase
//private Discountor D = new Appleweightdiscountor ();
At the purchase price discount
private Discountor d = new Applepricediscountor ();
Public double Getweight () {return
weight;
}
public void Setweight (double weight) {
this.weight = weight;
}
Public double GetPrice () {return price
;
}
public void Setprice (double price) {
This.price = Price;
}
Public Apple (double weight,double price) {
super ();
This.weight=weight;
This.price=price;
}
@Override public
void Discountsell () {
d.discount (this);
}
}
Banana.java
package strategy/** * * @author Andy */public class Banana implements
Able {//weight private double weight;////Unit price actual development involves money and other accurate calculations are used bigdecimal private double prices;
Public Banana (double weight, double) {super ();
This.weight = weight;
This.price = Price;
Public double Getweight () {return weight;
The public void Setweight (double weight) {this.weight = weight;
Public double GetPrice () {return price;
public void Setprice (double price) {this.price = Price; @Override public void Discountsell () {//Discount algorithm if (weight < 5) {System.out.println ("Banana not Discounted price:")
+ weight * price);
}else if (weight >= 5 && Weight <) {System.out.println ("banana dozen 88 percent prices:" + weight * price * 0.88);
}else if (weight >=) {System.out.println ("banana call 50 percent Price:" + weight * prices * 0.5); }
}
}
Market.java
Package strategy;
/**
* * @author Andy * * */public
class Market {
/**
* Discounts on a category of things to be discounted
* @param apple
*/public
static void Discountsell (Discountable d) {
D.discountsell ();
}
}
Discountable.java
Package strategy;
/**
* *
@author Andy
* *
/Public
interface discountable {public
void Discountsell ();
}
Test.java
Package strategy;
/** * * @author Andy * */public
class Test {
/**
* *
@param args
* * * Public
static void Main (string[] args) {
// only discounts on apples you can't discount on one of the common things and sell what you want to do. Discount algorithm
/ In fact, each kind of things discount algorithm is inconsistent
discountable d = new Apple (10.3, 3.6);
Discountable d1= New Banana (5.4,1.1);
Market.discountsell (d);
Market.discountsell (D1);
}