-2

My code keeps getting stuck whenever I continue after the first pause before changes are made. I can't figure out what else is the problem due to the fact I can't get past this, I have tried debugging but every change I make only seems to make the code not run. What is supposed to happen is after the original library books data are printed, I am supposed to check 3 out to patrons that I have made and other minor changes, then lastly the data is supposed to revert back to the original.

public static void Main(string[] args)
{
    LibraryPatron patron1 = new LibraryPatron("John", "A123");
    LibraryPatron patron2 = new LibraryPatron("Wick", "A122");
    LibraryPatron patron3 = new LibraryPatron("Smith", "A121");

    LibraryPatron[] x = { patron1, patron2, patron3 };

    LibraryBook book1 = new LibraryBook("The Wright Guide to C#", "Andrew Wright", "UofL Press",
        2011, "ZZ25 3G");  // 1st test book
    LibraryBook book2 = new LibraryBook("Harriet Pooter", "IP Thief", "Stealer Books",
        2001, "AG773 ZF"); // 2nd test book
    LibraryBook book3 = new LibraryBook("The Color Mauve", "Mary Smith", "Beautiful Books Ltd.",
        1986, "JJ438 4T"); // 3rd test book
    LibraryBook book4 = new LibraryBook("The Guan Guide to SQL", "Jeff Guan", "UofL Press",
        2019, "ZZ24 4F");  // 4th test book
    LibraryBook book5 = new LibraryBook("The Big Book of Doughnuts", "Homer Simpson", "Doh Books",
        2004, "AE842 7A"); // 5th test book

    LibraryBook[] theBooks = { book1, book2, book3, book4, book5 }; // Test array of books


    WriteLine("Original list of books");
    WriteLine("----------------------");
    PrintBooks(theBooks);
    Pause();

    // Make changes
    book1.CheckOut(patron1);
    book2.Publisher = "Borrowed Books";
    book3.CheckOut(patron2);
    book4.CallNumber = "AB123 4A";
    book5.CheckOut(patron3);
    book5.CopyrightYear = -1234; // Attempt invalid year

    WriteLine("After changes");
    WriteLine("-------------");
    PrintBooks(theBooks);
    Pause();

    // Return the books
    book1.ReturnToShelf();
    book3.ReturnToShelf();
    book5.ReturnToShelf();

    WriteLine("After returning the books");
    WriteLine("-------------------------");
    PrintBooks(theBooks);
}

// Precondition:  None
// Postcondition: The books have been printed to the console
public static void PrintBooks(LibraryBook[] books)
{
    foreach (LibraryBook b in books)
    {
        WriteLine(b);
        WriteLine();
    }
}

// Precondition:  None
// Postcondition: Pauses program execution until user presses Enter and
//                then clears the screen
public static void Pause()
{
    WriteLine("Press Enter to Continue...");
    ReadLine();

    Clear(); // Clear screen
}

public class LibraryBook

{

public readonly int DEFAULT_YEAR = DateTime.Now.Year; // Default copyright year is current year

private string _title;      // The book's title
private string _author;     // The book's author
private string _publisher;  // The book's publisher
private int _copyrightYear; // The book's year of copyright
private string _callNumber; // The book's call number in the library
private bool _checkedOut;   // The book's checked out status
public  static LibraryPatron Patron { get; private set; }

// Precondition:  theCopyrightYear >= 0
// Postcondition: The library book has been initialized with the specified
//                values for title, author, publisher, copyright year, and
//                call number. The book is not checked out.
public LibraryBook(String theTitle, String theAuthor, String thePublisher,
    int theCopyrightYear, String theCallNumber)
{
    Title = theTitle;
    Author = theAuthor;
    Publisher = thePublisher;
    CopyrightYear = theCopyrightYear;
    CallNumber = theCallNumber;

    ReturnToShelf(); // Make sure book is not checked out
}

public string Title
{
    // Precondition:  None
    // Postcondition: The title has been returned
    get
    {
        return _title;
    }

    // Precondition:  None
    // Postcondition: The title has been set to the specified value
    set
    {
        if (!string.IsNullOrWhiteSpace(_title))
        {
            throw new ArgumentOutOfRangeException($"{nameof(Title)} must not be null or empty values.");
        }
        _title = value.Trim();
    }
}

public string Author
{
    // Precondition:  None
    // Postcondition: The author has been returned
    get
    {
        return _author;
    }

    // Precondition:  None
    // Postcondition: The author has been set to the specified value
    set
    {
        _author = value;
    }
}

public string Publisher
{
    // Precondition:  None
    // Postcondition: The publisher has been returned
    get
    {
        return _publisher;
    }

    // Precondition:  None
    // Postcondition: The publisher has been set to the specified value
    set
    {
        _publisher = value;
    }
}

public int CopyrightYear
{
    // Precondition:  None
    // Postcondition: The copyright year has been returned
    get
    {
        return _copyrightYear;
    }

    // Precondition:  value >= 0
    // Postcondition: The copyright year has been set to the specified value
    set
    {
        if (value >= 0)
            _copyrightYear = value;
        else
            throw new ArgumentOutOfRangeException($"{nameof(CopyrightYear)} must be >= 0.");
    }
}

public string CallNumber
{
    // Precondition:  None
    // Postcondition: The call number has been returned
    get
    {
        return _callNumber;
    }

    // Precondition:  None
    // Postcondition: The call number has been set to the specified value
    set
    {
        if (!string.IsNullOrWhiteSpace(_callNumber))
        {
            throw new ArgumentOutOfRangeException($"{nameof(CallNumber)} must not be null or empty values.");
        }
        _callNumber = value.Trim();
    }
}

// Precondition:  None
// Postcondition: The book is checked out
public void CheckOut(LibraryPatron x)
{
    _checkedOut = true;
    x = Patron;
}

// Precondition:  None
// Postcondition: The book is not checked out
public void ReturnToShelf()
{
    _checkedOut = false;
    Patron = null;
}

// Precondition:  None
// Postcondition: true is returned if the book is checked out,
//                otherwise false is returned
public bool IsCheckedOut()
{
    return _checkedOut;
}

// Precondition:  None
// Postcondition: A string is returned representing the libary book's
//                data on separate lines
public override string ToString()
{
    string NL = Environment.NewLine; // Newline shortcut

    return $"Title: {Title}{NL}Author: {Author}{NL}Publisher: {Publisher}{NL}" +
        $"Copyright: {CopyrightYear}{NL}Call Number: {CallNumber}{NL}" +
        $"{(_checkedOut == true ? "Check Out By: {Patron}" : "Not Checked Out" )}";
}
public class LibraryPatron

{

private string _patronName; // Name of the patron
private string _patronID;   // ID of the patron

// Precondition:  None
// Postcondition: The patron has been initialized with the specified name
//                and ID
public LibraryPatron(string name, string id)
{
    PatronName = name;
    PatronID = id;
}

public string PatronName
{
    // Precondition:  None
    // Postcondition: The patron's name has been returned
    get
    {
        return _patronName;
    }

    // Precondition:  None
    // Postcondition: The patron's name has been set to the specified value
    set
    {
        if (!string.IsNullOrWhiteSpace(_patronName))
        {
            throw new ArgumentOutOfRangeException($"{nameof(PatronName)} must not be null or empty values.");
        }
        _patronName = value.Trim();
    }
}

public string PatronID
{
    // Precondition:  None
    // Postcondition: The patron's ID has been returned
    get
    {
        return _patronID;
    }

    // Precondition:  None
    // Postcondition: The patron's ID has been set to the specified value
    set
    {
        if (!string.IsNullOrWhiteSpace(_patronID))
        {
            throw new ArgumentOutOfRangeException($"{nameof(PatronID)} must not be null or empty values.");
        }
        _patronID = value.Trim();
    }
}

// Precondition:  None
// Postcondition: A string is returned presenting the libary patron's data on
//                separate lines
public override string ToString()
{
    string NL = Environment.NewLine; // NewLine shortcut

    return $"Name: {PatronName}{NL}ID: {PatronID}";
}

}

  • I'm going to look at the question deeper but just right off the top I suggest you stick to either the primitive type names `string`, `int`, etc, or the aliases `String`, `Int32` and not mix them up. Just bugging me trying to parse this. – NotTheBatman Jan 27 '20 at 21:12
  • I'm don't think I'm understanding what you mean. Do you think my code is not working bc I use string instead of String? – EmmanuelCIS19 Jan 27 '20 at 21:22
  • No, I just advise using one or the other consistently – NotTheBatman Jan 28 '20 at 13:15

1 Answers1

0

omit ! in if (!string.IsNullOrWhiteSpace(_callNumber))