Java homework 2020.4.17

Define a commodity category Goods

Attributes

: Name, quantity, unit price (all are private access rights) (3 points)

Method: (all public access rights)

Construction method:

2 parameters (name, unit price), the quantity is 0, call the construction method of 3 parameters for assignment; (5 points)

Construction method:

3 parameters (name, quantity, unit price), assign values ​​to the corresponding attributes according to the parameter values; (5 points)

Define the getter and setter accessors for name, quantity and unit price respectively (10 points)
buy method:

1 parameter (purchase quantity n), add n to the quantity of goods (4 points)

Sale method:

1 parameter (sales quantity n), first determine whether the quantity of goods is enough to sell, if it can, then reduce the quantity by n (5 points)

print method:

Export product information. (3 points)

Define GoodsManage class

Attribute: Commodity array (Goods type array) (5 points)

method:

Construction method:

1 parameter (commodity array length n), create a commodity array based on the value of the parameter, then loop through the array, enter the name, quantity, unit price and other information of each commodity, and call the construction method of the Goods class to create Goods for each array element Object. (10 points)

search method:

1 parameter (name), find whether the product with the same name as the parameter is found in the product array, if there is, call its print method to output the product information, and return its subscript. Otherwise, a search failure message is output and -1 is returned. (10 points)

search method:

2 parameters (lowest price, highest price), find all commodities with a commodity price between the lowest price and the highest price in the commodity array, if the search is successful, then output these commodity information in turn, and return true. If the search fails, an error message is output and false is returned. (10 points)

buyGoods method:

Two parameters (commodity name, purchase quantity), find the corresponding commodity object according to the name parameter in the commodity array, and call its buy method to implement the purchase operation. (5 points)

saleGoods method:

Two parameters (commodity name, sales quantity), find the corresponding commodity object according to the name parameter in the commodity array, and call its sale method to realize the sales operation. (5 points)

total method:

Traverse the array of commodities, and find the total price of all commodities in the array (note: the total price of a single commodity must also be calculated = quantity * unit price). (5 points)

printAll method:

Traverse the array and output information for each product. (5 points)

Code

package game;

import java.util.Scanner;

public class Java上机作业417{
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入创建多少个对象");
		GoodsManage aGoodsManage = new GoodsManage(in.nextInt());
		aGoodsManage.printAll();
		aGoodsManage.total();
		System.out.println("\n请输入要购买的商品数量");
		aGoodsManage.buyGoods(in.next(), in.nextInt());
		System.out.println("\n请输入要卖出的商品数量");
		aGoodsManage.saleGoods(in.next(), in.nextInt());
		System.out.println("\n请输入要查找的商品");
		aGoodsManage.search(in.next());
		System.out.println("\n请输入要查找的最低价格和最高价格");
		aGoodsManage.search(in.nextDouble(), in.nextDouble());
		in.close();
	}

}

class Goods{
	private String name;
	private  int number;
	private double prize;
	public Goods(String name, int number, double prize) {
		super();
		this.name = name;
		this.number = number;
		this.prize = prize;
	}
	public Goods(String name, double prize) {
		this(name, 0, prize);
	}
	public void setter_name(String a) { this.name = a; }
	public void setter_number(int a) { this.number = a; }
	public void setter_prize(double a) { this.prize = a; }
	
	public String getter_name() { return this.name; }
	public int getter_number() { return this.number; }
	public double getter_prize() { return this.prize; }
	
	public void bug(int n) {this.number+=n;}
	public void sale(int n) {
		if(this.number>=n) this.number-=n;
		else System.out.println("库存不足");
	}
	public void print() { System.out.println("商品名:"+this.name+"库存:"+this.number+"单价:"+this.prize); }
	
}
class GoodsManage{
	private Goods[] p;

	public GoodsManage(int n) {
		super();
		Scanner in = new Scanner(System.in);
		this.p = new Goods[n];
		Goods aGoods;
		System.out.println("请输入名字 数量 价格");
		for(int i = 0 ; i < n ; i++) {
			aGoods =new Goods(in.next(), in.nextInt(), in.nextDouble());
			this.p[i]= aGoods;
		}		
	}
	public Integer search(String a) {
		for(int i = 0 ; i < this.p.length ; i++)
			if(a.equals(p[i].getter_name())) {
				p[i].print();
				return null;
			}
		return -1;		
	}
	
	public boolean search(double low, double high) {
		boolean t =false;
		for(int i = 0 ; i < this.p.length ; i++) {
			if(p[i].getter_prize()>low && p[i].getter_prize()<high) {
				if(t==false) t = true;
				p[i].print();
			}
		}
		return t;
	}
	public void buyGoods(String a ,int n) {
		for(int i = 0 ; i < this.p.length ; i++)
			if(a.equals(p[i].getter_name())) {
				p[i].bug(n);
				return;
			}
	}
	public void saleGoods(String a ,int n) {
		for(int i = 0 ; i < this.p.length ; i++)
			if(a.equals(p[i].getter_name())) {
				p[i].sale(n);
				return;
			}
	}
	
	public void total() {
		double sum = 0;
		for(int i = 0 ; i < p.length ; i++) {
			System.out.println(p[i].getter_name()+"的总价为:"+(p[i].getter_number()*p[i].getter_prize()));
			sum+=p[i].getter_number()*p[i].getter_prize();
		}
		System.out.println("所有商品总价为:"+sum);
			
	}
	
	public void printAll() {
		for(int i = 0 ; i < p.length ; i++)
			p[i].print();
	}
	
}

to sum up

To build an array class and to call the class constructor need to use

public GoodsManage(int n) {
    super();
    Scanner in = new Scanner(System.in);
    this.p = new Goods[n];
    Goods aGoods;
    System.out.println("请输入名字 数量 价格");
    for(int i = 0 ; i < n ; i++) {
	aGoods =new Goods(in.next(), in.nextInt(), in.nextDouble());
	this.p[i]= aGoods;
    }		
}

At this time, this.p = new Goods[n];n objects have not been created,
but the size of p is determined. The inside is empty, and there is no address. The
back is to assign the created object to the space corresponding to p [i]
similar to the C language pointer.

Published 26 original articles · praised 11 · visits 2361

Guess you like

Origin blog.csdn.net/weixin_45696526/article/details/105577743