C# 辅助关联方法调用
下面是我一直在做的一个练习代码,不是作业,它是为了合并关联。大体上我有C2.buyPhoneApple(P1);这将调用我的Customer类中的方法。回到我的main,我有一个列表,这个列表由添加到phone类的项目组成,我的问题是,我使用P1测试调用,它可以工作,但是现在我需要方法调用来反映列表中的项目,并在下订单后从库存中提取1。我已经尽可能地解释了这一点 除此之外,我还使用一个名为model的变量来存储迭代列表(0、1、2)中的用户选择。我使用该变量通过索引applePhones库来确认用户选择,请原谅冗长的解释 另外,我确实有更多的代码,但不觉得有必要发布所有的类C# 辅助关联方法调用,c#,C#,下面是我一直在做的一个练习代码,不是作业,它是为了合并关联。大体上我有C2.buyPhoneApple(P1);这将调用我的Customer类中的方法。回到我的main,我有一个列表,这个列表由添加到phone类的项目组成,我的问题是,我使用P1测试调用,它可以工作,但是现在我需要方法调用来反映列表中的项目,并在下订单后从库存中提取1。我已经尽可能地解释了这一点 除此之外,我还使用一个名为model的变量来存储迭代列表(0、1、2)中的用户选择。我使用该变量通过索引applePhones库来确认
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_Phone_Shop_Test
{
class Program
{
static int thanksApple(ref int thankYou)
{
Random text = new Random();
thankYou = text.Next(1, 4);
switch (thankYou)
{
case 1:
Console.WriteLine("\nThank you for choosing Apple!\n");
break;
case 2:
Console.WriteLine("\nCongratualation on purchasing your Apple product!\n");
break;
case 3:
Console.WriteLine("\nWelcome to Apple, you shall receieve your account information and billing through the mail.\n");
break;
case 4:
Console.WriteLine("\nCell-Sale, number 1 for Apple products and services!\n");
break;
}
return thankYou;
}
static void menu()
{
}
static void Main(string[] args)
{
menu();
int model = 0;
bool menuRepeat = true;
string confirm = "";
int choice = 0;
do
{
AppleLibrary appleLib = new AppleLibrary();
SamsungLibrary samsungLib = new SamsungLibrary();
Customer C1 = new Customer("Poseidon", 500, 309487, "The Sea, Greece"); //Instantiation with predefined values due to predefined constructor.
Customer C2 = new Customer("Zeus", 601, 345928, "Olympus, Greece");
Windows W1 = new Windows();
W1.brand = "Windows";
W1.model = "Nokia Lumia";
W1.stock = 5;
Samsung Sam2 = new Samsung();
Sam2.brand = "Samsung, Andriod";
Sam2.Model = "Samsung Galaxy note 3";
Sam2.stock = 5;
Sam2.price = 300;
Apple P1 = new Apple();
P1.brand = "Apple";
P1.model = "iPhone 5";
P1.stock = 5;
P1.price = 350;
appleLib.ApplePhones.Add(new Phone("Apple", "iPhone 5", 350, 1));//Adding to the library object with initialization (Predefined constructor)
appleLib.ApplePhones.Add(new Phone("Apple", "iPhone 4s", 250, 1));
appleLib.ApplePhones.Add(new Phone("Apple", "iPhone 3", 150, 0));
samsungLib.samsungPhones.Add(new Phone("Samsung", "Galaxy Note 1", 100, 7));
samsungLib.samsungPhones.Add(new Phone("Samsung", "Galaxy Note 2", 150, 2));
samsungLib.samsungPhones.Add(new Phone("Samsung", "Galaxy Note 3", 350, 4));
Console.BackgroundColor = ConsoleColor.DarkGreen;
/*
for (int j = 0; j < windowsPhoneArray.Length; j++)
{
Console.WriteLine("{0}", windowsPhoneArray[j].model);
}
* */
Console.WriteLine("\nCustomer name: {0}\nCustomer balance: {1}\n", C1.name, C1.balance);
Console.WriteLine("\n1)Apple\n\n2)Samsung\n\n3)Windows\n");
Console.Write("Please select a brand: ");
try
{
choice = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.WriteLine("\n\n***Please enter a number***\n\n");
}
if (choice == 1)
{
Console.WriteLine("\nPlease Select a model");
for (int i = 0; i < appleLib.ApplePhones.Count(); i++)
{
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.Write("\nSelection {0}) = Model: {1}\nPrice: {2}\nStock: {3}\n", i, appleLib.ApplePhones[i].model, appleLib.ApplePhones[i].price, appleLib.ApplePhones[i].stock);
}
Console.Write("\nPlease select your model: ");
try
{
model = Convert.ToInt32(Console.ReadLine()); //This variable to show/confirm the users list<phone> selection.
}
catch (FormatException)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.WriteLine("\nPlease enter a number\n");
}
Console.WriteLine("\nYou have selected: {0}\nPrice: {1}", appleLib.ApplePhones[model].Brand, appleLib.ApplePhones[model].price);
Console.BackgroundColor = ConsoleColor.Blue;
Console.Write("\nConfirm purchase Y/N: ");
confirm = Console.ReadLine();
if (confirm == "y" || confirm == "Y")
{
menuRepeat = false;
C2.BuyPhoneApple(P1);// I want this call to reflect the chosen element within the list<phone> and call buyPhoneApple and remove a phone from the stock according to the users selection.
}
else
{
Console.WriteLine("Returning to the main menu...standby");
}
}
if (choice == 2)
{
for (int x = 0; x < samsungLib.samsungPhones.Count(); x++)
{
Console.Write("\nSelection {0}) = Model: {1}\nStock: {2}\n", x, samsungLib.samsungPhones[x].model, samsungLib.samsungPhones[x].stock);
}
Console.Write("\nPlease select your model: ");
try
{
model = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.WriteLine("\nPlease enter a number\n");
}
Console.WriteLine("\nYou have selected: {0}\nPrice: {1}", samsungLib.samsungPhones[model].brand, samsungLib.samsungPhones[model].price);
Console.Write("\nConfirm purchase Y/N: ");
confirm = Console.ReadLine();
if (confirm == "y" || confirm == "Y")
{
menuRepeat = false;
C2.BuyPhoneApple(P1);
}
}
} while (menuRepeat == true);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_Phone_Shop_Test
{
class Apple : Phone
{
public Apple(string newBrand, string newModel, int newPrice, int newStock)
: base(newBrand, newModel, newPrice, newStock)
{
}
public Apple()
{
}
static int thanksApple(ref int thankYou)
{
Random text = new Random();
thankYou = text.Next(1, 4);
switch (thankYou)
{
case 1:
Console.WriteLine("\nThank you for choosing Apple!\n");
break;
case 2:
Console.WriteLine("\nCongratualation on purchasing your Apple product!\n");
break;
case 3:
Console.WriteLine("\nWelcome to Apple, you shall receieve your account information and billing through the mail.\n");
break;
case 4:
Console.WriteLine("\nCell-Sale, number 1 for Apple products and services!\n");
break;
}
return thankYou;
}
public override void OrderPhoneApple(Customer objCustomer)
{
int thankyouApple = 0;
stock--;
Console.WriteLine("\n\nSold!\n\nName: {0}\nAccount Number: {1}\nAddress: {2}", objCustomer.name, objCustomer.AccountNumber, objCustomer.Address);
thanksApple(ref thankyouApple);
}
public override void AddStock()
{
stock++;
Console.WriteLine("Single stock added!");
}
public override void AddMassStock(int MassStock)
{
stock = stock + MassStock;
Console.WriteLine("stock added!");
}
public override void RemoveStock()
{
stock--;
Console.WriteLine("Single stock removed!");
}
public override void RemoveMassStock(int RemoveMassStock)
{
stock = stock - RemoveMassStock;
Console.WriteLine("stock removed!");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_Phone_Shop_Test
{
class Customer
{
public string name;
public double balance;
public double accountNumber;
public string address;
public Customer(string newName, double newBalance, double newAccountNumber, string newAddress)
{
name = newName;
balance = newBalance;
accountNumber = newAccountNumber;
address = newAddress;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public double Balance
{
get
{
return balance;
}
set
{
balance = value;
}
}
public double AccountNumber
{
get
{
return accountNumber;
}
set
{
accountNumber = value;
}
}
public string Address
{
get
{
return address;
}
set
{
address = value;
}
}
public void BuyPhoneApple(Apple objApplePhone)
{
if (balance >= objApplePhone.price)
{
objApplePhone.OrderPhoneApple(this);
}
else
{
Console.WriteLine("Not enouogh funds");
}
}
public void BuyPhoneSamsung(Samsung objSamsungPhone)
{
if (balance >= objSamsungPhone.price)
{
objSamsungPhone.OrderPhoneSamsung(this);
}
else
{
Console.WriteLine("Not enough funds");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间OOP\u电话\u商店\u测试
{
班级计划
{
静态整数谢谢(参考整数谢谢)
{
随机文本=新随机();
thankYou=text.Next(1,4);
开关(谢谢)
{
案例1:
Console.WriteLine(“感谢您选择Apple!\n”);
打破
案例2:
Console.WriteLine(“\n购买苹果产品的计划!\n”);
打破
案例3:
Console.WriteLine(“\n欢迎来到苹果,您将通过邮件接收您的帐户信息和账单。\n”);
打破
案例4:
Console.WriteLine(\n苹果产品和服务的销售排名第一!\n);
打破
}
谢谢你;
}
静态无效菜单()
{
}
静态void Main(字符串[]参数)
{
菜单();
int模型=0;
bool menuRepeat=true;
字符串confirm=“”;
int-choice=0;
做
{
AppleLibrary appleLib=新的AppleLibrary();
SamsungLibrary samsungLib=新SamsungLibrary();
Customer C1=新客户(“Poseidon”,500309487,“TheSea,Greece”);//由于预定义构造函数,使用预定义值进行实例化。
客户C2=新客户(“宙斯”,601345928,“希腊奥林巴斯”);
Windows W1=新窗口();
W1.brand=“Windows”;
W1.model=“诺基亚Lumia”;
W1.存量=5;
三星Sam2=新三星();
Sam2.brand=“三星,Andriod”;
Sam2.Model=“三星Galaxy note 3”;
Sam2.stock=5;
Sam2.price=300;
苹果P1=新苹果();
P1.brand=“苹果”;
P1.model=“iPhone 5”;
P1.stock=5;
P1.价格=350;
appleLib.ApplePhones.Add(新手机(“苹果”、“iPhone 5”、350、1”);//通过初始化(预定义构造函数)添加到库对象
添加(新手机(“苹果”、“iPhone4S”、250、1));
添加(新手机(“苹果”、“iPhone3”、150、0));
samsungLib.samsungPhones.Add(新手机(“三星”,“银河注1”,100,7));
samsungLib.samsungPhones.Add(新手机(“三星”,“银河注2”,150,2));
samsungLib.samsungPhones.Add(新手机(“三星”,“银河注3”,350,4));
Console.BackgroundColor=ConsoleColor.DarkGreen;
/*
对于(int j=0;jpublic class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public double AccountNumber { get; set; }
public double AvaliableBalance { get; set; }
public void Buy(Phone phone)
{
if (phone == null)
return;
var phoneService = new PhoneOrderService();
if (phoneService.PurchasePhone(this, phone.Model))
{
Console.WriteLine("{0} phone purchased!", phone.Model);
}
}
namespace PhoneApp
{
public enum PhoneType : int
{
Apple,
Windows,
Samsung
}
public class Phone
{
public string Brand { get; set; }
public string Model { get; set; }
public double Price { get; set; }
public int QuantityInStock { get; set; }
public PhoneType PhoneType { get; set; }
}
}
namespace PhoneApp
{
public class PhoneOrderService
{
// I used a private static list here as a backup storeage for ease of
// implementation and simplicity of the example.
private static List<Phone> _itemsInStock = new List<Phone>();
public void AddToStock(Phone phone)
{
if(phone == null)
return;
_itemsInStock.Add(phone);
}
//Now add those methods in your apple model to this service.
public bool PurchasePhone(Customer customer, string model)
{
if (customer == null)
return false;
if (string.IsNullOrWhiteSpace(model))
return false;
var phone = _itemsInStock.FirstOrDefault(a => a.Model == model);
if (phone != null)
{
if (customer.AvaliableBalance < phone.Price)
return false;
customer.AvaliableBalance -= phone.Price;
phone.QuantityInStock -= 1;
AddToStock(phone);
}
// then you use the phone to do whatever you like
//after return true to the calling method to show that this action ucceeded.
return true;
}
}
}
namespace PhoneApp
{
class Program
{
static void Main(string[] args)
{
//initialize the PhoneOrderService
var phoneOrderService = new PhoneOrderService();
//initialize the phone model
var phone1 = new Phone
{
Model = "IPhone 5",
Price = 450,
QuantityInStock = 5,
Brand = "Brand new",
PhoneType = PhoneType.Apple
};
//initialize the phone model
var phone2 = new Phone
{
Model = "Nokia Lumia",
Price = 500,
QuantityInStock = 3,
Brand = "Brand new",
PhoneType = PhoneType.Windows
};
//initialize the phone model
var phone3 = new Phone
{
Model = "Galaxy 4 tab",
Price = 350,
QuantityInStock = 7,
Brand = "Brand new",
PhoneType = PhoneType.Samsung
};
//Add the phones to the phone order service
phoneOrderService.AddToStock(phone1);
phoneOrderService.AddToStock(phone2);
phoneOrderService.AddToStock(phone3);
//Initialize the customer model
var customer = new Customer
{
Name = "Mr Moon",
AvaliableBalance = 1000,
Address = "41 new street road!",
AccountNumber = 1234567889
};
customer.Buy(phone1);
Console.ReadLine();
}
}
}