Question

In: Computer Science

I am struggling with this assignment. I can't get the program to run when I enter...

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;
        }
    }

Solutions

Expert Solution

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.


Related Solutions

I am struggling to even get started on this assignment. its a teach yourself kind of...
I am struggling to even get started on this assignment. its a teach yourself kind of situation and I have and no issues up to this point but I just can't wrap my head around it. Implement the Account class. Add a public enum called ACCOUNT_TYPE with two values CHECKING, SAVING Add the following private instance variables to the Account class: An instance variable called aType of type Enum ACCOUNT_TYPE. An instance variable called accountOwner of type String. An instance...
I am struggling with this assignment for my java class. Objectives: Your program will be graded...
I am struggling with this assignment for my java class. Objectives: Your program will be graded according to the rubric below. Please review each objective before submitting your work so you don’t lose points. 1.Create a new project and class in Eclipse and add the main method. (5 points) 2. Construct a Scanner to read input from the keyboard. (5 points) 3. Prompt the user with three questions from the Psychology Today quiz, and use the Scanner to read their...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
What am i doing wrong. I want for the program to run through then when it...
What am i doing wrong. I want for the program to run through then when it gets to "do you want to play again?enter yes or no" if they choose yes I want it to run again if they choose no then end. def play(): print("Welcome to the Game of Life!") print("A. Banker") print("B. Carpenter") print("C. Farmer") user_input = input("What is your occupation? ").upper() if user_input == "A": money = 100 elif user_input == "B": money = 70 elif user_input...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
When I run this C++ program that asks the user to enter the population of 4...
When I run this C++ program that asks the user to enter the population of 4 cities and produce the bar graph - it runs but ends with a Debug Error - run time check failure 2 stack around variable population was corrupted - any help would be appreciated #include <iostream> using namespace std; int main() { int population[4],k;    int i=1,n=5;       do    {        cout<<"Enter the population of city "<<i<<":"<<endl;             cin>>population[i];        if(population[i]<0)       ...
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
I need to write a program and can't seem to make it run properly. Someone can...
I need to write a program and can't seem to make it run properly. Someone can help with the coding? It's with python Thanks! Here is the program: The rules of Shut the Box are as follows (abbreviated version): + You have two 5-sided die + You have 5 wooden blocks (labeled 1 through 5) + Each turn, you roll the dice and knock down the blocks corresponding to the number on each die + You have to knock down...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT