Hi guys~ special prize for you all :)
This is my assignment, can you help me check any errors and report to me? Special thanks to you if you able to reply how to solve the founded problem :) THANKS
Code:
import java.util.*;
import java.io.*;
public class application {
public static void main(String[] args) {
Menus1();
}
public static void Menus1(){
Scanner input = new Scanner(System.in);
Item[] itemArray = new Item[100];
readFile(itemArray);
System.out.println("**********Menus**********\n1.Book\n2.CD\n3.Stationery\n4.Stop");;
System.out.print("Enter your choose : ");
int choose = input.nextInt();
switch(choose){
case 1:
itemArray[0] = new Book();
Menus(itemArray);
break;
case 2:
itemArray[0] = new CD();
Menus(itemArray);
break;
case 3:
5
itemArray[0] = new Stationery();
Menus(itemArray);
break;
case 4:
System.out.println("Thanks you for using this System.");
break;
default:
System.out.println("Your choose is not available.");
Menus1();
break;
}
}
public static void Menus(Item[] itemArray){
Scanner input = new Scanner(System.in);
try{
readFile(itemArray);
}catch(IOException ex){
}
System.out.println("1) Add An New Item.");
System.out.println("2) Search An Item.");
System.out.println("3) Amend The Details Of An Item.");
System.out.println("4) Update The Quantity Item.");
System.out.println("5) Stop Selling An Item.");
System.out.println("6) Display The Information of Item.");
System.out.println("7) Preview.");
System.out.print("Choose The Number You Want To Do(1-7) : ");
int choose = input.nextInt();
switch(choose){
case 1:
AddItem(itemArray);
break;
case 2:
SearchItem(itemArray);
break;
case 3:
AmendItem(itemArray);
break;
case 4:
UpdateQuantity(itemArray);
break;
case 5:
StopSelling(itemArray);
break;
case 6:
DisplayItem(itemArray);
break;
case 7:
try{
writeFile(itemArray);
}catch(IOException ex){
}
Menus1();
break;
default:
System.out.print("Incorrect input.Please try again.");
Menus(itemArray);
break;
}
}
public static void AddItem(Item[] itemArray){
Scanner input = new Scanner(System.in);
int count=0;
int repeat;
do{
for(int i = 0 ; i<itemArray.length ; i++){
if(itemArray[i].getItem()==""){
count=i;
break;
}
}
userInput(count, itemArray);
System.out.print("Do you want to continue [0=No/1=Yes]: ");
repeat = input.nextInt();
}while(repeat == 1);
Menus(itemArray);
}
public static void SearchItem(Item[] itemArray){
Scanner input = new Scanner(System.in);
int equal;
int i = 100;
System.out.print("\nEnter the item code you want to search : ");
String ItemCode = input.next();
for(int r = 0 ; r < itemArray.length ; r++){
if(ItemCode.equals(itemArray[r].getItem())){
i = r;
break;
}
}
if(i!=100){
equal = equalsTo(i, itemArray);
if(equal != 1)
search(i, itemArray, ItemCode);
}
else
System.out.println("The item code is not found.\n");
Menus(itemArray);
}
public static void search(int i, Item[] itemArray, String ItemCode){
if(ItemCode.equals(itemArray[i].getItem())){
System.out.println("The Item Code is found.\n");
System.out.println(itemArray[i].toString());
}
}
public static void AmendItem(Item[] itemArray){
Scanner input = new Scanner(System.in);
System.out.print("Enter The Item Code You Want To Amend : ");
String ItemCode = input.next();
int i = 100;
for(int r = 0 ; r < itemArray.length ; r++){
if(ItemCode.equals(itemArray[r].getItem())){
i = r;
break;
}
}
if(i!=100){
search(i, itemArray, ItemCode);
System.out.print("Is it?[0/1] : ");
int Yes = input.nextInt();
if(Yes == 1)
userInput(i, itemArray);
else
AmendItem(itemArray);
}
else
System.out.println("The item code is not found.\n");
Menus(itemArray);
}
public static void UpdateQuantity(Item[] itemArray){
Scanner input = new Scanner(System.in);
int i = 100;
System.out.print("Enter the item code that you need to update the quantity : ");
String ItemCode = input.next();
for(int r = 0; r < itemArray.length ; r++){
if(ItemCode.equals(itemArray[r].getItem())){
i = r;
break;
}
}
if(i!=100){
search(i, itemArray, ItemCode);
System.out.print("Is it?[0/1] : ");
int Yes = input.nextInt();
if(Yes == 1)
update(i, itemArray);
else
UpdateQuantity(itemArray);
}
else
System.out.println("The item code is not found.\n");
Menus(itemArray);
}
public static void update(int i, Item[] itemArray){
Scanner input = new Scanner(System.in);
int quantity = itemArray[i].getQuantity();
System.out.print("Enter 0 to spoilt or 1 to add quantity : ");
int choose = input.nextInt();
if(choose == 1){
System.out.print("Enter The number of reorder item : ");
int reorder = input.nextInt();
quantity += reorder;
itemArray[i].setQuantity(quantity);
}
else if(choose ==0){
System.out.print("Enter The number of spoilt item : ");
int spoilt = input.nextInt();
quantity -= spoilt;
itemArray[i].setQuantity(quantity);
}
else{
System.out.println("Incorrect input. Please try again.");
update(i, itemArray);
}
try{
writeFile(i, itemArray);
}catch(IOException ex){
}
Menus(itemArray);
}
public static void StopSelling(Item[] itemArray){
Scanner input = new Scanner(System.in);
System.out.print("Enter The Item Code You Want To Stop : ");
String ItemCode = input.next();
int i = 100;
for(int r = 0 ; r < itemArray.length ; r++){
if(ItemCode.equals(itemArray[r].getItem())){
i = r;
break;
}
}
if(i!=100){
search(i, itemArray, ItemCode);
System.out.print("Is it?[0=No/1=Yes] : ");
int Yes = input.nextInt();
if(Yes == 1){
System.out.print("Stop or Selling[0=Stop/1=Sell] : ");
int Stop = input.nextInt();
if(Stop == 1){
itemArray[i].setStatus(true);
System.out.println("This item is continue to sell.");
}
else if(Stop == 0){
itemArray[i].setStatus(false);
System.out.println("The Item is Stop to sell.\n");
}
else{
System.out.println("Incorrect input. Please try again.");
StopSelling(itemArray);
}
}
}
else
System.out.println("The item code is not found.\n");
try{
writeFile(i, itemArray);
}catch(IOException ex){
}
Menus(itemArray);
}
public static void DisplayItem(Item[] itemArray){
for(int i =0 ; i < itemArray.length ; i++){
if(itemArray[i].getItem() != "")
System.out.println(itemArray[i].toString());
else{
System.out.println("No More");
break;
}
}
Menus(itemArray);
}
public static int equalsTo(int i, Item[] itemArray){
int count = 0;
int equal = 0;
for(int r = 0; r < itemArray.length ; r++){
if(itemArray[i].getItem().equals(itemArray[r].getItem())){
count++;
}
}
System.out.println(count);
if(count >=2){
System.out.println("Got two or more item have the same item code. Please Amend the one of item.");
equal = 1;
}
return equal;
}
public static void userInput(int count, Item[] itemArray){
Scanner input = new Scanner(System.in);
try{
System.out.print("Enter the new item code : ");
String ItemCode = input.next();
System.out.print("Enter the new item Quantity : ");
int Quantity = input.nextInt();
System.out.print("Enter the new item Cost Price : ");
double CostPrice = input.nextDouble();
System.out.print("Enter the new item Sell Price : ");
double SellPrice = input.nextDouble();
System.out.print("Enter the new item Status : ");
boolean Status = input.nextBoolean();
System.out.print("Enter the new item Discount : ");
double Discount = input.nextDouble();
if(itemArray instanceof Book[]){
System.out.print("Enter the title of new item : ");
String title = input.next();
System.out.print("Enter the Author of new item : ");
String author = input.next();
System.out.print("Enter the publisher of new item : ");
String publisher = input.next();
System.out.print("Enter the ISBN10 of new item : ");
String ISBN10 = input.next();
System.out.print("Enter the ISBN13 of new item : ");
String ISBN13 = input.next();
System.out.print("Enter the fiction of new item : ");
String fiction = input.next();
itemArray[count] = new Book(ItemCode, Quantity, CostPrice, SellPrice, Status, Discount, title, author, publisher,
ISBN10, ISBN13, fiction);
try{
writeFile(itemArray);
}catch(IOException ex){
}
}
else if(itemArray instanceof CD[]){
System.out.print("Enter the title of new item : ");
String title = input.next();
System.out.print("Enter the description of new item : ");
String description = input.next();
System.out.print("Enter the distributer of new item : ");
String distributer = input.next();
System.out.print("Enter the No Of track of new item : ");
int NoOfTrack = input.nextInt();
System.out.print("Enter the TrackList of new item : ");
String TrackList = input.next();
itemArray[count] = new CD(ItemCode, Quantity, CostPrice, SellPrice, Status, Discount, title, description,
distributer, NoOfTrack, TrackList);
try{
writeFile(itemArray);
}catch(IOException ex){
}
}
else{
System.out.print("Enter the brand of new item : ");
String brand = input.next();
System.out.print("Enter the type of new item : ");
String type = input.next();
System.out.print("Enter the manufacturer of new item : ");
String manufacturer = input.next();
System.out.print("Enter the barcode of new item : ");
String barcode = input.next();
itemArray[count] = new Stationery(ItemCode, Quantity, CostPrice, SellPrice, Status, Discount, brand, type,
manufacturer, barcode);
try{
writeFile(itemArray);
}catch(IOException ex){
}
}
}catch(InputMismatchException e){
System.out.println("Error Input.Please try again.");
}
}
public static void readFile(Item[] itemArray)throws IOException{
DataInputStream in = null;
if(itemArray instanceof Book[]){
in = new DataInputStream(new BufferedInputStream(new FileInputStream("Book.txt")));
for(int i = 0; i < itemArray.length ; i++){
String ItemCode = in.readUTF();
int Quantity = in.readInt();
double Cprice = in.readDouble();
double Sprice = in.readDouble();
boolean status = in.readBoolean();
double discount = in.readDouble();
String title = in.readUTF();
String Author = in.readUTF();
String Publisher = in.readUTF();
String ISBN10 = in.readUTF();
String ISBN13 = in.readUTF();
String fiction = in.readUTF();
itemArray[i] = new Book(ItemCode, Quantity, Cprice, Sprice, status, discount, title, Author, Publisher
, ISBN10, ISBN13, fiction);
}
}
else if(itemArray instanceof CD[]){
in = new DataInputStream(new BufferedInputStream(new FileInputStream("CD.txt")));
for(int i = 0; i < itemArray.length ; i++){
String ItemCode = in.readUTF();
int Quantity = in.readInt();
double Cprice = in.readDouble();
double Sprice = in.readDouble();
boolean status = in.readBoolean();
double discount = in.readDouble();
String title = in.readUTF();
String description = in.readUTF();
String distributer = in.readUTF();
int NoOfTrack = in.readInt();
String TrackList = in.readUTF();
itemArray[i] = new CD(ItemCode, Quantity, Cprice, Sprice, status, discount, title, description, distributer
, NoOfTrack, TrackList);
}
}
else{
in = new DataInputStream(new BufferedInputStream(new FileInputStream("Stationery.txt")));
for(int i = 0; i < itemArray.length ; i++){
String ItemCode = in.readUTF();
int Quantity = in.readInt();
double Cprice = in.readDouble();
double Sprice = in.readDouble();
boolean status = in.readBoolean();
double discount = in.readDouble();
String brand = in.readUTF();
String type = in.readUTF();
String manufacturer = in.readUTF();
String barCode = in.readUTF();
itemArray[i] = new Stationery(ItemCode, Quantity, Cprice, Sprice, status, discount, brand, type, manufacturer, barCode);
}
}
in.close();
}
public static void writeFile (Item[] itemArray)throws IOException{
DataOutputStream out = null;
if(itemArray instanceof Book[]){
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Book.txt")));
for (int i = 0; i < itemArray.length; i++) {
if(itemArray[i].getItem() != ""){
out.writeUTF(itemArray[i].getItem());
out.writeInt(itemArray[i].getQuantity());
out.writeDouble(itemArray[i].getCPrice());
out.writeDouble(itemArray[i].getSPrice());
out.writeBoolean(itemArray[i].getStatus());
out.writeDouble(itemArray[i].getDiscount());
out.writeUTF(((Book)itemArray[i]).getTitle());
out.writeUTF(((Book)itemArray[i]).getAuthor());
out.writeUTF(((Book)itemArray[i]).getPublisher());
out.writeUTF(((Book)itemArray[i]).getISBN10());
out.writeUTF(((Book)itemArray[i]).getISBN13());
out.writeUTF(((Book)itemArray[i]).getFiction());
}
}
}
else if(itemArray instanceof CD[]){
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("CD.txt")));
for (int i = 0; i < itemArray.length; i++) {
if(itemArray[i].getItem() != ""){
out.writeUTF(itemArray[i].getItem());
out.writeInt(itemArray[i].getQuantity());
out.writeDouble(itemArray[i].getCPrice());
out.writeDouble(itemArray[i].getSPrice());
out.writeBoolean(itemArray[i].getStatus());
out.writeDouble(itemArray[i].getDiscount());
out.writeUTF(((CD)itemArray[i]).getTitle());
out.writeUTF(((CD)itemArray[i]).getDescription());
out.writeUTF(((CD)itemArray[i]).getDistributer());
out.writeInt(((CD)itemArray[i]).getNoOfTrack());
out.writeUTF(((CD)itemArray[i]).getTrackList());
}
}
}
else{
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Stationery.txt")));
for (int i = 0; i < itemArray.length; i++) {
if(itemArray[i].getItem() != ""){
out.writeUTF(itemArray[i].getItem());
out.writeInt(itemArray[i].getQuantity());
out.writeDouble(itemArray[i].getCPrice());
out.writeDouble(itemArray[i].getSPrice());
out.writeBoolean(itemArray[i].getStatus());
out.writeDouble(itemArray[i].getDiscount());
out.writeUTF(((Stationery)itemArray[i]).getBrand());
out.writeUTF(((Stationery)itemArray[i]).getType());
out.writeUTF(((Stationery)itemArray[i]).getManufacturer());
out.writeUTF(((Stationery)itemArray[i]).getBarCode());
}
}
}
out.close();
}
public static void writeFile (int i, Item[] itemArray)throws IOException{
DataOutputStream out = null;
if(itemArray instanceof Book[]){
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Book.txt")));
out.writeUTF(itemArray[i].getItem());
out.writeInt(itemArray[i].getQuantity());
out.writeDouble(itemArray[i].getCPrice());
out.writeDouble(itemArray[i].getSPrice());
out.writeBoolean(itemArray[i].getStatus());
out.writeDouble(itemArray[i].getDiscount());
out.writeUTF(((Book)itemArray[i]).getTitle());
out.writeUTF(((Book)itemArray[i]).getAuthor());
out.writeUTF(((Book)itemArray[i]).getPublisher());
out.writeUTF(((Book)itemArray[i]).getISBN10());
out.writeUTF(((Book)itemArray[i]).getISBN13());
out.writeUTF(((Book)itemArray[i]).getFiction());
}
else if(itemArray instanceof CD[]){
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("CD.txt")));
out.writeUTF(itemArray[i].getItem());
out.writeInt(itemArray[i].getQuantity());
out.writeDouble(itemArray[i].getCPrice());
out.writeDouble(itemArray[i].getSPrice());
out.writeBoolean(itemArray[i].getStatus());
out.writeDouble(itemArray[i].getDiscount());
out.writeUTF(((CD)itemArray[i]).getTitle());
out.writeUTF(((CD)itemArray[i]).getDescription());
out.writeUTF(((CD)itemArray[i]).getDistributer());
out.writeInt(((CD)itemArray[i]).getNoOfTrack());
out.writeUTF(((CD)itemArray[i]).getTrackList());
}
else{
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Stationery.txt")));
out.writeUTF(itemArray[i].getItem());
out.writeInt(itemArray[i].getQuantity());
out.writeDouble(itemArray[i].getCPrice());
out.writeDouble(itemArray[i].getSPrice());
out.writeBoolean(itemArray[i].getStatus());
out.writeDouble(itemArray[i].getDiscount());
out.writeUTF(((Stationery)itemArray[i]).getBrand());
out.writeUTF(((Stationery)itemArray[i]).getType());
out.writeUTF(((Stationery)itemArray[i]).getManufacturer());
out.writeUTF(((Stationery)itemArray[i]).getBarCode());
}
out.close();
}
}