public string CheckWeights() { for (int x = 0; x < COLUMNS; x++) { int weightSoFar = 0; string previousItemInitials = ""; for (int y = 0; y < ROWS; y++) { if (IsPositionValid(y, x)) { BackpackItem item = this.items[y, x]; if (weightSoFar == 0) { weightSoFar = item.weight; previousItemInitials = item.initials; } else { if (item.initials != previousItemInitials) { if (weightSoFar > item.weight) { return($"{item.name} ({item.initials}) was squashed!"); } weightSoFar += item.weight; previousItemInitials = item.initials; } } } } } return("Backpack is ok!"); }
public void AddItemAtPosition(BackpackItem itemToAdd, int y, int x) { if (IsPositionValid(y, x)) { this.items[y, x] = itemToAdd; } int n = 1; while (n < itemToAdd.verticalSize) { if (IsPositionValid(y - n, x)) { this.items[y - n, x] = itemToAdd; } n++; } }
public void PrintContent() { for (int y = 0; y < ROWS; y++) { for (int x = 0; x < COLUMNS; x++) { if (IsPositionValid(y, x)) { BackpackItem item = this.items[y, x]; // Console.Write(item.initials); Console.Write(item.initials.PadRight(2)); } else { Console.Write("".PadRight(2)); } } Console.WriteLine(); } }
static void Main(string[] args) { // - [C] Cuerda: 1Kg // - [A] Botella de agua: 1.1Kg // - [M] Manzana: 250g // - [P] Pan: 70g // - [B] Brújula: 300g -> 350g (*) // - [PB] Papel de baño: 320g // - [PL] Plátano: 80g Backpack backpack = new Backpack(); BackpackItem rope = new BackpackItem("C", "Rope", 1000); rope.SetVerticalSize(2); BackpackItem waterBottle = new BackpackItem("A", "Water bottle", 1100); waterBottle.SetVerticalSize(2); BackpackItem apple = new BackpackItem("M", "Apple", 250); BackpackItem bread = new BackpackItem("P", "Bread", 70); BackpackItem compass = new BackpackItem("B", "Compass", 350); BackpackItem toiletPaper = new BackpackItem("PB", "Toilet paper", 320); BackpackItem banana = new BackpackItem("PL", "Banana", 80); banana.SetVerticalSize(2); backpack.AddItemAtPosition(rope, 2, 0); backpack.AddItemAtPosition(bread, 0, 1); backpack.AddItemAtPosition(apple, 1, 1); backpack.AddItemAtPosition(compass, 2, 1); backpack.AddItemAtPosition(banana, 1, 2); backpack.AddItemAtPosition(toiletPaper, 2, 2); backpack.AddItemAtPosition(waterBottle, 2, 3); Console.WriteLine(backpack.CheckWeights()); backpack.PrintContent(); }