• Source code should adhere to the coding conventions outlined for this course
When I run this it just terminates out of it. It does not show me any errors. I don't understand what I am doing wrong.
public class Laptop4
{
int LaptopNumber;
String LaptopName;
int quantity;
double price;
public Laptop4(int number, String name, int quantity, double price)
{
LaptopNumber = number;
LaptopName = name;
this.quantity = quantity;
this.price = price;
}
public int getLaptopNumber()
{
return LaptopNumber;
}
public void setlaptopNumber(int value)
{
LaptopNumber = value;
}
public String getlaptopName()
{
return LaptopName;
}
public int getQuantity()
{
return quantity;
}
public void setQuantity(int value)
{
quantity = value;
}
public double getPrice()
{
return price;
}
public void setPrice(double value)
{
price = value;
}
public double getInventoryValue()
{
return getPrice() * getQuantity();
}
public String toString()
{
return String.format("\nINVENTORY INFO\n%s %s\n%s\t %s\n%s\t %d\n%s\t $%,.2f\n%s\t $%,.2f","Product Number: ", getLaptopNumber(),
"Product Name: ", getlaptopName(), "Units In Stock:", getQuantity(),
"Price per Unit:", getPrice(), "Total Inventory Value:", getInventoryValue());
}
}
public class Inventory {
private ExtendedLaptop[] list;
public Inventory(int size) {
list = new ExtendedLaptop[size];
}
public int size() {
return list.length;
}
public void add(ExtendedLaptop d, int p) {
list[p] = d;
}
public Laptop4 get(int i) {
return list[i];
}
public void sort() {
int n = list.length;
for (int search = 1; search < n; search++) {
for (int i = 0; i < n-search; i++) {
if (list[i].getlaptopName().compareToIgnoreCase(list[i+1].getlaptopName()) > 0) {
// swap
ExtendedLaptop temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
}
}
}
}
public void pack() {
// TODO Auto-generated method stub
}
public void setVisible(boolean B)/> {
// TODO Auto-generated method stub
}
public Object totalValue() {
// TODO Auto-generated method stub
return null;
}
}
import javax.swing.*;
import java.awt.event.*;
public class InventoryPart4 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private final Inventory i;
private final JTextArea txt;
private static int view = 0;
public static void main(final String[] args) {
final Inventory gui = new Inventory(view);
gui.pack();
gui.setVisible(true);
}
public void showLaptop() {
txt.setText("Laptop Details:\n");
txt.append(i.get(view) + "\n");
txt.append(String.format("\nTotal value = $%.2f", i.totalValue()));
}
public InventoryPart4() {
super("Laptop");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // quit when closed
final ExtendedLaptop p1 = new ExtendedLaptop(0, "Elite", 5, 349.99, "Compaq");
final ExtendedLaptop p2 = new ExtendedLaptop(1, "Core i3", 14, 749.99, "Hewlett Packard");
final ExtendedLaptop p3 = new ExtendedLaptop(2, "Aspire", 22, 299, "Acer");
final ExtendedLaptop p4 = new ExtendedLaptop(3, "Inspirion", 11, 799.99, "Dell");
final ExtendedLaptop p5 = new ExtendedLaptop(4, "Aspire 2", 5, 499, "Acer");
i = new Inventory(3);
i.add(p1, 0);
i.add(p2, 1);
i.add(p3, 2);
i.add(p4, 3);
i.add(p5, 4);
i.sort();
for (int k = 0; k < 3; k++) {
System.out.println(i.get(k));
}
System.out.println();
System.out.printf("Total value = $%.2f", i.totalValue());
final JPanel panel = new JPanel();
txt = new JTextArea(15,20);
txt.setEditable(false);
panel.add(txt);
final JButton next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if (view < i.size()-1) view++;
showLaptop();
}
});
panel.add(next);
getContentPane().add(panel);
showLaptop();
}
}
class ExtendedLaptop extends Laptop4 {
private String manufacturer = "";
public ExtendedLaptop(int item, String name, int units, double price, String manufacturer) {
super(item, name, units, price);
this.manufacturer = manufacturer;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String Manufacturer, String manufacturer) {
}
public double getInventoryValue() {
return 1.05*getQuantity()*getPrice();
}
public double fee() {
return 0.05*getQuantity()*getPrice();
}
public String toString()
{
return String.format("\nINVENTORY INFO\n%s %s\n%s\t %s\n%s\t %d\n%s\t $%,.2f\n%s\t%s\n%s\t $%,.2f \n%s\t$%,.2f","Product Number: ", getLaptopNumber(),
"Product Name: ", getlaptopName(), "Units In Stock:", getQuantity(),
"Price per Unit:", getPrice(),
"Manufacturer:", getManufacturer(),
"Price:", "Total Inventory Value:", getInventoryValue());
}
}

New Topic/Question
Reply
MultiQuote









|