📄 alliance.java
字号:
package tomato;
import java.util.*;
final public class Alliance extends Purchaser{
private Vector d_member = new Vector();
protected static Vector d_wantjoinmember = new Vector();
protected int[][] d_map = new int[MapBound.GetBound()][MapBound.GetBound()];
//private int[] d_mapEverysitePersonNumber = new int[MapBound.GetBound()*MapBound.GetBound()]
private int d_memberCount = 0;
private double d_minfreight;
protected static int incrementNumber;
public Alliance(Location location,Book book,int requirement){
super(location,book,requirement);
for(int i = 0; i < MapBound.GetBound(); i++){
for(int j = 0; j < MapBound.GetBound(); j++){
d_map[i][j] = 0;
}
}
}
//操作向量
public void Add( Purchaser purchaser ){
d_memberCount++;
d_member.addElement( purchaser );
d_map[purchaser.GetLocation().GetLongitude()][purchaser.GetLocation().GetLatitude()] = purchaser.GetRequirement(); //purchaser.GetRequirement()
}
public void Remove( Purchaser purchaser ){
d_memberCount--;
GetMember().removeElement(purchaser);
d_map[purchaser.GetLocation().GetLongitude()][purchaser.GetLocation().GetLatitude()] = 0; //d_map[purchaser.GetLocation().GetLongitude()][purchaser.GetLocation().GetLatitude()] = 0;
}
//访问
private Vector GetMember(){
return d_member;
}
public int GetRequirement( ){
d_requirement = 0; //每次计算前重新置0!
for(int index = 0; index < this.GetMember().size(); index++){
Purchaser p = (Purchaser)this.GetMember().elementAt(index);
this.d_requirement = this.d_requirement + p.GetRequirement();
}
return this.d_requirement;
}
//显示
private void DrawMap(Bookstore bookstore ){
System.out.println("The Alliance's map is:");
for(int i = 0; i < MapBound.GetBound()+2; i++){
System.out.print("--");
}
System.out.println();
for(int i = 0; i < MapBound.GetBound(); i++){
System.out.print("| ");
for(int j = 0; j < MapBound.GetBound(); j++){
if( i == bookstore.GetLocation().GetLongitude() && j == bookstore.GetLocation().GetLatitude()){
System.out.print("BS");
}
else if(i == GetLocation().GetLongitude() && j == GetLocation().GetLatitude()){
System.out.print(d_map[i][j]);
System.out.print("C");
}
else if(bookstore.GetLocation().GetLongitude() == GetLocation().GetLongitude() && bookstore.GetLocation().GetLatitude() == GetLocation().GetLatitude()){
System.out.print("BC");
}
else if(d_map[i][j] != 0){
System.out.print(d_map[i][j]);
System.out.print(" ");
}
else
System.out.print(" ");
}
System.out.println(" |");
}
for(int i = 0; i < MapBound.GetBound()+2; i++){
System.out.print("--");
}
System.out.println();
}
private void ShowEachPurchaser(){
System.out.println();
System.out.println("The Alliance cotains:");
for(int index = 0; index < this.GetMember().size(); index++){
Purchaser p = (Purchaser)this.GetMember().elementAt(index);
System.out.println("The No."+(index+1)+" Purchaser ");
p.Display();
}
}
private void ShowLucre(){
System.out.println("The Purchaser's iterater lucre and total lucre are:");
System.out.println("The Purchaser Iterater lucre Total lucre");
System.out.println();
for(int index = 0; index < GetMember().size(); index++){
Purchaser p = (Purchaser)GetMember().elementAt(index);
System.out.println("The No."+(index+1)+'\t'+(p.d_totalCharge_privousfinal-p.d_totalCharge_recently)+'\t'+'\t'+(p.d_totalCharge_byself-p.d_totalCharge_recently));
}
System.out.println();
}
public void Display(Bookstore bookstore ){
ShowEachPurchaser();
DrawMap(bookstore);
ShowLucre();
System.out.println("The total Person are: "+d_memberCount);
System.out.println("The total requriement is: "+GetRequirement());
System.out.println("The Purchaser's Freightbyperbook is "+Freightbyperbook());
System.out.println("The Alliance's payment is "+GetPayment());
System.out.println("The Alliance's Total charge is "+d_totalCharge_recently);
}
public void Display(Bookstore bookstore ,double perKmFreight){
//ShowEachPurchaser();
DrawMap(bookstore);
//ShowLucre();
System.out.println("The total Person are: "+d_memberCount);
System.out.println("The total requriement is: "+GetRequirement());
System.out.println("The Purchaser's Freightbyperbook is "+Freightbyperbook());
System.out.println("The Freightbyperbook's Distance is "+Freightbyperbook() / perKmFreight);
System.out.println("The Alliance's payment is "+GetPayment());
System.out.println("The Alliance's Total charge is "+d_totalCharge_recently);
}
//计算
private double[] CalculateFreightbyCenterEveryone(Bookstore bookstore,double perKmFreight,double[] freightArray){
for(int index_o = 0; index_o < GetMember().size(); index_o++){
freightArray[index_o] = 0;
Purchaser p = (Purchaser)GetMember().elementAt(index_o);
for(int index_i = 0; index_i < GetMember().size(); index_i++){
Purchaser q = (Purchaser)GetMember().elementAt(index_i);
freightArray[index_o] += p.GetLocation().CalculateDistance(q.GetLocation())*q.GetRequirement()*perKmFreight;
}
freightArray[index_o] += p.GetLocation().CalculateDistance(bookstore.GetLocation())*GetRequirement()*perKmFreight;
}
return freightArray;
}
private double[] CalculateMinFreight(double[] freightArray){
d_minfreight = freightArray[0];
for(int index = 0; index < GetMember().size()-1; index++){
if ( d_minfreight > freightArray[index+1] )
d_minfreight = freightArray[index+1];
}
return freightArray;
}
private void SelectCenter(double[] freightArray){
for(int index = 0; index < GetMember().size(); index++){
if ( d_minfreight == freightArray[index] ){
Purchaser p = (Purchaser)GetMember().elementAt(index);
GetLocation().SetLocation(p.GetLocation());
}
}
}
public void EstablishAlliance(Bookstore bookstore,double perKmFreight){
double[] freightArray = new double[GetMember().size()];
CalculateFreightbyCenterEveryone(bookstore,perKmFreight,freightArray); // 假设分别以每个购买者为联盟中心,计算相应联盟的总运费
CalculateMinFreight(freightArray);
SelectCenter(freightArray);
CalculateCharge(bookstore,perKmFreight);
}
public void CalculateCharge(Bookstore bookstore,double perKmFreight ){
d_totalCharge_recently = GetPayment() + d_minfreight;
}
public double Freightbyperbook(){
return d_minfreight / GetRequirement();
}
public double FreightbySinglePerson(Purchaser purchaser){
return Freightbyperbook() * purchaser.GetRequirement();
}
public double PaymentbySinglePerson(Purchaser purchaser){
return GetPayment() / GetRequirement() * purchaser.GetRequirement();
}
public boolean IsAllianceReasonable(Bookstore bookstore,double perKmFreight){
System.out.println();
for(int index = 0; index < GetMember().size(); index++){
Purchaser p = (Purchaser)GetMember().elementAt(index);
if (p.d_totalCharge_recently == 0) //Because Method JoinAlliance() will use d_totalCharge_recently,
//Make sure each purchaser's d_totalCharge_recently have been calculated.
p.CalculateCharge(bookstore,perKmFreight);
if(!p.JoinAlliance(this)){
return false;
}
}
return true;
}
//设置环境
public static void SetMapBound(){
System.out.print("1.Please Inupt MapBound: ");
int bound = SavitchIn.readInt();
MapBound.SetBound(bound);
System.out.println("The mapbound is:"+MapBound.GetBound());
System.out.println();
}
public static void SetRandomPurchasers(Book book,int maxper,int count,Purchaser[] pArr){ //人机交互,询问你希望随机产生购买者的个数
System.out.print("5.Please input how many purchaser your want to input: ");
incrementNumber = SavitchIn.readInt();
for (int i = 0; i < incrementNumber; i++){
int req = (int)(Math.random()*maxper+1);
int lon = (int)(Math.random()*MapBound.GetBound());
int lat = (int)(Math.random()*MapBound.GetBound());
System.out.print(req+" ");
System.out.print("("+lon+",");
System.out.print(lat+")");
System.out.println();
pArr[count+i] = new SinglePerson(new Location(lon,lat),book,req);
}
}
//测试
public static void RandomTest(Bookstore bookstore,Book book,int maxper,double perKmFreight){
Purchaser[] pArr = new Purchaser[10000];
Location Alliancelocation = new Location(0,0);
Alliance cTest = new Alliance(Alliancelocation,book,0);
int count = 0;
boolean isContinue = true;
while (isContinue){
SetRandomPurchasers(book,maxper,count,pArr);
for (int i = 0; i < incrementNumber; i++){
cTest.Add(pArr[count+i]);
}
cTest.EstablishAlliance(bookstore,perKmFreight);
System.out.println();
if ( cTest.IsAllianceReasonable(bookstore,perKmFreight) ){
count += incrementNumber;
System.out.println();
System.out.println("The Alliance is reasonable!");
cTest.Display(bookstore);
}
else {
System.out.println("The Alliance is NOT reasonable!");
for (int i = 0; i < incrementNumber; i++){
cTest.Remove(pArr[count+i]);
}
System.out.println("The join is failed!");
}
System.out.println();
System.out.print("Do you want continue add purchasers? (Y/N) ");
char con ;
con = SavitchIn.readChar();
if (con == 'Y')
isContinue = true;
else
isContinue = false;
}
}
public static void Test(){ //人际交互式
SetMapBound();
SetBookstore();
RandomTest(d_bookstore,d_bookstore.SelectBook(SettargetBook()),Setmaxper(),d_bookstore.SetperKmFreight());
}
public static void main(String[]args){
Test();
//TestAuto();
//TestAutoonebyone();
//TestAutoonebyoneWithsortqueue();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -