Searching a list of items with a specific name - java

I am doing an Object Oriented program which contains a class Catalog and a class Products. Catalog has a method which suppose to search a list of products with a specific name that are read from a file. Everything works but the getProducts is not working.
This is the Catalog class with the getProducts(String name)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*
public class Catalog{
private static int MAX_ITEMS = 10;
private Products[] list;
private int nextItem;
/**
* Default constructor
*/
public Catalog(){
list = new Products[MAX_ITEMS];
nextItem = 0;
}
/**
* Reads items from an input file.
*/
public void loadList(String fileName)
throws FileNotFoundException {
if ( (fileName != null) && (!fileName.equals("")) ) {
Scanner input = new Scanner(new File(fileName));
String newLine = null;
String name = null;
int quantity = 0;
double price = 0.0;
while (input.hasNextLine() && nextItem < MAX_ITEMS) {
if (input.hasNext()) {
name = input.next();
} else {
System.err.println("ERROR Not a String");
System.exit(2);
}
if (input.hasNextInt()) {
quantity = input.nextInt();
} else {
System.err.println("ERROR Not an integer");
System.exit(2);
}
if (input.hasNextDouble()) {
price = input.nextDouble();
} else {
System.err.println("ERROR Not a double");
System.exit(2);
}
list[nextItem] = new Products(name, quantity, price);
newLine = input.nextLine();
nextItem += 1;
}
}
return;
}
/**
* Calculate the total cost of products.
*/
public double getTotalPrice(){
double total = 0.0;
for(int i=0; i < nextItem; i++){
Products products = list[i];
total+=products.getTotalPrice();
}
return total;
}
/**
* Search Catalog items with a product name and returns it to caller
*/
public Products getProducts(String name){
**//search a list for string equality using the name of the product and returns it to the caller**
for(int i=0; i<nextItem; i++){
Products item = list[i];
if(item.equals(name)){
return item; //What is suppose to be returned or how to
//return it to the caller
}
public static void main(String[] args)
throws FileNotFoundException {
Catalog catalog= new Catalog();
catalog.loadList(args[0]);
System.out.println();
System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
}
}
This is Products class
public class Products {
private String name;
private int quantity;
private double price;
/**
* Constructor.
*/
public Products(String name, int quantity, double price){
this.name = name;
this.quantity = quantity;
this.price = price;
}
/**
* Gets name of the product.
*/
public String getName(){
return this.name;
}
/**
* Gets the quantity of products.
*/
public int getQuantity(){
return this.quantity;
}
/**
* Gets the cost per product.
*/
public double getPrice(){
return price;
}
/**
* set quantity of the products.
*/
public void setQuantity(int quantity){
this.quantity=quantity;
}
/**
* Calculate the total price.
*/
public double getTotalPrice(){
return quantity * price;
}
/**
* Returns a spaced-separated list of the attributes.
*/
public String toString(){
toStr="";
toStr= toStr + getName();
return toStr;
}
This is the file
Football 2 15.50
Football-Jersey 2 30.95
ChapStick 1 3.87
Book 4 10.00
Book-Journal 1 5.00

You have:
public Products getProducts(String name){
...
for(int i=0; i<nextItem; i++){
...
Products item = list[i];
if(item.equals(name)) {
...
Notice that item is a Products but you are comparing it to the String, name. You would want to compare the product's name, e.g.:
if(item.getName().equals(name)) {
You can also use String.equalsIgnoreCase() if names are case-insensitive, possibly using trim() first if leading/trailing whitespace is an issue, too.

Item is an Object, so you can try to get the name by using dot like this
if(item.getName().equals(name))
or
if(item.getName.equalsIgnoreCase(name))

Related

adding a book with multiple parameters to a single array list index

So I'm trying to add a book to an array list. I'm having a little trouble due to the fact the book must include 3 parameters (string author, string title, string id) The reason I'm having trouble is due to 2 methods that have to be used on it. One method is for finding matching titles the other wants me to list all books currently in the array. Right now my code looks like this:
import java.util.ArrayList;
import java.util.Iterator;
/**
* A class contaning books.
*
* #author ()
* #version (21/11/2021)
*/
public class Library
{
private ArrayList<String> books;
private String name;
/**
* Makes an arraylist and sets the librarys name
*/
public Library(String aName)
{
books = new ArrayList<>();
name = aName;
}
/**
* Adds a book to the arraylist books
*/
public void addBook(String author, String title, String id)
{
books.add(author);
books.add(title);
books.add(id);
}
/**
* calculates fine of a late book
*/public double calculateFine (double bookPrice, int dayLate)
{
return (double) (bookPrice/50)*dayLate;
}
/**
* creates a private list to store matched books
*/
public ArrayList<String> booksClone = new ArrayList<>();
/**
*
*/
public ArrayList getMatchingBooks(String bookMatch)
{
for (int i =0; i < books.size(); i++)
{
if(books.get(i).equals(bookMatch));
booksClone.add(bookMatch);
}
return booksClone;
}
/**
*
*/
public boolean isAvailable(String book, boolean onLoan)
{
for (int i=0; i <books.size();i++)
{
if(books.get(i)==book)
if (onLoan ==true);
}
return false;
}
/**
* Prints out all books currently listed.
*/
public void listAllBooks()
{for (int i=0; i < books.size();i++)
System.out.println(books.get(i));
}
/**
*
*/
public void loanBook(String bookLoan, boolean onLoan)
{
for (int i=0; i < books.size();i++)
{
if (books.get(i)==bookLoan)
onLoan = true;
}
}
/**
*
*/
public void removeBook(String book)
{
for (int i=0; i < books.size();i++)
{
if (books.get(i).equals(book))
books.remove(book);
else
System.out.println("Book not found");
}
}
}
as you can see my current code takes the title author and id and links them to separate index's so when i try to search for a title i can not find the author.
I'm sorry if this is a obvious question I think I've just gotten myself lost any help and or advice is appreciated.
If you add the 3 String like this on the library arraylist, nothing will link them together.
To save the 3 String togeter on the list, you need to create an Object - lets name it "Book" - that will have 3 variables: author, title & id.
import java.util.ArrayList;
import java.util.Iterator;
/**
* A class contaning books.
*
* #author ()
* #version (21/11/2021)
*/
public class Library {
private ArrayList<Book> books;
private String name;
/**
* Makes an arraylist and sets the librarys name
*/
public Library(String aName) {
books = new ArrayList<>();
name = aName;
}
/**
* Adds a book to the arraylist books
*/
public void addBook(String author, String title, String id) {
books.add(new Book(author, title, id));
}
/**
* calculates fine of a late book
*/public double calculateFine (double bookPrice, int dayLate) {
return (double) (bookPrice/50)*dayLate;
}
/**
* creates a private list to store matched books
*/
public ArrayList<String> booksClone = new ArrayList<>();
/**
*
*/
public ArrayList getMatchingBooks(String bookMatch) {
for (int i =0; i < books.size(); i++)
{
if(books.get(i).equals(bookMatch));
booksClone.add(bookMatch);
}
return booksClone;
}
/**
*
*/
public boolean isAvailable(String bookTitle, boolean onLoan) {
for (int i=0; i <books.size();i++)
{
if(books.get(i).title==bookTitle)
if (onLoan ==true);
}
return false;
}
/**
* Prints out all books currently listed.
*/
public void listAllBooks() {for (int i=0; i < books.size();i++)
System.out.println(books.get(i));
}
/**
*
*/
// public void loanBook(String bookLoan, boolean onLoan) {
// for (int i=0; i < books.size();i++) {
// if (books.get(i)==bookLoan)
// onLoan = true;
// }
// }
/**
*
*/
public void removeBook(String bookId) {
for (int i=0; i < books.size();i++) {
if (books.get(i).id.equals(bookId))
books.remove(i);
else
System.out.println("Book not found");
}
}
class Book {
private String author;
private String title;
private String id;
public Book (String author, String title, String id) {
this.author=author;
this.title=title;
this.id=id;
}
public String toString(){
return id+": "+title+" from "+author;
}
}
public static void main(String[] args) {
Library lib = new Library("lib1");
lib.addBook("Mr X", "title1", "0");
lib.addBook("Mr X", "an other title", "8");
lib.addBook("Ms Y", "last title", "213b");
lib.listAllBooks();
}
}
Ps: Please add tab on your code.

Cannot find symbol - class InventoryItem

I re-type these code from a book and somehow I got error " Cannot find symbol - class InventoryItem "
import java.util.Scanner;
public class ReturnObject {
public static void main(String[] args) {
InventoryItem item;
item = getData();
System.out.println("Des: " + item.getDescription() + " Unit: " +
item.Units());
}
public static InventoryItem getData() {
String desc;
int units;
Scanner keyboard = new Scanner(System.in);
System.out.print("enter descri: ");
desc = keyboard.nextLine();
System.out.print("number of unit: ");
units = keyboard.nextInt();
return new InventoryItem(desc, units);
}
}
I'm new to java please help
thank you.
I think this should be the InventoryItem you need.
/**
* This class uses three constructors.
*/
public class InventoryItem {
private String description; // Item description
private int units; // Units on-hand
/**
* No-arg constructor
*/
public InventoryItem() {
description = "";
units = 0;
}
/**
* The following constructor accepts a
* String argument that is assigned to the
* description field.
*/
public InventoryItem(String d) {
description = d;
units = 0;
}
/**
* The following constructor accepts a
* String argument that is assigned to the
* description field, and an int argument
* that is assigned to the units field.
*/
public InventoryItem(String d, int u) {
description = d;
units = u;
}
/**
* The setDescription method assigns its
* argument to the description field.
*/
public void setDescription(String d) {
description = d;
}
/**
* The setUnits method assigns its argument
* to the units field.
*/
public void setUnits(int u) {
units = u;
}
/**
* The getDescription method returns the
* value in the description field.
*/
public String getDescription() {
return description;
}
/**
* The getUnits method returns the value in
* the units field.
*/
public int getUnits() {
return units;
}
}
complete example click here and here
The class you are currently in cannot find the class (symbol) InventoryItem. You need to define this class & the getData method.
public class InventoryItem{
private String desc;
private int units;
public InventoryItem(){
Scanner keyboard = new Scanner(System.in);
System.out.print("enter descri: ");
desc = keyboard.nextLine();
System.out.print("number of unit: ");
units = keyboard.nextInt();
}
public static InventoryItem getData() {
return this;
}
}
maybe your InventoryItemclass:
public class InventoryItem {
private String desc;
private int units;
public InventoryItem(String desc, int units) {
this.desc=desc;
this.units=units;
}
public String getDescription() {
return desc;
}
public int Units() {
return units;
}
}

Sum all double values of objects stored in ArrayList (Java)

I have an ArrayList that contains objects. Each of the object has 3 values: String name, double price, int quantity. How to write method that will sum all doubles of objects and print the result. And also if int quantity>1, price will be multiplied by quantity.
Code that i wrote so far:
Product class
public class Product {
private String name;
private double price;
private int quantity;
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public static Product createProduct(String name, double price, int quantity){
return new Product(name, price, quantity);
}
}
Product list class
import java.util.ArrayList;
import java.util.List;
public class ProductList {
private String name;
List<Product> newList;
public ProductList(String name) {
this.name = name;
this.newList = new ArrayList<>();
}
public boolean addNewProduct(Product product) {
if (findProduct(product.getName()) >= 0) {
System.out.println("Product is already on the list");
return false;
}
newList.add(product);
return true;
}
public boolean removeProduct(Product product) {
if (findProduct(product.getName().toUpperCase()) < 0) {
System.out.println("Product not found");
return false;
}
newList.remove(product);
return true;
}
private int findProduct(String productName) {
for (int i = 0; i < newList.size(); i++) {
Product product = newList.get(i);
if (product.getName().equals(productName)) {
return i;
}
}
return -1;
}
public Product queryProduct(String name) {
int position = findProduct(name);
if (position >= 0) {
return this.newList.get(position);
}
return null;
}
public double sumProducts() {
double sum = 0;
for (int i = 0; i < newList.size(); i++) {
sum += newList.get(i).getPrice();
}
return sum;
}
/*public boolean listProducts(){};
public boolean updateProduct(){};
*/
}
Simulation class:
public class Simulation {
private static Scanner scanner = new Scanner(System.in);
private static ProductList myProductList = new ProductList("My list");
private static void addNewProduct() {
System.out.println("Enter new product name: ");
String name = scanner.nextLine();
System.out.println("Enter new product price: ");
double price = scanner.nextDouble();
System.out.println("Enter new product quantity");
int quantity = scanner.nextInt();
Product newProduct = Product.createProduct(name, price, quantity);
if (myProductList.addNewProduct(newProduct) == true) {
System.out.println("New product added: " + name + " | price: " + price + " | quantity: " + quantity);
}
}
private static void removeProduct() {
System.out.println("Enter product name: ");
String name = scanner.nextLine().toUpperCase();
Product existingProduct = myProductList.queryProduct(name);
if (existingProduct == null) {
System.out.println("No such product");
return;
}
if (myProductList.removeProduct(existingProduct)) {
System.out.println("Sucessfully deleted product: " + existingProduct.getName());
} else {
System.out.println("Error deleting");
}
}
private static void printActions() {
System.out.println("Avaiable actions");
System.out.println("press: ");
System.out.println("0 - to shut down\n" +
"1 - to add new product\n" +
"2 - to remove product\n" +
"3 - to sum all products");
}
private static void sumProducts(){
myProductList.sumProducts();
}
public static void main(String[] args) {
printActions();
boolean quit = false;
while (!quit)
try {
System.out.println("\nEnter action: ");
int action = scanner.nextInt();
scanner.nextLine();
switch ((action)) {
case 0:
System.out.println("\nShutting down...");
quit = true;
break;
case 1:
addNewProduct();
break;
case 2:
removeProduct();
break;
}
} catch (InputMismatchException e) {
System.out.println("Bad key pressed, only values form 0 to 2 accepted");
scanner.nextLine();
}
}
}
Thanks in advance
You can do it in one line using Java 8.
public double sumProducts() {
return newList.stream().mapToDouble(product -> product.getPrice() * product.getQuantity()).sum();
}
If you use double to store the price, you will get incorrect answers when you try to add and multiply the values. For example, 0.1 + 0.2 is NOT the same double as 0.3. If you want accurate arithmetic for decimal numbers, you should use the BigDecimal class in place of double. If you don't do that, I can guarantee that your program will sometimes give wrong answers.
So you need to change your Product class as follows.
public class Product {
private String name;
private BigDecimal price;
private int quantity;
public Product(String name, BigDecimal price, int quantity) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public BigDecimal getPrice() {
return price;
}
public static Product createProduct(String name, BigDecimal price, int quantity){
return new Product(name, price, quantity);
}
}
You will also need to make corresponding changes in the code that calls the methods of this class.
Once you've done that, you can use the methods of the BigDecimal class to do arithmetic. It might look something like this.
public BigDecimal calculateTotalPrice() {
BigDecimal total = BigDecimal.ZERO;
for (Product product : newList) {
BigDecimal linePrice = product.getPrice().multiply(new BigDecimal(product.getQuantity()));
total = total.add(linePrice);
}
return total;
}
the sum of each product was missing multiply by its quantity.
public double sumProducts() {
double sum = 0;
for (int i = 0; i < newList.size(); i++) {
Product product = newList.get(i);
sum += product.getPrice() * product.getQuantity();
}
return sum;
}

Calculate the number of items within the a Shopping Cart

I am quite new to the concepts of Java. I have designed a shopping cart application using core java. But i am not able to get the desired output.
Here is my pojo class item.java
package com.shop.data.*;
public class Item
{
private int Itemid;
private String category;
private String name;
private double price;
private String size;
/**
* #return the category
*/
public String getCategory() {
return category;
}
public Item(int itemid) {
super();
Itemid = itemid;
}
/**
* #param category the category to set
*/
public void setCategory(String category) {
this.category = category;
}
// -------------------------------------------------------
// Create a new item with the given attributes.
// -------------------------------------------------------
public Item (String itemcategory ,String itemName, double itemPrice,String itemSize)
{
name = itemName;
price = itemPrice;
size = itemSize;
category = itemcategory;
}
public Item(String itemName, int itemPrice, String size) {
// TODO Auto-generated constructor stub
}
/**
* #return the size
*/
public String getSize() {
return size;
}
/**
* #param size the size to set
*/
public void setSize(String size) {
this.size = size;
}
// -------------------------------------------------
// Returns the unit price of the item
// -------------------------------------------------
public double getPrice()
{
return price;
}
// -------------------------------------------------
// Returns the name of the item
// -------------------------------------------------
public String getName()
{
return name;
}
/**
* #return the itemid
*/
public int getItemid() {
return Itemid;
}
/**
* #param itemid the itemid to set
*/
public void setItemid(int itemid) {
Itemid = itemid;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
public String toString ()
{
return (name + "\t" + price + "\t"+ size + "\t");
}
}
The main class is ShopCartTest.java
package com.shop.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import com.shop.data.*;
public class ShoppingCartTest {
private static Scanner scan;
public static void main(String[] args) {
// *** declare and instantiate a variable cart to be an empty ArrayList
shopping();
}
public static void shopping() {
ArrayList<Item> ItemCart = new ArrayList<Item>();
Map<Item, Integer> Quantity = new HashMap<Item, Integer>();
Item item = null;
Inventory inventory = null;
String category = null;
String itemName;
String itemSize;
double itemPrice = 0;
double totalPrice = 0.0;
double sum = 0.0;
int itemquantity;
scan = new Scanner(System.in);
String keepShopping = "y";
System.out.println("****Shopping Cart****");
do {
System.out.println("Select Category: ");
category = scan.nextLine();
if (category.equals("men") || category.equals("Men")) {
System.out
.println("Available Items: \n1Shirt : 900 \n2.T-Shirt : 700 \n3.Jean: 1500.\n");
System.out.println("Select Item to Add to your Bag: ");
itemName = scan.nextLine();
if (itemName.equals("Shirt") || (itemName.equals("shirt"))) {
itemPrice = 900;
} else if (itemName.equals("T-Shirt")
|| (itemName.equals("t-shirt"))) {
itemPrice = 700;
} else
itemPrice = 1500;
} else {
System.out
.println("Available Items: \n1.Dress : 250 \n2.Top : 350 \n3.Jean: 1500.\n");
System.out.println("Select Item to Add to your Bag: ");
itemName = scan.nextLine();
// *** create a new item and add it to the cart
if (itemName.equals("Dress") || (itemName.equals("dress"))) {
itemPrice = 250;
} else if (itemName.equals("Top") || (itemName.equals("top"))) {
itemPrice = 350;
} else
itemPrice = 1500;
}
System.out.print("Available Sizes \nS \tM \tL: ");
itemSize = scan.nextLine();
System.out.print("Enter the quantity: ");
itemquantity = scan.nextInt();
item = new Item(category, itemName, itemPrice, itemSize);
ItemCart.add(item);
Quantity.put(item, itemquantity);
// *** print the contents of the cart object using println
for (int i = 0; i < ItemCart.size(); i++) {
Item itm = ItemCart.get(i);
System.out.println(itm);
}
// Print out the results
System.out.print("Continue shopping (y/n)? ");
scan.nextLine();
keepShopping = scan.nextLine();
} while (keepShopping.equals("y"));
for (int i = 0; i < ItemCart.size(); i++) {
Quantity.put(item, itemquantity);
Item itm = ItemCart.get(i);
System.out.println(itm);
totalPrice = itemquantity * itm.getPrice();
for (int j = 0; i < ItemCart.size(); i++)
sum += totalPrice;
}
System.out.println("The total price is: " + sum);
System.out.println("Do you wan to proceed to checkout (y/n): ");
if (scan.nextLine().equals("y")) {
System.out.println("Thank you for shopping with us!");
} else {
shopping();
}
}
}
There are 4 pojo's Cart, inventory,Item, User. The user must be able to select multiple items of multiple quanties. (For items a list is been created and for the quantity: map. where item object is the key and quantity is the value). The user slects category, item, then quatity. Then he can proceed to checkout. So when he decides to checkout. He must be able to see the various items purchased ie (itemName, SIxe ,UnitPrice, unitprice*quantity) and finally the Total Price.
How do i achieve this? I am kinda lost!
Some pointers:
You should use proper naming conventions (i.e. variables and methods name starts with a lower case letter)
ItemCart is redundant, you can remove it and use the keys of Quantity
a Map provides an entrySet method, using that on Quantity will help you list the items in the cart (keys) and compute the total price using the item (key) price and quantity (value)

Java Keyboard.readInput() error

I'm learning java and my programming skills are are good. I have been asked to find out the problem with codes below. when I paste them on netbeans, the error that had been detected was in the public class CheckoutProgram (String wordIn = Keyboard.readInput(); and wordIn = Keyboard.readInput();) and I noticed that the public static void method was empty but I'm not sure it has anything to do with the error. I have tried to find a solution myself but I can't sort it out. Can you help me with this issue? please
import java.io.*;
import java.text.DecimalFormat;
public class CheckoutProgram {
public static void main (String[] args) {
}
public void start() {
SalesItem[] items = getStock();
System.out.print("Type item code (press enter to finish):");
String wordIn = Keyboard.readInput();
SalesItem[] goods = new SalesItem[1000];
int count = 0;
while (wordIn.length()>=4 && wordIn.length()<=4){
for (int i=0;i<items.length;i++) {
if (items[i] != null && wordIn.equals(items[i].getItemCode())){
System.out.println(items[i]);
goods[count] = items[i];
}
}
System.out.print("Type item code (press enter to finish):");
wordIn = Keyboard.readInput();
count++;
}
System.out.println();
System.out.println("==========Bill==========");
double amountDue = 0.0;
for (int i=0; i<count; i++){
System.out.println(goods[i]);
amountDue = amountDue + goods[i].getUnitPrice();
}
System.out.println();
System.out.println("Amount due: $" + new DecimalFormat().format(amountDue));
System.out.println("Thanks for shopping with us!");
}
// method to read in "stock.txt" and store the items for sale in an array of type SalesItem
private SalesItem[] getStock(){
SalesItem[] items = new SalesItem[1000];
try {
BufferedReader br = new BufferedReader(new FileReader("stock.txt"));
String theLine;
int count = 0;
while ((theLine = br.readLine()) != null) {
String[] parts = theLine.split(",");
items[count] = new SalesItem(parts[0],parts[1],Double.parseDouble(parts[2]));
if (parts.length==4){
String discount = parts[3];
String numPurchases = discount.substring(0, discount.indexOf("#"));
String price = discount.substring(discount.indexOf("#")+1);
items[count].setNumPurchases(Integer.parseInt(numPurchases));
items[count].setDiscountedPrice(Double.parseDouble(price));
}
count++;
}
}
catch (IOException e) {
System.err.println("Error: " + e);
}
return items;
}
}
import java.text.DecimalFormat;
public class SalesItem {
private String itemCode; //the item code
private String description; // the item description
private double unitPrice; // the item unit price
// An item may offer a discount for multiple purchases
private int numPurchases; //the number of purchases required for receiving the discount
private double discountedPrice; // the discounted price of multiple purchases
// the constructor of the SalesItem class
public SalesItem (String itemCode, String description, double unitPrice){
this.itemCode = itemCode;
this.description = description;
this.unitPrice = unitPrice;
}
// accessor and mutator methods
public String getItemCode(){
return itemCode;
}
public void setItemCode(String itemCode){
this.itemCode = itemCode;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public double getUnitPrice(){
return unitPrice;
}
public void setUnitPrice(double unitPrice){
this.unitPrice = unitPrice;
}
public int getNumPurchases(){
return numPurchases;
}
public void setNumPurchases(int numPurchases){
this.numPurchases = numPurchases;
}
public double getDiscountedPrice(){
return discountedPrice;
}
public void setDiscountedPrice(double discountedPrice){
this.discountedPrice = discountedPrice;
}
// the string representation of a SalesItem object
public String toString(){
return description + "/$" + new DecimalFormat().format(unitPrice);
}
}
Keyboard most likely doesn't exist. It isn't a part of the standard Java library. You would have to import a class that uses Keyboard if you are trying to use some custom class to read user input.
I assume you are getting the error because you do not have the class Keyboard. Check for a file called Keyboard.java.. This is more of a comment than an answer.
Firstly you are using public for a class CheckoutProgram so the file name should be same as the class name when you used public access specifier for a class.
Secondly the Keyboard class is missing in your program so please check with these issues.

Categories