package?demo;
從網(wǎng)站建設到定制行業(yè)解決方案,為提供成都網(wǎng)站制作、成都做網(wǎng)站服務體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設解決方案,助力業(yè)務快速發(fā)展。創(chuàng)新互聯(lián)公司將不斷加快創(chuàng)新步伐,提供優(yōu)質的建站服務。
public?class?Fruit?{
//水果類屬性
String?kind,color;
double?weight;
//有參構造必須賦值
public?Fruit(String?kind,String?color,double?weight){
this.setKind(kind);
this.setColor(color);
this.setWeight(weight);
}
//屬性封裝
public?String?getKind()?{
return?kind;
}
public?void?setKind(String?kind)?{
this.kind?=?kind;
}
public?String?getColor()?{
return?color;
}
public?void?setColor(String?color)?{
this.color?=?color;
}
public?double?getWeight()?{
return?weight;
}
public?void?setWeight(double?weight)?{
this.weight?=?weight;
}
}
package?demo;
import?java.util.ArrayList;
import?java.util.Scanner;
public?class?Box?{
//箱子,有一個水果類集合
ArrayListFruit?f=new?ArrayListFruit();
public?ArrayListFruit?getF()?{
return?f;
}
public?void?setF(ArrayListFruit?f)?{
this.f?=?f;
}
//將水果添加到箱子(集合中)
public?void?addFruit(Fruit?temp)?{
this.f.add(temp);
}
void?manage(){
Box?box=new?Box();
int?choose;
try?{
do{
System.out.println("請選擇添加水果:1、蘋果,2、橘子,3、梨,4、查看,5、退出");
Scanner?input=new?Scanner(System.in);
choose=input.nextInt();
switch?(choose)?{
case?1:
System.out.println("請輸入類型");
String?type1=input.next();
System.out.println("請輸入顏色");
String?color1=input.next();
System.out.println("請輸入重量");
double?weight1=input.nextDouble();
Fruit?temp1=new?Fruit(type1,color1,weight1);
box.addFruit(temp1);
break;
case?2:
System.out.println("請輸入類型");
String?type2=input.next();
System.out.println("請輸入顏色");
String?color2=input.next();
System.out.println("請輸入重量");
double?weight2=input.nextDouble();
Fruit?temp2=new?Fruit(type2,color2,weight2);
box.addFruit(temp2);
break;
case?3:
System.out.println("請輸入類型");
String?type3=input.next();
System.out.println("請輸入顏色");
String?color3=input.next();
System.out.println("請輸入重量");
double?weight3=input.nextDouble();
Fruit?temp3=new?Fruit(type3,color3,weight3);
box.addFruit(temp3);
break;
case?4:
double?allWeight=0;
for?(int?i=0;ibox.getF().size();i++)?{
allWeight+=box.getF().get(i).weight;
}
System.out.println("總重量:"+allWeight);
System.out.println("顏色:");
for(Fruit?fruit:box.getF()){
System.out.println(fruit.getColor());
}
break;
case?5:
break;
default:
System.out.println("選擇錯誤");
break;
}
}while(choose!=5);
}?catch?(Exception?e)?{
System.out.println("輸入錯誤");
}
}
public?static?void?main(String[]?args)?{
Box?manage=new?Box();
manage.manage();
}
}
時間不夠 我得回宿舍 所有的驗證部分都沒做 代碼可以運行 但前提是你得正確輸入 否則不認
package com.Test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
//java程序題?。?!求大神指教!謝了!
//有一個水果箱(Box),箱子里裝有水果(Fruit),每一種水果都有不同的重量和顏色,
//水果有:蘋果,梨,橘子。每個蘋果(Apple)都有不同的重量和顏色,
//每個橘子(Orange)有不同的重量和顏色,每個梨(Pear)都有不同的重量和顏色,
//可以像水果箱(Box)里添加水果(addFruit),也可以取出水果(getFruit),
//還可以顯示水果的重量和顏色,寫出實現(xiàn)這些方法的代碼,要求實現(xiàn)上述功能!
public class HelpTest {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
Box b=new Box();
while(true)
{
System.out.println(" 請選擇功能 ");
System.out.println("1 添加水果");
System.out.println("2 刪除水果");
System.out.println("3 顯示水果信息");
System.out.println("4 退出系統(tǒng)");
int a =Integer.parseInt(br.readLine());
System.out.println(a);
switch (a) {
case 1:
System.out.println("請輸入水果名稱");
String name=br.readLine();
System.out.println("請輸入水果顏色");
String color=br.readLine();
System.out.println("請輸入水果重量");
float weight=Float.parseFloat(br.readLine());
Furit f=new Furit(name, color, weight);
b.AddFurit(f);
break;
case 2:
System.out.println("請輸入水果名稱");
String name1=br.readLine();
b.DelFurit(name1);
break;
case 3:
System.out.println("請輸入水果名稱");
String name2=br.readLine();
b.ShowInfo(name2);
break;
case 4:
System.exit(0);
break;
default:
break;
}
}
}
}
class Furit{
private String name;
private String color;
private float weight;
public Furit(String name,String color,float weight){
this.name=name;
this.color=color;
this.weight=weight;
}
public Furit(String name)
{
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
class Box{
ArrayListFurit al=new ArrayListFurit();
//添加水果的方法
public void AddFurit(Furit f){
al.add(f);
}
//刪除水果的方法
public void DelFurit(String name)
{
for(int i=0;ial.size();i++)
{
if(al.get(i).getName().equals(name))
{
al.remove(i);
}
}
}
//顯示水果的方法
public void ShowInfo(String name ){
for(int i=0;ial.size();i++)
{
Furit f=(Furit)al.get(i);
if(name.equals(f.getName()))
{
System.out.println(al.get(i).getName()+"的顏色是"+al.get(i).getColor()+" 重量是"+al.get(i).getWeight());
}
}
}
}
我給你寫了這么多你采納他的答案? 呵呵
可以定義一個FruitBox父類,然后定義水果蘋果Apple,橘子Orange,梨Pear的子類,顏色color和重量weight封裝成屬性,添加子類的get,set方法,定義一個測試類測試一下.