182k views
0 votes
I need the search price button to sort the textbox items by the price, and search model button to search by model in alphabetical order.

public partial class Form1 : Form
{
Car car = new Car();
Driver driver = new Driver();
InventoryManager manager;
int latestCarIndex = -1;
public Form1(InventoryManager invMan)
{
InitializeComponent();
manager = invMan;
}
private void btnShow_Click(object sender, EventArgs e)
{
carTextBox.Clear();
foreach (Car car in manager.inventory)
{
carTextBox.Text += ""Car make is "" + car.Make + ""\r\\"" + ""Car Model is: "" + car.Model + ""\r\\""+ ""Car Year is: "" + car.Year + ""\r\\""+ ""Car Miles Per Gallon is: "" + car.MilesPerGallon + ""\r\\""+ ""Car Horse Power is: "" + car.HorsePower + ""\r\\""+ ""Quantity of cars in stock is: "" + car.Quantity + ""\r\\\r\\"";
}
}
int numAddedCars = 0;
int numRemovedCars = 0;
public void btnAdd_Click(object sender, EventArgs e)
{
// Create a new instance of the Car class with the desired attributes
Car newCar = new Car()
{
Make = ""Chevy"",
Model = ""Corvette"",
Year = 2022,
MilesPerGallon = 30,
HorsePower = 500,
Quantity = 1
};
manager.AddItem(newCar);
latestCarIndex = manager.inventory.Count - 1;
btnShow_Click(sender, e);
}
private void removeItemButton_Click(object sender, EventArgs e)
{
if (manager.inventory.Count > 0 && latestCarIndex >= 0)
{
Car latestCar = manager.inventory[latestCarIndex];
if (latestCar.Quantity > 1)
{
latestCar.Quantity--;
}
else
{
manager.RemoveItem(latestCar);
latestCarIndex--;
}
btnShow_Click(sender, e);
}
}
private void restockItemButton_Click(object sender, EventArgs e)
{
foreach (Car car in manager.inventory)
{
car.Quantity = 5;
}
btnShow_Click(sender, e);
}
private void searchPriceButton_Click(object sender, EventArgs e)
{
}
private void searchModelButton_Click(object sender, EventArgs e)
{
}
}
internal class Driver
{
static void Main(string[] args)
{
InventoryManager manager = new InventoryManager(10);
manager.AddItem(new Car()
{
Make = ""Ford"",
Model = ""Mustang"",
Year = 2020,
MilesPerGallon = 22,
HorsePower = 450,
CarPrice = 35000,
Quantity = 5
});
manager.AddItem(new Car()
{
Make = ""Chevrolet"",
Model = ""Camaro"",
Year = 2019,
MilesPerGallon = 20,
HorsePower = 455,
CarPrice = 32000,
Quantity = 3
});
manager.AddItem(new Car()
{
Make = ""Dodge"",
Model = ""Charger"",
Year = 2018,
MilesPerGallon = 19,
HorsePower = 292,
CarPrice = 28000,
Quantity = 2
});
// Display all the cars in the inventory
manager.DisplayItems();
// Remove the Dodge Charger from the inventory
Car dodgeCharger = new Car()
{
Make = ""Dodge"",
Model = ""Charger"",
Year = 2018,
MilesPerGallon = 19,
HorsePower = 292,
CarPrice = 28000,
Quantity = 2
};
manager.RemoveItem(dodgeCharger);
// Display all the cars in the inventory again
manager.DisplayItems();
// Restock the Ford Mustang by 2 units
Car fordMustang = new Car()
{
Make = ""Ford"",
Model = ""Mustang"",
Year = 2020,
MilesPerGallon = 22,
HorsePower = 450,
CarPrice = 35000,
Quantity = 5
};
manager.RestockItem(fordMustang, 2);
// Display all the cars in the inventory again
manager.DisplayItems();
// Search for cars by name
Car[] searchResults = manager.SearchItemByName(""Mustang"");
foreach (Car car in searchResults)
{
Console.WriteLine(""Make: "" + car.Make + "", Model: "" + car.Model);
}
searchResults = manager.SearchItemByPrice(35000);
foreach (Car car in searchResults)
{
Console.WriteLine(""Make: "" + car.Make + "", Model: "" + car.Model + "", Price: "" + car.CarPrice);
}
}
public class InventoryManager
{
public List inventory;
public InventoryManager(int size)
{
//inventory = new Car[size]; Replacing for a list
inventory = new List(size);
}
public void AddItem(Car item)
{
inventory.Add(item);
}
public void RemoveItem(Car item)
{
inventory.Remove(item);
}
public void RestockItem(Car item, int quantity)
{
Car existingItem = inventory.Find(i => i.Equals(item));
if (existingItem != null)
{
existingItem.Quantity += quantity;
}
}
public void DisplayItems()
{
foreach (Car item in inventory)
{
Console.WriteLine(""Make: "" + item.Make +
"", Model: "" + item.Model + "", Year: ""
+ item.Year + "", MilesPerGallon: "" +
item.MilesPerGallon + "", HorsePower: ""
+ item.HorsePower);
}
}
public Car[] SearchItemByName(string name)
{
List result = new List();
foreach (Car item in inventory)
{
if (item != null && item.Make.ToLower().Contains(name.ToLower()))
{
result.Add(item);
}
}
return result.ToArray();
}
public Car[] SearchItemByPrice(int CarPrice)
{
List result = new List();
foreach (Car item in inventory)
{
if (item != null && item.CarPrice == CarPrice)
{
result.Add(item);
}
}
return result.ToArray();
}
}
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int MilesPerGallon { get; set; }
public int HorsePower { get; set; }
public double CarPrice { get; set; }
public int Quantity { get; internal set; }
}

User Luikore
by
8.2k points

1 Answer

2 votes

Final answer:

The student is asking how to implement a sorting feature in a Windows Form application using C#. Specifically, they want the search price button to sort the textbox items by the price, and the search model button to search by model in alphabetical order. To implement the sorting functionality, you can use the OrderBy method provided by LINQ. For example, to sort the cars by price, you can use var sortedCars = manager.inventory.OrderBy(car => car.CarPrice); Similarly, to search for cars by model in alphabetical order, you can use var searchResults = manager.inventory.Where(car => car.Model.Contains(model)).OrderBy(car => car.Model); Once you have the sorted or searched results, you can update the textbox with the items in the desired order by iterating through the results and appending the relevant information to the textbox using the Text property.

Step-by-step explanation:

The subject of this question is Computers and Technology. The student is asking how to implement a sorting feature in a Windows Form application using C#. Specifically, they want the search price button to sort the textbox items by the price, and the search model button to search by model in alphabetical order.

To implement the sorting functionality, you can use the OrderBy method provided by LINQ. For example, to sort the cars by price, you can use var sortedCars = manager.inventory.OrderBy(car => car.CarPrice); Similarly, to search for cars by model in alphabetical order, you can use var searchResults = manager.inventory.Where(car => car.Model.Contains(model)).OrderBy(car => car.Model);

Once you have the sorted or searched results, you can update the textbox with the items in the desired order by iterating through the results and appending the relevant information to the textbox using the Text property.

User Konstantin Zadiran
by
8.0k points