Question

In: Computer Science

Java program Statement: Provide a user interface to the invoice program in Section 12.3 that allows...

Java program Statement: Provide a user interface to the invoice program in Section 12.3 that allows a user to enter and print an arbitrary invoice. Do not modify any of the existing classes.

.....
.....
.....

/**
   Describes an invoice for a set of purchased products. 
*/
public class Invoice
{  
   /**
      Adds a charge for a product to this invoice. 
      @param aProduct the product that the customer ordered 
      @param quantity the quantity of the product 
   */
   public void add(Product aProduct, int quantity)
   { 
   }
 
   /**
      Formats the invoice. 
      @return the formatted invoice 
   */
   public String format()
   { 
   }
}
 
 
 
/**
   Describes a quantity of an article to purchase. 
*/
public class LineItem
{ 
   /**
      Computes the total cost of this line item. 
      @return the total price 
   */
   public double getTotalPrice()
   { 
   }
 
   /**
      Formats this item. 
      @return a formatted string of this item 
   */
   public String format()
   { 
   }
}
 
 
 
/**
   Describes a product with a description and a price. 
*/
public class Product
{ 
   /**
      Gets the product description. 
      @return the description 
   */
   public String getDescription()
   { 
   }
 
   /**
      Gets the product price. 
      @return the unit price 
   */
   public double getPrice()
   {
   }
}
 
 
 
/**
   Describes a mailing address. 
*/
public class Address
{ 
   /**
      Formats the address. 
      @return the address as a string with three lines 
   */
   public String format()
   { 
   }
}

Solutions

Expert Solution

public class Invoice
{
/**
Adds a charge for a product to this invoice.
@param aProduct the product that the customer ordered
@param quantity the quantity of the product
*/
public void add(Product aProduct, int quantity)
{ LineItem anItem = newLineItem(aProduct, quantity);
items.add(anItem);
}

/**
Formats the invoice.
@return the formatted invoice
*/
public String format()
{
String r = " I N V O I C E\n\n"
+ billingAddress.format()
+ String.format("\n\n%-30s%8s%5s%8s\n",
"Description", "Price", "Qty", "Total");

for (LineItem i : items)
{
r = r + i.format() + "\n";
}

r = r + String.format("\nAMOUNT DUE: $%8.2f", getAmountDue());

return r;
}

/**
Describes a quantity of an article to purchase.
*/
public class LineItem
{
/**
Constructs an item from the product and quantity.
@param aProduct the product
@param aQuantity the item quantity
*/
public LineItem(Product aProduct, int aQuantity)
{
theProduct = aProduct;
quantity = aQuantity;
}

/**
Computes the total cost of this line item.
@return the total price
*/
public Money getTotalPrice()
{
Money m = theProduct.getPrice();
for (int i = 2; i <= quantity; i++)
m.add(theProduct.getPrice());
return m;
}

/**
Formats this item.
@return a formatted string of this item
*/
public String format()
{
return String.format("%-30s%8s%5d%8s",
theProduct.getDescription(), theProduct.getPrice().format(),
quantity, getTotalPrice().format());
}

private int quantity;
private Product theProduct;
}

/**
Describes a product with a description and a price.
*/
public class Product
{
/**
Constructs a product from a description and a price.
@param aDescription the product description
@param aPrice the product price
*/
public Product(String aDescription, Money aPrice)
{
description = aDescription;
price = aPrice;
}

/**
Gets the product description.
@return the description
*/
public String getDescription()
{
return description;
}

/**
Gets the product price.
@return the unit price
*/
public double getPrice()
{
return price;
}

private String description;
private double price;
}

/**
Describes a mailing address.
*/
public class Address
{
/**
Constructs a mailing address.
@param aName the recipient name
@param aStreet the street
@param aCity the city
@param aState the two-letter state code
@param aZip the ZIP postal code
*/
public Address(String aName, String aStreet,
String aCity, String aState, String aZip)
{
name = aName;
street = aStreet;
city = aCity;
state = aState;
zip = aZip;
}   

/**
Formats the address.
@return the address as a string with three lines
*/
public String format()
{
return name + "\n" + street + "\n"
+ city + ", " + state + " " + zip;
}

private String name;
private String street;
private String city;
private String state;
private String zip;
}

I hope it helps. Refer screenshot for better understanding of syntax


Related Solutions

Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Java Language Add a recursive method to the program shown in the previous section that allows...
Java Language Add a recursive method to the program shown in the previous section that allows remove the last node from the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
Java Language Add a recursive method to the program shown in the previous section that allows...
Java Language Add a recursive method to the program shown in the previous section that allows insert a value at the end of the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() {...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
Write a program using Python that allows the user to play a guessing game (please provide...
Write a program using Python that allows the user to play a guessing game (please provide a picture of code so it is easier to read). The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: Normally, we would have the program select a random number as the "secret number". However, for the purpose of testing your program (as well as grading it), simply use an assignment...
Imagine that you are planning a user-based usability test to evaluate a new interface that allows...
Imagine that you are planning a user-based usability test to evaluate a new interface that allows people to track online their medical information, such as blood tests, diagnostics, annual check-ups, and patient visits. Since many governments have set the goal to move to full electronic patient records in the next few years, this is an important project. Doctors will also use this application but, for this exercise, we’re focused on patients. Where might you want to recruit potential participants? Would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT