I need the full code answear of this question Question 6 Julie is a shopaholic!
ID: 3605882 • Letter: I
Question
I need the full code answear of this question
Question 6 Julie is a shopaholic! Julie needs a software that helps her keep track of her shopping expenses. Every time Julie buys a new outfit, she would like to add it to a listof expenses andchecks if she has exceeded her allowance of 500$ per month. 1. Use the class "Outfit", that has a constructor, accessor and mutators and three attributes: month, type of outfit, price. Modify the Outfit class to implement the Serialization Interface. 2. Use the "Serializable Interface" to store all outfits in a file "Outfits.ser" 3. Use the driver in "shopaholic.java". Write a method that returns the amount of money that Julie spent for each month.Explanation / Answer
Please find the below code for the above requirements :
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;
import java.util.Vector;
class outfit implements Serializable{
private String typeofoutfit;
private int monthofprchase,priceofpurchase;
outfit(int month,String type,int price){
this.monthofprchase=month;
this.typeofoutfit=type;
this.priceofpurchase=price;
}
public String getTypeofOutfit(){
return typeofoutfit;
}
public int getMonthofPur(){
return monthofprchase;
}
public int getPriceofpur(){
return priceofpurchase;
}
}
public class OutfitDriver {
public static void main(String[] args) throws Exception{
int selectedoption;
int monthofpurchase,outfitprice;
String typeofoutfit;
Scanner sc=new Scanner(System.in);
System.out.println("Welcome to shopaholic software");
while(true){
System.out.println("- Menu");
System.out.println("1- Enter a new Purchase");
System.out.println("2- Track Spending per Month");
System.out.println("0- Exit");
selectedoption=sc.nextInt();
switch(selectedoption){
case 1: System.out.println("Please enter the month of purchase");
monthofpurchase=sc.nextInt();
System.out.println("Please enter the type of purchased");
typeofoutfit=sc.next();
System.out.println("Please enter price of outfit purchased");
outfitprice=sc.nextInt();
outfit of =new outfit(monthofpurchase,typeofoutfit,outfitprice);
FileOutputStream fileOut = new FileOutputStream("outfits.ser",true);
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fileOut));
out.writeObject(of);
out.close();
fileOut.close();
break;
case 2: System.out.println("Please enter the month of purchase");
monthofpurchase=sc.nextInt();
int totalpurchases=0;
Vector vector = new Vector<outfit>();
FileInputStream saveFile = new FileInputStream("outfits.ser");
ObjectInputStream save;
try{
for(;;){
save = new ObjectInputStream(saveFile);
outfit outfit = (outfit) save.readObject();
vector.add(outfit);
}
}catch(EOFException e){
//e.printStackTrace();
}
saveFile.close();
for(Object obj : vector){
outfit otf =(outfit)obj;
if(otf.getMonthofPur()==monthofpurchase)
totalpurchases+=otf.getPriceofpur();
}
System.out.println("The amount of money spent this month is : "+totalpurchases);
break;
case 0 : System.exit(0);
default: System.out.println("Please enter valid choice");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.