?? gallerypainting.java
字號(hào):
package Osbert;
import java.io.*;
public class GalleryPainting extends Painting
{
private static double TARGET_MARKUP = 2.15;
protected String classification; // gallery classification type
protected Date purchaseDate; // date painting was purchased
protected Date saleDate; // date painting was sold
protected String sellerName; // full name of seller
protected String buyerName; // full name of painting buyer
protected String sellerAddr; // address of seller
protected String buyerAddr; // address of buyer
protected double algPrice; // price determined by algorithm
protected double purchasePrice; // actual purchase price
protected double targetPrice; // target selling price
protected double sellPrice; // actual selling price
// getters-setters for GalleryPainting
public String getClassification () { return classification; }
public void setClassification (String c) { classification = c; }
public Date getPurchaseDate () { return purchaseDate; }
public void setPurchaseDate (Date d) { purchaseDate = d; }
public Date getSaleDate () { return saleDate; }
public void setSaleDate (Date d) { saleDate = d; }
public String getSellerName () { return sellerName; }
public void setSellerName (String n) { sellerName = n; }
public String getBuyerName () { return buyerName; }
public void setBuyerName (String n) { buyerName = n; }
public String getSellerAddr () { return sellerAddr; }
public void setSellerAddr (String a) { sellerAddr = a; }
public String getBuyerAddr () { return buyerAddr; }
public void setBuyerAddr (String a) { buyerAddr = a; }
public double getAlgPrice () { return algPrice; }
public void setAlgPrice (double p) { algPrice = p; }
public double getPurchasePrice () { return purchasePrice; }
public void setPurchasePrice (double p) { purchasePrice = p; }
public double getTargetPrice () { return targetPrice; }
public void setTargetPrice (double p) { targetPrice = p; }
public double getSellPrice () { return sellPrice; }
public void setSellPrice (double p) { sellPrice = p; }
public void getGalleryInformation ()
//
// retrieves gallery painting information
//
{
System.out.println("\n");
purchaseDate = Date.currentDate;
System.out.print("Enter the NAME of the seller: ");
sellerName = UserInterface.getString();
System.out.print("Enter the ADDRESS of the seller: ");
sellerAddr = UserInterface.getString();
System.out.print("Enter the purchase PRICE of the painting: ");
purchasePrice = UserInterface.getDouble();
targetPrice = purchasePrice * TARGET_MARKUP;
} // getGalleryInformation
// -------------------------------------------------------------------------------------------------------------------
public void addNewPainting ()
//
// inserts a gallery object in the proper place
//
throws IOException {
DataInputStream inTmp, inCopy; // stream objects used for file input
DataOutputStream outTmp, outCopy; // stream objects used for file output
boolean found = false; // indicates if object insertion point found
GalleryPainting tempGallery; // temporary object used for file copying
inTmp = new DataInputStream(new BufferedInputStream(new FileInputStream("gallery.dat")));
outTmp = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("tempG.dat")));
tempGallery = new GalleryPainting();
//
// copy the current gallery file to a temporary file
//
while (inTmp.available() != 0)
{
//
// read the temporary gallery object from the gallery file
//
tempGallery.readBought (inTmp);
//
// write the temporary gallery object to a temporary file
//
tempGallery.writeBought (outTmp);
}
inTmp.close ();
outTmp.close ();
inCopy = new DataInputStream(new BufferedInputStream(new FileInputStream("tempG.dat")));
outCopy = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("gallery.dat")));
//
// copy the temporary file to new gallery file
// while inserting the gallery object in the proper location
//
while (inCopy.available() != 0)
{
//
// read a temporary gallery object from the temporary file
//
tempGallery.readBought (inCopy);
//
// write the proper object to the gallery file
//
if ((classification.compareTo(tempGallery.classification) <= 0) &&
(purchaseDate.compare(tempGallery.purchaseDate) <= 0) && !found)
{
//
// write the gallery爋bject to the gallery file
//
writeBought (outCopy);
//
// write the temporary gallery object to the gallery file
//
tempGallery.writeBought (outCopy);
found = true;
}
else
tempGallery.writeBought (outCopy);
} // while (inCopy.available() != 0)
//
// write the gallery object to the end of the gallery file
//
if (!found)
writeBought (outCopy);
inCopy.close ();
outCopy.close ();
} // addNewPainting
// -------------------------------------------------------------------------------------------------------------------
public void buy ()
//
// allows user to buy a painting
//
throws IOException {
DataInputStream inFile; // stream object used for file input
char ch; // holds user response to Y/N question
boolean found = false; // indicates if painting already in gallery
GalleryPainting tempGallery; // temporary object used for file reading
getDescription ();
inFile = new DataInputStream(new BufferedInputStream(new FileInputStream("gallery.dat")));
tempGallery = new GalleryPainting();
//
// determine if the gallery object already exists in the gallery
//
while (inFile.available() != 0)
{
//
// read a temporary gallery object from the gallery file
//
tempGallery.readBought (inFile);
//
// check if there is a match with the gallery object
//
if ((UserInterface.compareStr (tempGallery.firstName, firstName) == 0) &&
(UserInterface.compareStr (tempGallery.lastName, lastName) == 0) &&
(UserInterface.compareStr (tempGallery.title, title) == 0))
{
found = true;
break;
}
} // while (inFile.available() != 0)
inFile.close ();
if (found == true)
{
System.out.println("\n\nThe painting you described has already been purchased!\n");
}
else
{
if(classification.compareTo("Masterpiece") == 0)
algPrice = ComputeMasterpiecePrice.getAlgorithmPrice(this);
if(classification.compareTo("Masterwork") == 0)
algPrice = ComputeMasterworkPrice.getAlgorithmPrice(this);
if(classification.compareTo("Other") == 0)
algPrice = ComputeOtherPrice.getAlgorithmPrice(this);
System.out.println("\n");
if (algPrice > 0)
{
System.out.println("The algorithm has determined the maximum offering price to be:");
System.out.println("$" + algPrice + " million dollars.\n");
System.out.print("Do you want to purchase this painting (Y/N)? ");
ch = UserInterface.getChar();
if ((ch == 'Y') || (ch == 'y'))
{
getGalleryInformation();
addNewPainting();
}
}
else
System.out.println("The algorithm has suggested that you should not buy this painting.");
}
System.out.println();
System.out.println("\n\n Press <ENTER> to return to main menu...");
UserInterface.pressEnter();
} // buy
// -------------------------------------------------------------------------------------------------------------------
public void readBought (DataInputStream fileName) // stream object where gallery information is read
//
// reads a gallery object from fileName
// Note: Java serialization could be used as an alternative to this approach
//
throws IOException {
String tempDate;
classification = fileName.readUTF();
firstName = fileName.readUTF();
lastName = fileName.readUTF();
title = fileName.readUTF();
tempDate = fileName.readUTF();
paintingDate = new Date();
paintingDate.parseDate(tempDate);
tempDate = fileName.readUTF();
purchaseDate = new Date();
purchaseDate.parseDate(tempDate);
medium = fileName.readUTF();
subject = fileName.readUTF();
sellerName = fileName.readUTF();
sellerAddr = fileName.readUTF();
algPrice = fileName.readDouble();
purchasePrice = fileName.readDouble();
targetPrice = fileName.readDouble();
height = fileName.readDouble();
width = fileName.readDouble();
} // readBought
// -------------------------------------------------------------------------------------------------------------------
public void writeBought (DataOutputStream fileName) // stream object where gallery information is written
//
// writes a gallery object to fileName
// Note: Java serialization could be used as an alternative to this approach
//
throws IOException {
if(classification.length() > 0)
{
fileName.writeUTF(classification);
fileName.writeUTF(firstName);
fileName.writeUTF(lastName);
fileName.writeUTF(title);
fileName.writeUTF(paintingDate.toString());
fileName.writeUTF(purchaseDate.toString());
fileName.writeUTF(medium);
fileName.writeUTF(subject);
fileName.writeUTF(sellerName);
fileName.writeUTF(sellerAddr);
fileName.writeDouble(algPrice);
fileName.writeDouble(purchasePrice);
fileName.writeDouble(targetPrice);
fileName.writeDouble(height);
fileName.writeDouble(width);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -