the errors only got worse with the previous fix
Input
Book1
Author1
10.99
200
Book2
Author2
19.99
100
Book3
Author2
25.00
600
Book4
Author3
5.00
20
Book5
Author4
8.00
120
Output
Book1 by Author1 Price $10.99 200 pages.
Book2 by Author2 Price $19.99 100 pages.
Book3 by Author2 Price $25 600 pages.
Book4 by Author3 Price $5 20 pages.
Book5 by Author4 Price $8 120 pages.
the results for this are in the image
my other error is
Unit TestIncomplete
BookException test
Build Status
Build Succeeded
Test Output
NUnit Console Runner 3.10.0 (.NET 2.0)
Copyright (c) 2019 Charlie Poole, Rob Prouse
Sunday, 26 March 2023 20:04:35
Runtime Environment
OS Version: Linux 5.0.0.27
CLR Version: 4.0.30319.42000
Test Files
NtTestce2b4463.dll
Errors, Failures and Warnings
1) Failed : TestBookException.FirstBookExceptionTest
Expected: <BookException>
But was: null
at TestBookException.FirstBookExceptionTest () [0x00000] in <79111348b1e14d73832a84bb9d5a15b5>:0
Run Settings
DisposeRunners: True
WorkDirectory: /root/sandbox6ae72429
ImageRuntimeVersion: 4.0.30319
ImageRequiresX86: False
ImageRequiresDefaultAppDomainAssemblyResolver: False
NumberOfTestWorkers: 2
Test Run Summary
Overall result: Failed
Test Count: 2, Passed: 1, Failed: 1, Warnings: 0, Inconclusive: 0, Skipped: 0
Failed Tests - Failures: 1, Errors: 0, Invalid: 0
Start time: 2023-03-26 20:04:36Z
End time: 2023-03-26 20:04:37Z
Duration: 1.073 seconds
my code is
using System;
class Book
{
// private fields
private string title;
private string author;
private double price; //data type changed
private int numPages;
// constructor that takes in arguments and sets the fields
public Book(string title, string author, double price, int numPages)
{
this.title = title;
this.author = author;
this.price = price;
this.numPages = numPages;
}
// public properties that allow access to the private fields
public string Title
{
get { return title; }
set { title = value; }
}
public string Author
{
get { return author; }
set { author = value; }
}
public double Price
{
// when setting the price, also calculate the price per page
// and check if it's greater than a certain threshold
get { return price; }
set
{
double pricePerPage = value / numPages;
if (pricePerPage > 0.1) //Removed m
{
// if the price per page is too high, throw a BookException
throw new BookException(title, price, numPages);
}
price = value;
}
}
public int NumPages
{
get { return numPages; }
set { numPages = value; }
}
// override the ToString method to return a string representation of the Book object
public override string ToString()
{
return title + " by " + author + " Price $" + price + " " + numPages + " pages.";
}
}
// custom exception class that inherits from Exception
class BookException : Exception
{
// constructor that takes in arguments and calls the base constructor with a formatted message
public BookException(string title, double price, int numPages) //data type changed
: base("For " + title + ", ratio is invalid....Price is $" + price + " for " + numPages + " pages.")
{
}
}
class BookExceptionDemo
{
static void Main()
{
// create an array of Book objects
Book[] books = new Book[5];
// prompt the user to enter information for each book and create a new Book object
for (int i = 0; i < 5; i++)
{
string title = Console.ReadLine();
string author = Console.ReadLine();
double price = double.Parse(Console.ReadLine());
int numPages = int.Parse(Console.ReadLine());
books[i] = new Book(title, author, price, numPages);
}
// print out each Book object using the overridden ToString method
foreach (Book book in books)
{
Console.WriteLine(book);
}
}
}