📄 gallerypainting.java
字号:
}
} // writeBought
// -------------------------------------------------------------------------------------------------------------------
public void addNewSale ()
//
// records that a gallery painting has been sold
//
throws IOException {
DataInputStream inTmp, inCopy; // stream objects used for file input
DataOutputStream outTmp, outCopy; // stream objects used for file output
boolean found; // indicates if object insertion point found
GalleryPainting tempGallery; // temporary object used for file copying
found = false;
inTmp = new DataInputStream(new BufferedInputStream(new FileInputStream("sold.dat")));
outTmp = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("tempS.dat")));
tempGallery = new GalleryPainting();
//
// copy the current sold file to a temporary file
//
while (inTmp.available() != 0)
{
//
// read a temporary gallery object from the sold file
//
tempGallery.readSold (inTmp);
//
// write the temporary gallery object to the temporary file
//
tempGallery.writeSold (outTmp);
} // while (inTmp.available() != 0)
inTmp.close ();
outTmp.close ();
inCopy = new DataInputStream(new BufferedInputStream(new FileInputStream("tempS.dat")));
outCopy = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("sold.dat")));
//
// copy the temporary file to a new sold file
// while inserting the painting object in the proper location
//
while (inCopy.available() != 0)
{
//
// read a temporary gallery object from the temporary file
//
tempGallery.readSold (inCopy);
//
// write the proper object to the sold file
//
if ((classification.compareTo(tempGallery.classification) <= 0) &&
(tempGallery.saleDate.compare(saleDate) <= 0) &&
!found)
{
//
// write the gallery object to the sold file
//
writeSold (outCopy);
//
// write the temporary gallery object to the sold file
//
tempGallery.writeSold (outCopy);
found = true;
}
else
tempGallery.writeSold (outCopy);
} // while (inCopy.available() != 0)
//
// write the gallery object to the end of the sold file
//
if (!found)
writeSold (outCopy);
inCopy.close ();
outCopy.close ();
} // addNewSale
// -------------------------------------------------------------------------------------------------------------------
public void sell ()
//
// allows user to sell a painting found in the gallery
//
throws IOException {
DataInputStream inSold, inGallery; // stream objects used for file input
boolean found; // indicates if painting already in gallery
boolean alreadySold; // indicates if painting already sold
String tempFn; // tempFn, tempLn, and tempTitle
String tempLn; // store temporary information about
String tempTitle; // the painting to be sold, namely first name, last name, and title, resp.
UserInterface.clearScreen ();
//
// retrieve information about the painting desired to be sold
//
System.out.println("\n\nPlease enter the following information describing the painting:\n");
System.out.println("Note: - Use an underscore in place of any spaces.");
System.out.println(" - Do not leave any request blank.\n\n");
System.out.print("Enter the FIRST name of the artist (append ? if uncertain): ");
tempFn = UserInterface.getString();
System.out.print( "Enter the LAST name of the artist (append ? if uncertain): ");
tempLn = UserInterface.getString();
System.out.print( "Enter the TITLE of the painting (append ? if uncertain): ");
tempTitle = UserInterface.getString();
alreadySold = false;
inSold = new DataInputStream(new BufferedInputStream(new FileInputStream("sold.dat")));
//
// determine if the desired painting has already been sold
//
while (inSold.available() != 0)
{
//
// read a gallery object from the sold file
//
readSold (inSold);
//
// check if there is a match with the gallery object
//
if ((UserInterface.compareStr (firstName, tempFn) == 0) &&
(UserInterface.compareStr (lastName, tempLn) == 0) &&
(UserInterface.compareStr (title, tempTitle) == 0))
{
alreadySold = true;
break;
}
} // while (inSold.available() != 0)
inSold.close ();
if (alreadySold)
System.out.println("\n\nThe painting you described has already been sold!\n");
else
{
found = false;
inGallery = new DataInputStream(new BufferedInputStream(new FileInputStream("gallery.dat")));
//
// check to make sure that the desired painting
// actually exists in the gallery
//
while (inGallery.available() != 0)
{
//
// read a gallery object from the gallery file
//
readBought (inGallery);
//
// check if there is a match with the desired painting
//
if ((UserInterface.compareStr (firstName, tempFn) == 0) &&
(UserInterface.compareStr (lastName, tempLn) == 0) &&
(UserInterface.compareStr (title, tempTitle) == 0))
{
found = true;
break;
}
} // while (inGallery.available() != 0)
inGallery.close ();
if (found == true)
{
System.out.println("\n\nPlease enter the following sale information:\n\n");
saleDate = Date.currentDate;
System.out.print("Enter the NAME of the buyer: ");
buyerName = UserInterface.getString();
System.out.print( "Enter the ADDRESS of the buyer: ");
buyerAddr = UserInterface.getString();
System.out.print("Enter the selling PRICE: ");
sellPrice = UserInterface.getDouble();
addNewSale();
UserInterface.addArtist(firstName, lastName);
System.out.print("\n\nThe sale has been recorded.");
}
else
{
System.out.println("\n\nThe painting you described cannot be found in the gallery.");
System.out.println("Please make sure you entered the information correctly.");
System.out.println("Proper case is required.");
}
} // else (alreadySold != TRUE)
System.out.println("\n\n Press <ENTER> to return to main menu...");
UserInterface.pressEnter ();
} // sell
// -------------------------------------------------------------------------------------------------------------------
public void readSold (DataInputStream fileName) // stream object where gallery information is read
//
// reads a sold 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);
tempDate = fileName.readUTF();
saleDate = new Date();
saleDate.parseDate(tempDate);
medium = fileName.readUTF();
subject = fileName.readUTF();
sellerName = fileName.readUTF();
buyerName = fileName.readUTF();
sellerAddr = fileName.readUTF();
buyerAddr = fileName.readUTF();
algPrice = fileName.readDouble();
purchasePrice = fileName.readDouble();
targetPrice = fileName.readDouble();
sellPrice = fileName.readDouble();
height = fileName.readDouble();
width = fileName.readDouble();
} // readSold
// -------------------------------------------------------------------------------------------------------------------
public void writeSold (DataOutputStream fileName) // stream object where gallery information is written
//
// writes a sold gallery object to fileName
// Note: Java serialization could be used as an alternative to this approach
//
throws IOException {
fileName.writeUTF(classification);
fileName.writeUTF(firstName);
fileName.writeUTF(lastName);
fileName.writeUTF(title);
fileName.writeUTF(paintingDate.toString());
fileName.writeUTF(purchaseDate.toString());
fileName.writeUTF(saleDate.toString());
fileName.writeUTF(medium);
fileName.writeUTF(subject);
fileName.writeUTF(sellerName);
fileName.writeUTF(buyerName);
fileName.writeUTF(sellerAddr);
fileName.writeUTF(buyerAddr);
fileName.writeDouble(algPrice);
fileName.writeDouble(purchasePrice);
fileName.writeDouble(targetPrice);
fileName.writeDouble(sellPrice);
fileName.writeDouble(height);
fileName.writeDouble(width);
} // writeSold
} // class GalleryPainting
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -