In: Computer Science
• User ADT: Describes and manipulates user information. You must track the following information about a user / provide the following methods:
o username
o firstName
o lastName
o a list of the 10 most recently purchased items
o A user can bid on and purchase Items
The User class should have a default constructor, as well as one accepting all parameters. It should also provide accessor (getter) and mutator (setter) methods for appropriate methods. By default, a user is able to buy products only (not sell them).
• Item ADT: Describes a single item being sold. An item object keeps track of the current high bid for that item in addition to information about the object itself. You must track the following information about an item:
o name
o description
o highBid (currentBid) o buyNowPrice
o location
o dateBiddingComplete
The Item class should have a default constructor, as well as one accepting all parameters. In addition, this class contains functions for retrieving information about the item.
• Seller ADT: Seller is a specialization of user. You should implement it as such. In addition to all user attributes, sellers also have:
o Seller is a specific type of User that has a list of up to 20 items for sale
Note that for each ADT, you will define both an interface and a class. For example, you will create an ItemADT interface that will include methods such as getItemName, among others. The Item class implements this interface and implements all methods defined in the interface.
Provide a test class and the output from running your own test cases on all aspects of your program.
//Test.java
import java.io.*;
class Test
{
public static void main(String[] arg) throws IOException
{
System.out.println("\nCREATING TEST USERS...");
UserADT u1 = new User(); // Creates a user using the default constructor
u1.setUserName("jdoe");
u1.setRealName("Jane Doe");
u1.setPassword("jane_password");
u1.setEmail("[email protected]");
UserADT u2 = new User("jsmith", "Joe Smith", "password", "[email protected]");
SellerADT s1 = new Seller("rnelson", "Ron Nelson", "password", "[email protected]", "Visa", "Cool Stuff for Sale");
s1.addItem("bike", "2006 Huffy", 0, 85);
System.out.println("Here is the info for " + u1.getUserName() + ": " + u1.printInfo());
System.out.println("Here is the info for " + u2.getUserName() + ": " + u2.printInfo());
System.out.println(u2.printInfo());
System.out.println("Here is the info for " + s1.getUserName() + "and their store" + s1.getAuctionStoreName() + ": \n" + s1.printInfo());
ItemADT i1 = new Item("laptop", "MacBook Pro", 0, 400);
s1.addItem(i1);
//Adding items to seller's account to reach max.
for (int i = 0; i < 8; i++){
i1 = new Item("test " + i, "This is a test", 0, 100);
s1.addItem(i1);
}
//Should print out a list of 10 items.
System.out.println(s1.printInfo());
//Should stop me from adding anymore items.
try{
i1 = new Item("Another item", "This is a test", 0, 100);
s1.addItem(i1);
} catch (ArrayIndexOutOfBoundsException aioob){
System.out.println(aioob.getMessage());
}
System.out.println(s1.printInfo());
//Should remove item 5 from the list.
s1.removeItem(5);
System.out.println(s1.printInfo());
//This should remove al1 items from list.
for (int i = 9; i > 0; i--)
s1.removeItem(i);
System.out.println(s1.printInfo());
//Testing to see if I can still remove.
try{
s1.removeItem(1);
} catch (ArrayIndexOutOfBoundsException aioob){
System.out.println(aioob.getMessage());
}
System.out.println(s1.printInfo());
try{
i1.setName("This is a name");
i1.setDescription("This is a description");
i1.setHighBid(20);
i1.setBuyNowPrice(10);
} catch (Exception e){
System.out.println(e.getMessage());
}
i1.setBuyNowPrice(50);
System.out.println(i1.getName() + " | " + i1.getDescription() + " | " + i1.getHighBid() + " | " + i1.getBuyNowPrice());
}
}
==============================================================================
//Item.java
public class Item implements ItemADT {
private String name;
private String description;
private int highBid;
private int buyNow;
/**
*
*/
public Item(){
this.name = "";
this.description = "";
this.highBid = 0;
this.buyNow = 0;
}
public Item(String name, String description, int highBid, int buyNow){
this.name = name;
this.description = description;
this.highBid = highBid;
this.buyNow = buyNow;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setHighBid(int highBid) {
if (highBid < 0)
throw new NumberFormatException("Value must be greater than or equal to 0");
this.highBid = highBid;
}
public void setBuyNowPrice(int buyNowPrice) {
if (buyNowPrice < 0)
throw new NumberFormatException("Value must be greater than or equal to 0");
if (buyNowPrice < this.highBid)
throw new NumberFormatException("The high bid of the item must be greater than or equal to the high price.");
this.buyNow = buyNowPrice;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public int getHighBid() {
return highBid;
}
public int getBuyNowPrice() {
return buyNow;
}
}
===========================================================================
//ItemADT.java
public interface ItemADT {
public void setName(String name);
public void setDescription(String description);
public void setHighBid(int highBid);
public void setBuyNowPrice(int buyNowPrice);
public String getName();
public String getDescription();
public int getHighBid();
public int getBuyNowPrice();
}
============================================================================
//Seller.java
public class Seller extends User implements SellerADT {
final int MAX_COUNT = 10;
private String auctionStoreName;
private String preferredPaymentMethod;
private ItemADT[] itemSelling;
private int count;
/**
* Default constructor, sets defaults
*/
public Seller(){
super();
setAuctionStoreName("");
setPreferredPaymentMethod("");
this.itemSelling = new ItemADT[MAX_COUNT];
this.count = 0;
}
public Seller(String userName, String realName, String password, String email, String preferredPaymentMethod, String auctionStoreName){
super(userName, realName, password, email);
setAuctionStoreName(auctionStoreName);
setPreferredPaymentMethod(preferredPaymentMethod);
this.itemSelling = new ItemADT[MAX_COUNT];
this.count = 0;
}
public void setPreferredPaymentMethod(String paymentMethod) {
this.preferredPaymentMethod = paymentMethod;
}
public void setAuctionStoreName(String storeName) {
this.auctionStoreName = storeName;
}
public String getPreferredPaymentMethod() {
return preferredPaymentMethod;
}
public String getAuctionStoreName() {
return auctionStoreName;
}
public void addItem(String name, String description, int highBid, int buyNow) {
testSize();
itemSelling[count] = new Item(name, description, highBid, buyNow);
count++;
}
public void addItem(ItemADT item) {
testSize();
itemSelling[count] = item;
count++;
}
private void testSize(){
if (count >= MAX_COUNT)
throw new ArrayIndexOutOfBoundsException("You have reached your max items being sold.");
}
public ItemADT removeItem(int number){
if (number > count || number < 1)
throw new ArrayIndexOutOfBoundsException("There are no items to be removed there.");
ItemADT item = itemSelling[(number - 1)];
ItemADT itemMoved = itemSelling[(count - 1)];
itemSelling[(number - 1)] = itemMoved;
itemSelling[(count - 1)] = null;
count--;
return item;
}
public String printInfo() {
String output = super.printInfo() + "Auction Store: " +
getAuctionStoreName() + "\nPreferred Payment: " +
getPreferredPaymentMethod() + "\n";
ItemADT item;
for (int i = 0; i < count; i++){
item = itemSelling[i];
output = output + "\nItem " + (i+1) + " for sale\nItem Name: " +
item.getName() + "\nDescription: " + item.getDescription() +
"\nHigh Bid: " + item.getHighBid() + "\nBuy Now Price: " +
item.getBuyNowPrice() + "\n";
}
return output;
}
}
============================================================================
//SellerADT.java
public interface SellerADT extends UserADT {
public void setPreferredPaymentMethod(String paymentMethod);
public void setAuctionStoreName(String storeName);
public String getPreferredPaymentMethod();
public String getAuctionStoreName();
public void addItem(String name, String description, int highBid, int buyNow);
public void addItem(ItemADT item);
public ItemADT removeItem(int number);
}
==============================================================================
//User.java
public class User implements UserADT {
private String userName;
private String realName;
private String password;
private String email;
/**
* Default constructor, sets the default values.
*/
public User(){
setUserName("");
setRealName("");
setPassword("");
setEmail("");
}
public User(String userName, String realName, String password, String email){
setUserName(userName);
setRealName(realName);
setPassword(password);
setEmail(email);
}
/**
* Sets the user's user name.
*/
public void setUserName(String userName){
this.userName = userName;
}
/**
* Sets the user's real name.
*/
public void setRealName(String realName){
this.realName = realName;
}
/**
* Sets the user's password.
*/
public void setPassword(String password){
this.password = password;
}
/**
* Sets the user's email.
*/
public void setEmail(String email){
this.email = email;
}
/**
* Returns a string representation of the user's user name.
*/
public String getUserName(){
return userName;
}
public String getRealName(){
return realName;
}
public String getPassword(){
return password;
}
public String getEmail(){
return email;
}
public String printInfo(){
return ("\nUser Name: " + getUserName() + "\nReal Name: " + getRealName() + "\nPassword: " + getPassword() + "\nEmail: " + getEmail() + "\n");
}
}
=============================================================================
//UserADT.java
public interface UserADT {
public void setUserName(String userName);
public void setRealName(String realName);
public void setPassword(String password);
public void setEmail(String email);
public String getUserName();
public String getRealName();
public String getPassword();
public String getEmail();
public String printInfo();
}
============================================================================
sample output