我正在尝试使用数据结构制作一个简单的食品配送系统。我将邻居名称保存在 ArrayList 中,并将交付计数、食品名称及其计数保存在 GenericList 中。我画了示意图并附上了照片。
我编写了一个打印“引擎盖名称及其交付数量”的程序,我的代码和我的输出在这里:
using System;
using System.Collections;
using System.Collections.Generic;
namespace temp
{
internal class delivery
{
public string food;
public int count;
}
internal class Hood
{
public string Name;
public int Number;
}
class programm
{
static void Main(string[] args)
{
string[] HoodName = { "Cherryhood", "NewCastle", "Greenlight", "Summerlin", "Westcity", "Paradise", "Legions", "Flamingos" };
int[] TeslimatSayisi = { 4, 2, 7, 2, 7, 3, 0, 1 };
ArrayList arrayList = new ArrayList();
int counter = 0;
List<Hood> genericList;
Hood ClassExample;
for (int i = 0; i < HoodName.Length;)
{
genericList = new List<Hood>();
int elementCount = (int)Math.Pow(2, counter);
for (int j = 0; j < elementCount; j++)
{
ClassExample = new Hood();
ClassExample.Name = HoodName[i];
ClassExample.Number = TeslimatSayisi[i];
genericList.Add(ClassExample);
i++;
if (i == HoodName.Length) break;
}
arrayList.Add(genericList);
counter++;
}
int counter2 = 0;
foreach (List<Hood> temp in arrayList)
{
foreach (Hood temp2 in temp)
Console.WriteLine("Hood: " + temp2.Name +" | "+" Delivery Count: " + temp2.Number);
}
我的输出是:
Hood: Cherryhood | Delivery Count: 4
Hood: NewCastle | Delivery Count: 2
Hood: Greenlight | Delivery Count: 7
Hood: Summerlin | Delivery Count: 2
Hood: Westcity | Delivery Count: 7
Hood: Paradise | Delivery Count: 3
Hood: Legions | Delivery Count: 0
Hood: Flamingos | Delivery Count: 1
我怎样才能得到这样的输出:
Hood: Cherryhood | Delivery Count: 4 | Food's, count: Salat:2, Taco:5, Pizza:1, Burger:2
Hood: NewCastle | Delivery Count: 2 | Food's, count: Pasta:15, Cake,7
Hood: Greenlight | Delivery Count: 7 | Food's, count: ................
Hood: Summerlin | Delivery Count: 2 | ..........
Hood: Westcity | Delivery Count: 7 |...........
Hood: Paradise | Delivery Count: 3 |.................
Hood: Legions | Delivery Count: 0 |...........
Hood: Flamingos | Delivery Count: 1 |.....................
我猜我必须像这样制作一份食物清单和计数清单:
foods = {pizza, taco, burger, salad, pasta, cake..........}
count = {1, 5, 2, 2, 15 ,7...........}
我需要创建配送类(包含餐食名称、数量字段)。然后我必须用相关邻域中的 Delivery 对象的数量填充 ArrayList 中的每个通用列表。我可以创建一个食物列表并从中随机选择食物信息。对于这个项目,我们可以假设每次交付只能包含一种类型的餐食(包括多少)。
我对 C# 和数据结构很陌生,非常感谢您的帮助。
我会使用
List
对象来定义你的数据结构。然后用LINQ来查询、操作等。我认为它会更灵活,让你更接近你想要的。像这样的东西:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var status = new List<Neighbourhood>{
new Neighbourhood{
Name = "Cherryhood",
Id = 1,
Orders = new List<Order>{
new Order{
Id = 300,
OrderItems = new List<string>{
"Salad",
"Taco",
"Pizza"
}
},
new Order{
Id = 301,
OrderItems = new List<string>{
"Cake",
"Taco",
"Pasta",
"Burger"
}
},
new Order{
Id = 302,
OrderItems = new List<string>{
"Salad",
"Pasta"
}
}
}
},
new Neighbourhood{
Name = "Newcastle",
Id = 2,
Orders = new List<Order>{
new Order{
Id = 400,
OrderItems = new List<string>{
"Salad",
"Taco",
"Pizza"
}
},
new Order{
Id = 401,
OrderItems = new List<string>{
"Cake",
"Taco",
"Pasta"
}
},
new Order{
Id = 402,
OrderItems = new List<string>{
"Salad",
"Pasta"
}
}
}
}
};
Console.WriteLine($"Neighbourhoods: {status.Count}");
foreach(var neighbourhood in status){
Console.WriteLine($"Neighbourhood: {neighbourhood.Name}");
Console.WriteLine($"Total Orders: {neighbourhood.Orders.Count}");
var allOrderItems = neighbourhood.Orders.SelectMany(i => i.OrderItems).ToList();
Console.WriteLine($"Total Ordered Items: {allOrderItems.Count}");
var groupedOrderItems = allOrderItems
.GroupBy(i=>i)
.Select(i => new {
Name = i.Key,
Total = i.Count()
})
.OrderBy(i => i.Name)
.ToList();
foreach(var groupedOrderItem in groupedOrderItems){
Console.WriteLine($"Order Item: {groupedOrderItem.Name} ({groupedOrderItem.Total})");
}
}
}
}
public class Neighbourhood{
public string Name {get;set;}
public int Id {get;set;}
public List<Order> Orders;
public Neighbourhood(){
Orders = new List<Order>();
}
}
public class Order{
public int Id {get;set;}
public List<string> OrderItems {get;set;}
public Order(){
OrderItems = new List<string>();
}
}
参见:https://dotnetfiddle.net/kN103N
输出:
Neighbourhoods: 2
Neighbourhood: Cherryhood
Total Orders: 3
Total Ordered Items: 9
Order Item: Burger (1)
Order Item: Cake (1)
Order Item: Pasta (2)
Order Item: Pizza (1)
Order Item: Salad (2)
Order Item: Taco (2)
Neighbourhood: Newcastle
Total Orders: 3
Total Ordered Items: 8
Order Item: Cake (1)
Order Item: Pasta (2)
Order Item: Pizza (1)
Order Item: Salad (2)
Order Item: Taco (2)
我注意到的第一件事是 hood 类没有交付列表,所以我添加了一个。其次,我从主方法中提取了方法及其职责。这就是结果:
public static void Main()
{
initDictionary();
printOutDeliveries();
}
static void printOutDeliveries()
{
foreach (var hood in hoods)
{
string foodNumbers="";
foreach (var del in hood.Value.Deliveries)
{
foodNumbers += del.food + ": " + del.count + ", ";
}
Console.WriteLine("Hood: " + hood.Key + " | Deliver Count: " + hood.Value.Number + " Foods count: "+foodNumbers);
}
}
static Dictionary<string, Hood> hoods = new Dictionary<string, Hood>();
static void initDictionary()
{
string[] HoodName = { "Cherryhood", "NewCastle", "Greenlight", "Summerlin", "Westcity", "Paradise", "Legions", "Flamingos" };
int[] TeslimatSayisi = { 4, 2, 7, 2, 7, 3, 0, 1 };
int i = 0;
foreach (var name in HoodName)
{
hoods.Add(name, new Hood()
{
Name = name,
Number = TeslimatSayisi[i++],
Deliveries = GetRandomDeliveries()
});
}
}
static List<delivery> GetRandomDeliveries()
{
List<string> foods = new List<string> { "Salat", "Taco", "Pizza", "Burger", "Pasta", "Cake" };
List<delivery> deliveries = new List<delivery>();
var randMaxVal = foods.Count - 1;
var random = new Random();
var forlooplength = random.Next(randMaxVal);
for (int i = 0; i < forlooplength; i++)
{
var rand = random.Next(randMaxVal);
deliveries.Add(new delivery()
{
food = foods[rand]
});
foods.RemoveAt(rand);
}
return deliveries;
}
internal class delivery
{
public string food;
public int count => new Random().Next(10);
}
internal class Hood
{
public string Name;
public int Number;
public List<delivery> Deliveries;
}
显示食品种类和各自的数量,以及社区名称以及送货数量。这是通过嵌套的 foreach 循环完成的,该循环将有助于遍历 ArrayList 以及嵌套的 List 对象。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace Temp
{
internal class Delivery
{
public string food;
public int count;
}
internal class Hood
{
public string Name;
public int Number;
public List<Delivery> Deliveries = new List<Delivery>();
}
class programm
{
static void Main(string[] args)
{
string[] HoodName = { "Cherryhood", "NewCastle", "Greenlight", "Summerlin", "Westcity", "Paradise", "Legions", "Flamingos" };
int[] TeslimatSayisi = { 4, 2, 7, 2, 7, 3, 0, 1 };
string[] foods = { "Pizza", "Taco", "Burger", "Salad", "Pasta", "Cake", "Chicken Nuggets", "French Fries", "Hot Dog", "Chicken Sandwich", "Burrito", "Fried Chicken", "Onion Rings", "Fish and Chips", "Mozzarella Sticks", "BBQ Ribs", "Grilled Cheese Sandwich", "Philly Cheesesteak", "Buffalo Wings", "Gyro", "Loaded Nachos", "Quesadilla", "Bacon Cheeseburger", "Meatball Sub", "Veggie Burger", "Donut" };
int[] count = { 1, 5, 2, 2, 15, 7, 11, 8, 1, 5, 2, 2, 15, 7, 11, 8, 1, 5, 2, 2, 15, 7, 11, 8, 6, 7 };
ArrayList arrayList = new ArrayList();
int counter = 0;
int counter2 = 0;
List<Hood> genericList;
Hood ClassExample;
for (int i = 0; i < HoodName.Length;)
{
genericList = new List<Hood>();
int elementCount = (int)Math.Pow(2, counter);
for (int j = 0; j < elementCount; j++)
{
ClassExample = new Hood();
ClassExample.Name = HoodName[i];
ClassExample.Number = TeslimatSayisi[i];
for (int k = 0; k < ClassExample.Number; k++)
{
ClassExample.Deliveries.Add(new Delivery { count = count[counter2], food = foods[counter2] });
counter2++;
}
genericList.Add(ClassExample);
i++;
if (i == HoodName.Length) break;
}
arrayList.Add(genericList);
counter++;
}
foreach (List<Hood> temp in arrayList)
{
foreach (Hood temp2 in temp)
{
Console.Write("Hood: " + temp2.Name + " | " + " Delivery Count: " + temp2.Number + " | Food's, count:");
for (int i = 0; i < temp2.Deliveries.Count; i++)
{
Console.Write(temp2.Deliveries[i].food + ":" + temp2.Deliveries[i].count);
if (i < temp2.Deliveries.Count - 1)
{
Console.Write(", ");
}
}
Console.WriteLine("");
}
}
}
}
}
输出:
Hood: Cherryhood | Delivery Count: 4 | Food's, count:Pizza:1, Taco:5, Burger:2, Salad:2
Hood: NewCastle | Delivery Count: 2 | Food's, count:Pasta:15, Cake:7
Hood: Greenlight | Delivery Count: 7 | Food's, count:Chicken Nuggets:11, French Fries:8, Hot Dog:1, Chicken Sandwich:5, Burrito:2, Fried Chicken:2, Onion Rings:15
Hood: Summerlin | Delivery Count: 2 | Food's, count:Fish and Chips:7, Mozzarella Sticks:11
Hood: Westcity | Delivery Count: 7 | Food's, count:BBQ Ribs:8, Grilled Cheese Sandwich:1, Philly Cheesesteak:5, Buffalo Wings:2, Gyro:2, Loaded Nachos:15, Quesadilla:7
Hood: Paradise | Delivery Count: 3 | Food's, count:Bacon Cheeseburger:11, Meatball Sub:8, Veggie Burger:6
Hood: Legions | Delivery Count: 0 | Food's, count:
Hood: Flamingos | Delivery Count: 1 | Food's, count:Donut:7