In: Computer Science
I am struggling with this assignment. I can't get the program to run when I enter a number with the $ symbol followed by a number below 10. any help would be greatly appreciated.
Create a program named Auction that allows a user to enter an amount bid on an online auction item. Include three overloaded methods that accept an int, double, or string bid. Each method should display the bid and indicate whether it is over the minimum acceptable bid of $10. If the bid is a string, accept it only if one of the following is true: it is numeric and preceded with a dollar sign, or it is numeric and followed by the word dollars. Otherwise, display a message that indicates the format was incorrect.
class Auction
{
public static void
Main(string[] args)
{
string bidamount;
Console.Write("Enter bid: ");
bidamount = Console.ReadLine();
double num;
int n;
if (double.TryParse(bidamount, out num))
{
double bid = Convert.ToDouble(bidamount);
if (valid(bid))
{
Console.WriteLine("Bid is $" + bid + ".");
Console.WriteLine("Bid is at/over $10.");
Console.WriteLine("Bid accepted.");
}
else
Console.WriteLine("Invalid. Bid not acceptable.");
}
else if (int.TryParse(bidamount, out n))
{
int bid = Convert.ToInt32(bidamount);
if (valid(bid))
{
Console.WriteLine("Bid is $" + bid + ".");
Console.WriteLine("Bid is at/over $10.");
Console.WriteLine("Bid accepted.");
}
else
Console.WriteLine("Bid not acceptable.");
}
else
{
string bid = (bidamount);
if (valid(bidamount))
{
Console.WriteLine("Bid is $" + bid + ".");
Console.WriteLine("Bid is at/over $10.");
Console.WriteLine("Bid accepted.");
}
else
Console.WriteLine("Bid not acceptable.");
}
Console.ReadKey();
}
public static bool
valid(int bid)
{
if (bid < 10)
return false;
else
return true;
}
public static bool
valid(double bid)
{
if (bid < 10)
return false;
else
return true;
}
public static bool
valid(string bid)
{
if (bid[0] == '$' && Convert.ToInt32(bid.Substring(1))
>= 10)
return true;
else if ((Convert.ToInt32(bid.Substring(0, 2)) >= 10) &&
(bid.Substring(2).Equals("dollars")))
{
return true;
}
else
return false;
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
using System;
class Auction {
public static void
Main(string[] args) {
string
bidamount;
Console.Write("Enter
bid: ");
bidamount =
Console.ReadLine();
double
num;
int
n;
if
(double.TryParse(bidamount,
out num)) {
double
bid = Convert.ToDouble(bidamount);
if
(valid(bid)) {
Console.WriteLine("Bid
is $" + bid + ".");
Console.WriteLine("Bid
is at/over $10.");
Console.WriteLine("Bid
accepted.");
}
else
Console.WriteLine("Invalid. Bid not
acceptable.");
}
else
if
(int.TryParse(bidamount,
out n)) {
int
bid = Convert.ToInt32(bidamount);
if
(valid(bid)) {
Console.WriteLine("Bid
is $" + bid + ".");
Console.WriteLine("Bid
is at/over $10.");
Console.WriteLine("Bid
accepted.");
}
else
Console.WriteLine("Bid not acceptable.");
}
else
{
string
bid = (bidamount);
if
(valid(bidamount)) {
//bid
is either in '$num' or 'num dollars' format, so we dont have
//to
prepend '$' symbol
Console.WriteLine("Bid
is " + bid + ".");
Console.WriteLine("Bid
is at/over $10.");
Console.WriteLine("Bid
accepted.");
}
else
Console.WriteLine("Bid not acceptable.");
}
Console.ReadKey();
}
public static
bool valid(int
bid) {
if
(bid < 10) return false;
else
return true;
}
public static
bool
valid(double bid) {
if
(bid < 10) return false;
else
return true;
}
//updated this method
public static
bool valid(string bid) {
double
num;
//trying first way
of checking if the input is a dollar sign followed by a
numeric
//value
if
(bid.Length > 0 && bid[0] == '$' &&
double.TryParse(bid.Substring(1),
out num)) {
//yes,
checking if the numeric value is greater than or equal to 10
if
(num >= 10) {
//valid
return
true;
}
else {
return
false;
}
}
num = 0;
//trying second
check to see if the input is a numeric value followed by the
word
//dollars.
if
(bid.Length > 0 &&
bid.Contains("dollars") &&
double.TryParse(bid.Substring(0,
bid.IndexOf("dollars")), out
num)) {
//yes,
checking if num>=10
if
(num >= 10) {
//valid
return
true;
}
else {
return
false;
}
}
//invalid
format
return
false;
}
}
/*OUTPUT (multiple runs)*/
Enter bid: $19
Bid is $19.
Bid is at/over $10.
Bid accepted.
Enter bid: 11 dollars
Bid is 11 dollars.
Bid is at/over $10.
Bid accepted.