我目前正在从事下面列出的项目。到目前为止,我无法从程序访问我的课程。我不确定这是我遗失的东西还是只是在脑海中。我将包括到目前为止提出的代码。
您的评分依据:
威基基希尔顿海滩租赁申请:
您将为Waikiki Hilton Beach Rentals创建一个程序。希尔顿酒店的客人可以在这里租用沙滩设备,例如浮木筏,浮潜用具,椅子,雨伞和明轮船。
您的申请将获得客人的姓名,所租物品,所租物品的数量以及所租总时间。客人可以租用任意数量的物品,也可以租用多个物品。您可以假定所有项目都在相同的时间范围内租用。因此,客人可以租用2张椅子和浮潜装备-但全部需要65分钟。 (例如,不是15的椅子,而是45的浮潜装备)
您将创建三个类,BeachRentalsApp将是程序的入口点,Rental类将用于创建租用对象,guest类将保存来宾信息。
BeachRentalsApp –程序开始的位置。这应该创建一个来宾对象。您的来宾对象包含来宾名称,合同号和一系列Rental对象。该应用程序应显示一条欢迎消息,然后获取访客信息以及租赁信息。应该在BeachRentalsApp类中的方法中调用main()方法中显示和收集的所有信息。
包含以下方法:
客人-包含客人信息的课程
属性:
适当的构造函数:至少需要一个来宾名称才能实例化来宾对象。
方法:
出租-描述出租的课程
适当的构造函数。此类的用户至少需要一个租赁项才能实例化对象。
静态成员:
属性:
方法
代码:
namespace BeachRentalsApp
{
public class Program
{
public static void Main()
{
Welcome();
string[] RentalList = { "1. Floatation Rafts", "2. Snorkel Gear", "3. Chairs", "4. Umbrellas", "5. Paddle Boat" };
int LenOfArray = RentalList.Length;
for (int i = 0; i < LenOfArray; i++)
{
Console.WriteLine(RentalList[i]);
}
}
public static void Welcome()
{
Console.WriteLine("******************************************");
Console.WriteLine("Welcome to Waikiki Hilton Beach Rentals!");
Console.WriteLine("******************************************");
Console.WriteLine(" ");
Console.WriteLine("You will be asked to enter your name and ");
Console.WriteLine("pick the equipment you would like to rent.");
Console.WriteLine("We hope that you enjoy your stay at Waikiki");
Console.WriteLine("and look forward to serving you again soon!");
Console.WriteLine(" ");
Console.WriteLine("Enter your name to start renting Equipment: ");
string GName = Console.ReadLine();
Console.Clear();
Console.WriteLine("Thank you {0}! Please take a look at equipment selection!", GName);
}
}
class Rentals
{
public int FloatationPrice = 15;
public int SnorkelPrice = 25;
public int ChairPrice = 8;
public int UmbrellaPrice = 10;
public int PaddlePrice = 40;
public int RentTime = 65;
public Rentals()
{
string[] RentalSelection = new string[5];
RentalSelection[0] = "";
RentalSelection[1] = "";
RentalSelection[2] = "";
RentalSelection[3] = "";
RentalSelection[4] = "";
Console.Write("Which equipment would you like to rent?");
RentalSelection[0] = Console.ReadLine();
Console.Write("Would you like to rent more items?");
}
}
}
答案 0 :(得分:1)
这是您可以执行的操作:
代码:
using System;
using System.Collections.Generic;
namespace BeachRentalsApp
{
internal static class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Welcome!");
string name;
while (true)
{
Console.WriteLine("Enter your name:");
name = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(name))
break;
}
Console.WriteLine("Enter your contract number or press Enter to generate one:");
var contract = Console.ReadLine();
if (string.IsNullOrWhiteSpace(contract))
{
contract = $"{name[0]}{new Random().Next(1000, 10000)}";
Console.WriteLine($"Generated the following contract number: {contract}");
}
var minutes = GetNumber("For how long? (minutes, between 1 and 8 hours):", 60, 60 * 8);
Console.WriteLine("We have the following items available:");
var items = Rentals.Items;
for (var i = 0; i < items.Length; i++)
{
var item = items[i];
Console.WriteLine($"{i + 1}. {item.Name} ({item.Price:C})");
}
var dictionary = new Dictionary<Item, Rental>();
while (true)
{
var index = GetNumber("Choose an item or 0 to complete your order:", 0, items.Length);
if (index == 0)
break;
var quantity = GetNumber("Enter quantity:", 1, 9999);
var item = items[index - 1];
if (dictionary.ContainsKey(item))
{
dictionary[item].Duration = minutes;
dictionary[item].Quantity += quantity;
}
else
{
var rental = new Rental
{
Duration = minutes,
Quantity = quantity
};
dictionary.Add(item, rental);
}
}
Console.WriteLine();
var total = 0.0m;
Console.WriteLine("Here's your bill :)");
Console.WriteLine();
foreach (var pair in dictionary)
{
var itemName = pair.Key.Name;
var itemQuantity = pair.Value.Quantity;
var itemDuration = pair.Value.Duration;
var itemPrice = pair.Key.Price;
var itemTotal = Math.Ceiling(itemDuration / 60.0m) * itemPrice * itemQuantity;
Console.WriteLine($"{itemName}:");
Console.WriteLine($"\tQuantity = {itemQuantity}");
Console.WriteLine($"\tDuration = {itemDuration}");
Console.WriteLine($"\tTotal = {itemTotal:C}");
Console.WriteLine();
total += itemTotal;
}
Console.WriteLine($"Grand total: {total:C}");
Console.WriteLine();
Console.WriteLine("We hope to see you again!");
Console.WriteLine();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
private static int GetNumber(string message, int min, int max)
{
if (message == null)
throw new ArgumentNullException(nameof(message));
while (true)
{
Console.WriteLine(message);
if (int.TryParse(Console.ReadLine(), out var number) && number >= min && number <= max)
return number;
}
}
}
public class Item
{
public Item(string name, decimal price)
{
if (price <= 0.0m)
throw new ArgumentOutOfRangeException(nameof(price));
Name = name ?? throw new ArgumentNullException(nameof(name));
Price = price;
}
public string Name { get; }
public decimal Price { get; }
public override string ToString()
{
return $"{nameof(Name)}: {Name}, {nameof(Price)}: {Price:C}";
}
#region Equality members
private bool Equals(Item other)
{
return string.Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != GetType())
return false;
return Equals((Item) obj);
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
#endregion
}
public class Rental
{
public int Duration { get; set; }
public int Quantity { get; set; }
}
public static class Rentals
{
public static Item[] Items { get; } =
{
new Item("Flotation Raft", 15.0m),
new Item("Snorkel Gear", 25.0m)
};
}
}
下一步:
有多种方法可以达到相同的结果,而这种方法可以做到:
我故意没有将其设为1:1,因此您必须先了解它并进行修改,很明显,否则您不会自己找到它:)
还,告诉你的老师他很愚蠢,这些练习只会让你讨厌编码!
(如果能帮到您,请不要忘记接受我的回答:)
答案 1 :(得分:0)
对象是包含有关特定项目的描述性数据的类。例如,Guest对象将具有名称,包含其租借列表的数据结构等。
因此,您应该从创建分配所定义的Guest对象和分配所定义的Rental对象开始。
假设您已创建对象,则需要访问它们,操作方法如下:
public static void Main()
{
Welcome(); // Get rid of getting their name in welcome, do it here
Console.WriteLine("Guest name");
String guestName = Console.readLine();
String guestContractNumber = Console.readLine();
if (guestContractNumber.equals("")
{
// Generate a random guestContract number if they didn't provide one
}
// The guest object is created below.
Guest guest = new Guest(guestName, guestContractNumber);
// The rental object is supposed to have the rental types
Rental rental = new Rental();
}
我希望这可以使您走上正确的道路。要创建对象(类),您需要:
TestClass testClass = new TestClass();
现在您可以访问该对象:
testClass.doSomething();
如果在创建类之前需要能够访问类中的数据,则可以使用 数据成员之前的static关键字:
class TestClass
{
public static String testString = "Rental items 1... 2..etc";
}
现在您可以这样做:
TestClass.testString
获取testString的值。
如果您需要更多帮助,我会尽力帮助您。