Create an Asset class. Assets have an asset ID, name, value, and sku (stock keep
ID: 3831188 • Letter: C
Question
Create an Asset class.
Assets have an asset ID, name, value, and sku (stock keeping unit) number.
Create a constuctor that accepts asset ID, name, value, and sku.
Create second constructor that accepts only name, value, and sku and creates a asset ID number that is one greater in value than the last asset ID used (or 100 if no previous asset ID was used).
Add a save method that stores the contents of an asset object to a text file named based on the assetID for that object. You must use printf to store the asset information in a comma separated format.
It should output something like:
Explanation / Answer
Hi,
Please see the class below:
Thanks
Asset.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
// Asset class
public class Asset {
//Member variables asset ID, name, value, and sku (stock keeping unit) number.
private static String asset_ID;
private String name;
private double value;
private String sku;
//constuctor that accepts asset ID, name, value, and sku.
public Asset(String asset_ID, String name, double value, String sku) {
super();
this.asset_ID = asset_ID;
this.name = name;
this.value = value;
this.sku = sku;
}
//constuctor that accepts asset ID, name, value, and sku.
public Asset(String name, double value, String sku) {
super();
//asset ID number that is one greater in value than the last asset ID used (or 100 if no previous asset ID was used).
if(null == getAsset_ID() || "".equalsIgnoreCase(getAsset_ID())){
this.asset_ID = "100";
}
else{
this.asset_ID = getAsset_ID() +1;
}
this.asset_ID = asset_ID;
this.name = name;
this.value = value;
this.sku = sku;
}
/**
* save method that stores the contents of an asset object to a text file named based on the assetID for that object
*/
public void save(){
FileWriter fw = null;
String content = this.getAsset_ID()+","+this.getName()+","+this.getValue()+","+this.getSku();
try {
BufferedWriter bw = null;
fw = new FileWriter(this.getAsset_ID()+".txt");
bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getAsset_ID() {
return asset_ID;
}
public void setAsset_ID(String asset_ID) {
this.asset_ID = asset_ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
}
SampleMain_Asset.java
public class SampleMain_Asset {
public static void main(String [] args){
//Creating an Asset object with asset ID, name, value, and sku.
Asset asset = new Asset("100012", "table saw",100.12, "982342");
asset.save();
//Creating an Asset object with name, value, and sku.
//Asset_ID will be automatically assigned
Asset assetNew = new Asset( "table",160.10, "6342874");
assetNew.save();
}
}
Sample output:
100012.txt
100012,table saw,100.12,982342
1000121.txt
1000121,table,160.1,6342874
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.