跳到内容

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

华夏公益教科书