Instructions
Objective
Write a java homework to implement stock item and crypto coin.
Requirements and Specifications
Source Code
CRYPTO COIN
package Assignment;
public class CryptoCoin extends StockItem {
public CryptoCoin(String stockCode, int variableQuantity, double variablePrice) {
super(stockCode, variableQuantity, variablePrice);
}
@Override
public String getStockName() {
return "C-Coin";
}
@Override
public String getStockDescription() {
return "CryptoCoin";
}
@Override
public String toString() {
return super.toString();
}
}
ELECTRIC CAR
package Assignment;
public class CryptoCoin extends StockItem {
public CryptoCoin(String stockCode, int variableQuantity, double variablePrice) {
super(stockCode, variableQuantity, variablePrice);
}
@Override
public String getStockName() {
return "C-Coin";
}
@Override
public String getStockDescription() {
return "CryptoCoin";
}
@Override
public String toString() {
return super.toString();
}
}
MOBILE TABLET
package Assignment;
public class MobileTablet extends StockItem {
public MobileTablet(String stockCode, int variableQuantity, double variablePrice) {
super(stockCode, variableQuantity, variablePrice);
}
@Override
public String getStockName() {
return "MPad";
}
@Override
public String getStockDescription() {
return "Mobile Tablet";
}
@Override
public String toString() {
return super.toString();
}
}
NAV SYS
package Assignment;
public class NavSys extends StockItem {
public NavSys(String stockCode, int variableQuantity, double variablePrice) {
super(stockCode, variableQuantity, variablePrice);
}
@Override
public String getStockName() {
return "Navigation System";
}
@Override
public String getStockDescription() {
return "GeoVision Sat Nav";
}
@Override
public String toString() {
return super.toString();
}
public static void main(String[] args) {
int q = 10;
double price = 99.99;
String code = "NS101";
System.out.println("Creating a stock with " + q + " units Navigation system, price "
+ price + " each, and item " + code);
System.out.println("Printing item stock information");
NavSys navSysItem = new NavSys(code, q, price);
System.out.println();
System.out.println(navSysItem);
int addedQ = 10;
System.out.println("Increasing " + addedQ + " more units");
System.out.println("Printing item stock information");
navSysItem.addStock(addedQ);
System.out.println();
System.out.println(navSysItem);
int soldQ = 2;
System.out.println("Sold " + soldQ + " units");
System.out.println("Printing item stock information");
navSysItem.sellStock(soldQ);
System.out.println();
System.out.println(navSysItem);
double newPrice = 100.99;
System.out.println("Set new price " + newPrice + " per unit");
System.out.println("Printing item stock information");
navSysItem.setVariablePrice(newPrice);
System.out.println();
System.out.println(navSysItem);
addedQ = 0;
System.out.println("Increasing " + addedQ + " more units");
System.out.println();
System.out.print("The error was: ");
navSysItem.addStock(addedQ);
}
}
STOCK ITEM
package Assignment;
public class StockItem {
private static final double VAT_RATE = 0.175;
private String stockCode;
private int variableQuantity;
private double variablePrice;
public StockItem(String stockCode, int variableQuantity, double variablePrice) {
this.stockCode = stockCode;
this.variableQuantity = variableQuantity;
this.variablePrice = variablePrice;
}
public String getStockName() {
return "Unknown Stock Name";
}
public String getStockDescription() {
return "Unknown Stock Description";
}
public String getStockCode() {
return stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
public int getVariableQuantity() {
return variableQuantity;
}
public void setVariableQuantity(int variableQuantity) {
this.variableQuantity = variableQuantity;
}
public double getVariablePrice() {
return variablePrice;
}
public void setVariablePrice(double variablePrice) {
this.variablePrice = variablePrice;
}
public double getPriceWithVAT() {
return variablePrice * (1 + VAT_RATE);
}
public boolean addStock(int q) {
if (q < 1) {
System.out.println("Increased item must be greater thanor equal to one");
return false;
}
if (variableQuantity + q > 100) {
System.out.println("Stock quantity exceeds 100");
return false;
}
variableQuantity += q;
return true;
}
public boolean sellStock(int q) {
if (q < 1) {
System.out.println("Sold amount must be positive");
return false;
}
if (variableQuantity < q) {
return false;
}
variableQuantity -= q;
return false;
}
@Override
public String toString() {
return "Stock Type: " + getStockName() + System.lineSeparator() +
"Description: " + getStockDescription() + System.lineSeparator() +
"StockCode: " + getStockCode() + System.lineSeparator() +
"PriceWithoutVAT: " + getVariablePrice() + System.lineSeparator() +
"PriceWithVAT: " + getPriceWithVAT() + System.lineSeparator() +
"Total units in stock: " + getVariableQuantity() + System.lineSeparator();
}
public static void main(String[] args) {
int q = 10;
double price = 99.99;
String code = "W101";
System.out.println("Creating a stock with " + q + " units Unknown item, price "
+ price + " each, and item " + code);
System.out.println("Printing item stock information");
StockItem stockItem = new StockItem(code, q, price);
System.out.println();
System.out.println(stockItem);
int addedQ = 10;
System.out.println("Increasing " + addedQ + " more units");
System.out.println("Printing item stock information");
stockItem.addStock(addedQ);
System.out.println();
System.out.println(stockItem);
int soldQ = 2;
System.out.println("Sold " + soldQ + " units");
System.out.println("Printing item stock information");
stockItem.sellStock(soldQ);
System.out.println();
System.out.println(stockItem);
double newPrice = 100.99;
System.out.println("Set new price " + newPrice + " per unit");
System.out.println("Printing item stock information");
stockItem.setVariablePrice(newPrice);
System.out.println();
System.out.println(stockItem);
addedQ = 0;
System.out.println("Increasing " + addedQ + " more units");
System.out.println();
System.out.print("The error was: ");
stockItem.addStock(addedQ);
}
}
TEST POLYMORPHISM
package Assignment;
public class TestPolymorphism {
public static void main(String[] args) {
int q = 10;
double price = 99.99;
String code = "W101";
System.out.println("Creating a stock with " + q + " units Unknown item, price "
+ price + " each, and item " + code);
StockItem stockItem = new StockItem(code, q, price);
itemInstance(stockItem);
System.out.println();
q = 10;
price = 99.99;
code = "NS101";
System.out.println("Creating a stock with " + q + " units Navigation item, price "
+ price + " each, and item " + code);
NavSys navSys = new NavSys(code, q, price);
itemInstance(navSys);
System.out.println();
q = 10;
price = 129.99;
code = "EC101";
System.out.println("Creating a stock with " + q + " units ECar item, price "
+ price + " each, and item " + code);
ElectricCar eCar = new ElectricCar(code, q, price);
itemInstance(eCar);
System.out.println();
q = 10;
price = 129.99;
code = "MP101";
System.out.println("Creating a stock with " + q + " units MPad item, price "
+ price + " each, and item " + code);
MobileTablet mPad = new MobileTablet(code, q, price);
itemInstance(mPad);
q = 10;
price = 149.99;
code = "CC101";
System.out.println("Creating a stock with " + q + " units CCoin item, price "
+ price + " each, and item " + code);
CryptoCoin cCoin = new CryptoCoin(code, q, price);
itemInstance(cCoin);
}
public static void itemInstance(StockItem stockItem) {
System.out.println("Printing item stock information");
System.out.println();
System.out.println(stockItem);
int addedQ = 10;
System.out.println("Increasing " + addedQ + " more units");
System.out.println("Printing item stock information");
stockItem.addStock(addedQ);
System.out.println();
System.out.println(stockItem);
int soldQ = 2;
System.out.println("Sold " + soldQ + " units");
System.out.println("Printing item stock information");
stockItem.sellStock(soldQ);
System.out.println();
System.out.println(stockItem);
double newPrice = 100.99;
System.out.println("Set new price " + newPrice + " per unit");
System.out.println("Printing item stock information");
stockItem.setVariablePrice(newPrice);
System.out.println();
System.out.println(stockItem);
addedQ = 0;
System.out.println("Increasing " + addedQ + " more units");
System.out.println();
System.out.print("The error was: ");
stockItem.addStock(addedQ);
}
}
Similar Samples
Explore our curated collection of programming homework samples at ProgrammingHomeworkHelp.com. Covering Java, Python, C++, and more, our samples showcase expertise in solving diverse coding challenges. Each example demonstrates clarity, precision, and adherence to academic standards. Dive into our samples to see how we can assist you in mastering programming concepts and achieving top grades.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java