Using C#: Create a program for Potsie\'s Pottery named FelxibleOrder that accept
ID: 3683238 • Letter: U
Question
Using C#: Create a program for Potsie's Pottery named FelxibleOrder that accept's a user's choice from the options in the accompanying table. Allow the user to enter either an integer number or a string description. Pass the user's entry to one of two overloaded GetDetails() methods and then display the returned string. The method version that accepts an integer looks up the description and price and returns a string with all the order details. The fersion that accepts a string description looks up the item number and price and returns a string with all the order details. The methods return an appropriate message if the item is not found.
Explanation / Answer
using System;
using System.Text;
using System.Collections.Generic;
public class FlexibleOrder {
private IList<Item> items = new List<Item>(0);
public static void Main() {
FlexibleOrder order = new FlexibleOrder();
Item item = order.Search();
if (item != null) {
Console.WriteLine(item);
} else {
Console.WriteLine("item not found");
}
}
public FlexibleOrder() {
items = BuildItem();
}
public Item Search() {
Item item = null;
Console.WriteLine("Enter item number or description? ");
string input = Console.ReadLine();
try {
int itemNum = Convert.ToInt32(input);
item = GetDetails(itemNum);
} catch {
item = GetDetails(input);
}
return item;
}
public Item GetDetails(string description) {
Item item = null;
foreach (Item i in this.items) {
if (i.Description.Equals(description)) {
item = i;
break;
}
}
return item;
}
public Item GetDetails(int itemNum) {
Item item = null;
foreach (Item i in this.items) {
if (i.ItemNum == itemNum) {
item = i;
break;
}
}
return item;
}
private static IList<Item> BuildItem() {
IList<Item> items = new List<Item>(0);
items.Add(new Item {ItemNum = 1, Description = "D1", Price = 1.0m});
items.Add(new Item {ItemNum = 2, Description = "D2", Price = 2.0m});
return items;
}
}
public class Item {
public int ItemNum { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("Item Num: {0},", this.ItemNum));
sb.Append(string.Format(" Description: {0},", this.Description));
sb.Append(string.Format(" Price: {0:c}", this.Price));
return sb.ToString();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.