Create a class called CashRegister based on the information given (Create appropriate constructors, accessors and mutator methods). Then write a program that tests the class. Your driver program must do the required actions.
Homework Three
MIS 6323
1
Homework Three
This homework requires you to create an application using three programs:
The first of the three programs have been written for you. I will refer to this one as ClassA.
You have to write the second one, which I will call ClassB.
You also need to write a driver program which I will refer to as ClassDriver
2
Example
Create a class called LibraryBook based on the information given below. Create appropriate constructors, accessors and mutator methods.
The class has the following attributes:
bookID – This is an int value denoting book id
bookName – This is a String value containing the book name
bookAuthor – this is an object of the Author class
bookPrice – This is a double value for book price
totInvValue – This is a double value and represents total value of inventory in the library
In addition to the constructor, accessor and mutator methods, the class has the following additional methods:
toString – This method will print out book details. Just the name of the book, the author name and price of the book will do.
getTotValue – returns the total value of inventory in the library.
Finally write a program that tests the class. Your driver program must allow the user to provide input for three LibraryBook objects. After each object is created, the program must print the LibraryBook object data. Finally, after three book objects are created, the program should print the total value of inventory in the library. Do not worry about currency formatting.
In this example, the Author class will serve as ClassA, the LibraryBook class will serve as ClassB and the LibraryDemo will be ClassDriver
3
ClassA
You must not write this program yourself. You MUST use the .class file I have provided for this assignment.
In the example application, Author has already been written and the class file is provided to you. All you need to know about the Author class has been given in the Author.txt file
4
Author.txt
public class Author {
public Author(String name, String type)
public String toString() }
Note the following:
The class has only one constructor which has two String values as parameters
The class has one additional method called toString() which has no input parameters, but returns a String value
5
Continuing with ClassA
You will use the ClassA.txt file to see how the constructor well as the methods for the class have been defined.
You will need this information to correctly call the constructor and the methods of the ClassA class from the other programs.
For example in LibraryDemo, we create an object of the Author class using the statement below.
auth = new Author(aName, aType);
To call the toString() method of the Author class, we will write the following, where bookAuthor is an object of the Author class:
bookAuthor.toString()
Continuing with ClassA
Put the .class file in the same folder as the programs you will be writing.
Once the other two programs (ClassB and ClassDriver) have been compiled, you will have three .class files in the folder which will work together to produce the results you need.
ClassB
You will need to write this class definition based on the assignment requirements.
In the help example, you are asked to create a LibraryBook class as follows:
The class has the following attributes:
bookID – This is an int value denoting book id
bookName – This is a String value containing the book name
bookAuthor – this is an object of the Author class
bookPrice – This is a double value for book price
totInvValue – This is a double value and represents total value of inventory in the library
In addition to the constructor, accessor and mutator methods, the class has the following additional methods:
toString – This method will print out book details. Just the name of the book, the author name and price of the book will do.
getTotValue – returns the total value of inventory in the library.
ClassB
Based on the requirements for the LibraryBook class, LibraryBook.java can be written as shown below:
public class LibraryBook
{ private int bookId;
private String bookName;
private Author bookAuthor;
private double bookPrice;
private static double totalInvValue;
public LibraryBook (int id, String name, Author author, double price)
{
bookId = id;
bookName = name;
bookAuthor = author;
bookPrice = price;
totalInvValue += price;
}
public String toString()
{
String response = "";
response += "Book Name is: " + bookName;
response += "\nIt costs: " + bookPrice;
response += "\nIt was " + bookAuthor.toString();
return response;
}
public static double getTotValue()
{
return totalInvValue;
}
}
Continuing with ClassB
Once this class definition is complete, compile the program to ensure you have made no mistakes.
If the .class file for your ClassA program is not in the same folder, you will get a compilation error, since the System will not recognize any references to ClassA (Author class in the example)
ClassDriver
Finally write your driver program to do what the assignment asks for.
In the example, you are asked to do the following:
Finally write a program that tests the class. Your driver program must allow the user to provide input for three LibraryBook objects. After each object is created, the program must print the LibraryBook object data. Finally, after three book objects are created, the program should print the total value of inventory in the library. Do not worry about currency formatting.
Continuing Driver Class
import java.util.*;
public class LibraryDemo
{
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String bName, aName, aType;
double bPrice;
int bID;
LibraryBook lBook = null;
Author auth = null;
for (int i = 1; i <=3; i++){
System.out.print("Enter the name of the book: ");
bName = kb.nextLine();
System.out.print("Enter the name of the author: ");
aName = kb.nextLine();
System.out.println("Enter the type of books the author writes");
System.out.print("Fiction or Non-Fiction: ");
aType = kb.nextLine();
System.out.print("Enter the price of the book: ");
bPrice = kb.nextDouble();
kb.nextLine();
System.out.print("Enter the id of the book: ");
bID = kb.nextInt();
kb.nextLine();
auth = new Author(aName, aType);
lBook = new LibraryBook(bID, bName, auth, bPrice);
System.out.println("\n");
System.out.println(lBook);
System.out.println("The value of inventory in the library after " + i);
System.out.println(" books is " + LibraryBook.getTotValue());
System.out.println("\n\n");
}
}
}
DONE!!