SlideShare a Scribd company logo
1 of 6
Download to read offline
This is my visual studio C# code, how do I make it so that when I click btnShow.click that it adds in
only one 'car' at a time with each click, so far it adds every car in the list to the listbox, i need to
make it so the show button only shows one, and the add button adds another car to it, i also need
to make it so the remove button removes the latest car.
public partial class Form1 : Form
{
Car car = new Car();
Driver driver = new Driver();
InventoryManager manager;
public Form1(InventoryManager invMan)
{
InitializeComponent();
manager = invMan;
}
private void btnShow_Click(object sender, EventArgs e)
{
Car car = new Car();
// Display all the cars in the inventory
manager.DisplayItems();
car.Make = "Challenger";
car.Model = "Dodge";
car.Year = 2019;
car.MilesPerGallon = 38;
car.HorsePower = 300;
car.CarPrice = 29000;
car.Quantity = 1;
foreach (Car vehicle in manager.inventory)
{
carTextBox.Text += "Car make is " + vehicle.Make + "rn"
+ "Car Model is: " + vehicle.Model + "rn"
+ "Car Year is: " + vehicle.Year + "rn"
+ "Car Miles Per Gallon is: " + vehicle.MilesPerGallon + "rn"
+ "Car Horse Power is: " + vehicle.HorsePower + "rn"
+ "Quantity of cars in stock is: " + vehicle.Quantity + "rn";
}
}
public void btnAdd_Click(object sender, EventArgs e)
{
manager.AddItem(car);
manager.DisplayItems();
}
private void removeItemButton_Click(object sender, EventArgs e)
{
manager.RemoveItem(car);
manager.DisplayItems();
}
internal class Driver
{
static void Main(string[] args)
{
// Create an inventory manager with a capacity of 10 items
InventoryManager manager = new InventoryManager(10);
Boolean windowOpen = true;
// Add some cars to the inventory
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);
}
// Search for cars by price
searchResults = manager.SearchItemByPrice(35000);
foreach (Car car in searchResults)
{
Console.WriteLine("Make: " + car.Make + ", Model: " + car.Model + ", Price: " + car.CarPrice);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(manager));
Console.ReadLine();
}
}
public class InventoryManager
{
//private Car[] inventory; Changed to make a list instead of Array
public List<Car> inventory;
public InventoryManager(int size)
{
//inventory = new Car[size]; Replacing for a list
inventory = new List<Car>(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<Car> result = new List<Car>();
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<Car> result = new List<Car>();
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; }
}
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; }
}

More Related Content

Similar to This is my visual studio C code how do I make it so that w.pdf

Software Engineer Screening Question - OOP
Software Engineer Screening Question - OOPSoftware Engineer Screening Question - OOP
Software Engineer Screening Question - OOP
jason_scorebig
 
#includeiostream #includecmath #includestringusing na.pdf
 #includeiostream #includecmath #includestringusing na.pdf #includeiostream #includecmath #includestringusing na.pdf
#includeiostream #includecmath #includestringusing na.pdf
anuradhaartjwellery
 
#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docx#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docx
mayank272369
 
Automobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdfAutomobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdf
anitasahani11
 
Scala Self Types by Gregor Heine, Principal Software Engineer at Gilt
Scala Self Types by Gregor Heine, Principal Software Engineer at GiltScala Self Types by Gregor Heine, Principal Software Engineer at Gilt
Scala Self Types by Gregor Heine, Principal Software Engineer at Gilt
Gilt Tech Talks
 
C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdf
ajantha11
 
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docxsrcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
rafbolet0
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
fasttracksunglass
 

Similar to This is my visual studio C code how do I make it so that w.pdf (15)

Software Engineer Screening Question - OOP
Software Engineer Screening Question - OOPSoftware Engineer Screening Question - OOP
Software Engineer Screening Question - OOP
 
#include iostream #include string #include fstream std.docx
#include iostream #include string #include fstream  std.docx#include iostream #include string #include fstream  std.docx
#include iostream #include string #include fstream std.docx
 
Oop in java script
Oop in java scriptOop in java script
Oop in java script
 
#includeiostream #includecmath #includestringusing na.pdf
 #includeiostream #includecmath #includestringusing na.pdf #includeiostream #includecmath #includestringusing na.pdf
#includeiostream #includecmath #includestringusing na.pdf
 
#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docx#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docx
 
Predicting model for prices of used cars
Predicting model for prices of used carsPredicting model for prices of used cars
Predicting model for prices of used cars
 
Classes and Objects In C# With Example
Classes and Objects In C# With ExampleClasses and Objects In C# With Example
Classes and Objects In C# With Example
 
Angular2: Quick overview with 2do app example
Angular2: Quick overview with 2do app exampleAngular2: Quick overview with 2do app example
Angular2: Quick overview with 2do app example
 
#include iostream #include string#includeiomanip using.docx
#include iostream #include string#includeiomanip using.docx#include iostream #include string#includeiomanip using.docx
#include iostream #include string#includeiomanip using.docx
 
Automobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdfAutomobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdf
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsPublic class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
 
Scala Self Types by Gregor Heine, Principal Software Engineer at Gilt
Scala Self Types by Gregor Heine, Principal Software Engineer at GiltScala Self Types by Gregor Heine, Principal Software Engineer at Gilt
Scala Self Types by Gregor Heine, Principal Software Engineer at Gilt
 
C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdf
 
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docxsrcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
srcCommissionCalculation.javasrcCommissionCalculation.javaimpo.docx
 
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf12.9 Program Online shopping cart (continued) (C++)This program e.pdf
12.9 Program Online shopping cart (continued) (C++)This program e.pdf
 

More from adinathfashion1

This problem is referred to as Exon chaining in bioinformati.pdf
This problem is referred to as Exon chaining in bioinformati.pdfThis problem is referred to as Exon chaining in bioinformati.pdf
This problem is referred to as Exon chaining in bioinformati.pdf
adinathfashion1
 
This project is broken up into Windows and Mac versions lis.pdf
This project is broken up into Windows and Mac versions lis.pdfThis project is broken up into Windows and Mac versions lis.pdf
This project is broken up into Windows and Mac versions lis.pdf
adinathfashion1
 
THis is from an Applied Mixed Models class Answer just part.pdf
THis is from an Applied Mixed Models class Answer just part.pdfTHis is from an Applied Mixed Models class Answer just part.pdf
THis is from an Applied Mixed Models class Answer just part.pdf
adinathfashion1
 
Title Media culture Objectives 1 To apply aseptic techniq.pdf
Title Media culture Objectives 1 To apply aseptic techniq.pdfTitle Media culture Objectives 1 To apply aseptic techniq.pdf
Title Media culture Objectives 1 To apply aseptic techniq.pdf
adinathfashion1
 
TitleInvestigating the effect of counterfeit permits on the.pdf
TitleInvestigating the effect of counterfeit permits on the.pdfTitleInvestigating the effect of counterfeit permits on the.pdf
TitleInvestigating the effect of counterfeit permits on the.pdf
adinathfashion1
 

More from adinathfashion1 (20)

This is based on lab activity in week 34 You will explore t.pdf
This is based on lab activity in week 34 You will explore t.pdfThis is based on lab activity in week 34 You will explore t.pdf
This is based on lab activity in week 34 You will explore t.pdf
 
This problem is referred to as Exon chaining in bioinformati.pdf
This problem is referred to as Exon chaining in bioinformati.pdfThis problem is referred to as Exon chaining in bioinformati.pdf
This problem is referred to as Exon chaining in bioinformati.pdf
 
This project is broken up into Windows and Mac versions lis.pdf
This project is broken up into Windows and Mac versions lis.pdfThis project is broken up into Windows and Mac versions lis.pdf
This project is broken up into Windows and Mac versions lis.pdf
 
Tim and Stephanie are devastated when they find out their ne.pdf
Tim and Stephanie are devastated when they find out their ne.pdfTim and Stephanie are devastated when they find out their ne.pdf
Tim and Stephanie are devastated when they find out their ne.pdf
 
THis is from an Applied Mixed Models class Answer just part.pdf
THis is from an Applied Mixed Models class Answer just part.pdfTHis is from an Applied Mixed Models class Answer just part.pdf
THis is from an Applied Mixed Models class Answer just part.pdf
 
This is not working for me at allAnd I cannot edit anythi.pdf
This is not working for me at allAnd I cannot edit anythi.pdfThis is not working for me at allAnd I cannot edit anythi.pdf
This is not working for me at allAnd I cannot edit anythi.pdf
 
This is false please explain Assuming that the reserve req.pdf
This is false please explain Assuming that the reserve req.pdfThis is false please explain Assuming that the reserve req.pdf
This is false please explain Assuming that the reserve req.pdf
 
This is NOT TRUE concerning courtship sounds Question 2 opt.pdf
This is NOT TRUE concerning courtship sounds Question 2 opt.pdfThis is NOT TRUE concerning courtship sounds Question 2 opt.pdf
This is NOT TRUE concerning courtship sounds Question 2 opt.pdf
 
This is what I got for 54 but Im unsure if its right Prepa.pdf
This is what I got for 54 but Im unsure if its right Prepa.pdfThis is what I got for 54 but Im unsure if its right Prepa.pdf
This is what I got for 54 but Im unsure if its right Prepa.pdf
 
This PNF technique starts with the same process as the the h.pdf
This PNF technique starts with the same process as the the h.pdfThis PNF technique starts with the same process as the the h.pdf
This PNF technique starts with the same process as the the h.pdf
 
This is where you set up the project infrastructure to help .pdf
This is where you set up the project infrastructure to help .pdfThis is where you set up the project infrastructure to help .pdf
This is where you set up the project infrastructure to help .pdf
 
This is a true case study A woman kept coming to the doctor.pdf
This is a true case study A woman kept coming to the doctor.pdfThis is a true case study A woman kept coming to the doctor.pdf
This is a true case study A woman kept coming to the doctor.pdf
 
To answer Questions 14 and 15 read the article Britain.pdf
To answer Questions 14 and 15 read the article Britain.pdfTo answer Questions 14 and 15 read the article Britain.pdf
To answer Questions 14 and 15 read the article Britain.pdf
 
To adhere to Endangered Species Act ESA guidelines the Fi.pdf
To adhere to Endangered Species Act ESA guidelines the Fi.pdfTo adhere to Endangered Species Act ESA guidelines the Fi.pdf
To adhere to Endangered Species Act ESA guidelines the Fi.pdf
 
Title Media culture Objectives 1 To apply aseptic techniq.pdf
Title Media culture Objectives 1 To apply aseptic techniq.pdfTitle Media culture Objectives 1 To apply aseptic techniq.pdf
Title Media culture Objectives 1 To apply aseptic techniq.pdf
 
TitleInvestigating the effect of counterfeit permits on the.pdf
TitleInvestigating the effect of counterfeit permits on the.pdfTitleInvestigating the effect of counterfeit permits on the.pdf
TitleInvestigating the effect of counterfeit permits on the.pdf
 
Tketici talebindeki kk deiiklikler aadaki nedenlerle ve.pdf
Tketici talebindeki kk deiiklikler aadaki nedenlerle ve.pdfTketici talebindeki kk deiiklikler aadaki nedenlerle ve.pdf
Tketici talebindeki kk deiiklikler aadaki nedenlerle ve.pdf
 
Titanic Project Management Blunders Links to an external s.pdf
Titanic  Project Management Blunders Links to an external s.pdfTitanic  Project Management Blunders Links to an external s.pdf
Titanic Project Management Blunders Links to an external s.pdf
 
This is my table met Mry anlea Nole isch studein las diru ev.pdf
This is my table met Mry anlea Nole isch studein las diru ev.pdfThis is my table met Mry anlea Nole isch studein las diru ev.pdf
This is my table met Mry anlea Nole isch studein las diru ev.pdf
 
TITLE MAKE A PROJECT ON THE WORKING OF PARLIAMENTARY FORM O.pdf
TITLE MAKE A PROJECT ON THE WORKING OF PARLIAMENTARY FORM O.pdfTITLE MAKE A PROJECT ON THE WORKING OF PARLIAMENTARY FORM O.pdf
TITLE MAKE A PROJECT ON THE WORKING OF PARLIAMENTARY FORM O.pdf
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

This is my visual studio C code how do I make it so that w.pdf

  • 1. This is my visual studio C# code, how do I make it so that when I click btnShow.click that it adds in only one 'car' at a time with each click, so far it adds every car in the list to the listbox, i need to make it so the show button only shows one, and the add button adds another car to it, i also need to make it so the remove button removes the latest car. public partial class Form1 : Form { Car car = new Car(); Driver driver = new Driver(); InventoryManager manager; public Form1(InventoryManager invMan) { InitializeComponent(); manager = invMan; } private void btnShow_Click(object sender, EventArgs e) { Car car = new Car(); // Display all the cars in the inventory manager.DisplayItems(); car.Make = "Challenger"; car.Model = "Dodge"; car.Year = 2019; car.MilesPerGallon = 38; car.HorsePower = 300; car.CarPrice = 29000; car.Quantity = 1; foreach (Car vehicle in manager.inventory) { carTextBox.Text += "Car make is " + vehicle.Make + "rn" + "Car Model is: " + vehicle.Model + "rn" + "Car Year is: " + vehicle.Year + "rn" + "Car Miles Per Gallon is: " + vehicle.MilesPerGallon + "rn" + "Car Horse Power is: " + vehicle.HorsePower + "rn" + "Quantity of cars in stock is: " + vehicle.Quantity + "rn"; } } public void btnAdd_Click(object sender, EventArgs e) { manager.AddItem(car);
  • 2. manager.DisplayItems(); } private void removeItemButton_Click(object sender, EventArgs e) { manager.RemoveItem(car); manager.DisplayItems(); } internal class Driver { static void Main(string[] args) { // Create an inventory manager with a capacity of 10 items InventoryManager manager = new InventoryManager(10); Boolean windowOpen = true; // Add some cars to the inventory 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,
  • 3. 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); } // Search for cars by price
  • 4. searchResults = manager.SearchItemByPrice(35000); foreach (Car car in searchResults) { Console.WriteLine("Make: " + car.Make + ", Model: " + car.Model + ", Price: " + car.CarPrice); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(manager)); Console.ReadLine(); } } public class InventoryManager { //private Car[] inventory; Changed to make a list instead of Array public List<Car> inventory; public InventoryManager(int size) { //inventory = new Car[size]; Replacing for a list inventory = new List<Car>(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() {
  • 5. 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<Car> result = new List<Car>(); 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<Car> result = new List<Car>(); 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; }
  • 6. public double CarPrice { get; set; } public int Quantity { get; internal set; } } 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; } }