1 Attachment(s)
Need Help Finishing Method - Program Attached
I was wondering if someone could help me. I've been trying to convert my program to a swing interface.
The problem I'm having is with the showlot function in the auction class. After entering various lots and then clicking the showlots button, a new instance of the program is started and only in this new instance the lots will show up.
I'm sorry if i've not been completely clear, that is why I have attached my program. Could someone please help me, i've been up all night trying to figure it out.
Many thanks
James
Code:
//MyApp (MAIN CLASS)
package auction;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class MyApp extends JFrame implements ActionListener {
Auction m_auction = new Auction();
// Auction m_auction;
JTextArea pText = new JTextArea(50,50);
JPanel enterlot = new JPanel();
JPanel showlot = new JPanel();
JPanel showunsold = new JPanel();
JPanel removelot = new JPanel();
JPanel closelot = new JPanel();
JButton enterlotB = new JButton("Enter Lot");
JButton showlotB = new JButton("Show Lots");
//JTextArea text = new JTextArea(1, 70);
JTextField value = new JTextField("Enter the Lot Here");
public MyApp() {
super("Visual Notebook");
setSize(430,150);
setLocation(100, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// m_auction = new Auction();
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("Enter lot", enterlot);
tabs.addTab("Show Lot", showlot);
tabs.addTab("Unsold Lot", showunsold);
tabs.addTab("Remove Lot", removelot);
tabs.addTab("Close Lot", closelot);
enterlot.add(value);
enterlot.add(enterlotB);
showlot.add(showlotB);
showlot.add(pText);
pText.setEditable(false);
enterlotB.addActionListener(this);
showlotB.addActionListener(this);
add(tabs);
add(tabs);
setVisible(true);
//Auction m_auction = new Auction();
}
public static void main(String args[]) {
System.out.println("Starting App");
Auction m_auction = new Auction();
MyApp ma = new MyApp();
// nb.enterLot("Chinese clock");
// nb.enterLot("Bacon");
// nb.enterLot("Cheese");
// nb.enterLot("Doughnut");
// nb.enterLot("Eggs");
// nb.enterLot("Fruit");
// nb.showLots();
System.out.println("\nAfter a bid ...");
// nb.showLots();
// nb.showLots();
// nb.closeAuction();
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == enterlotB) {
m_auction.enterLot(value.getText());
} else if (source == showlotB){
m_auction.showLots();
}
}
}
//Auction CLASS
package auction;
import java.util.*;
/**
* A simple model of an auction.
* The auction maintains a list of lots of arbitrary length.
*/
public class Auction {
// The list of Lots in this auction.
private Vector<Lot> m_lots;
// The list of people bidding in this auction.
private Vector<Person> m_bidders;
// The number that will be given to the next lot entered
// into this auction.
private int m_nextLotNumber;
/**
* Create a new auction.
*/
public Auction() {
m_lots = new Vector<Lot>();
m_bidders = new Vector<Person>();
m_nextLotNumber = 1;
}
/**
* Enter a new lot into the auction.
* @param description A description of the lot.
*/
public void enterLot(String description) {
m_lots.add(new Lot(m_nextLotNumber, description)); //new instance of lot created using the nextlotnumber and the description
m_nextLotNumber++; //lot number is increased
System.out.println("The lot was added");
}
/**
* Show the full list of lots in this auction.
*/
public void showLots() {
MyApp myApp = new MyApp();
System.out.println("\nNow displaying All lots.\n");
for(Lot lot : m_lots) {
// System.out.println("\n" + m_lots.toString());
myApp.pText.append(lot.getDescription() + "\n");
}
}
public void showUnsoldLots() {
System.out.println("\nNow displaying All unsold lots.\n");
for(Lot lot : m_lots) {
try{
if (lot.getHighestBid().getValue() <= 0)
{
// System.out.println(lot.toString());
// System.out.println(lot.getHighestBid().getValue());
}
}catch(NullPointerException n)
{System.out.println(lot.toString());
}
}
}
/**
* Bid for a lot.
* A message indicating whether the bid is successful or not
* is printed.
* @param number The lot number being bid for.
* @param biddername The name of the person bidding for the lot.
* @param value The value of the bid.
*/
public void bidFor(int lotNumber, String bidderName, long value) { //TAKE PARAMS FROM MAIN APP
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null) { //IF SELECTED LOT DOES NOT EQUAL "NULL"
Person bidder = findBidderNamed(bidderName); // //Create A Person object using findbidder.
if (bidder == null) {
bidder = new Person(bidderName);
m_bidders.add(bidder);
System.out.println("added");
}
boolean successful = selectedLot.bidFor(new Bid(bidder, value));
if(successful) {
System.out.println("The bid for lot number " +
lotNumber + " was successful");}
else {
// Report which bid is higher.
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " + lotNumber +
" already has a bid of: " +
highestBid.getValue());
}
}
}
public Person findBidderNamed(String name) {
Person bidder = null;
if (m_bidders.isEmpty()) { //if vector is empty
return bidder; //return object bidder
}
Iterator<Person> ite = m_bidders.iterator(); //itearate through vector m_bidders
boolean bfound = true; // //found = false
while ((ite .hasNext())&&(!bfound)) {
bidder = ite.next();
System.out.println(bidder.getName());
if (bidder.getName().equals(name) == true)
bfound = true;
}
return bidder;
}
/**
* Return the lot with the given number. Return null
* if a lot with this number does not exist.
* @param lotNumber The number of the lot to return.
*/
protected Lot getLot(int lotNumber) {
if((lotNumber >= 1) && (lotNumber < m_nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = m_lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
protected Lot removeLot(int lotNumber) {
if((lotNumber >= 1) && (lotNumber < m_nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = m_lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
}
m_lots.remove(lotNumber -1);
System.out.println("\nLot Number " + lotNumber + " was successfully removed.");
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
public void closeAuction(){
System.out.println("\nClosing Auction");
int count = 0;
for(Lot lots : m_lots) {
if (lots.getHighestBid() != null)
{
System.out.println("\nThe item sold was.." + lots.getDescription());
System.out.println( "The item was won by " +lots.getHighestBid().getBidder().getName());
System.out.println("for the price of " +lots.getHighestBid().getValue());
}
else
{
System.out.println("\nUnfortunately the item " + lots.getDescription() + " was not sold as it did not recieve any bids.");
}
}
}
public void throughNames(){
Person bidder = null;
Iterator<Person> ite = m_bidders.iterator();
while ((ite .hasNext())) {
bidder = ite.next();
System.out.println(bidder.getName());
}
}
}
Re: Need Help Finishing Method - Program Attached
Quote:
Originally Posted by
jimmyo88
After entering various lots and then clicking the showlots button, a new instance of the program is started and only in this new instance the lots will show up.
Well, you seem to be constructing a couple different instances of your MyApp class. If you only want one instance of your gui to show up, that's probably not what you want to be doing.