In: Computer Science
Programming language to be used: Java
Exercises
Part 1) The Dog Class
In the first part of the lab, we are writing a class to represent a Dog. It should not have a main method.
Sebastian the Husky is 5 years
old.
$45.56 adoption fee
Part 2) The Sales Class
In the second part of the lab, we are writing a class to represent the buying of a Dog or multiple Dogs. Let’s not consider the logistics of buying Dogs in bulk.
Part 3) The DogMarket Class
This class should get a file name from the user. It should use the file to create a Sales class object. Then, it should prompt the user for details of the dogs they want to buy. Use a while loop, which uses the word “quit” to not buy a dog and the word “buy” to buy a dog – other words should be ignored, but should not quit the program. If the user says “buy”, ask them for all of the dog’s info, then make a new Dog object and save it to the file using the Sales object. Once the user is done buying dogs, the program should exit, being sure to use finalizeReport() on the sales object
//Dog.java
public class Dog {
private double price;
private String breed;
private String name;
private int age;
// default constructor
public Dog()
{
price = 45.56;
breed= "Husky";
name = "Sebastian";
age = 5;
}
//parameterized constructor
public Dog(String name, String breed, double price,
int age)
{
this.name = name;
this.breed = breed;
this.price = price;
this.age = age;
}
// setters
public void setName(String name)
{
this.name = name;
}
public void setBreed(String breed)
{
this.breed = breed;
}
public void setPrice(double price)
{
this.price = price;
}
public void setAge(int age)
{
this.age = age;
}
// getters
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
public double getPrice()
{
return price;
}
public int getAge()
{
return age;
}
//toString
public String toString()
{
return(name+" the "+breed+" is
"+age+" years old.\n"+"$"+String.format("%.2f",price)+" adoption
fee");
}
}
//end of Dog.java
//Sales.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Sales {
private PrintWriter writer;
private double totalPrice;
// constructor
public Sales(String filename) throws
FileNotFoundException
{
writer = new PrintWriter(new
File(filename));
totalPrice = 0;
}
// method to add dog details in file and add the price
of dog to total price
public void addDog(Dog dog)
{
writer.write(dog.toString()+"\n");
totalPrice += dog.getPrice();
}
// output the total price to file and close the
file
public void finalizeReport()
{
writer.write("----\n");
writer.write("Total price :
$"+String.format("%.2f", totalPrice));
writer.close();
}
}
//end of Sales.java
// DogMarket.java
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DogMarket {
public static void main(String[] args) throws
FileNotFoundException {
Scanner scan = new
Scanner(System.in);
String filename;
String command, name, breed;
double price;
int age;
// input the filename
System.out.print("Enter the output
filename : ");
filename = scan.next();
scan.nextLine();
Sales sales = new Sales(filename);
// create a Sales object
// loop that continues till the
user quits
do {
System.out.println("Buy or Quit ");
System.out.print("Enter your choice (buy or quit) : ");
command =
scan.nextLine();
// if user
selects to buy a dog, input details of Dog and add the dog to Sales
object
if(command.equalsIgnoreCase("buy"))
{
System.out.print("Name of the Dog : ");
name = scan.nextLine();
System.out.print("Breed of the Dog : ");
breed = scan.nextLine();
System.out.print("Enter age of the Dog :
");
age = scan.nextInt();
scan.nextLine();
System.out.print("Enter the price of the Dog :
");
price = scan.nextDouble();
scan.nextLine();
sales.addDog(new Dog(name,breed,price,
age));
}
}while(!command.equalsIgnoreCase("quit"));
sales.finalizeReport(); // once
user quits, call finalize report
scan.close();
}
}
//end of DogMarket.java
Output:
Output file: