Question

Create a class called Invoice that a hardware store might use to represent an invoice for...

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables – a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initialize the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiples the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application names InvoiceTest that demonstrates class Invoice’s capabilities.

Homework Answers

Answer #1

import java.io.*;
class Invoice
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String partnum;
    String partdes;
    int numofitems;
    double priceofitems;
    double amount;
    Invoice()
    {
        partnum="1";
        partdes="hdd";
        numofitems=0;
        priceofitems=1000;

    }
    public String getpartnum()throws IOException
    {
        System.out.println("Enter part number:");
        partnum=br.readLine();
        return partnum;
    }
     public String getpartdes()throws IOException
    {
        System.out.println("Enter part description: ");
        partdes=br.readLine();
        return partdes;
    }
    public int getnumofitems()throws IOException
    {
        System.out.println("Enter number of items:");
        numofitems=Integer.parseInt(br.readLine());
        return numofitems;
    }
     public double getpriceofitems()throws IOException
    {
        System.out.println("Enter price of items:");
        priceofitems=Double.parseDouble(br.readLine());
        return priceofitems;
    }
    public String setpartnum(String num)
    {
        partnum=num;
        return partnum;
    }
     public String setpartdes(String des)
    {
        partdes=des;
        return partdes;
    }
    public int setnumofitems(int numitem)
    {
        numofitems=numitem;
        return numofitems;
    }
     public double setpriceofitems(double price )
    {
        priceofitems=price;
        return priceofitems;
    }
    public double getInvoiceamount()
    {
        amount = priceofitems*numofitems;
        amount = (amount>0)?amount:0;
        return amount;
    }
    public void displayInfo()
        {
            System.out.printf("part number:"+partnum);
System.out.printf("\n part description:"+partdes);
System.out.printf("\n num of items:"+numofitems);
System.out.printf("\n price of items:"+priceofitems);
            System.out.printf("\n Amount:"+amount);
            System.out.println();
        }
}
public class InvoiceTest
{
    public static void main(String [] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Invoice i1 = new Invoice();
        Invoice i2 = new Invoice();
        i1.getpartnum();
        i1.getpartdes();
        i1.getnumofitems();
        i1.getpriceofitems();
        i1.getInvoiceamount();
        i1.displayInfo();
        i2.setpartnum("1");
        i2.setpartdes("chip");
        i2.setnumofitems(12);
        i2.setpriceofitems(12);
        i2.getInvoiceamount();
        i2.displayInfo();
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Activity 1:  Review Chapter 8 and All Inheritance examples. Build an application that handles setting up...
Activity 1:  Review Chapter 8 and All Inheritance examples. Build an application that handles setting up vacation plans. Create an abstract superclass called Vacation Instance Variables destination - String budget - double  Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - abstract method that returns the amount the vacation is under or over budget.  Under budget is a positive number and over budget is a negative number. Create a concrete class called AllInclusive...