It seems that 20 regiments were in a continuous process of formation. The first had 1000 men, the second had 950, the third 900, and so on down to the twentieth regiment, which garrisoned only 50. During each week, 100 men were added to each regiment, and at week's end, the largest regiment was sent off to the front.This lasted for a total of 20 weeks.
For this program I have already managed to print out the original number of men for each regiment. But I am having difficult adding 100 men to each regiment.The adding men must be a method in the army class. I am getting the regiment objects using a .txt file. All this files contains is the names of regiments numbered 1-20.
public class Regiment {
private String name; //name of regiment
private int regNumber; //regiment number
private int men; // regiment men
/**
* Creates a Regiment object.
*
* #param regNumber the regiment number
* #param name the name of the regiment
* #param men the number of men in a regiment
*/
public Regiment(int regNumber, String name, int men) {
this.name = name;
this.regNumber = regNumber;
this.men = men;
}
/**
* Returns the name of the regiment.
*
* #return the regiment name
*/
public String getName() {
return name;
}
/**
* Returns the number of the regiment.
*
* #return regiment number
*/
public int getregNumber() {
return regNumber;
}
/**
* Returns the number of men in a regiment.
*
* #return men in regiment
*/
public int getMen() {
return men;
}
/**
* Computes the number of men in a regiment
*/
public int addMen2(int RegNumber) {
int men = 1050 - (regNumber * 50);
return men;
}
}
class ArmyDataList {
public ArrayList<Regiment> list; // list of regiment objects
/**
* Creates an empty list
*/
public ArmyDataList() {
list = new ArrayList<Regiment>();
}
/**
* Appends a regiment object to the list.
*
* #param current the object to be appended to the list
*/
public void AddToList(Regiment current) {
list.add(current);
}
/**
* Removes a regiment object to the list.
*
* #param current the object to be removed from the list
*/
public void RemoveFromList(Regiment current) {
list.remove(current);
}
/**
* Gets the largest regiment based on men.
*/
public Regiment getLargest() {
if (list.isEmpty()) {
return null;
}
Regiment Reg1 = list.get(0);
for (int i = 1; i < list.size(); i++) {
Regiment current = list.get(i); // get next regiment
// is current regiment > largest
if (current.getMen() > Reg1.getMen()) {
Reg1 = current;
}
}
return Reg1;
}
/**
* Adds men to each regiment.
*/
public void addMen() {
}
/**
* Converts the list to a multi-line string, with each line containing the
* data for one regiment.
*
* #return the String containing all the data on the list
*/
public String toString() {
String out
= String.format("%28s%12s%n", "Regiments", " Men")
+ String.format("%12s%n", "Number")
+ String.format("%12s%16s%14s%n", "=======", "===============",
"=========");
for (int i = 0; i < list.size(); i++) {
Regiment regim = list.get(i);
int regNumber = regim.getregNumber();
String name = regim.getName();
int men = regim.addMen2(regNumber);
out = out + String.format("%12s", regNumber)
+ String.format("%16s", name)
+ String.format("%10s", men)
+ "\n";
}
return out + "\n";
}
}
public class RegimentTest {
public static void main(String[] args) throws IOException
{
ArmyDataList army = new ArmyDataList();
// create Scanner object to read each line of file until eof
Scanner fileScan = new Scanner(new File("regiments.txt"));
System.out.println("Report Summary:\n");
while (fileScan.hasNext()) // while not eof...
{
// read next line
String line = fileScan.nextLine();
// "echo print" data entered
System.out.println(line);
// 1. create a Scanner object
Scanner in = new Scanner(line) ;
// 2. extract tokens from current line
int regNumber = in.nextInt();
String name = in.next();
int men = 0 ; //men is set to 0 only because I havent add the men yet
// 3. create Regiment object passing the tokens to the constructor
Regiment adder = new Regiment(regNumber, name, men );
// 4. add object to list
army.AddToList(adder) ;
}
System.out.println(army.toString());
}
You can write addNewMen() method in Regiment Class where you can add 100 more men as men is the property of regiment.
Related
Having successfully created a policy holder and writing the information to csv I try to run the program again using a do while loop but receive an inputmismatchexception after typing surname.
Can someone point me in the right direction or advise what I am doing wrong with this piece of code?
Here is example of what happens...
console
/**
*
*/
package InsurancePolicySystem;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* #author
*
*/
public class InsurancePolicy {
// create variables
private String surname;
private int age;
private String motorType;
private String motorPolicyId;
private String addAnother;
private String pattern = "^[a-zA-Z](\\s?[a-zA-Z]){2,29}$";
// import scanner for user input
Scanner scanner = new Scanner(System.in);
// time stamp number. -1 used on month as time stamp displays next year instead
// of current year
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH) - 1;
// sum of the year and month
int timeStamp = year + month;
/**
* default constructor
*/
public InsurancePolicy() {
}
/**
* non default constructor
*
* #param surname
* #param age
* #param motorType
* #param motorPolicyId
*/
public InsurancePolicy(String surname, int age, String motorType, String motorPolicyId) {
this.surname = surname;
this.age = age;
this.motorType = motorType;
this.motorPolicyId = motorPolicyId;
}
/**
*
* #return
*/
public String getSurname() {
return surname;
}
/**
* Surname should be more than 3 letters and not greater than 20. Anything more
* will cause a not valid error!
*
* #param surname
* #throws IllegalArgumentException
*/
public void setSurname(String surname) throws IllegalArgumentException {
this.surname = surname;
}
/**
*
* #return
*/
public int getAge() {
return age;
}
/**
* Age must be between 18 & 50 Any other age will produce not eligible error!
*
* #param age
* #throws IllegalArgumentException
*/
public void setAge(int age) throws IllegalArgumentException {
if ((age >= 18) && (age <= 50)) {
this.age = age;
} else {
throw new IllegalArgumentException("Not eligible for an insurance policy!");
}
}
/**
*
* #return
*/
public String getMotorType() {
return motorType;
}
/**
* motor type should only be either CAR, TAXI or BUS. if statement to prevent
* other vehicle types and throw error if user inputs a different vehicle.
*
* #param motorType
* #throws IllegalArgumentException
*/
public void setMotorType(String motorType) throws IllegalArgumentException {
if (motorType.equals("CAR") || motorType.equals("TAXI") || motorType.equals("BUS")) {
this.motorType = motorType;
} else {
throw new IllegalArgumentException("Not eligible for an insurance policy!");
}
this.motorType = motorType;
}
/**
*
* #return
*/
public String getMotorPolicyId() {
return motorPolicyId;
}
/**
*
* #param motorPolicyId
*/
public void setMotorPolicyId(String motorPolicyId) {
this.motorPolicyId = motorPolicyId;
}
/**
* Method to write policy holder particulars to a csv file. Creates ArrayList
* and uses collect joining to add comma for file.
*
* #throws IOException
*/
public void writeToFile() throws IOException {
BufferedWriter writer = new BufferedWriter(
new FileWriter("/Users/johnj/eclipse-workspace/InsuranceProject/insurance.csv", true));
// creates policy holder (insurance) array
ArrayList<String> insurance = new ArrayList<String>();
insurance.add(surname);
insurance.add(Integer.toString(age));
insurance.add(motorType);
insurance.add(motorPolicyId);
// write array to csv file
String collect = insurance.stream().collect(Collectors.joining(", "));
System.out.println(collect);
writer.write(collect);
writer.newLine();
writer.flush();
writer.close();
}
/**
* Builds and generates user input. Will then add it into an array list and
* append to writeToFile(). Included do while loop for user to continue creating
* policy profiles without having to re run program.
*
* #param motorType
*/
public void addPolicyHolder() throws IOException {
try {
do {
// enter policy holder surname. Checks pattern to ensure compliance
System.out.println("Please type your surname...");
surname = scanner.next(pattern).toUpperCase().trim();
setSurname(this.surname);
// user input age
System.out.println("Please type your age...");
age = scanner.nextInt();
setAge(this.age);
// user input motor type which is converted to upper case
System.out.println("Please type your motor type i.e. CAR, TAXI or BUS...");
motorType = scanner.next().toUpperCase().trim();
setMotorType(this.motorType);
System.out.println(); // used to create next line space
// Motor Policy Id should be 3 characters from surname as well as time stamp. If
// time stamp odd number add 1 or else add 0 if even.
motorPolicyId = surname.substring(0, 3) + timeStamp;
if (timeStamp % 2 == 0) {
motorPolicyId = this.motorPolicyId + 0;
} else {
motorPolicyId = this.motorPolicyId + 1;
}
// creates csv file
writeToFile();
// prints completed user input of policy holder
System.out.println();
System.out.println("Surname :" + this.surname);
System.out.println("Age :" + this.age);
System.out.println("Policy Ref :" + this.motorPolicyId);
System.out.println("Motor Type: :" + this.motorType);
System.out.println(); // used to create next line space
// asks user if they would like to create a different policy holder
System.out.println("Add another Policy Holder (Y or N)..");
addAnother = scanner.next();
} while (addAnother.equalsIgnoreCase("Y")); // If N program ends.
} catch (Exception exception) { // if exception program ends
System.out.println(
"There is a problem ... Policy Holder does meet necessary criteria, run the program again.");
} finally {
scanner.close();
System.out.println("Program ended!"); // end of program
}
}
public static void main(String[] args) throws IOException {
InsurancePolicy insurancePolicy = new InsurancePolicy();
insurancePolicy.addPolicyHolder();
}
}
I am able to complete a first run but do while loop fails when trying to add another policy holder.
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 3 years ago.
I am not able to print out the info of the candy bar. Whenever I try to use the method I get an error: cannot find symbol-method printINFO.
I have tried using n.printInfo() and system.out.println(n.printinfo()), but still it doesnt work. What can I do?
Method class:
public class CandyBar
{
public enum Color
{
BLUE, BROWN, GOLD, GREEN, ORANGE,
PURPLE, RED, SILVER, WHITE, YELLOW
}
// instance variables
private String name;
private double weight;
private Color wrapper;
/**
* Constructor for objects of class CandyBar
*/
public CandyBar(String n, Color c, double w)
{
this.name = n;
this.weight = w;
this.wrapper = c;
}
public String getName()
{
return this.name;
}
public double getWeight()
{
return this.weight;
}
public Color getWrapperColor()
{
return this.wrapper;
}
public void printInfo()
{
System.out.println(this.name);
System.out.printf("%.1f oz with a ", this.weight);
System.out.println(this.wrapper + " wrapper");
}
}
Main class:
import java.util.ArrayList;
public class Lab16
{
public static void main(String[] args)
{
ArrayList<CandyBar> bars = new ArrayList<CandyBar>();
addCandyBarsToList(bars);
/**
* Use the methods you wrote below to answer all of
* the following questions.
*/
/**
* Part A:
*/
/**
* Is Twix in the list?
* If so, print all information about it.
* If not, print a message to the user.
*/
/**
* Is Mounds in the list?
* If so, print all information about it.
* If not, print a message to the user.
*/
/**
* Part B:
*/
/**
* Print all the names of candy bars with yellow wrappers.
*/
/**
* Part C:
*/
/**
* Count how many candy bars are 1.75 oz or more.
*/
/**
* Part D:
*/
/**
* Is there a candy bar that is 1.86 oz?
* If so, print the information.
* If not, print a message to the user.
* Write a binary search method to get the answer.
*/
}
/**
* This method searches a list to find a candy bar by name.
*
* #param list the list to search
* #param n a name to find
* #return the index of the candy bar
*/
public static int findCandyBar(ArrayList<CandyBar> list, String n)
{
for(int i = 0; i < list.size(); i++){
if(list.get(i).getName() == n){
return n.printInfo(i);
}else{
System.out.println("The list doesnt have that Candy");
}
}
return 1;
}
/**
* This method prints the names of the candy bars that have a certain
* wrapper color.
*
* #param list the list to search
* #param c the color wrapper to find
*/
public static void findByColor(ArrayList<CandyBar> list, CandyBar.Color c)
{
}
/**
* This method counts the number of candy bars that weigh greater than
* or equal to the weight input parameter.
*
* #param list the list to search
* #param w the weight to compare to
* #return the count of candy bars
*/
public static int countByWeight(ArrayList<CandyBar> list, double weight)
{
return 0;
}
/**
* This method searches a list using binary search.
*
* #param list the list to search
* #param first the first index
* #param last the last index
* #param w the value to search for
* #return the index of the candy bar
*/
public static int binaryFind(ArrayList<CandyBar> list, int first, int last, double w)
{
return -1;
}
/**
* This method adds candy bars to a list.
*
* #param list the list to add to
*/
public static void addCandyBarsToList(ArrayList<CandyBar> list)
{
CandyBar kitkat = new CandyBar("KitKat", CandyBar.Color.RED, 1.5);
list.add(kitkat);
CandyBar grand = new CandyBar("One-hundred Grand", CandyBar.Color.RED, 1.5);
list.add(grand);
CandyBar crunch = new CandyBar("Crunch", CandyBar.Color.BLUE, 1.55);
list.add(crunch);
CandyBar hershey = new CandyBar("Hershey", CandyBar.Color.BROWN, 1.55);
list.add(hershey);
CandyBar krackel = new CandyBar("Krackel", CandyBar.Color.RED, 1.55);
list.add(krackel);
CandyBar caramello = new CandyBar("Caramello", CandyBar.Color.PURPLE, 1.6);
list.add(caramello);
CandyBar what = new CandyBar("Whatchamacallit", CandyBar.Color.YELLOW, 1.6);
list.add(what);
CandyBar almond = new CandyBar("Almond Joy", CandyBar.Color.BLUE, 1.61);
list.add(almond);
CandyBar goodbar = new CandyBar("Mr. Goodbar", CandyBar.Color.YELLOW, 1.75);
list.add(goodbar);
CandyBar twix = new CandyBar("Twix", CandyBar.Color.GOLD, 1.79);
list.add(twix);
CandyBar henry = new CandyBar("Oh Henry", CandyBar.Color.YELLOW, 1.8);
list.add(henry);
CandyBar milkyWay = new CandyBar("Milky Way", CandyBar.Color.GREEN, 1.84);
list.add(milkyWay);
CandyBar payDay = new CandyBar("PayDay", CandyBar.Color.ORANGE, 1.85);
list.add(payDay);
CandyBar snickers = new CandyBar("Snickers", CandyBar.Color.BLUE, 1.86);
list.add(snickers);
CandyBar butterfinger = new CandyBar("Butterfinger", CandyBar.Color.YELLOW, 1.9);
list.add(butterfinger);
CandyBar musketeers = new CandyBar("Three Musketeers", CandyBar.Color.SILVER, 1.92);
list.add(musketeers);
CandyBar reeses = new CandyBar("Reese's FastBreak", CandyBar.Color.ORANGE, 2);
list.add(reeses);
CandyBar babyRuth = new CandyBar("Baby Ruth", CandyBar.Color.SILVER, 2.1);
list.add(babyRuth);
}
}
The printInfo() method is declared on the CandyBar object, but here you are trying to call printInfo() on a String object. This method does not exist on String.
/**
* This method searches a list to find a candy bar by name.
*
* #param list the list to search
* #param n a name to find
* #return the index of the candy bar
*/
public static int findCandyBar(ArrayList<CandyBar> list, String n)
{
for(int i = 0; i < list.size(); i++){
if(list.get(i).getName() == n){
return n.printInfo(i); <-- calling n.printInfo(i) will not work as printInfo() does not exist on String
You instead need to pay attention to the method signature of printInfo():
It exists on CandyBar, not on String.
It does not accept any parameter arguments.
It is a void method, i.e. does not return an object.
So instead of:
return n.printInfo(i);
Use this:
i.printInfo();
I have a project that is a hardware store consisting of methods that save, search, and edit data. The data is the items stored in a txt database. I have to create a JUNIT test case that tests the methods of the hardware store class. There are three classes in total: hardwarestore, mainapp, and item. But the only one that needs to be tested is hardwarestore. And the only methods that don't need to be tested are the readDatabase() and the writeDatabase().
For my JUNIT classes I have hardwarestoretest that I will implement with test methods for the hardwarestore class. I also have a testrunner class that runs the test methods in hardwarestoretest. The test methods will compose of assertion tests.
Assertions
I am fairly new with JUNIT testing and need just some help getting started. I wrote part of the test classes but they fail when ran. The output I get says that there are no runnable tests even though I tried to implement a test method for addNewItem.
Here is my code for hardwarestore in a hardwarestore package
Item.java
package hardwarestore;
/**
* This class is a very simple representation of a hardware item. There are only getter
* methods and no setter methods and as a result an item cannot be mutated once
* initialized. An item object can also call the two override methods
* <CODE>toString()</CODE> and <CODE>equals()</CODE>
*
* #author Junye Wen
*/
public class Item {
private final String idNumber;
private final String name;
private final String category;
private int quantity;
private final float price;
/**
* This constructor initializes the item object. The constructor provides no
* user input validation. That should be handled by the class that creates a
* item object.
*
* #param idNumber a <b><CODE>String</CODE></b> that represents the ID
* random string of length 5 – can contain letters and numbers
*
* #param name a <b><CODE>String</CODE></b> that represents the name.
*
* #param category a <b><CODE>String</CODE></b> that represents the category.
* Door&Window, Cabinet& Furniture, Fasteners, Structural, Other.
*
* #param quantity a <b><CODE>int</CODE></b> that represents the quantity
*
* #param price an <b><CODE>float</CODE></b> that represents the price
*
*/
public Item(String idNumber, String name, String category, int quantity, float price) {
this.idNumber = idNumber;
this.name = name;
this.category = category;
this.quantity = quantity;
this.price = price;
}
/**
* This method returns the item's tracking number.
*
* #return a <b><CODE>String</CODE></b> that is the ID number of the item.
*/
public String getIdNumber() {
return idNumber;
}
/**
* This method returns the item's name.
*
* #return a <b><CODE>String</CODE></b> that is the item's name.
*/
public String getName() {
return name;
}
/**
* This method returns the item's category.
*
* #return a <b><CODE>String</CODE></b> that is the item's category.
*/
public String getCategory() {
return category;
}
/**
* This method returns the item's quantity.
*
* #return an <b><CODE>int</CODE></b> that is the item's weight
*/
public int getQuantity() {
return quantity;
}
/**
* This method set the item's quantity.
*
* #param quantity a <b><CODE>int</CODE></b> that represents the quantity
*/
public void setQuantity(int quantity) {
this.quantity= quantity;
}
/**
* This method returns the item's price.
*
* #return a <b><CODE>float</CODE></b> that is the item's price
*/
public float getPrice() {
return price;
}
/**
* This method returns the item's fields as a string representation.
*
* #return a <b><CODE>String</CODE></b> that lists the fields of the item
* object delineated by a space and in the same order as the constructor
*/
#Override
public String toString() {
return idNumber + "~" + name + "~" + category + "~" + quantity + "~"
+ String.format("%.2f", price) + "\n";
}
/**
* This method provides a way to compare two item objects.
*
* #param c a <b><CODE>Item</CODE></b> object that is used to compare to
* <b><CODE>this</CODE></b> item. Two orders are equal if their ID is the
* same.
* #return the <CODE>boolean</CODE> value of the comparison.
*/
public boolean equals(Item c) {
return c.getIdNumber().equals(this.idNumber);
}
}
HardwareStore.java
package hardwarestore;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class is used to represent a database interface for a list of
* <CODE>item</CODE>'s. It using a plain-text file "database.txt" to store and
* write item objects in readable text form. It contains an
* <CODE>ArrayList</CODE> called <CODE>itemList</CODE> to store the database in
* a runtime friendly data structure. The <CODE>itemList</CODE> is written to
* "database.txt" at the end of the <CODE>HardwareStore</CODE> object's life by
* calling <CODE>writeDatabase()</CODE>. This class also provides methods for
* adding, removing, and searching for items in the list.
*
* #author Junye Wen
*/
public class HardwareStore {
private ArrayList<Item> itemList;
private static final String DATA_FILE_NAME = "database.txt";
/**
* This constructor creates an empty ArrayList and then calls the
* <CODE>readDatabase()</CODE> method to populate items previously stored.
*
* #throws IOException
*/
public HardwareStore() throws IOException {
itemList = new ArrayList<>();
readDatabase();
}
/**
* Method getAllItemsFormatted returns the current list of items in the Arraylist in
* no particular order.
*
* #return a formatted String representation of all the items in itemList.
*/
public String getAllItemsFormatted() {
return getFormattedItemList(itemList);
}
/**
* Private method getFormattedPackageList used as an auxiliary method to return a given ArrayList
* of items in a formatted manner.
*
* #param items the item list to be displayed.
* #return a formatted String representation of all the items in the list give as a parameter.
*/
private String getFormattedItemList(ArrayList<Item> items) {
String text = " ------------------------------------------------------------------------------------\n" +
String.format("| %-10s| %-25s| %-20s| %-10s| %-10s|%n", "ID Number", "Name", "Category", "Quantity", "Price") +
" ------------------------------------------------------------------------------------\n";
for (int i = 0; i < items.size(); i++) {
text += String.format("| %-10s| %-25s| %-20s| %-10s| %-10s|%n",
items.get(i).getIdNumber(),
items.get(i).getName(),
items.get(i).getCategory(),
Integer.toString(items.get(i).getQuantity()),
String.format("%.2f", items.get(i).getPrice()));
}
text += " ------------------------------------------------------------------------------------\n";
return text;
}
/**
* This method is used to add a item to the itemList ArrayList.
*
* #param idNumber a <CODE>String</CODE> representing the ID number of item
* #param name a <CODE>String</CODE> representing the name of item
* #param category a <CODE>String</CODE> representing the category of item
* #param quantiy an <CODE>int</CODE> representing the quantiy of item
* #param price a <CODE>float</CODE> representing the price of item
*/
public void addNewItem(String idNumber, String name, String category, int quantiy, float price) {
//If passed all the checks, add the item to the list
itemList.add(new Item(idNumber, name, category, quantiy, price));
System.out.println("Item has been added.\n");
}
/**
* Add a certain quantity of the given item index.
* Preconditions: 1. Item exists.
* #param itemIndex the index of the item in the itemList
* #param quantity the quantity to remove
*/
public void addQuantity(int itemIndex, int quantity) {
Item temp = getItem(itemIndex);
temp.setQuantity(temp.getQuantity() + quantity);
System.out.println("Quantity updated.\n");
}
/**
* Removes a certain quantity of the given item index.
* Preconditions: 1. Item exists. 2. Quantity to remove smaller than current quantity.
* #param itemIndex the index of the item in the itemList
* #param quantity the quantity to remove
*/
public void removeQuantity(int itemIndex, int quantity) {
Item temp = getItem(itemIndex);
temp.setQuantity(temp.getQuantity() - quantity);
System.out.println("Quantity updated.\n");
}
/**
* Returns all the items that (partially) match the given name.
* #param name the name to match.
* #return a string containing a table of the matching items.
*/
public String getMatchingItemsByName(String name) {
ArrayList<Item> temp = new ArrayList<Item>();
for (Item tempItem : itemList) {
if (tempItem.getName().toLowerCase().contains(name.toLowerCase())) {
temp.add(tempItem);
}
}
if (temp.size() == 0) {
return null;
} else {
return getFormattedItemList(temp);
}
}
/**
* Returns all the items with current quantity lower than (or equal) the
* given threshold.
* #param quantity the quantity threshold.
* #return a string containing a table of the matching items.
*/
public String getMatchingItemsByQuantity(int quantity) {
ArrayList<Item> temp = new ArrayList<Item>();
for (Item tempItem : itemList) {
if (tempItem.getQuantity() <= quantity) {
temp.add(tempItem);
}
}
if (temp.isEmpty()) {
return null;
} else {
return getFormattedItemList(temp);
}
}
/**
* This method can be used to find a item in the Arraylist of items.
*
* #param idNumber a <CODE>String</CODE> that represents the ID number of
* the item that to be searched for.
* #return the <CODE>int</CODE> index of the items in the Arraylist of
* items, or -1 if the search failed.
*/
public int findItem(String idNumber) {
int index = -1;
for (int i = 0; i < itemList.size(); i++) {
String temp = itemList.get(i).getIdNumber();
if (idNumber.equalsIgnoreCase(temp)) {
index = i;
break;
}
}
return index;
}
/**
* This method is used to retrieve the Item object from the
* <CODE>itemList</CODE> at a given index.
*
* #param i the index of the desired <CODE>Item</CODE> object.
* #return the <CODE>Item</CODE> object at the index or null if the index is
* invalid.
*/
public Item getItem(int i) {
if (i < itemList.size() && i >= 0) {
return itemList.get(i);
} else {
System.out.println("Invalid Index.\n");
return null;
}
}
/**
* This method opens the database file and overwrites it with a
* text representation of all the items in the <CODE>itemList</CODE>. This
* should be the last method to be called before exiting the program.
*
* #throws IOException
*/
public void writeDatabase() throws IOException {
PrintWriter pw = new PrintWriter(DATA_FILE_NAME);
for (Item c : itemList) {
pw.print(c.toString());
}
pw.close();
}
/**
* The method opens the database file and initializes the <CODE>itemList</CODE>
* with its contents. If no such file exists, then one is created.
* The contents of the file are "loaded" into the itemList ArrayList in no
* particular order. The file is then closed during the duration of the
* program until <CODE>writeDatabase()</CODE> is called.
*
* #throws IOException
*/
public void readDatabase() throws IOException {
File dataFile = new File(DATA_FILE_NAME);
// If data file does not exist, create it.
if (!dataFile.exists()) {
System.out.println("database.txt does not exist, creating one now . . .");
//if the file doesn't exists, create it
PrintWriter pw = new PrintWriter(DATA_FILE_NAME);
//close newly created file so we can reopen it
pw.close();
return; // No need to try to read anything from an empty file, so return.
}
Scanner itemScanner = new Scanner(new FileReader(dataFile));
//Initialize the Array List with items from database.txt
while (itemScanner.hasNextLine()) {
// split values using the space character as separator
String[] temp = itemScanner.nextLine().split("~");
itemList.add(new Item(temp[0], temp[1], temp[2],
Integer.parseInt(temp[3]), Float.parseFloat(temp[4])));
}
//item list is now in the ArrayList completely so we can close the file
itemScanner.close();
}
}
MainApp.java
package hardwarestore;
import java.io.IOException;
import java.util.Scanner;
/**
* This is the main class of the Hardware Store database manager. It provides a
* console for a user to use the 5 main commands.
*
* #author Junye Wen
*/
public class MainApp {
// This object will allow us to interact with the methods of the class HardwareStore
private final HardwareStore hardwareStore;
private static final Scanner CONSOLE_INPUT = new Scanner(System.in); // Used to read from System's standard input
/**
* Default constructor. Initializes a new object of type HardwareStore
*
* #throws IOException
*/
public MainApp() throws IOException {
hardwareStore = new HardwareStore();
}
/**
* Shows all items in the inventory.
*/
public void showAllItems() {
System.out.print(hardwareStore.getAllItemsFormatted());
}
/**
* This method will add items quantity with given number. If the item does
* not exist, it will call another method to add it.
*
*/
public void addItemQuantity() {
System.out.println("Please input the ID of item");
String idNumber = CONSOLE_INPUT.nextLine();
if (!idNumber.matches("[A-Za-z0-9]{5}")) {
System.out.println("Invalid ID Number: not proper format. "
+ "ID Number must be 5 alphanumeric characters.\n");
return;
}
int itemIndex = hardwareStore.findItem(idNumber);
if (itemIndex != -1) { // If item exists in the database
System.out.println("Item found in database. Please enter quantity to add.");
int quantity = CONSOLE_INPUT.nextInt();
if (quantity <= 0) {
System.out.println("Invalid quantity. "
+ "The addition amount must be larger than 0.\n");
return;
}
hardwareStore.addQuantity(itemIndex, quantity);
} else {
//If it reaches here, the item does not exist. We need to add new one.
System.out.println("Item with given number does not exist.\n");
// Enter name
System.out.println("\nPlease type the name of item.");
String name = CONSOLE_INPUT.nextLine();
// Entery category
String category = null;
System.out.println("\nPlease select the category of item.");
System.out.println("1: Door&Window\n2: Cabinet&Furniture\n3: Fasteners\n4: Structural\n5: Other");
int selection = CONSOLE_INPUT.nextInt();
switch (selection) {
case 1:
category = "Door&Window";
break;
case 2:
category = "Cabinet&Furniture";
break;
case 3:
category = "Fasteners";
break;
case 4:
category = "Structural";
break;
case 5:
category = "Other";
break;
default:
System.out.println("Invalid category number.");
return;
}
// Entery quantity
System.out.println("\nPlease type the quantity of the item.");
int quantity = CONSOLE_INPUT.nextInt();
if (quantity < 0) {
System.out.println("Invalid price. "
+ "The quantity cannot be smaller than 0.");
return;
}
// Enter price
System.out.println("\nPlease type the price of the item.");
float price = CONSOLE_INPUT.nextFloat();
if (price < 0) {
System.out.println("Invalid price. "
+ "The price cannot be smaller than 0.");
return;
}
hardwareStore.addNewItem(idNumber, name, category, quantity, price);
}
}
/**
* This method will remove the given quantity of an item with given number.
* If the item does not exist, it will show an appropriate message.
*/
public void removeItemQuantity() {
System.out.println("Please input the ID of item");
String idNumber = CONSOLE_INPUT.nextLine();
if (!idNumber.matches("[A-Za-z0-9]{5}")) {
System.out.println("Invalid ID Number: not proper format. "
+ "ID Number must be at least 5 alphanumeric characters.");
return;
}
int itemIndex = hardwareStore.findItem(idNumber);
int currentQuantity;
if (itemIndex == -1) {
System.out.println("Item does not exist.\n");
return;
} else {
currentQuantity = hardwareStore.getItem(itemIndex).getQuantity();
System.out.println("Current quantity: " + currentQuantity + "\n");
}
System.out.println("Please input the quantity to remove.");
int quantity = CONSOLE_INPUT.nextInt();
if (quantity > currentQuantity) {
System.out.println("Invalid quantity. "
+ "The removal amount must be smaller than current quantity.\n");
} else {
hardwareStore.removeQuantity(itemIndex, quantity);
}
}
/**
* This method can search item by a given name (part of name.
* Case-insensitive.) Will display all items with the given name.
*/
public void searchItemByName() {
System.out.println("Please input the name of item.\n");
String name = CONSOLE_INPUT.nextLine();
String output = hardwareStore.getMatchingItemsByName(name);
if (output == null) {
System.out.println("Item not found.");
} else {
System.out.println(output);
}
}
/**
* This method can search item below a certain quantity. Will display all
* items fits such condition.
*/
public void searchItemByQuantity() {
System.out.println("Please enter the quantity:\n");
int quantity = CONSOLE_INPUT.nextInt();
if (quantity < 0) {
System.out.println("Quantity should be at least 0.\n");
}
String output = hardwareStore.getMatchingItemsByQuantity(quantity);
if (output == null) {
System.out.println("No items found below given quantity.");
} else {
System.out.println(output);
}
}
public void saveDatabase() throws IOException {
hardwareStore.writeDatabase();
}
/**
* This method will begin the user interface console. Main uses a loop to
* continue executing commands until the user types '6'.
*
* #param args this program expects no command line arguments
* #throws Exception
*/
public static void main(String[] args) throws Exception {
MainApp app = new MainApp();
String welcomeMessage = "\nWelcome to the Hardware Store database. Choose one of the following functions:\n\n"
+ "\t1. Show all existing items in stock and their quantities.\n"
+ "\t2. Add a new quantity of a specific item to the stock.\n"
+ "\t3. Remove a certain quantity of a specific item type.\n"
+ "\t4. Search for an item (given its name or part of its name).\n"
+ "\t5. Show a list of all items below a certain quantity.\n"
+ "\t6. Exit program.\n";
System.out.println(welcomeMessage);
int selection = CONSOLE_INPUT.next().charAt(0);
CONSOLE_INPUT.nextLine();
while (selection != '6') {
switch (selection) {
case '1':
app.showAllItems();
break;
case '2':
app.addItemQuantity();
break;
case '3':
app.removeItemQuantity();
break;
case '4':
app.searchItemByName();
break;
case '5':
app.searchItemByQuantity();
break;
case 'h':
System.out.println(welcomeMessage);
break;
default:
System.out.println("That is not a recognized command. Please enter another command or 'h' to list the commands.");
break;
}
System.out.println("Please enter another command or 'h' to list the commands.\n");
selection = CONSOLE_INPUT.next().charAt(0);
CONSOLE_INPUT.nextLine();
}
CONSOLE_INPUT.close();
System.out.print("Saving database...");
app.saveDatabase();
System.out.println("Done!");
}
}
Here are my test classes in a test package
HardwareStoreTest.java
package test;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Test;
import hardwarestore.*;
public class HardwareStoreTest {
public static HardwareStore hardwarestore = null;
#BeforeClass
public static void createEnvironment() throws IOException {
hardwarestore = new HardwareStore();
}
#AfterClass
public static void clearEnvironment() {
hardwarestore = null;
System.out.println("Environment cleared");
}
#Test
public static void testAddItem() {
hardwarestore.addNewItem("123543","sink","other",23,24.95f);
assertNotNull("Test Failed message", hardwarestore.getItem(0));
}
}
TestRunner.java
package test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(HardwareStoreTest.class);
if(result.getFailures().size()==0) {
System.out.println("All tests successfull!");
}else {
System.out.println("No. of failed test cases = "+ result.getFailures().size());
for(Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
}
Change your import at the top and the Test shouldn't be static.
import org.junit.Test
#Test
public void addNewItem(){
hardwarestore.addNewItem("123RT", "sink", "kitchen", 34, 4.51f);
assertNotNull("Test Failed Message", hardwarestore.getItem(0));
}
I had a project for school that I could just not get to compile correctly.
The instructions can be found at this link.
I believe I have the class created correctly in Product.java. My code is below:
import java.util.*;
public class Product {
// Private member variables go here - you will need to add them yourself.
private String name;
private String code;
private int quantity;
private double price;
private String type;
private ArrayList<Integer> userRatings;
/*
* Product constructor
*/
public Product() {
name = "";
code = "";
quantity = 0;
price = 0;
type = "";
userRatings = new ArrayList<Integer>();
}
/*
* setName
* #param name - new name for the product
*/
public void setName(String name) {
this.name = name;
}
/*
* getName
* #return the name of the product
*/
public String getName() {
return name;
}
/*
* setType
* #param type - the type of the product
*/
public void setType(String type) {
this.type = type;
}
/*
* getType
* #return - the product type
*/
public String getType() {
return type;
}
/*
* setPrice
* #param price - the price of the product
*/
public void setPrice(double price) {
this.price = price;
}
/*
* getPrice
* #return the price of the product
*/
public double getPrice() {
return price;
}
/*
* setQuantity
* #param quantity - the number of this product in inventory
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
/*
* getQuantity
* #return the number of this product in inventory
*/
public int getQuantity() {
return quantity;
}
/*
* setInventoryCode
* #param code - the new inventory code for the product
*/
public void setInventoryCode(String code) {
this.code = code;
}
/*
* getInventoryCode
* #return the inventory code of the product
*/
public String getInventoryCode() {
return code;
}
/*
* addUserRating
* NOTE: Each individual rating is stored with the product, so you need to maintain a list
* of user ratings. This method should append a new rating to the end of that list
* #param rating - the new rating to add to this product
*/
public void addUserRating(int rating) {
userRatings.add(rating);
}
/*
* getUserRating
* NOTE: See note on addUserRating above. This method should be written to allow you
* to access an individual value from the list of user ratings
* #param index - the index of the rating we want to see
* #return the rating indexed by the value index
*/
public int getUserRating(int index) {
return userRatings.get(index);
}
/*
* getUserRatingCount
* NOTE: See note on addUserRating above. This method should be written to return
* the total number of ratings this product has associated with it
* #return the number of ratings associated with this product
*/
public int getUserRatingCount() {
return userRatings.size();
}
/*
* getAvgUserRating
* NOTE: see note on addUserRating above. This method should be written to compute
* the average user rating on demand from a stored list of ratings.
* #return the average rating for this product as a whole integer value (use integer math)
*/
public int getAvgUserRating() {
int sum = 0;
if(userRatings.size() > 0){
for (int i = 0; i < userRatings.size(); i++){
sum += userRatings.get(i);
}
return sum / userRatings.size();
}
else return 0;
}
}
But the problem lies within the test code. I have tried multiple ways and keep getting the same InputMismatchException error. I will need an ArrayList of Product objects for part 2 so I tried to incorporate that into the test code. Below is ProductTest.java:
import java.util.*;
import java.io.*;
public class ProductTest {
/*
* A simple main loop to load product objects from a file
* and then display them to the console
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter an inventory filename: ");
String fname = keyboard.nextLine();
ArrayList<Product> products = loadProducts (fname);
displayProducts(products);
}
/*
* loadProducts
* Given a filename, opens the file and reads Products from
* the file into an ArrayList of Product objects. Returns the
* Arraylist.
*
* #param fname - String containing the input file name
* #return - An ArrayList of Product objects
*/
public static ArrayList<Product> loadProducts(String fname) {
ArrayList<Product> products = new ArrayList<Product>();
try {
Scanner inFile = new Scanner(new File(fname));
while (inFile.hasNext()) {
Product pr = new Product();
pr.setName(inFile.next());
pr.setInventoryCode(inFile.next());
pr.setQuantity(inFile.nextInt());
pr.setPrice(inFile.nextDouble());
pr.setType(inFile.next());
while(inFile.nextInt() != -1){
pr.addUserRating(inFile.nextInt());
}
products.add(pr);
}
inFile.close();
}
catch(IOException e) {
System.out.println("ERROR: "+e);
}
return products;
}
/*
* displayProducts
* Given an ArrayList of Product objects, outputs them
* to the console in the specified format.
*
* The format for this method is:
* NAME, INVENTORY_CODE, QUANTITY, PRICE, TYPE, RATING
*
* #param products - ArrayList of Product objects
*/
public static void displayProducts(ArrayList<Product> products) {
for(int i = 0; i<products.size(); i++) {
Product tmpProduct = products.get(i);
System.out.println(tmpProduct.getName());
System.out.println(tmpProduct.getInventoryCode());
System.out.println(tmpProduct.getQuantity());
System.out.println(tmpProduct.getPrice());
System.out.println(tmpProduct.getType());
System.out.println(tmpProduct.getAvgUserRating());
System.out.println();
}
}
}
This is the error message that results from running the current ProductTest:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at osu.cse2123.ProductTest.loadProducts(ProductTest.java:46)
at osu.cse2123.ProductTest.main(ProductTest.java:23)
The text file I'm using contains the following:
The Shawshank Redemption
C0000001
100
19.95
DVD
4
5
3
1
-1
The Dark Knight
C0000003
50
19.95
DVD
5
2
3
-1
Casablanca
C0000007
137
9.95
DVD
5
4
5
3
-1
The Girl With The Dragon Tattoo
C0000015
150
14.95
Book
4
4
2
-1
Vertigo
C0000023
55
9.95
DVD
5
5
3
5
2
4
-1
A Game of Thrones
C0000019
100
8.95
Book
-1
Any help on the issues would be much appreciated.
You are reading one word at a time. Currently your file is being read by the Scanner like this:
inFile.next() = The
inFile.next() = Shawshank
Tip: inFile.nextLine() = The Shawshank Redeption
You problem is in the while loop reading ratings:
while(inFile.nextInt() != -1){
pr.addUserRating(inFile.nextInt());
}
Each round of the loop reads two items - one in the while condition, and another one inside the loop. Since there's a String (the next title) after the ratings end, if there was an even number of ratings you'd fail here when trying to interpret it as an int (and if it doesn't fail you'd just get the wrong result, since you're skipping half the ratings). One way to fix this is to extract the rating to a local variable:
int rating;
while(rating = inFile.nextInt() != -1){
pr.addUserRating(rating);
}
I suggest you realise how many times you are invoking readInt consecutively. Imagine an alternative way to read a value, check it, and reuse it without re-reading.
Without a better formating of your file (no end of line), you need to use a regex matching :
Something like this :
public static ArrayList<Product> loadProducts(String fname) {
ArrayList<Product> products = new ArrayList<Product>();
Pattern p = Pattern.compile("([\\w ]*) (C[0-9]*) (\\d*) ([\\d\\.]*) (\\w*) ([\\d -]*)");
Matcher m = p.matcher(fname);
while (m.find()) {
Product pr = new Product();
pr.setName(m.group(1));
pr.setInventoryCode(m.group(2));
pr.setQuantity(Integer.parseInt(m.group(3)));
pr.setPrice(Double.parseDouble(m.group(4)));
pr.setType(m.group(5));
for (String rate : m.group(6).split(" ")) {
pr.addUserRating(Integer.parseInt(rate));
}
products.add(pr);
System.out.println(pr);
}
return products;
}
The InputMismatchException occurs when you call nextDouble() or nextInt() from a Scanner instance while instead you enter something other than a number. The error message shows that the flaw lies within the loadProducts method.
In that method you make a call to next() to get product name and inventory code. However, since your product name contains spaces, you should call nextLine() instead.
What happens in your program is this: Calling next() on "The Shawshank Redemption" will return "The". By the time you reach pr.setQuantity(inFile.nextInt()); your program will throw the InputMismatchException because it tries to read an integer from "Redemption".
Also take into account the advice from mureinik:
https://stackoverflow.com/a/25936439/4056420
I searched throughout the forums and could not find what I was looking for. I am doing an assignment for school and I need to implement some methods. I did most of them and am getting the output I need. However, I'm having trouble implementing the Cartesian Product (xproduct) method.
Here is my code so far:
import java.util.*;
public class Set
{
private ArrayList<String>elements;
/**
* creates an empty set
*/
public Set()
{
elements = null;
}
/**
* creates a set using the elements of the ArrayList s.
* #param s the ArrayList whose elements are used to create this set.
* #throws IllegalArgumentException if s contains duplicity.
*/
public Set(ArrayList<String> s)
{
int i;
elements = new ArrayList<String>();
for(i=0;i<s.size();i++)
{
if(elements.contains(s.get(i)))
{throw new IllegalArgumentException("Set(ArrayList<String>)duplicity not allowed in sets");}
elements.add(s.get(i));
}
}
/**
* creates a set using the elements of the array s.
* #param s the array whose elements are used to create this set.
* #throws illegalArgumentException if s contains duplicity.
*/
public Set(String[] s)
{
int i;
elements = new ArrayList<String>();
for(i=0; i<s.length; i++)
{
if (elements.contains(s[i]))
{throw new IllegalArgumentException("Set(String[]):duplicity not allowed in sets");}
elements.add(s[i]);
}
}
/**
* determines whether a set contains the specified element
* #param elt an element
* #return true if elt is an element of this set; otherwise, false
*/
public boolean isElement(String elt)
{
return elements.contains(elt);
}
/**
* determines the size of this set.
* #return the size of this set.
*/
public int cardinality()
{
return elements.size();
}
/**
* computes the intersection of this set and the
* specified set.
* #param s a set
* #return a set representing the intersection of this set
* and s.
*/
public Set intersect(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
for (i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)))
{result.add(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the union of this set and the specified set.
* #param s a sets
* #return a set representing the union of this set
* and s.
*/
public Set union(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
result.addAll(this.elements);
result.addAll(s.elements);
for(i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)))
{result.remove(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the difference between this set and the
* specified set.
* #param s a set
* #return a set representing the difference between
* this set and s.
*/
public Set diff(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
result.addAll(this.elements);
for(i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)))
{result.remove(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the symmetric difference between this set
* and the specified set.
* #param s a set
* #return a set representing the symmetrical difference
* between this set and s.
*/
public Set symDiff(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
result.addAll(this.elements);
result.addAll(s.elements);
for(i=0;i<s.cardinality();i++)
{
if (this.isElement(s.elements.get(i)) && s.isElement(this.elements.get(i)))
{result.remove(this.elements.get(i));
result.remove(s.elements.get(i));}
}
return new Set(result);
}
/**
* computes the Cartesian product for this set
* and the specified set.
* #param s a set
* #return a set representing the Cartesian product
* of this set and s.
*/
public Set xProduct(Set s)
{
int i;
ArrayList<String> result = new ArrayList<String>();
result.addAll(this.elements);
result.addAll(s.elements);
}
/**
* determines whether a set is empty
* #return true if this set is empty; otherwise, false
*/
public boolean isEmpty()
{
return elements.isEmpty();
}
/**
* determines whether this set is equal to the specified
* set.
* #param s a set
* #return true if this set is equal to s; otherwise, false
*/
public boolean equals(Set s)
{
return elements.equals(s.elements);
}
/**
* determines whether this set is a subset of the specified set.
* #param s a set
* #return true if this set is a subset of s; otherwise, false
*/
public boolean subset(Set s)
{
return elements.containsAll(s.elements);
}
/**
* determines whether this set is a proper subset of the specified set.
* #param s a set
* #return true if this set is a proper subset of s; otherwise, false
*/
public boolean properSubset(Set s)
{
if(elements.equals(s.elements) && elements.containsAll(s.elements))
{return false;}
else{
return true;
}
}
/**
* returns a string {x1,x2,...,xn} representing this set,
* where x1,x2,...,xn are elements of this set.
* #return a string representation of this set formatted
* as specified.
*/
#Override
public String toString()
{
return "{"+this.elements+"}";
}
public static void main(String[] args)
{
String[]a1 = {"2","4","6","8"};
String[]a2 = {"2","3","5","7"};
String[]a3 = {"1","3","5"};
Set s1 = new Set(a1);
Set s2 = new Set(a2);
Set s3 = new Set(a3);
System.out.print("S1 ="); System.out.printf("%s",s1);
System.out.println();
System.out.print("S2 ="); System.out.printf("%s",s2);
System.out.println();
System.out.print("S3 ="); System.out.printf("%s",s3);
System.out.println();System.out.println();
System.out.println("(S1 \u222A S2:)");
System.out.printf("%s \u222A %s = %s%n",s1,s2,s1.union(s2));
System.out.println();
System.out.println("(S1 \u2296 S2) \u222a (S1 \u2229 S2) \u222a (S2 \u2296 S1)");
System.out.printf("%s \u2296 %s \u222a %s \u2229 %s \u222a %s \u2296 %s = %s%n",s1,s2,s1,s2,s2,s1,s1.diff(s2).union(s1.intersect(s2).union(s2.diff(s1))));
//Cartesian Product of s1 and s2
//Cartesian product of s2 and s1
}
}
Any guidance would be appreciated.
I do not understand why you have diffculties, but the cartezian product of Set1 x Set2 could be computed something like this:
Product = {}
Set1. iterate with i
Set2.iterate with j
Product.append (pair(i,j))
If you still have difficulties, please tell me to code it in Java. For now I posted only the pseudocode.