跳到內容

A-level 計算機/AQA/試卷 1/骨架程式/2020

來自華夏公益教科書,開放的書籍,開放的世界

這是針對 AQA A Level 計算機科學規範的。

這是可以提出關於一些題目內容和解決方案的建議的地方。

請尊重他人,不要破壞頁面,因為這會影響學生備考!- 馬丁·珀迪克
請勿在本頁討論問題。請使用討論頁面:即將推出!

AQA Skeleton Program 2020 - Class Diagram
AQA 骨架程式 2020 - 類圖

C 部分預測

[編輯 | 編輯原始碼]
  • 2020 年試卷 1 將包含 C 部分的 1 道題:兩部分,分別價值 1 分和 2 分。
  • 在定義 LargeSettlement 類時,super() 的作用是什麼?- **與所有語言無關 - 問題可以重新措辭使其更現實嗎?(另一個選項可能是:“解釋 Settlement 如何在程式中用作超類”)**
  • 在模擬類建構函式中,Ben Thor Cuisine 預設公司定義之後,應該新增一行程式碼以使其與 ModifyCompany() 函式保持一致。
  • 每個類之間存在什麼關係(組合、聚合和繼承)
  • 每個事件發生的機率分別為(i)多少? (ii) 所有事件同時發生的機率是多少?

D 部分預測

[編輯 | 編輯原始碼]

關於骨架程式的程式設計問題

  • 2020 年試卷 1 將包含 4 道題:6 分題、8 分題、11 分題和 12 分題 - 這些分數包括截圖,因此編碼的實際分數可能低 1-2 分。
  • 2019 年試卷 1 包含 4 道題:5 分題、8 分題、9 分題和 13 分題 - 這些分數包括截圖,因此編碼的實際分數可能低 1-2 分。
  • 2018 年試卷 1 包含一道 2 分題、一道 5 分題、兩道 9 分題和一道 12 分題 - 這些分數包括截圖。
  • 2017 年試卷 1 包含一道 5 分題、三道 6 分題和一道 12 分題。

目前的問題是本頁面貢獻者推測的。答案正在維護中,對此造成的不便我們深感抱歉。

  • 確保所有成本都以以下格式顯示 £X.XX(4 分)
  • 新增一個新的 Small Settlement 類和一個在模擬啟動選單中建立 Small Settlement 的選項(6 分)
  • 新增“關閉門店”事件,該事件會導致門店從模擬中移除(11 分)
  • ExpandOutlet() 添加當容量變為零時的選項
  • AlterCapacity(Change) 正確計算新容量限制為最大容量時的每日成本
  • AddCompany(self) 新增檢查以檢視 CompanyName 是否已存在於 _Companies 中
  • RemoveCompany() 新增一個選項,用於移除公司,而不是分別移除其每個門店(這很可能出現,選單中沒有選項 5,因此這似乎很合乎邏輯)
  • 新增一個更快的選項來載入預設場景
  • 關閉指定門店,持續一定天數
  • 合併兩家公司
  • 提前指定天數
  • 顯示已過的天數
  • 顯示公司淨收益/虧損
  • 顯示每個門店的淨周收益/虧損以及平均周收益/虧損
  • 確保燃料成本永遠不會變為負數
  • 透過輸入的值(或預定義的值)增加或減少公司的聲譽
  • 修改 CalculateDeliveryCost() 函式,以便使用最短路徑遍歷所有門店,從而最小化配送成本(使用廣度優先圖遍歷)
  • 新增建立新的定居點並將門店設定在那裡的功能
  • 對程式執行選單的初始條目進行驗證
  • 對門店、家庭等的座標進行驗證,因為兩個座標可能會隨機地重疊在 x 和 y 座標上
  • 使用 Dijkstra 演算法找到餐廳和配送地點之間的最短路徑。然後,計算總配送成本。
  • 在每天結束時,如果任何公司的餘額超過 £20,000,則在隨機位置開設另一家門店,並從其餘額中減去 £20,000。
  • 只允許在新的位置(即還沒有該型別公司門店的位置)建立新門店
  • 在每天結束時,如果一個家庭外出就餐,則外出就餐的機率應該更改為另一個隨機數。但是,如果一個家庭沒有外出就餐,則外出就餐的機率應該增加(例如,增加 0.1)(4 分)
  • 建立一個公司或門店進行促銷的隨機例項,在此期間,其費用會增加,但其聲譽評分也會上升
  • 門店關閉時減少公司的餘額
  • 新增 foodTruck 類,該類繼承自 outlet 類,並具有 move() 子例程
  • 新增對任何使用者輸入的驗證,以確保輸入了內容
  • 根據型別新增驗證,以確保需要輸入數字的輸入是數字
  • 建立一個額外的餐廳類別,並將一個新的該型別公司作為預設公司的一部分建立
  • 關閉連續 5 天虧損的門店,並在事件中新增一條通知,說明發生了這種情況
  • 顯示餐廳的詳細資訊,該餐廳要麼是利潤最高的餐廳,要麼是訪問量最大的餐廳,要麼是聲譽評分最高的餐廳
  • 生成一個隨機預算並將其儲存在每個家庭物件內的屬性中;外出就餐的家庭只能在價格在預算範圍內的公司就餐
  • 將天氣納入模擬;它可以隨機下雨,在這種情況下,外出就餐的機率都會減半,並且事件中會顯示下雨情況
  • 新增一個功能,允許送餐,而不是顧客去門店就餐;選擇不外出就餐的家庭可能會根據隨機機會訂餐送貨,但這會讓公司根據送貨距離額外支付燃料費
  • 廚師門店通常會盈利,而其他類別門店會虧損;編寫一個子例程,以確定每種型別公司在前一天應該收取多少餐費才能盈利

計算兩個門店之間的配送成本

[編輯 | 編輯原始碼]

計算配送成本,其中使用者提供公司和兩個門店以計算配送成本

Java

用於顯示選單

public void displayMenu() {
        Console.writeLine(System.lineSeparator() + "*********************************");
        Console.writeLine("**********    MENU     **********");
        Console.writeLine("*********************************");
        Console.writeLine("1. Display details of households");
        Console.writeLine("2. Display details of companies");
        Console.writeLine("3. Modify company");
        Console.writeLine("4. Add new company");
        Console.writeLine("5. remove company");
        Console.writeLine("6. Advance to next day");
        Console.writeLine("7. Remove company");
        Console.writeLine("8. Merge company");
        Console.writeLine("9. calculate delivery costs");
        Console.writeLine("Q. Quit");
        Console.write(System.lineSeparator() + "Enter your choice: ");
    }

用於執行方法

public void run() {
        String choice = "";
        int index;
        while (!choice.equals("Q")) {
            displayMenu();
            choice = Console.readLine();
            if(choice.isEmpty()){
                Console.println("You have not entered an option. Check menu again. ");
                continue;
            }
            switch (choice) {
         
                case "1":
                    simulationSettlement.displayHouseholds();
                    break;
                case "2":
                    displayCompanies();
                    break;
                case "3":
                    for(int i = 0; i <companies.size(); i++){
                        System.out.println((i+1)+". "+companies.get(i).getName());
                    }
                    int companyindex;
                    do {
                        Console.write("Enter company index: ");
                        companyindex = Integer.parseInt(Console.readLine());
                        
                    } while (companyindex == -1);
                    
                    modifyCompany(companyindex-1);
                    break;
                case "4":
                    addCompany();
                    break;
                case "5":
                    removeCompany();
                    break;
                case "6":
                    processDayEnd();
                    break;
                case "7":
                    removeCompany();
                    break;
                case "8":
                    mergeCompany();
                    break;
                case "9":
                    Console.print("Which company: ");
                    String company = Console.readLine();
                    shortestDistance(company);
                    break;
                case "Q":
                    Console.writeLine("Simulation finished, press Enter to close.");
                    Console.readLine();
                    break;
            }
             
            
        }
    }

用於計算距離

public void shortestDistance(String Company){
        for(int i = 0; i<companies.size(); i++){
            if(companies.get(i).getName().equals(Company)){
                
                Console.println(companies.get(i).getDetails());
                Console.println("");
                Console.print("Enter number of first outlet: ");
                int firstOutlet = Integer.parseInt(Console.readLine());
                Console.print("Enter number of second outlet: ");
                int secondOutlet = Integer.parseInt(Console.readLine());
                int x1 = companies.get(i).outlets.get(firstOutlet-1).getX();
                int y1 =companies.get(i).outlets.get(firstOutlet-1).getY();
                int x2 = companies.get(i).outlets.get(secondOutlet-1).getX();
                int y2 = companies.get(i).outlets.get(secondOutlet-1).getY();
                long distance = Math.round(Math.pow(Math.pow((x2-x1), 2)+Math.pow((y2-y1), 2), 0.5) );
                
                
                    
                    float cost = distance * baseCostForDelivery;
                    System.out.println("The total delivery cost is "+cost+" and the total distance to be travelled is "+distance);
                    
                
            }
        }
    }


VB.NET


C#


Delphi/Pascal


Python


使用者輸入驗證,確保使用者選項非空

[編輯 | 編輯原始碼]

檢查使用者選項是否非空

Java

在 run() 方法中包含以下程式碼

public void run() {
        String choice = "";
        int index;
        while (!choice.equals("Q")) {
            displayMenu();
            choice = Console.readLine();
            if(choice.isEmpty()){
                Console.println("You have not entered an option. Check menu again. ");
                continue;
            }
            switch (choice) {
         
                case "1":
                    simulationSettlement.displayHouseholds();
                    break;
                case "2":
                    displayCompanies();
                    break;
                case "3":
                    for(int i = 0; i <companies.size(); i++){
                        System.out.println((i+1)+". "+companies.get(i).getName());
                    }
                    int companyindex;
                    do {
                        Console.write("Enter company index: ");
                        companyindex = Integer.parseInt(Console.readLine());
                        
                    } while (companyindex == -1);
                    
                    modifyCompany(companyindex-1);
                    break;
                case "4":
                    addCompany();
                    break;
                case "5":
                    removeCompany();
                    break;
                case "6":
                    processDayEnd();
                    break;
                case "7":
                    removeCompany();
                    break;
                case "8":
                    mergeCompany();
                case "Q":
                    Console.writeLine("Simulation finished, press Enter to close.");
                    Console.readLine();
                    break;
            }
             
            
        }
    }


VB.NET


C#


Delphi/Pascal


Python


公司推廣

[編輯 | 編輯原始碼]

建立一個公司或門店進行促銷的隨機例項,在此期間,其費用會增加,但其聲譽評分也會上升

Java

用於推廣方法

public void promotion(){
        int outletOrCompany = rnd.nextInt(((1-0)+1)+0);
        if(outletOrCompany == 0){
            int compmax = companies.size()-1;
            int compmin = 0;
            int companyIndex = rnd.nextInt(((compmax-compmin)+1)+compmin);
            companies.get(companyIndex).dailyCosts += 1000;
            companies.get(companyIndex).reputationScore += 3;
            System.out.println("company "+companies.get(companyIndex).getName()+" is promoted");
        } else{
            System.out.print("Enter company to include promotion ");
            String company = Console.readLine();
            for(int i = 0; i<companies.size(); i++){
                if(companies.get(i).getName().equals(company)){
                    int outletmax = companies.get(i).outlets.size()-1;
                    int outletmin = 0;
                    int outletNo = rnd.nextInt(((outletmax-outletmin)+1)+outletmin);
                    companies.get(i).outlets.get(outletNo).dailyCosts += 1000;
                    companies.get(i).reputationScore += 3;
                    System.out.println("outlet "+outletNo+" is promoted");
                }
            }
            
        }
        
    }

用於 processdayEnd 方法

private void displayEventsAtDayEnd() {
        Console.writeLine(System.lineSeparator() + "***********************");
        Console.writeLine("*****   Events:   *****");
        Console.writeLine("***********************" + System.lineSeparator());
        float eventRanNo;
        eventRanNo = rnd.nextFloat();
        if (eventRanNo < 0.25f) {
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.25f) {
                processAddHouseholdsEvent();
            }
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.9f) {
                promotion();
            }
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.5f) {
                processCostOfFuelChangeEvent();
            }
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.5f) {
                processReputationChangeEvent();
            }
            eventRanNo = rnd.nextFloat();
            if (eventRanNo >= 0.5f) {
                processCostChangeEvent();
            }
        } else {
            Console.writeLine("No events.");
        }
    }


VB.NET


C#


Delphi/Pascal


Python


使用 Dijkstra 演算法計算配送成本。(我認為這可能是 12 分的題目)

[編輯 | 編輯原始碼]

迪傑斯特拉演算法只能用於查詢兩個節點之間的最短路徑。為了最佳化送貨成本,我們需要計算透過所有節點的最短路徑。迪傑斯特拉演算法不適用於此。
找到從第一個店面到所有其他店面的最短路線。

Java


VB.NET


C#


Delphi/Pascal

這是我在Delphi中編寫的一個通用迪傑斯特拉演算法。它需要針對Food Magnate程式進行調整。

宣告一個節點類。

TNode = class
  public
    NodeIdentity, Original, Weight : integer;
    Visited : boolean;
    constructor Create(myNodeIdentity, myOriginal, myWeight : integer);
end;

const Area = Settlement.XSize * Settlement.YSize;
var HorizontalHeap, VerticalHeap : array of integer;

構建類

constructor TNode.Create(myNodeIdentity: Integer; myOriginal: Integer; myWeight: Integer);
begin
  NodeIdentity := MyNodeIdentity;
  Original := myOriginal;
  Weight := myWeight;
end;

從列表中獲取下一個節點

function GetNodeFromIdentity(NodeIdentity : integer; ListOfNodes : TList) : TNode; overload;
var
  I : integer;
  Node : TNode;
begin
  result := nil;
  for I := 0 to (ListOfNodes.Count - 1) do
    begin
      Node := ListOfNodes[I];
      if Node.NodeIdentity = NodeIdentity then
        begin
          result := Node;
          break;
        end;
    end;
end;

找到在兩個節點之間遍歷的最小權重

function GetNodeOfMiniWeight(ListOfNodes: TList) : TNode; overload;
var
  I, minimum : Integer;
  Node : TNode;
begin
  result := nil;
  minimum := maxint;
  for I := 0 to ListOfNodes.Count - 1 do
    begin
      Node := ListOfNodes[I];
      if Node.Visited then continue;
      if Node.Weight < minimum then
        begin
          result := Node;
          Minimum := Node.Weight;
        end;
    end;
end;

找到起點(餐廳)和終點(房屋)之間的最短路徑。

procedure ShortestPath(origin, destination : integer);
var
  NewWeight : integer;
  ListOfNodes : TList;
  CurrentNode, NextNode : TNode;
  ShortestPath : string;
begin
  ListOfNodes := TList.Create;
  CurrentNode := TNode.Create(origin, MaxInt, 0);
  ListOfNodes.Add(CurrentNode);
  while not (CurrentNode.NodeIdentity = destination) do
    begin
      if (CurrentNode.NodeIdentity > Area) and not (CurrentNode.Original - CurrentNode.NodeIdentity = Area) then
        begin
          NewWeight := CurrentNode.Weight + HorizontalHeap[CurrentNode.NodeIdentity - Area];
          NextNode := GetNodeFromIdentity(CurrentNode.NodeIdentity - Area, ListOfNodes);
          if NextNode <> Nil then
            begin
              NextNode.Original := CurrentNode.NodeIdentity;
              NextNode.Weight := NewWeight;
            end;
        end
      else
        begin
          NextNode := TNode.Create(CurrentNode.NodeIdentity - Area, CurrentNode.NodeIdentity, NewWeight);
          ListOfNodes.Add(NextNode);
        end;
    end;

    if (CurrentNode.NodeIdentity < Area*Area-Area) and not (CurrentNode.Original - CurrentNode.NodeIdentity = Area) then
      begin
        NewWeight := CurrentNode.Weight + HorizontalHeap[CurrentNode.NodeIdentity];
        NextNode := GetNodeFromIdentity(CurrentNode.NodeIdentity + Area, ListOfNodes);
        if NextNode <> nil then
          begin
            if NextNode.Weight > NewWeight then
              begin
                NextNode := TNode.Create(CurrentNode.NodeIdentity + Area, CurrentNode.NodeIdentity, NewWeight);
                ListOfNodes.Add(NextNode);
              end;
          end;
      end;

    if not (CurrentNode.NodeIdentity mod Area = Area - 1) and not (CurrentNode.Original - CurrentNode.NodeIdentity - 1) then
      begin
        NewWeight := CurrentNode.Weight + VerticalHeap[CurrentNode.NodeIdentity];
        NextNode := GetNodeFromIdentity(CurrentNode.NodeIdentity + 1, ListOfNodes);
        if NextNode <> nil then
          begin
            if NextNode.Weight > NewWeight then
              begin
                NextNode := TNode.Create(CurrentNode.NodeIdentity + 1, CurrentNode.NodeIdentity, NewWeight);
                ListOfNodes.Add(NextNode);
              end;
          end;
      end;

    if not (CurrentNode.NodeIdentity mod Area = 0) and not (CurrentNode.Original - CurrentNode.NodeIdentity = -1) then
      begin
        NewWeight := CurrentNode.Weight + VerticalHeap[CurrentNode.NodeIdentity - 1];
        NextNode := GetNodeFromIdentity(CurrentNode.NodeIdentity - 1, ListOfNodes);
        if NextNode <> Nil then
          begin
            if NextNode.Weight > NewWeight then
              begin
                NextNode.Original := CurrentNode.NodeIdentity;
                NextNode.Weight := NewWeight;
              end;
          end
        else
          begin
            NextNode := TNode.Create(CurrentNode.NodeIdentity - 1, CurrentNode.NodeIdentity, NewWeight);
            ListOfNodes.Add(NextNode);
          end;
      CurrentNode.Visited := true;
      CurrentNode := GetNodeOfMiniWeight(ListOfNodes);
      end;

    ShortestPath := 'The shortest path from ' + inttostr(destination) + ' to ' + inttostr(Origin) + ' is ';
    while (CurrentNode.NodeIdentity <> origin) do
      begin
        ShortestPath := ShortestPath + inttostr(CurrentNode.NodeIdentity) + ', ';
        CurrentNode := GetNodeFromIdentity(CurrentNode.Original, ListOfNodes);
      end;
end;

該演算法由Chris | Peter Symonds College提供


Python

另一個需要改編到Skeleton Program中的通用迪傑斯特拉演算法。
依賴關係:Matplotlib、NetworkX。(這隻用於顯示加權圖的圖形版本)。
Datetime是python3內建庫,因此無需安裝。

# Imports Below
import datetime
import networkx as nx
import matplotlib.pyplot as plt

# Variable Declarations Below
adjacencyList = list()
Graph = nx.Graph()

# Function Declarations Below
def initiateAdjacencyList(length):
    for i in range(length):
        adjacencyList.append([])

def addEdge(source, destination, weight): # Function for adding edges to a priority queue
    adjacencyList[source].append([destination, weight])
    adjacencyList[destination].append([source, weight])
    Graph.add_edge(source, destination, weight = weight)

def dijkstra(adjacencyList, source, destination): # Dijkstras algorithm function 
    parents = list()
    distances = list()
    tempQueue = list()
    startWeight = float('inf') # Initiates a float with the largest possible value
    shortestPath = list()
    vertexCount = len(adjacencyList) # Initiates the Vertex count as the number of edges on the graph

    for i in range(vertexCount):
        weight = startWeight
        if source == i:
            weight = 0
        distances.append(weight)
        parents.append(None)

    tempQueue.append((0, source))

    while tempQueue: # While the Queue has items in it
        v_tuple = tempQueue.pop()
        v = v_tuple[1]

        for Edge in adjacencyList[v]: # Loops through every edge in the List
            candidate_distance = distances[v] + Edge[1]
            if distances[Edge[0]] > candidate_distance:
                distances[Edge[0]] = candidate_distance
                parents[Edge[0]] = v
                tempQueue.append([distances[Edge[0]], Edge[0]])

    end = destination
    while end is not None:
        shortestPath.append(end)
        end = parents[end]

    shortestPath.reverse() # Reverses the list to show the first point visited first

    return shortestPath, distances[destination]

# Main Program Below
initiateAdjacencyList(10) # Initiate Graph with 10 points maximum

# Add edges onto the graph (Just an arbitrary layout, can be edited for testing)
addEdge(0, 1, 2)
addEdge(0, 2, 3)
addEdge(1, 2, 2)
addEdge(1, 5, 4)
addEdge(2, 3, 2)
addEdge(2, 4, 8)
addEdge(3, 4, 5)
addEdge(4, 5, 1)
addEdge(5, 4, 6)
addEdge(5, 6, 4)
addEdge(6, 0, 9)
addEdge(7, 6, 11)

# Find the shortest path between two points
startTime = datetime.datetime.now()
print('Points Visited: {} \nTotal Distance: {}'.format(*dijkstra(adjacencyList, 0, 6))) # Prints the point dijkstras went through as-well as the path length

pos = nx.shell_layout(Graph) # Sets the layout for the graph nodes on the plot
labels = nx.get_edge_attributes(Graph,'weight') # Plots the nodes on the graph

nx.draw(Graph, pos, with_labels = True) # Draws the graph
nx.draw_networkx_edge_labels(Graph, pos, edge_labels=labels) # Draws the weights on the graph

plt.show() # Shows the plot

#endTime = datetime.datetime.now() # End time doesn't work with matplotlib
#print(f'runtime {endTime.second - startTime.second}.{endTime.microsecond - startTime.second}s')
  • 作者:Harry - Peter Symonds College


根據使用者的評分更改信譽

[edit | edit source]

Java

  • 更改信譽方法
private void changeReputation(){
        System.out.print("Which company: ");
        String compCR = Console.readLine();
        int indCR = getIndexOfCompany(compCR);
        System.out.print("How do you rate the company? - good or bad : ");
        String rate = Console.readLine();
        if(rate.equals("good")){
            companies.get(indCR).alterReputation(3);
        } else{
            companies.get(indCR).alterReputation(-3);
        }
    }
  • 新增到顯示選單
public void displayMenu() {
        Console.writeLine(System.lineSeparator() + "*********************************");
        Console.writeLine("**********    MENU     **********");
        Console.writeLine("*********************************");
        Console.writeLine("1. Display details of households");
        Console.writeLine("2. Display details of companies");
        Console.writeLine("3. Modify company");
        Console.writeLine("4. Add new company");
        Console.writeLine("6. Advance to next day");
        Console.writeLine("7. Change Reputation");
        Console.writeLine("Q. Quit");
        Console.write(System.lineSeparator() + "Enter your choice: ");
    }
  • 新增到執行
public void run() {
        String choice = "";
        int index;
        while (!choice.equals("Q")) {
            displayMenu();
            choice = Console.readLine();
            switch (choice) {
                case "1":
                    simulationSettlement.displayHouseholds();
                    break;
                case "2":
                    displayCompanies();
                    break;
                case "3":
                    String companyName;
                    do {
                        Console.write("Enter company name: ");
                        companyName = Console.readLine();
                        index = getIndexOfCompany(companyName);
                    } while (index == -1);
                    modifyCompany(index);
                    break;
                case "4":
                    addCompany();
                    break;
                case "6":
                    processDayEnd();
                    break;
                case "7":
                    changeReputation();
                    break;
                case "Q":
                    Console.writeLine("Simulation finished, press Enter to close.");
                    Console.readLine();
                    break;
            }
        }
    }


VB.NET


C#


Delphi/Pascal


對Simulation.Run()進行微調;只是新增對ChangeReputation過程的呼叫;

procedure Simulation.Run();
var
  CompanyName : String;
  Choice : Char;
  Index : Integer;
begin
  Choice := ' ';
  while Choice <> 'Q' do
    begin
      DisplayMenu();
      readln(Choice);
      case (Choice) of
        '1': Self.SimulationSettlement.DisplayHouseholds();
        '2': DisplayCompanies();
        '3':
          begin
            repeat
              write('Enter company name: ');
              readln(CompanyName);
              Index := GetIndexOfCompany(CompanyName);
            until Index <> - 1;
            ModifyCompany(Index);
          end;
        '4': AddCompany();
        '5': ChangeReputation();
        '6': ProcessDayEnd();
        'Q':
          begin
            writeln('Simulation finished, press Enter to close.');
            readln;
          end;
      end;
    end;
end;

更改選單(拼寫糾正 - Chris)

procedure Simulation.DisplayMenu();
begin
  writeln;
  writeln('*********************************');
  writeln('**********    MENU     **********');
  writeln('*********************************');
  writeln('1. Display details of households');
  writeln('2. Display details of companies');
  writeln('3. Modify company');
  writeln('4. Add new company');
  writeln('5. Change Reputation');
  writeln('6. Advance to next day');
  writeln('Q. Quit');
  writeln;
  write('Enter your choice: ');
end;

宣告過程。還要求在模擬類中宣告過程。

procedure Simulation.DisplayCompanyNames();
var I : Integer;
begin
  for I := low(Self.Companies) to high(Self.Companies) do
    writeln('[' + IntToStr(I) + '] ' + Self.Companies[I].GetName);
end;

procedure Simulation.ChangeReputation();
var
ValidCompanyNum : Boolean;
CompanyNumberInput : String;
CompanyNumber : Integer;
CompanyChange : Company;
ValidChangeInt : Boolean;
ReputationChangeInput : String;
ReputationChange : Integer;
begin
  ValidCompanyNum := False;
  writeln('What company would you like to alter the reputation of?');
  repeat
    try
      DisplayCompanyNames();
      write('Select company number: ');
      Readln(CompanyNumberInput);
      CompanyNumber := StrToInt(CompanyNumberInput);
      CompanyChange := Self.Companies[CompanyNumber];
      if( (CompanyNumber >= low(Self.Companies))  and (high(Self.Companies) >= CompanyNumber) ) then
      ValidCompanyNum := True
      else raise Exception.Create('Invalid Company Number');
      except
      writeln('Error: Not real company number');
    end;

  until (ValidCompanyNum);
  ValidChangeInt := False;
  repeat
    try
      write('What would you like to change the reputation by?: ');
      Readln(ReputationChangeInput);
      ReputationChange := StrToInt(ReputationChangeInput);
      CompanyChange.AlterReputation(ReputationChange);
      ValidChangeInt := True;
    except
      writeln('Invalid Input Change!');

    end;

  until (ValidChangeInt);
  writeln('Reputation Changed Press Any Key To Continue');
  readln;

end;

該解決方案由Alex Finch - Peter Symonds College提供


Python


停電事件

[edit | edit source]

Java

private void processPowerOutageEvent() {
        int numOfDays = rnd.nextInt(3) + 1;
        float amountToLose = rnd.nextFloat();
        Console.writeLine("There has been a powercut, which will last for " + numOfDays + " days, and each company has lost " + amountToLose);
        for (Company c : companies)
            c.balance -= amountToLose;
    }

    private void displayEventsAtDayEnd() {
        boolean hasEvent = false;
        Console.writeLine(System.lineSeparator() + "***********************");
        Console.writeLine("*****   Events:   *****");
        Console.writeLine("***********************" + System.lineSeparator());
        float eventRanNo;
        eventRanNo = rnd.nextFloat();
        if (!companies.isEmpty()) {
            if (eventRanNo < 0.25f) {
                eventRanNo = rnd.nextFloat();
                if (eventRanNo < 0.25f) {
                    processAddHouseholdsEvent();
                    hasEvent = true;
                }
                eventRanNo = rnd.nextFloat();
                if (eventRanNo < 0.5f) {
                    processCostOfFuelChangeEvent();
                    hasEvent = true;
                }
                eventRanNo = rnd.nextFloat();
                if (eventRanNo < 0.5f) {
                    processReputationChangeEvent();
                    hasEvent = true;
                }
                eventRanNo = rnd.nextFloat();
                if (eventRanNo >= 0.5f) {
                    processCostChangeEvent();
                    hasEvent = true;
                }
                eventRanNo = rnd.nextFloat();
                if (eventRanNo >= 0.25f) {
                    processPowerOutageEvent();
                    hasEvent = true;
                }
            }
            if (processGoBankruptEvent())
                hasEvent = true;
            if (!hasEvent) {
                Console.writeLine("No events.");
            }
        } else {
            Console.writeLine("No more companies. Simulation finished, press Enter to close.");
            Console.readLine();
            System.exit(0);
        }
    }
  • 作者:Lucy W


C#

這兩個都在模擬類中,一天結束時顯示事件是更新後的版本

private void ProcessPowerCutEvent() //  making an event for the company to loose power for a random amount of time
        {
            int count = 0;
            double numOfDays = Math.Round((rnd.NextDouble() * 10), 0);
            double amountToLose = rnd.NextDouble() *numOfDays * 1000;
            Console.WriteLine("There has been a powercut, which will last for " + numOfDays + " days, and each company has lost " + amountToLose);
            do
            {
                companies[count].ChangeBal(amountToLose);
                count++;
            } while (count < 3);
            dayCounter += (int)(numOfDays);
        }

        
private void DisplayEventsAtDayEnd()
        {
            Console.WriteLine("\n***********************");
            Console.WriteLine("*****   Events:   *****");
            Console.WriteLine("***********************\n");
            double eventRanNo;
            eventRanNo = rnd.NextDouble();
            if (eventRanNo < 0.25)
            {
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.25)
                {
                    ProcessAddHouseholdsEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessCostOfFuelChangeEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo < 0.5)
                {
                    ProcessReputationChangeEvent();
                }
                eventRanNo = rnd.NextDouble();
                if (eventRanNo >= 0.5)
                {
                    ProcessCostChangeEvent();
                }
                if (eventRanNo >= 0.5)
                {
                    ProcessPowerCutEvent();
                }
            }
            else
            {
                Console.WriteLine("No events.");
            }
        }

This needs to go in the company class


public void ChangeBal(double amounttoloose) // changing the balance for a power cut event
        {
            balance -= amounttoloose;

        }

*By Michael Tomkinson


Delphi/Pascal

此停電事件透過為整個“世界”建立一個停電來運作。停電期間,公司不會獲利/虧損。在一天結束時,所有公司都將獲得當天收入的補償。所有其他事件將照常進行。所有新的子過程都需要在類宣告中宣告。

// Two lines below changed in the Company.ProcessDayEnd subprocess. Puts the change in balance in a separate variable to be accessed later
Self.BalanceChange := ProfitLossFromOutlets - Self.DailyCosts - DeliveryCosts; // Calculates the change in balance that occurs for the day
Self.Balance := Self.Balance + Self.BalanceChange; // updates the current balance

procedure Simulation.ProcessPowerOutageEvent(); // Procedure added to handle the power outage event
var
  company : integer;
begin
  writeln('There has been a power cut and no companies have been running');

  for company := 0 to length(self.companies) - 1 do
  begin
    if companies[company].BalanceChange < 0 then
       writeln(companies[company].getName() + ' Prevented: ' + floatToStr(companies[company].BalanceChange) + ' worth of losses')
    else
       writeln(companies[company].getName() + ' Lost: ' + floatToStr(companies[company].BalanceChange) + ' worth of earnings');
    companies[company].Balance := companies[company].Balance - companies[company].BalanceChange
  end;
end;

procedure Simulation.DisplayEventsAtDayEnd(); // Procedure edited to add support for the new subprocess
var
  EventRanNo : Real;
begin
  inc(self.Day);
  writeln;
  writeln('***********************');
  writeln('*****   Events:   *****');
  writeln('***********************');
  writeln;
  EventRanNo := random();
  if EventRanNo < 0.25 then
    begin
      EventRanNo := random(1);
      if (EventRanNo < 0.25) then // Entry added for the chance to run power outage event, change the percentage to make the event run more/ less frequently
        ProcessPowerOutageEvent(); 
      if EventRanNo < 0.25 then
        ProcessAddHouseholdsEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessCostOfFuelChangeEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessReputationChangeEvent();
      EventRanNo := random();
      if EventRanNo >= 0.5 then
        ProcessCostChangeEvent();
    end
    else
      writeln('No events.');
    writeln('Day: ' + inttostr(self.Day));
end;
  • 作者:Harry - Peter Symonds College


VB.NET


隨機公司隨機時間(以小時為單位)的停電事件。

新增到模擬類

Public Sub ProcessPowerOutage()

        Randomize()
        Dim noOfDays As Integer = Int((Rnd() * 7) + 1)
        Dim moneyLost As Integer = Int((Rnd() * 1000) + 1)
        Dim totalLoss As Integer
        Dim CompanyNo As Integer = Int(Rnd() * Companies.Count)

        Console.WriteLine(Environment.NewLine & Companies(CompanyNo).GetName() & " is experiencing a powercut:")
        Console.WriteLine(Environment.NewLine & Companies(CompanyNo).GetName() & ". Balance: £" & Companies(CompanyNo).getBalance())

        For i = 1 To noOfDays
            Companies(CompanyNo).alterbalance(moneyLost)
            totalLoss += moneyLost
            Console.WriteLine(Environment.NewLine & "Hour: " & i)
            Console.WriteLine(Companies(CompanyNo).GetName() & " has lost £" & totalLoss & " in " & i & " hours.")
            Console.WriteLine(Companies(CompanyNo).GetName() & ". Balance: £" & Companies(CompanyNo).getBalance())
        Next

        Console.WriteLine(Environment.NewLine & Companies(CompanyNo).GetName() & " has lost £" & totalLoss & " in total." & Environment.NewLine)
    End Sub

編輯DisplayEventsAtDayEnd()子例程

Private Sub DisplayEventsAtDayEnd()
        Console.WriteLine(Environment.NewLine & "***********************")
        Console.WriteLine("*****   Events:   *****")
        Console.WriteLine("***********************" & Environment.NewLine)
        Dim EventRanNo As Single
        EventRanNo = Rnd()
        If EventRanNo < 0.25 Then
            EventRanNo = Rnd()
            If EventRanNo < 0.25 Then
                ProcessAddHouseholdsEvent()
            End If
            EventRanNo = Rnd()
            If EventRanNo < 0.5 Then
                ProcessCostOfFuelChangeEvent()
            End If
            EventRanNo = Rnd()
            If EventRanNo < 0.5 Then
                ProcessReputationChangeEvent()
            End If
            EventRanNo = Rnd()
            If EventRanNo >= 0.5 Then
                ProcessCostChangeEvent()
            End If
            EventRanNo = Rnd()
            If EventRanNo < 0.1 Then
                ProcessPowerOutage()
            End If
        Else
            Console.WriteLine("No events.")
        End If
    End Sub

在公司類中新增

Public Function GetBalance() As Single
        Return Balance
    End Function

Public Sub AlterBalance(ByVal Change As Single)
        Balance -= Change
    End Sub


'Hubert (Ousedale School) - 09/11/2019


世界末日事件

[edit | edit source]

Python

import sys

  def EndOfWorldEvent(self):
    RandNumber = random.random()
    if RandNumber < 0.1:
      RandNumber = random.random()
      if RandNumber < 0.1:
        print("An asteroid hit Earth! Bye Bye!!!")
        input()
        sys.exit()
      elif RandNumber > 0.1 and RandNumber < 0.3:
        print("Earth has died because of global warming! Bye Bye !!!")
        input()
        sys.exit()
      elif RandNumber > 0.3 and RandNumber < 0.5:
        print("The sun became a red giant! Bye Bye !!!")
        input()
        sys.exit()
      elif RandNumber > 0.5 and RandNumber < 0.7:
        print("A nuclear World War just finshed all life in Earth! Bye Bye !!!")
        input()
        sys.exit()


In the __DisplayEventsAtDayEnd method:

if EventRanNo < 0.1:
        self.EndOfWorldEvent()


Java

在模擬類中,建立一個方法processEndOfWorld()

private void processEndOfWorld() 
{
    Console.println("An asteroid has hit the earth. The world has ended.");
    System.exit(0);
}

在模擬類中的displayEventsAtDayEnd()方法中新增

if (eventRanNo <= 0.1f)
{
    processEndOfWorld();
}


Delphi/Pascal

每個機會的自定義世界末日訊息對於這個問題來說不是必要的(如果在考試中出現,這可能會有所不同),如果你時間不夠,你可能只選擇一個選項並省略隨機選擇。新程式也應該在類定義中宣告

procedure Simulation.ProcessEndOfWorldEvent(); // Procedure added to handle End of World events
var
  randomNumber : Float;
begin
       randomNumber := Random();

       if randomNumber < 0.1 then
       begin
          write('An asteroid hit Earth! Bye Bye!!!'); //End of World text courtesy of the author of the Python Solutions
          readln;
          Halt;
       end
       else if (randomNumber > 0.1) and (randomNumber < 0.3) then
       begin
          write('Earth has died because of Global Warming! Bye Bye!!!');
          readln;
          Halt;
       end
       else if (randomNumber > 0.3) and (randomNumber < 0.5) then
       begin
           write('The sun became a red giant! Bye Bye !!!');
           readln;
           Halt;
       end
       else if (randomNumber > 0.5) and (randomNumber < 0.7) then
       begin
         write('A nuclear World War just finshed all life in Earth! Bye Bye !!!');
         readln;
         Halt;
       end
       else
       begin
         write('The Earth was destroyed in the midst of an alien invasion! Bye Bye !!!');
         readln;
         Halt;
       end;

end;

procedure Simulation.DisplayEventsAtDayEnd();
var
  EventRanNo : Real;
begin
  writeln;
  writeln('***********************');
  writeln('*****   Events:   *****');
  writeln('***********************');
  writeln;
  EventRanNo := random();
  if EventRanNo < 0.25 then
    begin
      EventRanNo := random();
      if EventRanNo < 0.25 then
        ProcessAddHouseholdsEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessCostOfFuelChangeEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessReputationChangeEvent();
      EventRanNo := random();
      if EventRanNo >= 0.5 then
        ProcessCostChangeEvent();
      if EventRanNo < 0.1 then // Event Added to trigger the End of world event
        ProcessEndOfWorldEvent();
    end
    else
      writeln('No events.');
end;
  • 作者:Harry - Peter Symonds College


VB.net

在模擬類中建立一個方法Dropworld()

Sub Dropworld()
        console.writeline("Darkrai used black hole eclipse. A quadrillion quadrillion joules of energy was released in two seconds. The world is gone")
        console.readline()
        Stop
    End Sub

在模擬類中的DisplayEventsAtDayEnd()子例程中新增

If EventRanNo < 0.1 Then
                DropWorld()
            End If


合併公司

[edit | edit source]

Java

/* in the Simulation class */
public void mergeCompanies() {
        System.out.println("\n******* Merge Companies *********\n");
        for(int i = 0; i < companies.size();i++ ){
            System.out.println((i+1) + "." + companies.get(i).getName());
        } System.out.println("");
        int com1, com2;
        do {
            System.out.print("Enter 1st Company: ");
            String comTemp1 = Console.readLine();
            try{
                com1 = Integer.parseInt(comTemp1)-1;
            } catch (NumberFormatException e) {
                com1 = -1;
            }
        } while (com1 < 0);
        Company company1 = companies.get(com1);
        companies.remove(com1);
        
        System.out.println("");
        for(int i = 0; i < companies.size();i++ ){
            Console.writeLine((i+1) + "." + companies.get(i).getName());
        } System.out.println("");
        do {
            System.out.print("Enter 2nd Company: ");
            String comTemp2 = Console.readLine();
            try{
                com2 = Integer.parseInt(comTemp2)-1;
            } catch (NumberFormatException e) {
                com2 = -1;
            }
        } while (com2 < 0);
        Company company2 = companies.get(com2);
        companies.remove(com2);
        System.out.print("Enter new Company name: ");
        String comName = Console.readLine();
        float comBal = company2.getBalance() + company1.getBalance();
        float comReputation = (company1.getReputationScore() > company2.getReputationScore())
                ?company1.getReputationScore()
                :company2.getReputationScore();
        float comAvgCost = (company1.getAvgCostPerMeal() + company2.getAvgCostPerMeal())/2;
        float comAvgPrice = (company2.getAvgPricePerMeal() + company2.getAvgPricePerMeal())/2;
        float comDaily = (company1.getDailyCosts() + company2.getDailyCosts())/2;
        float comFuel = (company1.getFuelCostPerUnit() + company2.getFuelCostPerUnit())/2;
        float comBase = (company1.getBaseCostOfDelivery()+company2.getBaseCostOfDelivery())/2;
        List<Outlet> comOutlets = company1.getOutlets();
        comOutlets.addAll(company2.getOutlets());
        Company newCom = new Company(comName, company1.getCategory(), comBal, comReputation, comAvgCost, comAvgPrice, comDaily, comFuel, comBase, comOutlets);
        companies.add(newCom);
    }
/* in the company class */
public Company(String name, String category, float balance, float reputationScore, float avgCostPerMeal, 
            float avgPricePerMeal, float dailyCosts, float fuelCostPerUnit, float baseCostOfDelivery, List<Outlet> outlets) {
        familyOutletCost = 1000;
        fastFoodOutletCost = 2000;
        namedChefOutletCost = 15000;
        familyFoodOutletCapacity = 150;
        fastFoodOutletCapacity = 200;        
        namedChefOutletCapacity = 50;
        this.name = name;
        this.category = category;
        this.balance = balance;
        this.reputationScore = reputationScore;
        this.avgCostPerMeal = avgCostPerMeal;
        this.avgPricePerMeal = avgPricePerMeal;
        this.dailyCosts = dailyCosts;
        this.fuelCostPerUnit = fuelCostPerUnit;
        this.baseCostOfDelivery = baseCostOfDelivery;
        this.outlets = outlets;
    }
    
    public float getBalance() { return balance; }
    public String getCategory() { return category;} 
    public float getAvgCostPerMeal() { return avgCostPerMeal; }
    public float getAvgPricePerMeal() {  return avgPricePerMeal; }
    public float getDailyCosts() { return dailyCosts; }
    public float getFuelCostPerUnit() { return fuelCostPerUnit; }
    public float getBaseCostOfDelivery() {return baseCostOfDelivery;}
    public List<Outlet> getOutlets() {return outlets;}


破產事件

[edit | edit source]

新增公司在-10,000破產的事件

Python3

類公司

   def __init__(self, Name, Category, Balance, X, Y, FuelCostPerUnit, BaseCostOfDelivery):
       self._Outlets = []
       self._FamilyOutletCost = 1000
       self._NamedChefOutletCapacity = 50
       self._Name = Name
  # Added this function, which allows you to grab the balance into the "processdayend" function in the 
  # Simulation class.
   def GetBalance(self):
       return self._Balance
       

類模擬

   def __init__(self):
       self._Companies = []
       self._FuelCostPerUnit = 0.0098
       self._BaseCostforDelivery = 100
   
   # Added this code at the start of the processdayend function.
       def ProcessDayEnd(self):
       for Current in self._Companies:
           Balance = Current.GetBalance()
           Name = Current.GetName()
           Index = self.GetIndexOfCompany(Name)
           if Balance < -10000:
               del (self._Companies[Index])
               print("The company {} has gone into bankruptcy".format(Name))


作者:skesh

Java

// create isBankrupt() method in Company class
public boolean isBankrupt() {
	return balance <= -10000;
}
// create method for going bankrupt in Simulation
private boolean processGoBankruptEvent() {
        boolean goneBankrupt = false;
        for (int i = 0; i < companies.size(); i++) {
            if (companies.get(i).isBankrupt()) {
                System.out.println(companies.get(i).getName() + " has gone bankrupt and is closing.");
                companies.remove(i);
                i--;
                goneBankrupt = true;
            }
        }
        return goneBankrupt;
    }
// edit method in Simulation
private void displayEventsAtDayEnd() {
        boolean hasEvent = false;
        Console.writeLine(System.lineSeparator() + "***********************");
        Console.writeLine("*****   Events:   *****");
        Console.writeLine("***********************" + System.lineSeparator());
        float eventRanNo;
        eventRanNo = rnd.nextFloat();
        if (!companies.isEmpty()) {
            if (eventRanNo < 0.25f) {
                eventRanNo = rnd.nextFloat();
                if (eventRanNo < 0.25f) {
                    processAddHouseholdsEvent();
                    hasEvent = true;
                }
                eventRanNo = rnd.nextFloat();
                if (eventRanNo < 0.5f) {
                    processCostOfFuelChangeEvent();
                    hasEvent = true;
                }
                eventRanNo = rnd.nextFloat();
                if (eventRanNo < 0.5f) {
                    processReputationChangeEvent();
                    hasEvent = true;
                }
                eventRanNo = rnd.nextFloat();
                if (eventRanNo >= 0.5f) {
                    processCostChangeEvent();
                    hasEvent = true;
                }
            }
            if (processGoBankruptEvent())
                hasEvent = true;
            if (!hasEvent) {
                Console.writeLine("No events.");
            }
        } else {
            Console.writeLine("No more companies. Simulation finished, press Enter to close.");
            Console.readLine();
            System.exit(0);
        }
    }
  • 作者:Lucy W


Delphi/Pascal

function Company.BankruptEvent() : Boolean; // Function added to process if a company is going to go bankrupt
var
  bankruptThreshold : integer;

begin
  bankruptThreshold := -10000; // Variable allows the user to change the threshold for when a company goes bankrupt
    if self.Balance < bankruptThreshold then
       BankruptEvent := True
    else
      BankruptEvent := False
end;

procedure Simulation.RemoveCompany(index : integer); // Following Code edited from Alex Finch's remove company solution
var CompanyNumber, I : integer;

begin
  CompanyNumber := index;

if(CompanyNumber = high(Self.Companies)) then SetLength(Self.Companies, high(Self.Companies))
else
begin
  for I := CompanyNumber to high(Self.Companies)-1 do
  begin
    Self.Companies[I] := Self.Companies[I+1];
  end;
  setLength(Self.Companies, high(Self.Companies));
end;

end;  

procedure Simulation.DisplayEventsAtDayEnd(); // Small change to the events section allowing the program to display if a company has closed
var
  EventRanNo : Real;
  index : integer;
  companyIsBankrupt : boolean;
begin
  writeln;
  writeln('***********************');
  writeln('*****   Events:   *****');
  writeln('***********************');
  writeln;
  EventRanNo := random();
  if EventRanNo < 0.25 then
    begin
      EventRanNo := random();
      if EventRanNo < 0.25 then
        ProcessAddHouseholdsEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessCostOfFuelChangeEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessReputationChangeEvent();
      EventRanNo := random();
      if EventRanNo >= 0.5 then
        ProcessCostChangeEvent();
    end
    else
      writeln('No events.');

    for index := low(self.companies) to high(self.companies) do;
        companyIsBankrupt := Companies[index].BankruptEvent();
        if companyIsBankrupt = True then
          begin
           writeln(self.Companies[index].GetName() + ' has closed down due to bankruptcy');
           self.RemoveCompany(index);
          end;
end;
  • 作者:Harry - Peter Symonds College


C#

我做了一些不同的事情,我讓它在負債 7 天后強制關閉公司類

   {
       private static Random rnd = new Random();
       protected string name, category;
       protected double balance, reputationScore, avgCostPerMeal, avgPricePerMeal, dailyCosts, familyOutletCost, fastFoodOutletCost, namedChefOutletCost, fuelCostPerUnit, baseCostOfDelivery;
       protected List<Outlet> outlets = new List<Outlet>();
       protected int familyFoodOutletCapacity, fastFoodOutletCapacity, namedChefOutletCapacity;
       protected bool isindebt;                                                                         // mmaking a bool to store if the company is in debt
       protected int daysindebt;
 


“第 390 行到第 420 行”

public bool GetIfInDebt() // 公司是否負債的函式

       {
           if (balance < 0)
           {
               daysindebt++; // increments how many days the company has been in debt for
               if (daysindebt == 7)
               {
                   CompanyForcedClose();
                   return true;
               }
               return true;
           }
           else
           {
               daysindebt = 0;
               return false;
           }
       }
       public void CompanyForcedClose()                           // procedure for a company to close when in debt for more than a week. could be changed to close outlets down at first.
       {
           if (isindebt == true && daysindebt ==7)   
           {
               while (outlets.Count > 0) // calls the other subroutines while there are more than one outlet
               {
                   CloseOutlet(0);
               }             
           }
       }

更新 ProcessDayEnd 子例程

public string ProcessDayEnd()

       {
           string details = "";
           double profitLossFromOutlets = 0;
           double profitLossFromThisOutlet = 0;
           double deliveryCosts;
           if (outlets.Count > 1)
           {
               deliveryCosts = baseCostOfDelivery + CalculateDeliveryCost();
           }
           else
           {
               deliveryCosts = baseCostOfDelivery;
           }
           details += "Daily costs for company: " + dailyCosts.ToString() + "\nCost for delivering produce to outlets: " + deliveryCosts.ToString() + "\n";
           for (int current = 0; current < outlets.Count; current++)
           {
               profitLossFromThisOutlet = outlets[current].CalculateDailyProfitLoss(avgCostPerMeal, avgPricePerMeal);
               details += "Outlet " + (current + 1) + " profit/loss: " + profitLossFromThisOutlet.ToString() + "\n";
               profitLossFromOutlets += profitLossFromThisOutlet;
           }
           details += "Previous balance for company: " + balance.ToString() + "\n";
           balance += profitLossFromOutlets - dailyCosts - deliveryCosts;
           details += "New balance for company: " + balance.ToString();
           isindebt = GetIfInDebt();
           // calling the subroutines that i have made  
           
           
           return details;
       }


獲取詳細資訊子例程

public string GetDetails()

       {
           string details = "";
           details += "Name: " + name + "\nType of business: " + category + "\n";
           details += "Current balance: " + balance.ToString() + "\nAverage cost per meal: " + avgCostPerMeal.ToString() + "\n";
           details += "Average price per meal: " + avgPricePerMeal.ToString() + "\nDaily costs: " + dailyCosts.ToString() + "\n";
           details += "Delivery costs: " + CalculateDeliveryCost().ToString() + "\nReputation: " + reputationScore.ToString() + "\n";
           details += "Number of outlets: " + outlets.Count.ToString() + "\nOutlets\n";
           details += "\nIs the company in debt? " + isindebt.ToString() + "\n";                                                                                     // puts whether the company is in debt in the details
         
           if (isindebt == true)                                                                                                                          // displays how many days in debt if the company is in debt 
           {
               details += "\n Days the company has been in debt for " + daysindebt.ToString() + " days \n\n";
           }
            
           for (int current = 1; current < outlets.Count + 1; current++)
           {
               details += current + ". " + outlets[current - 1].GetDetails() + "\n";
           }
           return details;
       }

這需要在模擬類中。

public void DeleteCompany(int index) // 用於刪除所有門店都關閉的公司的新的子例程

       {
           int outlets = companies[index].GetNumberOfOutlets();
           if (outlets == 0)
           {
             
                   Console.WriteLine("That company has now closed down as it has no outlets.");
                   companies.RemoveAt(index);
                                               
           }
       }

它在 ProcessDayEnd 中被呼叫,如這裡所示

public void ProcessDayEnd()
       {
           int index = 0;
           
           do                                              // calls the other subroutine while the company count is changing
           {
               DeleteCompany(index);
               index++;
           } while (index < companies.Count);
           double totalReputation = 0;
           List<double> reputations = new List<double>();
           int companyRNo, current, loopMax, x = 0, y = 0;
           foreach (var c in companies)
           {
               c.NewDay();
               totalReputation += c.GetReputationScore();
               reputations.Add(totalReputation);
           }
           loopMax = simulationSettlement.GetNumberOfHouseholds() - 1;
           for (int counter = 0; counter < loopMax + 1; counter++)
           {
               if (simulationSettlement.FindOutIfHouseholdEatsOut(counter, ref x, ref y))
               {
                   companyRNo = rnd.Next(1, Convert.ToInt32(totalReputation) + 1);
                   current = 0;
                   while (current < reputations.Count)
                   {
                       if (companyRNo < reputations[current])
                       {
                           companies[current].AddVisitToNearestOutlet(x, y);
                           break;
                       }
                       current++;
                   }
               }
           }
           DisplayCompaniesAtDayEnd();
           DisplayEventsAtDayEnd();
           dayCounter++;                                                                //incrementing the days
         
       }
  • 作者:Michael Tomkinson*

移除公司

[edit | edit source]

C#

建立一個名為 RemoveCompany 的新方法。

public void removeCompany()
        {
            Console.Write("Enter company name: ");
            string name = Console.ReadLine();
            bool found = false;
            for(int i = 0; i < companies.Count; i++)
            {
                Console.WriteLine(companies[i]);
                if(companies[i].GetName() == name)
                {
                    companies.RemoveAt(i);
                    Console.WriteLine("Company removed");
                    found = true;
                }
            }
            if (!found)
            {
                Console.WriteLine("Company not found.");
            }
        }

修改 Run 方法以新增選項 5 的新案例

public void Run()
        {
            string choice = "";
            int index;
            while (choice != "Q")
            {
                DisplayMenu();
                choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        simulationSettlement.DisplayHouseholds();
                        break;
                    case "2":
                        DisplayCompanies();
                        break;
                    case "3":
                        string companyName;
                        index = -1;
                        while (index == -1)
                        {
                            Console.Write("Enter company name: ");
                            companyName = Console.ReadLine();
                            index = GetIndexOfCompany(companyName);
                        }
                        ModifyCompany(index);
                        break;
                    case "4":
                        AddCompany();
                        break;
                    case "5":
                        removeCompany();
                        break;
                    case "6":
                        ProcessDayEnd();
                        break;
                    case "Q":
                        Console.WriteLine("Simulation finished, press Enter to close.");
                        Console.ReadLine();
                        break;
                }
            }
        }

最後,修改 DisplayMenu 方法以將選項寫入控制檯。

public void DisplayMenu()
        {
            Console.WriteLine("\n*********************************");
            Console.WriteLine("**********    MENU     **********");
            Console.WriteLine("*********************************");
            Console.WriteLine("1. Display details of households");
            Console.WriteLine("2. Display details of companies");
            Console.WriteLine("3. Modify company");
            Console.WriteLine("4. Add new company");
            Console.WriteLine("5. Remove a company");
            Console.WriteLine("6. Advance to next day");
            Console.WriteLine("Q. Quit");
            Console.Write("\n Enter your choice: ");
        }



Delphi/Pascal


對 Simulation.Run() 進行微調;只是新增對 RemoveCompany(); 過程的呼叫。

procedure Simulation.Run();
var
  CompanyName : String;
  Choice : Char;
  Index : Integer;
begin
  Choice := ' ';
  while Choice <> 'Q' do
    begin
      DisplayMenu();
      readln(Choice);
      case (Choice) of
        '1': Self.SimulationSettlement.DisplayHouseholds();
        '2': DisplayCompanies();
        '3':
          begin
            repeat
              write('Enter company name: ');
              readln(CompanyName);
              Index := GetIndexOfCompany(CompanyName);
            until Index <> - 1;
            ModifyCompany(Index);
          end;
        '4': AddCompany();
        '5': RemoveCompany();
        '6': ProcessDayEnd();
        'Q':
          begin
            writeln('Simulation finished, press Enter to close.');
            readln;
          end;
      end;
    end;
end;

對選單進行微調以顯示新選項

procedure Simulation.DisplayMenu();
begin
  writeln;
  writeln('*********************************');
  writeln('**********    MENU     **********');
  writeln('*********************************');
  writeln('1. Display details of households');
  writeln('2. Display details of companies');
  writeln('3. Modify company');
  writeln('4. Add new company');
  writeln('5. Remove Company');
  writeln('6. Advance to next day');
  writeln('Q. Quit');
  writeln;
  write('Enter your choice: ');
end;

新增 Simulation.RemoveCompany(); 過程。還要確保你在模擬類宣告中宣告它。

procedure Simulation.RemoveCompany();
var
ValidCompanyNum : Boolean;
CompanyNumberInput : String;
CompanyNumber : Integer;
CompanyChange : Company;
ValidChangeInt : Boolean;
ReputationChangeInput : String;
ReputationChange : Integer;
I : Integer;
begin
  ValidCompanyNum := False;
  writeln('What company would you like to remove?');
  repeat
    try
      DisplayCompanyNames();
      write('Select company number: ');
      Readln(CompanyNumberInput);
      CompanyNumber := StrToInt(CompanyNumberInput);
      CompanyChange := Self.Companies[CompanyNumber];
      if( (CompanyNumber >= low(Self.Companies))  and (high(Self.Companies) >= CompanyNumber) ) then
      begin
        ValidCompanyNum := True;
        //Code to Remove from an Array
        if(CompanyNumber = high(Self.Companies)) then SetLength(Self.Companies, high(Self.Companies))
        else
        begin
          for I := CompanyNumber to high(Self.Companies)-1 do
          begin
            Self.Companies[I] := Self.Companies[I+1];
          end;
          setLength(Self.Companies, high(Self.Companies));
        end;
      end
      else raise Exception.Create('Invalid Company Number');
      except
      writeln('Error: Not real company number');
    end;

  until ValidCompanyNum;
  writeln('Company Removed! Press any key to continue');
  readln;

end;

該解決方案由Alex Finch - Peter Symonds College提供


Java 但實際上很好


顯示要刪除的公司列表

public void removeCompany(){
        for(int i = 0; i < companies.size();i++ ){
            Console.writeLine((i+1) + "." + companies.get(i).getName());
        }
        Console.writeLine("Choose company to be removed: ");
        int removeCompany = (Integer.parseInt(Console.readLine())-1);
        companies.remove(removeCompany);
        run();
    }

public void run() {
        String choice = "";
        int index;
        while (!choice.equals("Q")) {
            displayMenu();
            choice = Console.readLine();
            switch (choice) {
                case "1":
                    simulationSettlement.displayHouseholds();
                    break;
                case "2":
                    displayCompanies();
                    break;
                case "3":
                    String companyName;
                    do {
                        Console.write("Enter company name: ");
                        companyName = Console.readLine();
                        index = getIndexOfCompany(companyName);
                    } while (index == -1);
                    modifyCompany(index);
                    break;
                case "4":
                    addCompany();
                    break;
                case "6":
                    processDayEnd();
                    break;
                case "7":
                    removeCompany();
                case "Q":
                    Console.writeLine("Simulation finished, press Enter to close.");
                    Console.readLine();
                    break;
            }
        }
    }

當然,在選單中新增選項 7,這樣他們就知道要按它。作者:Owain Bestley


Java


“在 Java 中從給定的使用者輸入中移除公司的方法

public void removeCompany(){
        System.out.print("Which company: ");
        String rem = Console.readLine();
        int ind = getIndexOfCompany(rem);
        companies.remove(ind);
        
        }

“在 Java 中顯示移除公司的選單選項

public void displayMenu() {
        Console.writeLine(System.lineSeparator() + "*********************************");
        Console.writeLine("**********    MENU     **********");
        Console.writeLine("*********************************");
        Console.writeLine("1. Display details of households");
        Console.writeLine("2. Display details of companies");
        Console.writeLine("3. Modify company");
        Console.writeLine("4. Add new company");
        Console.writeLine("6. Advance to next day");
        Console.writeLine("7. Remove company");
        Console.writeLine("Q. Quit");
        Console.write(System.lineSeparator() + "Enter your choice: ");
    }

“在 run() 下新增移除公司

public void run() {
        String choice = "";
        int index;
        while (!choice.equals("Q")) {
            displayMenu();
            choice = Console.readLine();
            switch (choice) {
                case "1":
                    simulationSettlement.displayHouseholds();
                    break;
                case "2":
                    displayCompanies();
                    break;
                case "3":
                    String companyName;
                    do {
                        Console.write("Enter company name: ");
                        companyName = Console.readLine();
                        index = getIndexOfCompany(companyName);
                    } while (index == -1);
                    modifyCompany(index);
                    break;
                case "4":
                    addCompany();
                    break;
                case "6":
                    processDayEnd();
                    break;
                case "7":
                    removeCompany();
                    break;
                case "Q":
                    Console.writeLine("Simulation finished, press Enter to close.");
                    Console.readLine();
                    break;
            }
        }
    }


Python

'Changes to add a new option for remove company in Run Subroutine

  def Run(self):
    Choice = ""
    while Choice != "Q":
      self.DisplayMenu()
      Choice = input()
      if Choice == "1":
        self._SimulationSettlement.DisplayHouseholds()
      elif Choice == "2":
        self.DisplayCompanies()
      elif Choice == "3":
        Index = -1
        while Index == -1:
          CompanyName = input("Enter company name: ")
          Index = self.GetIndexOfCompany(CompanyName)
        self.ModifyCompany(Index)
      elif Choice == "4":
        self.AddCompany()
      elif Choice == "5":
        CompanyName = input("what company would you like to delete: ")
        del(self._Companies[self.GetIndexOfCompany(CompanyName)])
      elif Choice == "6":
        self.ProcessDayEnd()
      elif Choice == "Q":
        print("Simulation finished, press Enter to close.")
        input()

* Sir Graham Balfour

替代解決方案

# Submitted by Jake Talling
  def removeCompany(self):
    for i in range(len(self._Companies)):
      print((i+1), self._Companies[i]._Name)
    print("Please select which company to remove: (ID)")
    try:
      removeC = int(input())
    except:
      print("Input invalid. Returning to menu")
      return
    del(self._Companies[removeC])

        
  def Run(self):
    Choice = ""
    while Choice != "Q":
      self.DisplayMenu()
      Choice = input()
      if Choice == "1":
        self._SimulationSettlement.DisplayHouseholds()
      elif Choice == "2":
        self.DisplayCompanies()
      elif Choice == "3":
        Index = -1
        while Index == -1:
          CompanyName = input("Enter company name: ")
          Index = self.GetIndexOfCompany(CompanyName)
        self.ModifyCompany(Index)
      elif Choice == "4":
        self.AddCompany()
      elif Choice == "5":
        self.removeCompany()
      elif Choice == "6":
        self.ProcessDayEnd()
      elif Choice == "Q":
        print("Simulation finished, press Enter to close.")
        input()


VB.NET

'Changes to add a new option for remove company in Run Subroutine

    Public Sub Run(ByVal Days As Integer)
        Dim Choice As String = ""
        Dim Index As Integer
        While Choice <> "Q"
            DisplayMenu(Days)
            Choice = Console.ReadLine()
            Select Case Choice
                Case "1"
                    SimulationSettlement.DisplayHouseholds()
                Case "2"
                    DisplayCompanies()
                Case "3"
                    Dim CompanyName As String
                    Do
                        Console.Write("Enter company name: ")
                        CompanyName = Console.ReadLine()
                        Index = GetIndexOfCompany(CompanyName)
                    Loop Until Index <> -1
                    ModifyCompany(Index)
                Case "4"
                    AddCompany()

                'Add Fith option in Subroutine run
                Case "5"
                    RemoveCompany()
                Case "6"
                    Dim HowMany As Integer

                    Console.WriteLine("How many days would you like to advance? ")
                    HowMany = Console.ReadLine()

                    Days = Days + HowMany

                    ProcessDayEnd(HowMany)
                Case "Q"
                    Console.WriteLine("Simulation finished, press Enter to close.")
                    Console.ReadLine()
            End Select
        End While
    End Sub

    'New Remove Company Subroutine in simulation class

    Public Sub RemoveCompany()

        Dim CompanyToRemove As String
        Dim Index As String

        Console.WriteLine("Enter the name of the company you would like to remove: ")
        CompanyToRemove = Console.ReadLine()

        Try

            Index = GetIndexOfCompany(CompanyToRemove)
            Companies.RemoveAt(Index)

            Console.WriteLine()
            Console.WriteLine(CompanyToRemove & " removed!")
            Console.WriteLine()

        Catch

            Console.WriteLine()
            Console.WriteLine("Company: " & CompanyToRemove & " doesn't exist")
            Console.WriteLine()

        End Try

    End Sub

'Code created by Folkestone Grammar School for Girls Student - 20/09/2019


執行多天

[edit | edit source]

新增一個選項來執行模擬多天(>1)

C#

這是選單的子例程。我還在其中驗證了選單選項。

public void Run()

       {
           string choice = "";
           int index;
           while (choice != "Q")
           {
               DisplayMenu();
               do {                                                                                                 // validation for the menu choice
                   choice = Console.ReadLine();
                   switch (choice)
                   {
                       case "1":
                           simulationSettlement.DisplayHouseholds();
                           break;
                       case "2":
                           DisplayCompanies();
                           break;
                       case "3":
                           string companyName;
                           index = -1;
                           while (index == -1)
                           {
                               Console.Write("Enter company name: ");
                               companyName = Console.ReadLine();
                               index = GetIndexOfCompany(companyName);
                           }
                           ModifyCompany(index);
                           break;
                       case "4":
                           AddCompany();
                           break;
                       
                           RemoveCompany();
                           break;
                       case "6":
                           ProcessDayEnd();
                           break;
                       case "7":
                           AdvanceMultipleDays(); // added a new case that will advance many days
                           break;
                       case "Q":
                           Console.WriteLine("Simulation finished, press Enter to close.");
                           Console.ReadLine();
                           break;
                   }
                   if((int.Parse(choice) > 0 && int.Parse(choice) < 8) || choice != "Q")                             // repeats untill this = true
                   {
                       Console.WriteLine("Pleas Re Enter a valid choice");
                   }
               } while ((int.Parse(choice) > 0 && int.Parse(choice) < 8) || choice != "Q");
       } }
   }

這是推進多天的子例程

public void AdvanceMultipleDays()     // new subroutine to advance many days
       {
           Console.WriteLine("How many days would you like to advance?");
           int AdvancingDays = int.Parse(Console.ReadLine());
           for (int i = 0; i < AdvancingDays ; i++)
           {
               ProcessDayEnd();
           }
       }
  • 作者:Michael Tomkinson


Delphi/Pascal

procedure Simulation.advanceDays(daysToAdvance : integer); // Added new subprocess for advancing multiple days
var index : integer;
begin
  for index := 1 to daysToAdvance do // Loops the following code the number of times given by the user
  ProcessDayEnd(); // Calls the ProcessDayEnd subprocess
  writeln;
  writeln('Advanced Simulation ' + IntToStr(daysToAdvance) + ' Days');
end;

procedure Simulation.Run();
var
  CompanyName : String;
  Choice : Char;
  Index, daysToAdvance : Integer;
begin
  Choice := ' ';
  while Choice <> 'Q' do
    begin
      DisplayMenu();
      readln(Choice);
      case (Choice) of
        '1': Self.SimulationSettlement.DisplayHouseholds();
        '2': DisplayCompanies();
        '3':
          begin
            repeat
              write('Enter company name: ');
              readln(CompanyName);
              Index := GetIndexOfCompany(CompanyName);
            until Index <> - 1;
            ModifyCompany(Index);
          end;
        '4': AddCompany();
        '6': ProcessDayEnd();
        'Q':
          begin
            writeln('Simulation finished, press Enter to close.');
            readln;
          end;
        '7' : // Added new entry for advancing multiple days
          begin
            write('Please enter the number of days to advance: ');
            try // Validation for user input
               readln(daysToAdvance);
               advanceDays(daysToAdvance);
            except on E : Exception do
               writeln('Input is invalid');
            end;
          end;
      end;
    end;
end;
  • 作者:Harry - Peter Symonds


Java

System.out.print("Enter the number of days you want to advance: ");
int days = Integer.parseInt(Console.readLine());
for (int i = 0; i < days; i++) {
processDayEnd();
}
  • 作者:Benny


Python

  def Run(self):
    Choice = ""
    while Choice != "Q":
      self.DisplayMenu()
      Choice = input()
      if Choice == "1":
        self._SimulationSettlement.DisplayHouseholds()
      elif Choice == "2":
        self.DisplayCompanies()
      elif Choice == "3":
        Index = -1
        while Index == -1:
          CompanyName = input("Enter company name: ")
          Index = self.GetIndexOfCompany(CompanyName)
        self.ModifyCompany(Index)
      elif Choice == "4":
        self.AddCompany()
      elif Choice == "6":
        self.ProcessDayEnd()
      elif Choice == "7":
        self.MultipleDayEnd()
      elif Choice == "Q":
        print("Simulation finished, press Enter to close.")
        input()


def DisplayMenu(self):
    print("\n*********************************")
    print("**********    MENU     **********")
    print("*********************************")
    print("1. Display details of households")
    print("2. Display details of companies")
    print("3. Modify company")
    print("4. Add new company")
    print("6. Advance to next day")
    print("7. Advance a set number of days")
    print("Q. Quit")
    print("\nEnter your choice: ", end = "")
def MultipleDayEnd(self):
    try:
      numberOfDays = int(input("How many days would you like to advance?"))
      for i in range(numberOfDays +1):
        self.ProcessDayEnd()
    except:
      print("Integer not accepted")


VB.NET

'Add option 7 in Run() Subroutine in Simulation class

Public Sub Run()
        Dim Choice As String = ""
        Dim Index As Integer
        While Choice <> "Q"
            DisplayMenu()
            Choice = Console.ReadLine()
            Select Case Choice
                Case "1"
                    SimulationSettlement.DisplayHouseholds()
                Case "2"
                    DisplayCompanies()
                Case "3"
                    Dim CompanyName As String
                    Do
                        Console.Write("Enter company name: ")
                        CompanyName = Console.ReadLine()
                        Index = GetIndexOfCompany(CompanyName)
                    Loop Until Index <> -1
                    ModifyCompany(Index)
                Case "4"
                    AddCompany()
                Case "6"
                    ProcessDayEnd()

'Option 7 added
                Case "7"
                    ProcessMultipleDays()
                Case "Q"
                    Console.WriteLine("Simulation finished, press Enter to close.")
                    Console.ReadLine()
            End Select
        End While
    End Sub


'Add ProcessMultipleDays() Subroutine to Simulation Class

Public Sub ProcessMultipleDays()

        Dim noOfDays As Integer

        Try
            Console.Write("How many days do you want to advance? ")
            noOfDays = Console.ReadLine

            For i = 1 To noOfDays
                ProcessDayEnd()
            Next

        Catch ex As Exception
            Console.WriteLine("")
            Console.WriteLine("Invalid Input")
            Console.WriteLine("")
        End Try

    End Sub

'Hubert (Ousedale School) - 13/11/2019


天數計數器

[edit | edit source]

顯示已過去的天數計數器

C#

在模擬類中建立一個新的 int 變數並在建構函式中初始化它。

class Simulation
{
  private static Random rnd = new Random();
  '''private int dayCounter;'''
  protected Settlement simulationSettlement;
  protected int noOfCompanies;
  protected double fuelCostPerUnit, baseCostForDelivery;
  protected List<Company> companies = new List<Company>();

  public Simulation()
  {
    fuelCostPerUnit = 0.0098;
    baseCostForDelivery = 100;
    '''dayCounter = 1;'''
  }
}

修改 ProcessDayEnd 方法以遞增計數器

public void ProcessDayEnd()
        {
            double totalReputation = 0;
            List<double> reputations = new List<double>();
            int companyRNo, current, loopMax, x = 0, y = 0;
            foreach (var c in companies)
            {
                c.NewDay();
                totalReputation += c.GetReputationScore();
                reputations.Add(totalReputation);
            }
            loopMax = simulationSettlement.GetNumberOfHouseholds() - 1;
            for (int counter = 0; counter < loopMax + 1; counter++)
            {
                if (simulationSettlement.FindOutIfHouseholdEatsOut(counter, ref x, ref y))
                {
                    companyRNo = rnd.Next(1, Convert.ToInt32(totalReputation) + 1);
                    current = 0;
                    while (current < reputations.Count)
                    {
                        if (companyRNo < reputations[current])
                        {
                            companies[current].AddVisitToNearestOutlet(x, y);
                            break;
                        }
                        current++;
                    }
                }
            }
            DisplayCompaniesAtDayEnd();
            DisplayEventsAtDayEnd();
            '''dayCounter++;''' 
        }


修改選單以顯示天數

public void DisplayMenu()
        {
            Console.WriteLine("\n*********************************");
            Console.WriteLine("**********    MENU     **********");
            Console.WriteLine("*********************************");
            '''Console.WriteLine($"Day: {dayCounter}");'''
            Console.WriteLine("*********************************");
            Console.WriteLine("1. Display details of households");
            Console.WriteLine("2. Display details of companies");
            Console.WriteLine("3. Modify company");
            Console.WriteLine("4. Add new company");
            Console.WriteLine("5. Advance multiple days");
            Console.WriteLine("6. Advance to next day");
            Console.WriteLine("Q. Quit");


Delphi/Pascal

type
  Simulation = class
    protected
      Day : Integer; // Added new class variable 'Day' to simulation class

procedure Simulation.DisplayEventsAtDayEnd();
var
  EventRanNo : Real;
begin
  inc(self.Day); // Increment day before displaying the events
  writeln;
  writeln('***********************');
  writeln('*****   Events:   *****');
  writeln('***********************');
  writeln;
  EventRanNo := random();
  if EventRanNo < 0.25 then
    begin
      EventRanNo := random();
      if EventRanNo < 0.25 then
        ProcessAddHouseholdsEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessCostOfFuelChangeEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessReputationChangeEvent();
      EventRanNo := random();
      if EventRanNo >= 0.5 then
        ProcessCostChangeEvent();
    end
    else
      writeln('No events.');
    writeln('Day: ' + inttostr(self.Day)); // show the current day at the bottom of the Events section
end;
  • 作者:Harry - Peter Symonds College


Java

在模擬類中新增整數 days 並將其設定為零

class Simulation {
    protected Settlement simulationSettlement;
    protected int noOfCompanies;
    protected float fuelCostPerUnit, baseCostForDelivery;
    protected List<Company> companies = new ArrayList();
    private static Random rnd = new Random();
    public int days = 0;

............................}

“在模擬類下,新增以下方法:”1. 遞增天數

public void DayCounter(){
        days += 1;
    }

2. 輸出已過去的天數

public void NoOfDays(){
        if(days != 1){
        System.out.println(days +" days have passed");
        } else{
            System.out.println(days +" day has passed");
        }
    }

“在執行() 方法中新增它,順序和方法使用正確

public void run() {
        String choice = "";
        int index;
        
        while (!choice.equals("Q")) {
            displayMenu();
            choice = Console.readLine();
            DayCounter();
            switch (choice) {
                case "1":
                    simulationSettlement.displayHouseholds();
                    break;
                case "2":
                    displayCompanies();
                    break;
                case "3":
                    String companyName;
                    do {
                        Console.write("Enter company name: ");
                        companyName = Console.readLine();
                        index = getIndexOfCompany(companyName);
                    } while (index == -1);
                    modifyCompany(index);
                    break;
                case "4":
                    addCompany();
                    break;
                case "6":
                    processDayEnd();
                    break;
                case "7":
                    removeCompany();
                    break;
                case "Q":
                    Console.writeLine("Simulation finished, press Enter to close.");
                    Console.readLine();
                    break;
             
            }
            NoOfDays();
            
            
        }
    }


Python

class Simulation:
  def __init__(self):
    self._DayCount = 0
    self._Companies = []
    self._FuelCostPerUnit = 0.0098
    self._BaseCostforDelivery = 100

 def ProcessDayEnd(self):
    TotalReputation = 0.0
    Reputations = []
    for C in self._Companies:
      C.NewDay()
      TotalReputation += C.GetReputationScore()
      Reputations.append(TotalReputation)
    LoopMax = self._SimulationSettlement.GetNumberOfHouseholds() - 1
    for Counter in range (0, LoopMax + 1):
      EatsOut, X, Y = self._SimulationSettlement.FindOutifHouseholdEatsOut(Counter)
      if EatsOut:
        CompanyRNo = random.randint(1, int(TotalReputation))
        Current = 0
        while Current < len(Reputations):
          if CompanyRNo < Reputations[Current]:
            self._Companies[Current].AddVisitToNearestOutlet(X, Y)
            break
          Current += 1
    self.__DisplayCompaniesAtDayEnd()
    self.__DisplayEventsAtDayEnd()
    self._DayCount += 1
    print("day:", self._DayCount)

- Sir Graham Balfour


VB.NET

Public Sub Run()

'Add new variable that will be the counter of days
        Dim daysCount As Integer = 0
        Dim Choice As String = ""
        Dim Index As Integer
        While Choice <> "Q"
            DisplayMenu()
            Choice = Console.ReadLine()
            Select Case Choice
                Case "1"
                    SimulationSettlement.DisplayHouseholds()
                Case "2"
                    DisplayCompanies()
                Case "3"
                    Dim CompanyName As String
                    Do
                        Console.Write("Enter company name: ")
                        CompanyName = Console.ReadLine()
                        Index = GetIndexOfCompany(CompanyName)
                    Loop Until Index <> -1
                    ModifyCompany(Index)
                Case "4"
                    AddCompany()
                Case "5"
                    RemoveCompany()
                Case "6"
                    ProcessDayEnd()
'Every time we advance 1 day, the counter increments by 1.
                    daysCount += 1
                    Console.WriteLine("Day: " & daysCount)
                Case "Q"
                    Console.WriteLine("Simulation finished, press Enter to close.")
                    Console.ReadLine()
            End Select
        End While
    End Sub

'Hubert (Ousedale School) - 13/11/2019


顯示成本(£x.xx)

[edit | edit source]

以 £x.xx 的格式顯示成本,而不是僅僅顯示整數。

C#

public static string getPoundsFormat(double toChange)
        {
            string changed = "";
            changed = toChange.ToString("C", CultureInfo.CurrentCulture);
            return changed;
        } //doubt this will be a question since you'd have to change a lot of lines


Delphi/Pascal

請注意:RoundValue 被選為該函式的名稱,因為 round() 是 Pascal 中使用的關鍵字,在複製程式碼時不要將函式名稱更改為 round,否則會導致程式其他部分出現問題。

function RoundValue(Value : float) : String; // New Function added to round numbers and return a string
var noDecimalPlaces : integer;
begin
         noDecimalPlaces := 2; // Variable to define how many decimal places the resulting string should be
         RoundValue := FloatToStrF(Value, ffFixed, 8, noDecimalPlaces);
end;

要將此應用於程式,只需用 RoundValue() 函式替換所有適用的 FloatToStr() 函式。

  • 作者:Harry - Peter Symonds College


Java

“對於給定的貨幣格式,在每個類中新增

public DecimalFormat df = new DecimalFormat("£.00");

“以 £x.xx 顯示任何成本,例如

df.format(dailyCosts)


Python

def Round(money):
  money = "£" + str('{:.2f}'.format(money))
  return money

  def GetDetails(self):
    Details = "Coordinates: (" + str(self._XCoord) + ", " + str(self._YCoord) + ")     Capacity: " + str(self._Capacity) + "      Maximum Capacity: "
    Details += str(self._MaxCapacity) + "      Daily Costs: " + Round(self._DailyCosts) + "      Visits today: " + str(self._VisitsToday)
    return Details

  def GetDetails(self):
    Details = ""
    Details += "Name: " + self._Name + "\nType of business: " + self._Category + "\n"
    Details += "Current balance: " + Round(self._Balance) + "\nAverage cost per meal: " + Round(self._AvgCostPerMeal) + "\n"
    Details += "Average price per meal: " + Round(self._AvgPricePerMeal) + "\nDaily costs: " + Round(self._DailyCosts) + "\n"
    Details += "Delivery costs: " + Round(self.CalculateDeliveryCost()) + "\nReputation: " + str(self._ReputationScore) + "\n\n"
    Details += "Number of outlets: " + str(len(self._Outlets)) + "\nOutlets\n"
    for Current in range (1, len(self._Outlets) + 1):
      Details += str(Current) + ". " + self._Outlets[Current - 1].GetDetails() + "\n"
    return Details


VB.NET

'Note: This would have to be done throughout the entire program for all variables or a subroutine could be created this is just an example, Line 267
FormatCurrency(CalculateDeliveryCost.ToString, 2)


檢查公司名稱是否已存在

[edit | edit source]

AddCompany(self) 新增一個檢查以檢視 CompanyName 是否已存在於 _Companies 中

C#


Delphi/Pascal

procedure Simulation.AddCompany();
var
  Balance, X, Y : Integer;
  CompanyName, TypeOfCompany : String;
  NewCompany, ExistingCompany : Company;
  NameAvailable : Boolean;
begin
  write('Enter a name for the company: ');
  readln(CompanyName);

  if length(self.Companies) <> 0 then
    begin
      for ExistingCompany in self.Companies do
        begin
          if CompanyName = ExistingCompany.GetName then
            begin
              writeln('Name Taken');
              NameAvailable := False;
              self.AddCompany(); //Recursive call.
            end
          else
            begin
              NameAvailable := True;
            end;
        end;
    end
  else
    begin
      NameAvailable := True; //Name is available if no other companies exist.
    end;

  if NameAvailable then
    begin
      write('Enter the starting balance for the company: ');
      readln(Balance);
      repeat
        write('Enter 1 for a fast food company, 2 for a family company or 3 for a named chef company: ');
        readln(TypeOfCompany);
      until ((TypeOfCompany = '1') or (TypeOfCompany = '2') or (TypeOfCompany = '3'));
      if TypeOfCompany = '1' then
        TypeOfCompany := 'fast food'
      else if TypeOfCompany = '2' then
        TypeOfCompany := 'family'
      else
        TypeOfCompany := 'named chef';
      Self.SimulationSettlement.GetRandomLocation(X, Y);
      NewCompany := Company.New(CompanyName, TypeOfCompany, Balance, X, Y, FuelCostPerUnit, Self.BaseCostForDelivery);
      SetLength(Self.Companies, length(Self.Companies) + 1);
      Self.Companies[High(Self.Companies)] := NewCompany;
    end;
end;

作者:Lawrence Dennison-Hall | Peter Symonds College


Java

這只是對模擬類中 addCompany() 函式的編輯。

 public void addCompany() {
        int balance, x, y;
        String companyName, typeOfCompany;
        Console.write("Enter a name for the company: ");
        companyName = Console.readLine();
        String existingNames;
        for(int i = 0; i < companies.size();i++ ){
            existingNames = companies.get(i).getName();
            while(companyName.equals(existingNames)){
                Console.print("Company name taken, please enter another name: ");
                companyName = Console.readLine();
            }
        }

作者:獨一無二的 Jorge Bishop


Python

  def AddCompany(self):
    NameCheck = True
    while NameCheck == True:
      CompanyName = input("Enter a name for the company: ")
      NameCheck = False
      for name in self._Companies:
        checking=name.GetName()
        if checking==CompanyName:
          print('company name taken')
          NameCheck = True
    Balance = int(input("Enter the starting balance for the company: "))
    TypeOfCompany = ""
    while not(TypeOfCompany == "1" or TypeOfCompany == "2" or TypeOfCompany == "3"):
      TypeOfCompany = input("Enter 1 for a fast food company, 2 for a family company or 3 for a named chef company: ")
    if TypeOfCompany == "1":
      TypeOfCompany = "fast food"
    elif TypeOfCompany == "2":
      TypeOfCompany = "family"
    else:
      TypeOfCompany = "named chef"
    X, Y = self._SimulationSettlement.GetRandomLocation()
    NewCompany = Company(CompanyName, TypeOfCompany, Balance, X, Y, self._FuelCostPerUnit, self._BaseCostforDelivery)
    self._Companies.append(NewCompany)


VB.NET

Public Sub AddCompany()
        'Use this variable as a condition for your while loop to stop you from continuing with a name already in use
        Dim checkName As Boolean = True
        Dim Balance, X, Y As Integer
        Dim CompanyName, TypeOfCompany As String
        'Use the name variable as a holder for the name you're checking your chosen name against in the ArrayList
        Dim name As String
        'Add in a while loop to check through the ArrayList of company names to check if the chosen name has already been used.
        While checkName = True
            Console.Write("Enter a name for the company: ")
            CompanyName = Console.ReadLine()
            checkName = False
            For i = 0 To Me.Companies.Count - 1
                name = Me.Companies(i).GetName()
                If CompanyName = name Then
                    Console.WriteLine("Company name is already in use")
                    checkName = True
                End If
            Next
        End While

        Console.Write("Enter the starting balance for the company: ")
        Balance = Console.ReadLine()
        Do
            Console.Write("Enter 1 for a fast food company, 2 for a family company or 3 for a named chef company: ")
            TypeOfCompany = Console.ReadLine()
        Loop Until TypeOfCompany = "1" Or TypeOfCompany = "2" Or TypeOfCompany = "3"
        If TypeOfCompany = "1" Then
            TypeOfCompany = "fast food"
        ElseIf TypeOfCompany = "2" Then
            TypeOfCompany = "family"
        Else
            TypeOfCompany = "named chef"
        End If
        SimulationSettlement.GetRandomLocation(X, Y)
        Dim NewCompany As New Company(CompanyName, TypeOfCompany, Balance, X, Y, FuelCostPerUnit, BaseCostForDelivery)
        Companies.Add(NewCompany)
    End Sub

TM ~ Ouse


燃料成本始終為正

[edit | edit source]

確保燃料成本永遠不會變為負數

興趣點

燃料成本變為負數是因為
DeliveryCosts = Distance * FuelCostPerUnit
因此,當 FuelCostPerUnit 變為負數時,燃料成本將很快崩潰。
只需確保 FuelCostPerUnit 不變為負數即可。


C#

 
in the company class makes sure it is always positive
 public void AlterFuelCostPerUnit(double change)
        {
            fuelCostPerUnit += change;
            if (fuelCostPerUnit < 0) // checks if below zero 
            {
                fuelCostPerUnit -= fuelCostPerUnit; // if below zero then makes the negative positive
            }
        }

*By Michael Tomkinson


Delphi/Pascal

procedure Company.AlterFuelCostPerUnit(Change : Real);
begin
  Self.FuelCostPerUnit := Self.FuelCostPerUnit + Change;
  if Self.FuelCostPerUnit < 0 then
    Self.FuelCostPerUnit := 0; //Sets fuel costs to 0 if they fall below 0.
end;

作者:Lawrence Dennison-Hall | Peter Symonds College


Java


Python

def __ProcessCostOfFuelChangeEvent(self):
    FuelCostChange = random.randint(1, 9) / 10.0
    UpOrDown = random.randint(0, 1)
    CompanyNo = random.randint(0, len(self._Companies) - 1)
    if UpOrDown == 1:
      if self._Companies[CompanyNo]._FuelCostPerUnit<FuelCostChange:
        UpOrDown=0
    if UpOrDown == 0:
      print("The cost of fuel has gone up by " + str(FuelCostChange) + " for " + self._Companies[CompanyNo].GetName())
    else:
      print("The cost of fuel has gone down by " + str(FuelCostChange) + " for " + self._Companies[CompanyNo].GetName())
      FuelCostChange *= -1
    self._Companies[CompanyNo].AlterFuelCostPerUnit(FuelCostChange)


VB.NET

'Add a new public function to the company class to get access to the current value of FuelCostPerUnit
    Public Function GetFuelCostPerUnit() As Integer
        Return FuelCostPerUnit
    End Function

'Edit the ProcessCostOfFuelChangeEvent() Sub in the Simulation Class
    Private Sub ProcessCostOfFuelChangeEvent()
        Dim FuelCostChange As Single = (Int(Rnd() * 9) + 1) / 10
        Dim UpOrDown As Integer = Int(Rnd() * 2)
        Dim CompanyNo As Integer = Int(Rnd() * Companies.Count)
        'Add in a separate if statement to change the value of the upordown to 0 if the current fuelcost change is going to result in a negative fuelcostchange
        If UpOrDown = 1 Then
            If Me.Companies(CompanyNo).GetFuelCostPerUnit() < FuelCostChange Then
                UpOrDown = 0
            End If
        End If

        If UpOrDown = 0 Then
            Console.WriteLine("The cost of fuel has gone up by " & FuelCostChange.ToString() & " for " & Companies(CompanyNo).GetName())
        Else
            Console.WriteLine("The cost of fuel has gone down by " & FuelCostChange.ToString() & " for " & Companies(CompanyNo).GetName())
            FuelCostChange *= -1
        End If
        Companies(CompanyNo).AlterFuelCostPerUnit(FuelCostChange)
    End Sub

TM ~ Ouse


小型定居點

[edit | edit source]

新增小型定居點類並在啟動選單中提供一個選項

C#


Delphi/Pascal


小型定居點類

type
  SmallSettlement = class(Settlement)
    public
      constructor New(ReducedXSize : Integer; ReducedYSize : Integer; ReducedHouseholds : Integer); overload;
  end;

小型定居點建構函式

constructor SmallSettlement.New(ReducedXSize : Integer; ReducedYSize : Integer; ReducedHouseholds : Integer);
var
  Count : Integer;
begin
  Self.New();
  Self.XSize := Self.XSize - ReducedXSize;
  Self.YSize := Self.YSize - ReducedYSize;
  Self.StartNoOfHouseholds := Self.StartNoOfHouseholds - ReducedHouseholds;
  for Count := 1 to Self.StartNoOfHouseholds do
    Self.AddHousehold();
end;

更新的選單選項(模擬建構函式)

constructor Simulation.New();
var
  Choice : String;
  ExtraX, ExtraY, ExtraHouseholds, ReducedX, ReducedY, ReducedHouseholds, Count : Integer;
  Company1, Company2, Company3 : Company;
begin
  Self.FuelCostPerUnit := 0.0098;
  Self.BaseCostForDelivery := 100;
  write('Enter L for a large settlement OR S for a small settlement, anything else for a normal size settlement: ');
  readln(Choice);
  if Choice = 'L' then
    begin
      write('Enter additional amount to add to X size of settlement: ');
      readln(ExtraX);
      write('Enter additional amount to add to Y size of settlement: ');
      readln(ExtraY);
      write('Enter additional number of households to add to settlement: ');
      readln(ExtraHouseholds);
      Self.SimulationSettlement := LargeSettlement.New(ExtraX, ExtraY, ExtraHouseholds);
    end
  else if Choice = 'S' then
    begin
      write('Enter reduced amount to add to X size of settlement: ');
      readln(ReducedX);
      write('Enter reduced amount to add to Y size of settlement: ');
      readln(ReducedY);
      write('Enter reduced number of households to add to settlement: ');
      readln(ReducedHouseholds);
      Self.SimulationSettlement := SmallSettlement.New(ReducedX, ReducedY, ReducedHouseholds);
    end
  else
    Self.SimulationSettlement := Settlement.New();
  write('Enter D for default companies, anything else to add your own start companies: ');
  readln(Choice);
  if Choice = 'D' then
    begin
      Self.NoOfCompanies := 3;
      SetLength(Self.Companies, Self.NoOfCompanies);
      Company1 := Company.New('AQA Burgers', 'fast food', 100000, 200, 203, Self.FuelCostPerUnit, Self.BaseCostForDelivery);
      Self.Companies[0] := Company1;
      Self.Companies[0].OpenOutlet(300, 987);
      Self.Companies[0].OpenOutlet(500, 500);
      Self.Companies[0].OpenOutlet(305, 303);
      Self.Companies[0].OpenOutlet(874, 456);
      Self.Companies[0].OpenOutlet(23, 408);
      Self.Companies[0].OpenOutlet(412, 318);
      Company2 := Company.New('Ben Thor Cuisine', 'named chef', 100400, 390, 800, Self.FuelCostPerUnit, Self.BaseCostForDelivery);
      Self.Companies[1] := Company2;
      Company3 := Company.New('Paltry Poultry', 'fast food', 25000, 800, 390, Self.FuelCostPerUnit, Self.BaseCostForDelivery);
      Self.Companies[2] := Company3;
      Self.Companies[2].OpenOutlet(400, 390);
      Self.Companies[2].OpenOutlet(820, 370);
      Self.Companies[2].OpenOutlet(800, 600);
    end
  else
    begin
      write('Enter number of companies that exist at the start of simulation: ');
      readln(Self.NoOfCompanies);
      for Count := 1 to Self.NoOfCompanies do
        AddCompany();
    end;
end;

作者:Lawrence Dennison-Hall | Peter Symonds College


Java


透過將 Settlement 類更改為 0 而不是僅僅減去一個整數,我們可以避免出現任何可能導致出現負整數的問題。我們現在也可以建立一個自定義大小的 Settlement。

public Settlement() {
        xSize = 0;
        ySize = 0;
        startNoOfHouseholds = 0;
        createHouseholds();
    }

class DefaultSettlement extends Settlement{
    public DefaultSettlement() {
        xSize = 1000;
        ySize = 1000;
        startNoOfHouseholds = 250;
        createHouseholds();
    }
}

class LargeSettlement extends DefaultSettlement {
    public LargeSettlement(int extraxSize, int extraySize, int extraHouseholds) {
        super();
        super.xSize += extraxSize;
        super.ySize += extraySize;
        super.startNoOfHouseholds += extraHouseholds;
        for (int count = 1; count <= extraHouseholds; count++) {
            addHousehold();
        }
    }
}

class SmallSettlement extends Settlement {
    public SmallSettlement(int XSize, int YSize, int Households) {
        super();
        super.xSize = XSize;
        super.ySize = YSize;
        super.startNoOfHouseholds = Households;
        for (int count = 1; count <= Households; count++) {
            addHousehold();
        
    }
}

你還必須修改模擬類以適應更改,呼叫預設的模擬類而不是僅僅呼叫模擬。

 public Simulation() {
        fuelCostPerUnit = 0.0098f;
        baseCostForDelivery = 100;
        String choice;
        Console.write("Enter L for a large settlement, S for a small settlement and anything else for a normal size settlement: ");
        choice = Console.readLine();
        if (choice.toUpperCase().equals("L")) {
            int extrax, extray, extraHouseholds;
            Console.write("Enter additional amount to add to x size of settlement: ");
            extrax = Integer.parseInt(Console.readLine());
            Console.write("Enter additional amount to add to y size of settlement: ");
            extray = Integer.parseInt(Console.readLine());
            Console.write("Enter additional number of households to add to settlement: ");
            extraHouseholds = Integer.parseInt(Console.readLine());
            simulationSettlement = new LargeSettlement(extrax, extray, extraHouseholds);
        }
        else if(choice.toUpperCase().equals("S")){
            int sizeX, sizeY, noHouseholds;
            Console.write("Enter  x size of settlement: ");
            sizeX = Integer.parseInt(Console.readLine());
            Console.write("Enter y size of settlement: ");
            sizeY = Integer.parseInt(Console.readLine());
            Console.write("Enter number of households in settlement: ");
            noHouseholds = Integer.parseInt(Console.readLine());
            simulationSettlement = new SmallSettlement(sizeX,sizeY,noHouseholds);
        }
        else {
            simulationSettlement = new DefaultSettlement();            
        }

作者:Owain Bestley(聽聽忠犬八公的故事)


Python


class SmallSettlement(Settlement):
  def __init__(self, LowerXSize, LowerYSize, LowerHouseholds):
    self._XSize = 1000 - LowerXSize
    self._YSize = 1000 - LowerYSize
    self._StartNoOfHouseholds = 250 -  LowerHouseholds
    self._Households = []
    for Count in range (1, self._StartNoOfHouseholds + 1):
      self.AddHousehold()


class Simulation:
  def __init__(self):
    self._DayCount = 0
    self._Companies = []
    self._FuelCostPerUnit = 0.0098
    self._BaseCostforDelivery = 100
    Choice = input("Enter L for a large settlement, S for a small settlement,  anything else for a normal size settlement: ")
    if Choice == "L":
      ExtraX = int(input("Enter additional amount to add to X size of settlement: "))
      ExtraY = int(input("Enter additional amount to add to Y size of settlement: "))
      ExtraHouseholds = int(input("Enter additional number of households to add to settlement: "))
      self._SimulationSettlement = LargeSettlement(ExtraX, ExtraY, ExtraHouseholds)
      ###
    elif Choice == "S":
      LowerX = 1001
      LowerY = 1001
      LowerHouseholds = 251
      while LowerX >= 1000 or LowerY >= 1000 or LowerHouseholds >= 250:
        print("X and Y reductions cannot equal to or more than 1000 and Household reduction cannot be equal to or more than 250")
        LowerX = int(input("Enter the amount of X size you want to reduce the settlement by: "))
        LowerY = int(input("Enter the amount of y size you want to reduce the settlement by: "))
        LowerHouseholds = int(input("Enter the amount of households you want to reduce in the settlement: "))
      self._SimulationSettlement = SmallSettlement(LowerX, LowerY, LowerHouseholds)
      ###
    else:
      self._SimulationSettlement = Settlement()            
    Choice = input("Enter D for default companies, anything else to add your own start companies: ")
    if Choice == "D":
      self._NoOfCompanies = 3
      Company1 = Company("AQA Burgers", "fast food", 100000, 200, 203, self._FuelCostPerUnit, self._BaseCostforDelivery)
      self._Companies.append(Company1)
      self._Companies[0].OpenOutlet(300, 987)
      self._Companies[0].OpenOutlet(500, 500)
      self._Companies[0].OpenOutlet(305, 303)
      self._Companies[0].OpenOutlet(874, 456)
      self._Companies[0].OpenOutlet(23, 408)
      self._Companies[0].OpenOutlet(412, 318)
      Company2 = Company("Ben Thor Cuisine", "named chef", 100400, 390, 800, self._FuelCostPerUnit, self._BaseCostforDelivery)
      self._Companies.append(Company2)
      Company3 = Company("Paltry Poultry", "fast food", 25000, 800, 390, self._FuelCostPerUnit, self._BaseCostforDelivery)
      self._Companies.append(Company3)
      self._Companies[2].OpenOutlet(400, 390)
      self._Companies[2].OpenOutlet(820, 370)
      self._Companies[2].OpenOutlet(800, 600)
    else:
      self._NoOfCompanies = int(input("Enter number of companies that exist at start of simulation: "))
      for Count in range (1, self._NoOfCompanies + 1):
        self.AddCompany()


VB.NET

'Add in a new class called SmallSettllement that inherits its values from settlement
Class SmallSettlement
    Inherits Settlement

    Public Sub New(ByVal ReduceXBy As Integer, ByVal ReduceYBy As Integer, ByVal ReduceHousingBy As Integer)
        Me.XSize = 1000 - ReduceXBy
        Me.YSize = 1000 - ReduceYBy
        Me.StartNoOfHouseholds = 250 - ReduceHousingBy
        For count = 1 To (StartNoOfHouseholds + 1)
            Me.AddHousehold()
        Next

    End Sub
End Class

Public Sub New()
        FuelCostPerUnit = 0.0098
        BaseCostForDelivery = 100
        Dim Choice As String
        'Add in a choice for a small settlement on the choice display in the public sub new of simulation
        Console.Write("Enter L for a large settlement, S for a small settlement & anything else for a normal size settlement: ")
        Choice = Console.ReadLine()
        If Choice = "L" Then
            Dim ExtraX, ExtraY, ExtraHouseholds As Integer
            Console.Write("Enter additional amount to add to X size of settlement: ")
            ExtraX = Console.ReadLine()
            Console.Write("Enter additional amount to add to Y size of settlement: ")
            ExtraY = Console.ReadLine()
            Console.Write("Enter additional number of households to add to settlement: ")
            ExtraHouseholds = Console.ReadLine()
            SimulationSettlement = New LargeSettlement(ExtraX, ExtraY, ExtraHouseholds)
            'Add in an elseif for the choice of a small settlement in the public sub new of simulation.
        ElseIf Choice = "S" Then
            Dim reducexby As Integer = 1001
            Dim reduceyby As Integer = 1001
            Dim reducehousingby As Integer = 251
            While reducexby >= 1000 Or reduceyby >= 1000 Or reducehousingby >= 250
                Console.WriteLine("X & Y cannot be reduced by more than 999 and Housing cannot be reduced by more than 249.")
                Console.WriteLine("Please enter how much you would like to reduce the X size by: ")
                reducexby = Console.ReadLine()
                Console.WriteLine("Please enter how much you would like to reduce the Y size by: ")
                reduceyby = Console.ReadLine()
                Console.WriteLine("Please enter how much you would like to reduce the number of houses by: ")
                reducehousingby = Console.ReadLine()
            End While
            SimulationSettlement = New SmallSettlement(reducexby, reduceyby, reducehousingby)
            '
        Else
            SimulationSettlement = New Settlement()
        End If

TM ~ Ouse


不接受帶有額外X/Y的整數

[編輯 | 編輯原始碼]

問題描述

C#


Delphi/Pascal


Java


Python


VB.NET


新增一個關閉出口事件

[編輯 | 編輯原始碼]

新增一個隨機事件函式,該函式從公司中移除一個出口

C#


Delphi/Pascal

新新增的函式和過程需要在類定義中宣告。

procedure Simulation.RemoveCompany(CompanyNumber : integer); // Following code adapted from Alex Finch's Remove Company program (Added here to remove companies that have no outlets)
var i : integer;
begin
if(CompanyNumber = high(Self.Companies)) then
                 SetLength(Self.Companies, high(Self.Companies))
else
    begin
         for i := CompanyNumber to high(Self.Companies)-1 do
                  Self.Companies[i] := Self.Companies[i+1];
         setLength(Self.Companies, high(Self.Companies));
    end;
end;

procedure Simulation.ProcessCloseOutletEvent(); // Procedure added to handle closing an outlet
var
  CompanyNumber, NumberOfOutlets : integer;
  CompanyName : String;

begin
  CompanyNumber := trunc(Random() * length(Self.Companies)); // Pick a random company
  CompanyName := self.companies[CompanyNumber].getName(); // Get that companies name
  NumberOfOutlets := self.companies[CompanyNumber].getNumberOfOutlets(); // Get the number of outlets that company has
  self.companies[CompanyNumber].CloseOutlet(Random(NumberOfOutlets)); // Close a random outlet from that company

  if NumberOfOutlets -1 = 0 then // the built in CloseOutlet() function returns whether the company should close or not, however I think this way is easier
  begin
     writeln(CompanyName + ' has closed their last outlet and has closed down');
     RemoveCompany(CompanyNumber)
  end
  else
      writeln(CompanyName + ' has closed one of their outlets, they have ' + IntToStr(NumberOfOutlets -1) + ' outlet(s) left');
end;

procedure Simulation.DisplayEventsAtDayEnd(); // Procedure edited to add in the chance to close an outlet
var
  EventRanNo : Real;
begin
  writeln;
  writeln('***********************');
  writeln('*****   Events:   *****');
  writeln('***********************');
  writeln;
  EventRanNo := random();
  if EventRanNo < 0.25 then
    begin
      EventRanNo := random();
      if EventRanNo < 0.25 then // Close outlet procedure added to this event, could be added to any events
        begin
             ProcessAddHouseholdsEvent();
             ProcessCloseOutletEvent(); // Calls procedure to close an outlet of a company
        end;
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessCostOfFuelChangeEvent();
      EventRanNo := random();
      if EventRanNo < 0.5 then
        ProcessReputationChangeEvent();
      EventRanNo := random();
      if EventRanNo >= 0.5 then
        ProcessCostChangeEvent();
    end
    else
      writeln('No events.');
end;
  • 作者:Harry - Peter Symonds College


Java

  • 在 Simulation 類下新增以下方法
public void closeOutletEvent(){
        int indexCOE = rnd.nextInt((companies.size()-1)+1) +1;
        int outletIndexCOE = rnd.nextInt((companies.get(indexCOE).getNumberOfOutlets()-1)+1) +1;
        for(int i = 0; i<=companies.size();i++){
            if(i == indexCOE){
                boolean closeCompany = companies.get(indexCOE).closeOutlet(outletIndexCOE-1);
                    if (closeCompany) {
                        Console.writeLine("That company has now closed down as it has no outlets.");
                        companies.remove(indexCOE);
                    }
            }
        }
        System.out.println("An outlet has been closed");
    }
  • 同樣在 displayEventsAtDayEnd() 方法下,新增以下一些額外的程式碼行
private void displayEventsAtDayEnd() {
        Console.writeLine(System.lineSeparator() + "***********************");
        Console.writeLine("*****   Events:   *****");
        Console.writeLine("***********************" + System.lineSeparator());
        float eventRanNo;
        eventRanNo = rnd.nextFloat();
        if (eventRanNo<100f){
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.5f) {
                closeOutletEvent();
            }
        }
        if (eventRanNo < 0.25f) {
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.25f) {
                processAddHouseholdsEvent();
            }
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.5f) {
                processCostOfFuelChangeEvent();
            }
            eventRanNo = rnd.nextFloat();
            if (eventRanNo < 0.5f) {
                processReputationChangeEvent();
            }
            eventRanNo = rnd.nextFloat();
            if (eventRanNo >= 0.5f) {
                processCostChangeEvent();
            }
            
        } else {
            Console.writeLine("No events.");
        }
    }


Python

def __ProcessCloseOutletEvent(self):
  indx = random.randrange(0,len(self._Companies))
  randomCompany = self._Companies[indx]

  print(str(randomCompany._Name+" has closed an outlet"))
  randomCompany.CloseOutlet(random.randrange(0,randomCompany.GetNumberOfOutlets()))
  remove = randomCompany.GetNumberOfOutlets()
  print(remove)
  if remove <= 0:
    print(randomCompany._Name+" has closed down due to not having any more outlets open")
    del(self._Companies[indx])
  else:
    pass


VB.NET


新增一個更快的選項來載入預設場景

[編輯 | 編輯原始碼]

新增一個更快的選項來載入預設場景

C#


Delphi/Pascal

procedure Simulation.initiateDefaultCompanies(); // New procedure added to initiate the default companies and ensure no repeated code
var Company1, Company2, Company3 : Company;
begin
  Self.NoOfCompanies := 3;
  SetLength(Self.Companies, Self.NoOfCompanies);
  Company1 := Company.New('AQA Burgers', 'fast food', 100000, 200, 203, Self.FuelCostPerUnit, Self.BaseCostForDelivery);
  Self.Companies[0] := Company1;
  Self.Companies[0].OpenOutlet(300, 987);
  Self.Companies[0].OpenOutlet(500, 500);
  Self.Companies[0].OpenOutlet(305, 303);
  Self.Companies[0].OpenOutlet(874, 456);
  Self.Companies[0].OpenOutlet(23, 408);
  Self.Companies[0].OpenOutlet(412, 318);
  Company2 := Company.New('Ben Thor Cuisine', 'named chef', 100400, 390, 800, Self.FuelCostPerUnit, Self.BaseCostForDelivery);
  Self.Companies[1] := Company2;
  Company3 := Company.New('Paltry Poultry', 'fast food', 25000, 800, 390, Self.FuelCostPerUnit, Self.BaseCostForDelivery);
  Self.Companies[2] := Company3;
  Self.Companies[2].OpenOutlet(400, 390);
  Self.Companies[2].OpenOutlet(820, 370);
  Self.Companies[2].OpenOutlet(800, 600);
end;

constructor Simulation.New();
var
  Choice : String;
  ExtraX, ExtraY, ExtraHouseholds, Count : Integer;

begin
  Self.FuelCostPerUnit := 0.0098;
  Self.BaseCostForDelivery := 100;
  write('Enter D for default scenario, anything else for a custom settlement: ');
  readln(Choice);
  if Choice = 'D' then
    begin
      writeln('Default Simulation initiated');

      Self.SimulationSettlement := Settlement.New(); // Creates a normal size settlement
      initiateDefaultCompanies(); // Initiates default companies

      Exit; // Exits out of the procedure to ensure that none of the other options are asked
    end;

  write('Enter L for a large settlement, anything else for a normal size settlement: ');
  readln(Choice);
  if Choice = 'L' then
    begin
      write('Enter additional amount to add to X size of settlement: ');
      readln(ExtraX);
      write('Enter additional amount to add to Y size of settlement: ');
      readln(ExtraY);
      write('Enter additional number of households to add to settlement: ');
      readln(ExtraHouseholds);
      Self.SimulationSettlement := LargeSettlement.New(ExtraX, ExtraY, ExtraHouseholds);
    end
  else
    Self.SimulationSettlement := Settlement.New();
  write('Enter D for default companies, anything else to add your own start companies: ');
  readln(Choice);
  if Choice = 'D' then
    begin
      initiateDefaultCompanies(); // Initiates the default companies
    end
  else
    begin
      write('Enter number of companies that exist at the start of simulation: ');
      readln(Self.NoOfCompanies);
      for Count := 1 to Self.NoOfCompanies do
        AddCompany();
    end;
end;
  • 作者:Harry - Peter Symonds College


Java


public Simulation() {
        fuelCostPerUnit = 0.0098f;
        baseCostForDelivery = 100;
        String choice;
        Console.write("Enter L for a large settlement, anything else for a normal size settlement: ");
        choice = Console.readLine();
        if (choice.equals("L")) {
            int extrax, extray, extraHouseholds;
            Console.write("Enter additional amount to add to x size of settlement: ");
            extrax = Integer.parseInt(Console.readLine());
            Console.write("Enter additional amount to add to y size of settlement: ");
            extray = Integer.parseInt(Console.readLine());
            Console.write("Enter additional number of households to add to settlement: ");
            extraHouseholds = Integer.parseInt(Console.readLine());
            simulationSettlement = new LargeSettlement(extrax, extray, extraHouseholds);
        } else {
            simulationSettlement = new Settlement();            
        }
        Console.write("Enter D for default companies, anything else to add your own start companies: ");
        choice = Console.readLine();
        if (choice.equals("D")) {
            System.out.print("Do you want to load faster or slower? - faster or slower");
            String speedOption = Console.readLine();
            if(speedOption.equals("faster")){
                LoadFasterInDefaultScenario();
            } else{
                noOfCompanies = 3;
            Company company1 = new Company("AQA Burgers", "fast food", 100000, 200, 203, fuelCostPerUnit, baseCostForDelivery);
            companies.add(company1);
            companies.get(0).openOutlet(300, 987);
            companies.get(0).openOutlet(500, 500);
            companies.get(0).openOutlet(305, 303);
            companies.get(0).openOutlet(874, 456);
            companies.get(0).openOutlet(23, 408);
            companies.get(0).openOutlet(412, 318);
            Company company2 = new Company("Ben Thor Cuisine", "named chef", 100400, 390, 800, fuelCostPerUnit, baseCostForDelivery);
            companies.add(company2);
            Company company3 = new Company("Paltry Poultry", "fast food", 25000, 800, 390, fuelCostPerUnit, baseCostForDelivery);
            companies.add(company3);
            companies.get(2).openOutlet(400, 390);
            companies.get(2).openOutlet(820, 370);
            companies.get(2).openOutlet(800, 600);
            }
            
        } else {
            Console.write("Enter number of companies that exist at start of simulation: ");
            noOfCompanies = Integer.parseInt(Console.readLine());
            for (int count = 1; count <= noOfCompanies; count++) {
                addCompany();
            }
        }
    }

public void LoadFasterInDefaultScenario(){
        noOfCompanies = 3;
            Company company1 = new Company("AQA Burgers", "fast food", 100000, 200, 203, fuelCostPerUnit, baseCostForDelivery);
            companies.add(company1);
            companies.get(0).openOutlet(300, 987);
            companies.get(0).openOutlet(500, 500);
            companies.get(0).openOutlet(305, 303);
            companies.get(0).openOutlet(874, 456);
            companies.get(0).openOutlet(23, 408);
            companies.get(0).openOutlet(412, 318);
            Company company2 = new Company("Ben Thor Cuisine", "named chef", 100400, 390, 800, fuelCostPerUnit, baseCostForDelivery);
            companies.add(company2);
            Company company3 = new Company("Paltry Poultry", "fast food", 25000, 800, 390, fuelCostPerUnit, baseCostForDelivery);
            companies.add(company3);
            companies.get(2).openOutlet(400, 390);
            companies.get(2).openOutlet(820, 370);
            companies.get(2).openOutlet(800, 600);
}


Python

 Choice = input("Enter D for default scenarios, anything else for your own scenario.")
        Choice = Choice.upper()
        if Choice == "D":
            self._SimulationSettlement = Settlement()
            self._NoOfCompanies = 3
            Company1 = Company("AQA Burgers", "fast food", 100000, 200, 203, self._FuelCostPerUnit,
                               self._BaseCostforDelivery)
            self._Companies.append(Company1)
            self._Companies[0].OpenOutlet(300, 987)
            self._Companies[0].OpenOutlet(500, 500)
            self._Companies[0].OpenOutlet(305, 303)
            self._Companies[0].OpenOutlet(874, 456)
            self._Companies[0].OpenOutlet(23, 408)
            self._Companies[0].OpenOutlet(412, 318)
            Company2 = Company("Ben Thor Cuisine", "named chef", 100400, 390, 800, self._FuelCostPerUnit,
                               self._BaseCostforDelivery)
            self._Companies.append(Company2)
            Company3 = Company("Paltry Poultry", "fast food", 25000, 800, 390, self._FuelCostPerUnit,
                               self._BaseCostforDelivery)
            self._Companies.append(Company3)
            self._Companies[2].OpenOutlet(400, 390)
            self._Companies[2].OpenOutlet(820, 370)
            self._Companies[2].OpenOutlet(800, 600)
        else:
            Choice = input("Enter L for a large settlement, anything else for a normal size settlement: ")
            Choice = Choice.upper()
            if Choice == "L":
                while True:

                    ExtraX = input("Enter additional amount to add to X size of settlement: ")
                    ExtraY = input("Enter additional amount to add to Y size of settlement: ")
                    ExtraHouseholds = input("Enter additional number of households to add to settlement: ")
                    try:
                        ExtraX = int(ExtraX)
                        ExtraY = int(ExtraY)
                        ExtraHouseholds = int(ExtraHouseholds)
                        break
                    except ValueError:
                        print("Those are not a number try again")
                        continue


VB.NET

華夏公益教科書