JAVA项目2

TASK1 

public abstract class Product {

    private static int invSize;//static variable
    private int ID;
    private String name;
    private double price;
    private char type;


    public Product(int id, String n, double p, char t) {
        ID=id;
        name = n;
        price = p;
        type = t;

    }
public String gettype(){
        return type+"";
}
    public int getID() {
        return ID;
    }

    public double getprice() {
        return price;
    }

    public String getname() {
        return name;
    }

    public int getinvSize() {
        return invSize;
    }

    public String toString() {
        return ID+"  "+name+"  "+price+"  "+type;
    }

    public abstract double total(double qty,double p);


    }
public class Order {
    private String userName;
    private Product product;
    private int qty;
    private float total;
    private Date date;
    private String c3;
    private double p3;
    public Order(Product[] p,int q,String name3){

        for(int i=0;i<q;i++){
            for(int j=0;j<q-1;j++){
                if(p[j].getname().compareTo(p[j+1].getname())>0){
                    Product pro=p[j];
                    p[j]=p[j+1];
                    p[j+1]=pro;
                }
            }
        }
        int left=0;
        int right=q-1;
        while(left<=right){
            int mid=left+(right-left)/2;
            String name2=p[mid].getname();
            int result=name2.compareTo(name3);
            if(result==0){
                product=p[mid];
               c3=product.gettype();
               p3=product.getprice();
                toString();
                break;
            }else if(result>0){
                right=mid-1;
            }else{
                left=mid+1;
            }
        }


    }
public String gettype(){
        return c3;
}
public double getprice(){
        return p3;
}
    public String toString(){
        System.out.println(product.toString());
        return null;
    }
}
public class Date {
    private int day;
    private int month;
    private int year;
    public Date(String dateString){
String[] NAME=dateString.split("/");

    month=Integer.parseInt(NAME[0]);
    day= Integer.parseInt(NAME[1]);
    year= Integer.parseInt(NAME[2]);
    if(month<0||month>12){
        System.out.println("it is wrong");
    }
    switch(month){
        case 1:
            if(day<0||day>31){
                System.out.println("it is wrong");
            }
            break;

            case 3:
                if(day<0||day>31){
                    System.out.println("it is wrong");
                }
                break;

                case 5:
                    if(day<0||day>31){
                        System.out.println("it is wrong");
                    }
                    break;

                    case 7:
                        if(day<0||day>31){
                            System.out.println("it is wrong");
                        }
                        break;

                        case 9:
                            if(day<0||day>31){
                                System.out.println("it is wrong");
                            }
                            break;

                            case 10:
                                if(day<0||day>31){
                                    System.out.println("it is wrong");
                                }
                                break;

                                case 12:
                                    if(day<0||day>31){
                                        System.out.println("it is wrong");
                                    }
                                    break;
        case 4:
            if(day<0||day>30){
                System.out.println("it is wrong");
            }
            break;
            case 6:
                if(day<0||day>30){
                    System.out.println("it is wrong");
                                    }
                break;

                    case 11:
                        if(day<0||day>30){
                            System.out.println("it is wrong");
                        }
                        break;
        case 2:
            if(day<0||day>29){
                System.out.println("it is wrong");
            }
            if(year%4!=0&&year%100!=0){
                if(day==29){
                    System.out.println("it is wrong");
                }
            }
         break;
    }
    }
    public String toString(){
        System.out.println("Date:"+month+"/"+day+"/"+year);

        return null;
    }
}
public class ProductR extends Product{

    public ProductR(int id,String n,double p,char t){
    super(id,n,p,t);
    }
public double total(double amount,double p){
       double v=amount*p;
       return v;
}
}
public class ProductB extends Product{

    public ProductB(int id,String n,double p,char t){
     super(id,n,p,t);
    }
    public double total(double amount,double p3){
        double end;

      if(amount<100){
          end=amount*p3;
      }
      else if(amount>100&&amount<500){
          end=(1-0.05)*amount*p3;
      }
      else if(amount>500&&amount<1500){
          end=(1-0.15)*amount*p3;
      }else{
          end=(1-0.25)*amount*p3;
      }
      return end;
    }
}
public class ProductS extends Product{
    public ProductS(int id,String n,double p,char t){
      super(id,n,p,t);
    }
    public double total(double qty,double p){
        double p4=(1-0.4)*p*qty;
        return p4;
    }
    public double total(int qty,Date d){
      return 0;
    }

}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;
import java.io.*;
public class loadarray {
    public static void main(String[] args) throws IOException {
        int t=6;
        int[]ID=new int[t];
        String []name=new String[t];
        double[] price=new double[t];
        String[] type=new String[t];
        String filename="data.txt";

        Product[] pro=new Product[t];
        loadArray(filename,t,pro,ID,name,price,type);
        int m1=0;
int[]amount=new int[t];
        Date[] date=new Date[t];
        for (int IO = 0; ;IO++) {
            System.out.println("(I)nventory\n");
            System.out.println("(O)rder\n");
            System.out.println("(R)eview the order\n");
            System.out.println("(E)xit\n");
            System.out.println("your choice");
            Scanner sx = new Scanner(System.in);
            String uu = sx.nextLine();

            switch(uu){
                case "i":
                    System.out.println("The inventory:");
                  for(int i=0;i<t;i++){
                      System.out.println(pro[i]);
                  }
                    break;
                case "o":

                    System.out.println("Enter product name:");
                    Scanner sx1 = new Scanner(System.in);
                    name[m1]=sx1.nextLine();
                    System.out.println("Enter quantity:");
                    Scanner sx2 = new Scanner(System.in);
                    amount[m1]=sx2.nextInt();
                    System.out.println("What is the date of purchasing? (mm/dd/yyyy)");
                    Scanner sx3 = new Scanner(System.in);
                    date[m1]=new Date(sx3.nextLine());
                    m1++;
                    break;
                case "r":
                    for(int xxx=0;xxx<m1;xxx++) {
                        date[xxx].toString();
                        System.out.println("+---+-----------------+------+----+\n");
                        System.out.println("|ID |Name     |Price|Type|\n");
                        System.out.println("+---+-----------------+------+----+\n");
                        Order o = new Order(pro, t, name[xxx]);
                        type[xxx] = o.gettype();
                        price[xxx] = o.getprice();
                        ;
                        System.out.println("+---+-----------------+------+----+\n");
                        System.out.println("QTY:" + amount[xxx]);

                        if (Objects.equals(type[xxx], "R")) {

                            ProductR prr = new ProductR(ID[xxx], name[xxx], price[xxx], 'R');
                            System.out.println("Total:" + prr.total(amount[xxx], price[xxx]));
                        } else if (Objects.equals(type[xxx], "B")) {
                            ProductB pbb = new ProductB(ID[xxx], name[xxx], price[xxx], 'B');
                            System.out.println("Total:" + pbb.total(amount[xxx], price[xxx]));

                        } else {
                            ProductS pss = new ProductS(ID[xxx], name[xxx], price[xxx], 'S');
                            System.out.println("Total:" + pss.total(amount[xxx], price[xxx]));
                        }
                    }

                    break;
                    case "e":
                        BufferedWriter bw=new BufferedWriter(new FileWriter("order.txt"));
                        ArrayList<String> orders=new ArrayList<>();
                        for(int i=0;i<t;i++){
                            orders.add(String.valueOf(pro[i]));
                        }
                        for(String s:orders){
                            bw.write(s);
                            bw.newLine();
                            bw.flush();
                        }
                        System.out.println("Your order is saved.Goodbye!");
                        System.exit(0);
                    break;
                case "w":
                    System.out.println("Invalid choice");
                    break;


            }
        }
    }
    public static void array(Product[]arr,String[]v,int t,int[]ID,String[]name,double[]price,String[]type){

        for(int i=0;i<t;i++){
            String[] my=v[i].split(",");
             ID[i]=Integer.parseInt(my[0]);
             name[i]=my[1];
           price[i]=Double.parseDouble(my[2]);
            type[i]=my[3];


            if (Objects.equals(type[i], "B")) {
                arr[i]=new ProductB(ID[i],name[i],price[i],'B');
            }
            else if(Objects.equals(type[i], "R")){
                arr[i]=new ProductR(ID[i],name[i],price[i],'R');
            }
             else {
                 arr[i]= new ProductB(ID[i], name[i], price[i],'S');}
        }
    }
    public static String[] loadArray(String filename, int t, Product[] pro,int[]ID,String[]name,double[]price,String[]type) throws IOException {
        //Put the data in the file into an array
        // We first read the file to count the number of line
String[] v=new String[t];
        FileReader f = new FileReader(filename);
        BufferedReader in = new BufferedReader(f);
        int n = 0;
        String line = in.readLine();
        while (line != null) {
            n++;
            line = in.readLine();
        }
        t = n;
        f.close();
// Creation of the array

        f = new FileReader(filename);
        in = new BufferedReader(f);
        int i = 0;
        line = in.readLine();
        while ((line != null) && (i < n)) {

            v[i]= line;
            line = in.readLine();
            i++;
        }
        array(pro,v,t,ID,name,price,type);
        f.close();
        return v;

    }
}

注:order.txt

1  Light Bulb 60W  3.0  R
2  Light Bulb 100W  5.99  R
3  Steel Bolt M5  0.15  B
4  Steel Bolt M8  0.25  B
5  Hose 25 feet  10.0  S
6  Hose 50 feet  15.0  S

  • 23
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值